orm bug 修复

pull/2/head
lyy 8 years ago
parent 7a962edeae
commit cb9188423f
  1. 30
      app/src/main/java/com/arialyy/simple/activity/SimpleTestActivity.java
  2. 12
      app/src/main/res/layout/content_simple.xml
  3. 6
      downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbEntity.java
  4. 30
      downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbUtil.java
  5. 1
      downloadutil/src/main/java/com/arialyy/downloadutil/util/Task.java

@ -53,39 +53,49 @@ public class SimpleTestActivity extends BaseActivity<ActivitySimpleBinding> {
switch (msg.what) {
case DOWNLOAD_PRE:
mSize.setText(Util.formatFileSize((Long) msg.obj));
mStart.setEnabled(false);
setBtState(false);
break;
case DOWNLOAD_FAILE:
Toast.makeText(SimpleTestActivity.this, "下载失败", Toast.LENGTH_SHORT).show();
setBtState(true);
break;
case DOWNLOAD_STOP:
Toast.makeText(SimpleTestActivity.this, "暂停下载", Toast.LENGTH_SHORT).show();
mStart.setText("恢复");
mStart.setEnabled(true);
setBtState(true);
break;
case DOWNLOAD_CANCEL:
mPb.setProgress(0);
Toast.makeText(SimpleTestActivity.this, "取消下载", Toast.LENGTH_SHORT).show();
mStart.setEnabled(true);
mStart.setText("开始");
setBtState(true);
break;
case DOWNLOAD_RESUME:
Toast.makeText(SimpleTestActivity.this,
"恢复下载,恢复位置 ==> " + Util.formatFileSize((Long) msg.obj),
Toast.LENGTH_SHORT).show();
mStart.setEnabled(false);
setBtState(false);
break;
case DOWNLOAD_COMPLETE:
Toast.makeText(SimpleTestActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
mStart.setText("重新开始");
mStart.setEnabled(true);
mStart.setText("重新开始?");
mCancel.setEnabled(false);
mStop.setEnabled(false);
setBtState(true);
break;
}
}
};
/**
* 设置start stop 按钮状态
*
* @param state
*/
private void setBtState(boolean state) {
mStart.setEnabled(state);
mStop.setEnabled(!state);
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
long len = 0;
@ -170,6 +180,12 @@ public class SimpleTestActivity extends BaseActivity<ActivitySimpleBinding> {
if (mEntity != null) {
mPb.setProgress((int) ((mEntity.getCurrentProgress() * 100) / mEntity.getFileSize()));
mSize.setText(Util.formatFileSize(mEntity.getFileSize()));
if (mEntity.getState() == DownloadEntity.STATE_DOWNLOAD_ING) {
setBtState(false);
} else if (mEntity.isDownloadComplete()) {
mStart.setText("重新开始?");
setBtState(true);
}
} else {
mEntity = new DownloadEntity();
}

@ -33,40 +33,40 @@
/>
<LinearLayout
android:layout_below="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar"
android:orientation="horizontal"
>
<Button
android:text="开始"
android:id="@+id/start"
style="?buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="开始"
/>
<Button
android:onClick="onClick"
android:text="暂停"
android:id="@+id/stop"
style="?buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="暂停"
/>
<Button
android:onClick="onClick"
android:text="取消"
android:id="@+id/cancel"
style="?buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="删除任务"
/>
</LinearLayout>
</RelativeLayout>

@ -24,14 +24,14 @@ public class DbEntity {
* 获取所有行的rowid
*/
public int[] getRowId() {
return mUtil.getRowId(this);
return mUtil.getRowId(getClass());
}
/**
* 获取rowid
*/
public int getRowId(@NonNull Object[] wheres, @NonNull Object[] values) {
return mUtil.getRowId(this, wheres, values);
return mUtil.getRowId(getClass(), wheres, values);
}
/**
@ -60,7 +60,7 @@ public class DbEntity {
* 保存自身如果表中已经有数据则更新数据否则插入数据
*/
public synchronized void save() {
if (mUtil.tableExists(this) && thisIsExist()) {
if (mUtil.tableExists(getClass()) && thisIsExist()) {
update();
} else {
insert();

@ -62,7 +62,8 @@ public class DbUtil {
/**
* 删除某条数据
*/
protected void delData(Class clazz, @NonNull Object[] wheres, @NonNull Object[] values) {
protected <T extends DbEntity> void delData(Class<T> clazz, @NonNull Object[] wheres,
@NonNull Object[] values) {
mDb = mHelper.getWritableDatabase();
if (wheres.length <= 0 || values.length <= 0) {
Log.e(TAG, "输入删除条件");
@ -135,6 +136,9 @@ public class DbUtil {
*/
protected <T extends DbEntity> List<T> findData(Class<T> clazz, @NonNull String[] wheres,
@NonNull String[] values) {
if (!tableExists(clazz)) {
createTable(clazz);
}
mDb = mHelper.getReadableDatabase();
if (wheres.length <= 0 || values.length <= 0) {
Log.e(TAG, "请输入查询条件");
@ -160,11 +164,11 @@ public class DbUtil {
* 插入数据
*/
protected void insertData(DbEntity dbEntity) {
if (!tableExists(dbEntity)) {
createTable(dbEntity);
Class<?> clazz = dbEntity.getClass();
if (!tableExists(clazz)) {
createTable(clazz);
}
mDb = mHelper.getWritableDatabase();
Class<?> clazz = dbEntity.getClass();
Field[] fields = Util.getFields(clazz);
if (fields != null && fields.length > 0) {
StringBuilder sb = new StringBuilder();
@ -207,7 +211,7 @@ public class DbUtil {
/**
* 查找某张表是否存在
*/
public synchronized boolean tableExists(DbEntity dbEntity) {
public synchronized boolean tableExists(Class clazz) {
if (mDb == null || !mDb.isOpen()) {
mDb = mHelper.getReadableDatabase();
}
@ -215,7 +219,7 @@ public class DbUtil {
try {
StringBuilder sb = new StringBuilder();
sb.append("SELECT COUNT(*) AS c FROM sqlite_master WHERE type ='table' AND name ='");
sb.append(Util.getClassName(dbEntity));
sb.append(Util.getClassName(clazz));
sb.append("'");
print(TABLE_EXISTS, sb.toString());
cursor = mDb.rawQuery(sb.toString(), null);
@ -237,14 +241,14 @@ public class DbUtil {
/**
* 创建表
*/
private void createTable(DbEntity dbEntity) {
private void createTable(Class clazz) {
if (mDb == null || !mDb.isOpen()) {
mDb = mHelper.getWritableDatabase();
}
Field[] fields = Util.getFields(dbEntity.getClass());
Field[] fields = Util.getFields(clazz);
if (fields != null && fields.length > 0) {
StringBuilder sb = new StringBuilder();
sb.append("create table ").append(Util.getClassName(dbEntity)).append("(");
sb.append("create table ").append(Util.getClassName(clazz)).append("(");
for (Field field : fields) {
field.setAccessible(true);
Ignore ignore = field.getAnnotation(Ignore.class);
@ -322,9 +326,9 @@ public class DbUtil {
/**
* 获取所在行Id
*/
protected int[] getRowId(DbEntity dbEntity) {
protected int[] getRowId(Class clazz) {
mDb = mHelper.getReadableDatabase();
Cursor cursor = mDb.rawQuery("SELECT rowid, * FROM " + Util.getClassName(dbEntity), null);
Cursor cursor = mDb.rawQuery("SELECT rowid, * FROM " + Util.getClassName(clazz), null);
int[] ids = new int[cursor.getCount()];
int i = 0;
while (cursor.moveToNext()) {
@ -339,7 +343,7 @@ public class DbUtil {
/**
* 获取行Id
*/
protected int getRowId(DbEntity dbEntity, Object[] wheres, Object[] values) {
protected int getRowId(Class clazz, Object[] wheres, Object[] values) {
mDb = mHelper.getReadableDatabase();
if (wheres.length <= 0 || values.length <= 0) {
Log.e(TAG, "请输入删除条件");
@ -349,7 +353,7 @@ public class DbUtil {
return -1;
}
StringBuilder sb = new StringBuilder();
sb.append("SELECT rowid FROM ").append(Util.getClassName(dbEntity)).append(" WHERE ");
sb.append("SELECT rowid FROM ").append(Util.getClassName(clazz)).append(" WHERE ");
int i = 0;
for (Object where : wheres) {
sb.append(where).append("=").append("'").append(values[i]).append("'");

@ -192,6 +192,7 @@ public class Task {
}
private void sendIntent(String action, long location) {
downloadEntity.setDownloadComplete(action.equals(DownloadManager.ACTION_COMPLETE));
downloadEntity.setCurrentProgress(location);
downloadEntity.update();
Uri.Builder builder = new Uri.Builder();

Loading…
Cancel
Save