“例子编写”
This commit is contained in:
4
.idea/codeStyleSettings.xml
generated
4
.idea/codeStyleSettings.xml
generated
@ -63,9 +63,7 @@
|
|||||||
</extensions>
|
</extensions>
|
||||||
</Objective-C-extensions>
|
</Objective-C-extensions>
|
||||||
<XML>
|
<XML>
|
||||||
<option name="XML_KEEP_LINE_BREAKS" value="false" />
|
<option name="XML_LEGACY_SETTINGS_IMPORTED" value="true" />
|
||||||
<option name="XML_ALIGN_ATTRIBUTES" value="false" />
|
|
||||||
<option name="XML_SPACE_INSIDE_EMPTY_TAG" value="true" />
|
|
||||||
</XML>
|
</XML>
|
||||||
<codeStyleSettings language="XML">
|
<codeStyleSettings language="XML">
|
||||||
<option name="FORCE_REARRANGE_MODE" value="1" />
|
<option name="FORCE_REARRANGE_MODE" value="1" />
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
|
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
|
||||||
<uses-permission android:name="android.permission.INTERNET"/>
|
<uses-permission android:name="android.permission.INTERNET"/>
|
||||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:name=".base.BaseApplication"
|
android:name=".base.BaseApplication"
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.arialyy.simple.activity;
|
package com.arialyy.simple.activity;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
@ -20,6 +21,9 @@ import com.arialyy.downloadutil.core.command.IDownloadCommand;
|
|||||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||||
import com.arialyy.downloadutil.util.DownLoadUtil;
|
import com.arialyy.downloadutil.util.DownLoadUtil;
|
||||||
import com.arialyy.downloadutil.util.Util;
|
import com.arialyy.downloadutil.util.Util;
|
||||||
|
import com.arialyy.frame.permission.OnPermissionCallback;
|
||||||
|
import com.arialyy.frame.permission.PermissionManager;
|
||||||
|
import com.arialyy.frame.util.show.L;
|
||||||
import com.arialyy.simple.R;
|
import com.arialyy.simple.R;
|
||||||
import com.arialyy.simple.base.BaseActivity;
|
import com.arialyy.simple.base.BaseActivity;
|
||||||
import com.arialyy.simple.databinding.ActivitySimpleBinding;
|
import com.arialyy.simple.databinding.ActivitySimpleBinding;
|
||||||
@ -83,8 +87,34 @@ public class SimpleTestActivity extends BaseActivity<ActivitySimpleBinding> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||||
@Override public void onReceive(Context context, Intent intent) {
|
long len = 0;
|
||||||
|
|
||||||
|
@Override public void onReceive(Context context, Intent intent) {
|
||||||
|
String action = intent.getAction();
|
||||||
|
switch (action) {
|
||||||
|
case DownloadManager.ACTION_PRE:
|
||||||
|
DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ACTION_PRE);
|
||||||
|
len = entity.getFileSize();
|
||||||
|
L.d(TAG, "download pre");
|
||||||
|
break;
|
||||||
|
case DownloadManager.ACTION_START:
|
||||||
|
L.d(TAG, "download start");
|
||||||
|
break;
|
||||||
|
case DownloadManager.ACTION_RESUME:
|
||||||
|
L.d(TAG, "download resume");
|
||||||
|
break;
|
||||||
|
case DownloadManager.ACTION_RUNNING:
|
||||||
|
long current = intent.getLongExtra(DownloadManager.ACTION_RUNNING, 0);
|
||||||
|
if (len == 0) {
|
||||||
|
mPb.setProgress(0);
|
||||||
|
} else {
|
||||||
|
mPb.setProgress((int) ((current * 100) / len));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case DownloadManager.ACTION_STOP:
|
||||||
|
L.d(TAG, "download stop");
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -126,6 +156,21 @@ public class SimpleTestActivity extends BaseActivity<ActivitySimpleBinding> {
|
|||||||
switch (view.getId()) {
|
switch (view.getId()) {
|
||||||
case R.id.start:
|
case R.id.start:
|
||||||
start();
|
start();
|
||||||
|
// if (PermissionManager.getInstance()
|
||||||
|
// .checkPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
|
||||||
|
// start();
|
||||||
|
// } else {
|
||||||
|
// PermissionManager.getInstance()
|
||||||
|
// .requestPermission(this, new OnPermissionCallback() {
|
||||||
|
// @Override public void onSuccess(String... permissions) {
|
||||||
|
// start();
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override public void onFail(String... permissions) {
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
// }, Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
||||||
|
// }
|
||||||
break;
|
break;
|
||||||
case R.id.stop:
|
case R.id.stop:
|
||||||
stop();
|
stop();
|
||||||
@ -136,8 +181,9 @@ public class SimpleTestActivity extends BaseActivity<ActivitySimpleBinding> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DownloadEntity entity = new DownloadEntity();
|
||||||
|
|
||||||
private void start() {
|
private void start() {
|
||||||
DownloadEntity entity = new DownloadEntity();
|
|
||||||
entity.setFileName("test.apk");
|
entity.setFileName("test.apk");
|
||||||
entity.setDownloadUrl(mDownloadUrl);
|
entity.setDownloadUrl(mDownloadUrl);
|
||||||
entity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk");
|
entity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk");
|
||||||
@ -148,12 +194,14 @@ public class SimpleTestActivity extends BaseActivity<ActivitySimpleBinding> {
|
|||||||
.createCommand(this, entity, CommandFactory.TASK_START);
|
.createCommand(this, entity, CommandFactory.TASK_START);
|
||||||
commands.add(addCommand);
|
commands.add(addCommand);
|
||||||
commands.add(startCommand);
|
commands.add(startCommand);
|
||||||
DownloadManager.getInstance(this).setCommands(commands).exe();
|
DownloadManager.getInstance().setCommands(commands).exe();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void stop() {
|
private void stop() {
|
||||||
mUtil.stopDownload();
|
// mUtil.stopDownload();
|
||||||
|
IDownloadCommand stopCommand = CommandFactory.getInstance()
|
||||||
|
.createCommand(this, entity, CommandFactory.TASK_STOP);
|
||||||
|
DownloadManager.getInstance().setCommand(stopCommand).exe();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void cancel() {
|
private void cancel() {
|
||||||
|
@ -2,6 +2,7 @@ package com.arialyy.simple.base;
|
|||||||
|
|
||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
|
|
||||||
|
import com.arialyy.downloadutil.core.DownloadManager;
|
||||||
import com.arialyy.frame.core.AbsFrame;
|
import com.arialyy.frame.core.AbsFrame;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -11,5 +12,6 @@ public class BaseApplication extends Application {
|
|||||||
@Override public void onCreate() {
|
@Override public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
AbsFrame.init(this);
|
AbsFrame.init(this);
|
||||||
|
DownloadManager.init(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ public class DownloadModule extends BaseModule {
|
|||||||
*/
|
*/
|
||||||
public IntentFilter getDownloadFilter(){
|
public IntentFilter getDownloadFilter(){
|
||||||
IntentFilter filter = new IntentFilter();
|
IntentFilter filter = new IntentFilter();
|
||||||
filter.addCategory(getContext().getPackageName());
|
filter.addDataScheme(getContext().getPackageName());
|
||||||
filter.addAction(DownloadManager.ACTION_PRE);
|
filter.addAction(DownloadManager.ACTION_PRE);
|
||||||
filter.addAction(DownloadManager.ACTION_RESUME);
|
filter.addAction(DownloadManager.ACTION_RESUME);
|
||||||
filter.addAction(DownloadManager.ACTION_START);
|
filter.addAction(DownloadManager.ACTION_START);
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
package com.arialyy.downloadutil.core;
|
package com.arialyy.downloadutil.core;
|
||||||
|
|
||||||
|
import android.app.Application;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import com.arialyy.downloadutil.core.command.IDownloadCommand;
|
import com.arialyy.downloadutil.core.command.IDownloadCommand;
|
||||||
|
import com.arialyy.downloadutil.orm.DbUtil;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -12,6 +15,7 @@ import java.util.List;
|
|||||||
* 下载管理器,通过命令的方式控制下载
|
* 下载管理器,通过命令的方式控制下载
|
||||||
*/
|
*/
|
||||||
public class DownloadManager {
|
public class DownloadManager {
|
||||||
|
private static final String TAG = "DownloadManager";
|
||||||
private static final Object LOCK = new Object();
|
private static final Object LOCK = new Object();
|
||||||
private static volatile DownloadManager INSTANCE = null;
|
private static volatile DownloadManager INSTANCE = null;
|
||||||
/**
|
/**
|
||||||
@ -74,13 +78,26 @@ public class DownloadManager {
|
|||||||
|
|
||||||
private DownloadManager(Context context) {
|
private DownloadManager(Context context) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
|
DownloadTarget.init(context);
|
||||||
|
DbUtil.init(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DownloadManager getInstance(Context context) {
|
public static DownloadManager init(Context context) {
|
||||||
if (INSTANCE == null) {
|
if (context instanceof Application) {
|
||||||
synchronized (LOCK) {
|
if (INSTANCE == null) {
|
||||||
INSTANCE = new DownloadManager(context.getApplicationContext());
|
synchronized (LOCK) {
|
||||||
|
INSTANCE = new DownloadManager(context.getApplicationContext());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Log.e(TAG, "Context 只能为application");
|
||||||
|
}
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DownloadManager getInstance() {
|
||||||
|
if (INSTANCE == null) {
|
||||||
|
throw new NullPointerException("请在Application中调用init进行下载器注册");
|
||||||
}
|
}
|
||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
}
|
}
|
||||||
@ -88,8 +105,9 @@ public class DownloadManager {
|
|||||||
/**
|
/**
|
||||||
* 设置命令
|
* 设置命令
|
||||||
*/
|
*/
|
||||||
public void setCommant(IDownloadCommand command) {
|
public DownloadManager setCommand(IDownloadCommand command) {
|
||||||
mCommands.add(command);
|
mCommands.add(command);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -15,7 +15,14 @@ public class DownloadTarget extends IDownloadTarget {
|
|||||||
private static volatile DownloadTarget INSTANCE = null;
|
private static volatile DownloadTarget INSTANCE = null;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
|
|
||||||
public static DownloadTarget getInstance(Context context) {
|
public static DownloadTarget getInstance() {
|
||||||
|
if (INSTANCE == null) {
|
||||||
|
throw new NullPointerException("请在Application中调用init进行注册");
|
||||||
|
}
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static DownloadTarget init(Context context) {
|
||||||
if (INSTANCE == null) {
|
if (INSTANCE == null) {
|
||||||
synchronized (LOCK) {
|
synchronized (LOCK) {
|
||||||
INSTANCE = new DownloadTarget(context.getApplicationContext());
|
INSTANCE = new DownloadTarget(context.getApplicationContext());
|
||||||
@ -86,20 +93,14 @@ public class DownloadTarget extends IDownloadTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override public void removeTask(DownloadEntity entity) {
|
@Override public void removeTask(DownloadEntity entity) {
|
||||||
Task task = mCachePool.getTask(entity.getDownloadUrl());
|
Task task = mExecutePool.getTask(entity.getDownloadUrl());
|
||||||
if (task != null) {
|
if (task != null) {
|
||||||
Log.d(TAG, "任务删除" + (mCachePool.removeTask(task) ?
|
Log.d(TAG, "从执行池删除任务,删除" + (mExecutePool.removeTask(task) ? "成功" : "失败"));
|
||||||
"成功" :
|
|
||||||
"失败"
|
|
||||||
));
|
|
||||||
} else {
|
} else {
|
||||||
task = mExecutePool.getTask(entity.getDownloadUrl());
|
task = mCachePool.getTask(entity.getDownloadUrl());
|
||||||
}
|
}
|
||||||
if (task != null) {
|
if (task != null) {
|
||||||
Log.d(TAG, "任务删除" + (mCachePool.removeTask(task) ?
|
Log.d(TAG, "从缓存池删除任务,删除" + (mCachePool.removeTask(task) ? "成功" : "失败"));
|
||||||
"成功" :
|
|
||||||
"失败"
|
|
||||||
));
|
|
||||||
} else {
|
} else {
|
||||||
Log.w(TAG, "没有找到下载链接为【" + entity.getDownloadUrl() + "】的任务");
|
Log.w(TAG, "没有找到下载链接为【" + entity.getDownloadUrl() + "】的任务");
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package com.arialyy.downloadutil.core;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.net.Uri;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@ -39,7 +40,7 @@ public class Task {
|
|||||||
listener = new DownloadListener(context, downloadEntity, outHandler);
|
listener = new DownloadListener(context, downloadEntity, outHandler);
|
||||||
}
|
}
|
||||||
util.download(context, downloadEntity.getDownloadUrl(),
|
util.download(context, downloadEntity.getDownloadUrl(),
|
||||||
downloadEntity.getDownloadPath(), listener);
|
downloadEntity.getDownloadPath(), listener);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,8 +87,12 @@ public class Task {
|
|||||||
this.context = context;
|
this.context = context;
|
||||||
this.outHandler = outHandler;
|
this.outHandler = outHandler;
|
||||||
this.downloadEntity = downloadEntity;
|
this.downloadEntity = downloadEntity;
|
||||||
sendIntent = new Intent();
|
sendIntent = new Intent(DownloadManager.ACTION_RUNNING);
|
||||||
sendIntent.addCategory(context.getPackageName());
|
Uri.Builder builder = new Uri.Builder();
|
||||||
|
builder.scheme(context.getPackageName());
|
||||||
|
Uri uri = builder.build();
|
||||||
|
sendIntent.setData(uri);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onPreDownload(HttpURLConnection connection) {
|
@Override public void onPreDownload(HttpURLConnection connection) {
|
||||||
@ -116,6 +121,7 @@ public class Task {
|
|||||||
if (currentLocation - lastLen > INTERVAL) { //不要太过于频繁发送广播
|
if (currentLocation - lastLen > INTERVAL) { //不要太过于频繁发送广播
|
||||||
sendIntent.putExtra(DownloadManager.ACTION_RUNNING, currentLocation);
|
sendIntent.putExtra(DownloadManager.ACTION_RUNNING, currentLocation);
|
||||||
lastLen = currentLocation;
|
lastLen = currentLocation;
|
||||||
|
context.sendBroadcast(sendIntent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -162,8 +168,11 @@ public class Task {
|
|||||||
|
|
||||||
private void sendIntent(String action, long location) {
|
private void sendIntent(String action, long location) {
|
||||||
downloadEntity.save();
|
downloadEntity.save();
|
||||||
Intent intent = new Intent();
|
Uri.Builder builder = new Uri.Builder();
|
||||||
intent.addCategory(context.getPackageName());
|
builder.scheme(context.getPackageName());
|
||||||
|
Uri uri = builder.build();
|
||||||
|
Intent intent = new Intent(action);
|
||||||
|
intent.setData(uri);
|
||||||
intent.putExtra(action, downloadEntity);
|
intent.putExtra(action, downloadEntity);
|
||||||
if (location != -1) {
|
if (location != -1) {
|
||||||
intent.putExtra(DownloadManager.CURRENT_LOCATION, location);
|
intent.putExtra(DownloadManager.CURRENT_LOCATION, location);
|
||||||
|
@ -17,6 +17,10 @@ public class CommandFactory {
|
|||||||
* 启动任务
|
* 启动任务
|
||||||
*/
|
*/
|
||||||
public static final int TASK_START = 0x123;
|
public static final int TASK_START = 0x123;
|
||||||
|
/**
|
||||||
|
* 恢复任务
|
||||||
|
*/
|
||||||
|
public static final int TASK_RESUME = 0x127;
|
||||||
/**
|
/**
|
||||||
* 取消任务
|
* 取消任务
|
||||||
*/
|
*/
|
||||||
@ -56,6 +60,7 @@ public class CommandFactory {
|
|||||||
switch (type) {
|
switch (type) {
|
||||||
case TASK_CREATE:
|
case TASK_CREATE:
|
||||||
return createAddCommand(context, entity);
|
return createAddCommand(context, entity);
|
||||||
|
case TASK_RESUME:
|
||||||
case TASK_START:
|
case TASK_START:
|
||||||
return createStartCommand(context, entity);
|
return createStartCommand(context, entity);
|
||||||
case TASK_CANCEL:
|
case TASK_CANCEL:
|
||||||
|
@ -24,7 +24,7 @@ public abstract class IDownloadCommand {
|
|||||||
if (!CheckHelp.checkDownloadEntity(entity)) {
|
if (!CheckHelp.checkDownloadEntity(entity)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
target = DownloadTarget.getInstance(context);
|
target = DownloadTarget.getInstance();
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mEntity = entity;
|
mEntity = entity;
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import android.os.Parcel;
|
|||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
|
|
||||||
import com.arialyy.downloadutil.orm.DbEntity;
|
import com.arialyy.downloadutil.orm.DbEntity;
|
||||||
|
import com.arialyy.downloadutil.orm.Ignore;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by lyy on 2015/12/25.
|
* Created by lyy on 2015/12/25.
|
||||||
@ -13,30 +14,37 @@ public class DownloadEntity extends DbEntity implements Parcelable, Cloneable {
|
|||||||
/**
|
/**
|
||||||
* 其它状态
|
* 其它状态
|
||||||
*/
|
*/
|
||||||
|
@Ignore
|
||||||
public static final int STATE_OTHER = -1;
|
public static final int STATE_OTHER = -1;
|
||||||
/**
|
/**
|
||||||
* 失败状态
|
* 失败状态
|
||||||
*/
|
*/
|
||||||
|
@Ignore
|
||||||
public static final int STATE_FAIL = 0;
|
public static final int STATE_FAIL = 0;
|
||||||
/**
|
/**
|
||||||
* 完成状态
|
* 完成状态
|
||||||
*/
|
*/
|
||||||
|
@Ignore
|
||||||
public static final int STATE_COMPLETE = 1;
|
public static final int STATE_COMPLETE = 1;
|
||||||
/**
|
/**
|
||||||
* 停止状态
|
* 停止状态
|
||||||
*/
|
*/
|
||||||
|
@Ignore
|
||||||
public static final int STATE_STOP = 2;
|
public static final int STATE_STOP = 2;
|
||||||
/**
|
/**
|
||||||
* 未开始状态
|
* 未开始状态
|
||||||
*/
|
*/
|
||||||
|
@Ignore
|
||||||
public static final int STATE_WAIT = 3;
|
public static final int STATE_WAIT = 3;
|
||||||
/**
|
/**
|
||||||
* 下载中
|
* 下载中
|
||||||
*/
|
*/
|
||||||
|
@Ignore
|
||||||
public static final int STATE_DOWNLOAD_ING = 4;
|
public static final int STATE_DOWNLOAD_ING = 4;
|
||||||
/**
|
/**
|
||||||
* 取消下载
|
* 取消下载
|
||||||
*/
|
*/
|
||||||
|
@Ignore
|
||||||
public static final int STATE_CANCEL = 5;
|
public static final int STATE_CANCEL = 5;
|
||||||
|
|
||||||
private String downloadUrl; //下载路径
|
private String downloadUrl; //下载路径
|
||||||
@ -50,6 +58,8 @@ public class DownloadEntity extends DbEntity implements Parcelable, Cloneable {
|
|||||||
private long currentProgress = 0; //当前下载进度
|
private long currentProgress = 0; //当前下载进度
|
||||||
private int failNum = 0;
|
private int failNum = 0;
|
||||||
|
|
||||||
|
public DownloadEntity(){}
|
||||||
|
|
||||||
public String getStr() {
|
public String getStr() {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
@ -134,9 +144,6 @@ public class DownloadEntity extends DbEntity implements Parcelable, Cloneable {
|
|||||||
return (DownloadEntity) super.clone();
|
return (DownloadEntity) super.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
public DownloadEntity() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public int describeContents() {
|
@Override public int describeContents() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -165,6 +172,7 @@ public class DownloadEntity extends DbEntity implements Parcelable, Cloneable {
|
|||||||
this.failNum = in.readInt();
|
this.failNum = in.readInt();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Ignore
|
||||||
public static final Creator<DownloadEntity> CREATOR = new Creator<DownloadEntity>() {
|
public static final Creator<DownloadEntity> CREATOR = new Creator<DownloadEntity>() {
|
||||||
@Override public DownloadEntity createFromParcel(Parcel source) {
|
@Override public DownloadEntity createFromParcel(Parcel source) {
|
||||||
return new DownloadEntity(source);
|
return new DownloadEntity(source);
|
||||||
|
@ -1,17 +1,9 @@
|
|||||||
package com.arialyy.downloadutil.orm;
|
package com.arialyy.downloadutil.orm;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.database.Cursor;
|
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
import com.arialyy.downloadutil.util.Util;
|
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -19,53 +11,18 @@ import java.util.List;
|
|||||||
* 所有数据库实体父类
|
* 所有数据库实体父类
|
||||||
*/
|
*/
|
||||||
public class DbEntity {
|
public class DbEntity {
|
||||||
private static final String TAG = "DbEntity";
|
protected int rowID = -1;
|
||||||
private volatile static SQLiteDatabase mDb = null;
|
private DbUtil mUtil = DbUtil.getInstance();
|
||||||
private volatile static DbUtil mUtil;
|
|
||||||
private Context mContext;
|
|
||||||
private static final Object LOCK = new Object();
|
|
||||||
protected int rowID = -1;
|
|
||||||
|
|
||||||
protected DbEntity() {
|
protected DbEntity() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public DbEntity(Context context) {
|
|
||||||
this(context, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public DbEntity(Context context, boolean newTable) {
|
|
||||||
mContext = context;
|
|
||||||
init(newTable);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void init(boolean newTable) {
|
|
||||||
if (mDb == null) {
|
|
||||||
synchronized (LOCK) {
|
|
||||||
if (mDb == null) {
|
|
||||||
SqlHelper mHelper = new SqlHelper(mContext);
|
|
||||||
mDb = mHelper.getWritableDatabase();
|
|
||||||
mUtil = DbUtil.getInstance(mDb);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (newTable && !mUtil.tableExists(this)) {
|
|
||||||
mUtil.createTable(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取所有行的rowid
|
* 获取所有行的rowid
|
||||||
*/
|
*/
|
||||||
public int[] getRowId() {
|
public int[] getRowId() {
|
||||||
Cursor cursor = mUtil.getRowId(this);
|
return mUtil.getRowId(this);
|
||||||
int[] ids = new int[cursor.getCount()];
|
|
||||||
int i = 0;
|
|
||||||
while (cursor.moveToNext()) {
|
|
||||||
ids[i] = cursor.getInt(cursor.getColumnIndex("rowid"));
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return ids;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -96,10 +53,47 @@ public class DbEntity {
|
|||||||
mUtil.modifyData(this);
|
mUtil.modifyData(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存自身,如果表中已经有数据,则更新数据,否则插入数据
|
||||||
|
*/
|
||||||
|
public void save() {
|
||||||
|
if (thisIsExist()) {
|
||||||
|
update();
|
||||||
|
} else {
|
||||||
|
insert();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查找数据在表中是否存在
|
||||||
|
*/
|
||||||
|
private boolean thisIsExist() {
|
||||||
|
try {
|
||||||
|
Field[] fields = getClass().getFields();
|
||||||
|
List<String> where = new ArrayList<>();
|
||||||
|
List<String> values = new ArrayList<>();
|
||||||
|
for (Field field : fields) {
|
||||||
|
field.setAccessible(true);
|
||||||
|
Ignore ignore = field.getAnnotation(Ignore.class);
|
||||||
|
if (ignore != null && ignore.value()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
where.add(field.getName());
|
||||||
|
values.add((String) field.get(getClass()));
|
||||||
|
}
|
||||||
|
return findData(getClass(), (String[]) where.toArray(),
|
||||||
|
(String[]) values.toArray()) != null;
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 插入数据
|
* 插入数据
|
||||||
*/
|
*/
|
||||||
public void save() {
|
public void insert() {
|
||||||
mUtil.insertData(this);
|
mUtil.insertData(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,10 +103,7 @@ public class DbEntity {
|
|||||||
* @return 没有数据返回null
|
* @return 没有数据返回null
|
||||||
*/
|
*/
|
||||||
public <T extends DbEntity> List<T> findAllData(Class<T> clazz) {
|
public <T extends DbEntity> List<T> findAllData(Class<T> clazz) {
|
||||||
Cursor cursor = mUtil.findAllData(this);
|
return mUtil.findAllData(clazz, this);
|
||||||
return cursor.getCount() > 0 ?
|
|
||||||
newInstanceEntity(clazz, cursor) :
|
|
||||||
null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -120,12 +111,9 @@ public class DbEntity {
|
|||||||
*
|
*
|
||||||
* @return 没有数据返回null
|
* @return 没有数据返回null
|
||||||
*/
|
*/
|
||||||
public <T extends DbEntity> List<T> findDatas(Class<T> clazz, @NonNull Object[] wheres,
|
public <T extends DbEntity> List<T> findDatas(Class<T> clazz, @NonNull String[] wheres,
|
||||||
@NonNull Object[] values) {
|
@NonNull String[] values) {
|
||||||
Cursor cursor = mUtil.findData(this, wheres, values);
|
return mUtil.findData(clazz, this, wheres, values);
|
||||||
return cursor.getCount() > 0 ?
|
|
||||||
newInstanceEntity(clazz, cursor) :
|
|
||||||
null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -133,71 +121,9 @@ public class DbEntity {
|
|||||||
*
|
*
|
||||||
* @return 没有数据返回null
|
* @return 没有数据返回null
|
||||||
*/
|
*/
|
||||||
public <T extends DbEntity> T findData(Class<T> clazz, @NonNull Object[] wheres,
|
public <T extends DbEntity> T findData(Class<T> clazz, @NonNull String[] wheres,
|
||||||
@NonNull Object[] values) {
|
@NonNull String[] values) {
|
||||||
Cursor cursor = mUtil.findData(this, wheres, values);
|
List<T> datas = mUtil.findData(clazz, this, wheres, values);
|
||||||
return cursor.getCount() > 0 ?
|
return datas.size() > 0 ? datas.get(0) : null;
|
||||||
newInstanceEntity(clazz, cursor).get(0) :
|
|
||||||
null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据数据游标创建一个具体的对象
|
|
||||||
*/
|
|
||||||
private <T extends DbEntity> List<T> newInstanceEntity(Class<T> clazz, Cursor cursor) {
|
|
||||||
Field[] fields = Util.getFields(clazz);
|
|
||||||
List<T> entitys = new ArrayList<>();
|
|
||||||
if (fields != null && fields.length > 0) {
|
|
||||||
try {
|
|
||||||
while (cursor.moveToNext()) {
|
|
||||||
Class[] paramTypes = {Context.class,
|
|
||||||
boolean.class};
|
|
||||||
Object[] params = {mContext,
|
|
||||||
false};
|
|
||||||
Constructor<T> con = clazz.getConstructor(paramTypes);
|
|
||||||
T entity = con.newInstance(params);
|
|
||||||
for (Field field : fields) {
|
|
||||||
field.setAccessible(true);
|
|
||||||
Ignore ignore = field.getAnnotation(Ignore.class);
|
|
||||||
if (ignore != null && ignore.value()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Class<?> type = field.getType();
|
|
||||||
int column = cursor.getColumnIndex(field.getName());
|
|
||||||
if (type == String.class) {
|
|
||||||
field.set(entity, cursor.getString(column));
|
|
||||||
} else if (type == int.class || type == Integer.class) {
|
|
||||||
field.setInt(entity, cursor.getInt(column));
|
|
||||||
} else if (type == float.class || type == Float.class) {
|
|
||||||
field.setFloat(entity, cursor.getFloat(column));
|
|
||||||
} else if (type == double.class || type == Double.class) {
|
|
||||||
field.setDouble(entity, cursor.getDouble(column));
|
|
||||||
} else if (type == long.class || type == Long.class) {
|
|
||||||
field.setLong(entity, cursor.getLong(column));
|
|
||||||
} else if (type == boolean.class || type == Boolean.class) {
|
|
||||||
field.setBoolean(entity,
|
|
||||||
!cursor.getString(column).equalsIgnoreCase("false"));
|
|
||||||
} else if (type == java.util.Date.class || type == java.sql.Date.class) {
|
|
||||||
field.set(entity, new Date(cursor.getString(column)));
|
|
||||||
} else if (type == byte[].class) {
|
|
||||||
field.set(entity, cursor.getBlob(column));
|
|
||||||
}
|
|
||||||
// field.set(entity, cursor.getColumnIndex("entity_id"));
|
|
||||||
}
|
|
||||||
entity.rowID = cursor.getInt(cursor.getColumnIndex("rowid"));
|
|
||||||
entitys.add(entity);
|
|
||||||
}
|
|
||||||
} catch (InstantiationException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (NoSuchMethodException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (InvocationTargetException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cursor.close();
|
|
||||||
return entitys;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.arialyy.downloadutil.orm;
|
package com.arialyy.downloadutil.orm;
|
||||||
|
|
||||||
|
import android.app.Application;
|
||||||
|
import android.content.Context;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
@ -8,6 +10,9 @@ import android.util.Log;
|
|||||||
import com.arialyy.downloadutil.util.Util;
|
import com.arialyy.downloadutil.util.Util;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by AriaLyy on 2015/2/11.
|
* Created by AriaLyy on 2015/2/11.
|
||||||
@ -15,7 +20,8 @@ import java.lang.reflect.Field;
|
|||||||
*/
|
*/
|
||||||
public class DbUtil {
|
public class DbUtil {
|
||||||
private static final String TAG = "DbUtil";
|
private static final String TAG = "DbUtil";
|
||||||
private volatile static DbUtil mDbUtil = null;
|
private volatile static DbUtil INSTANCE = null;
|
||||||
|
private static final Object LOCK = new Object();
|
||||||
private int CREATE_TABLE = 0;
|
private int CREATE_TABLE = 0;
|
||||||
private int TABLE_EXISTS = 1;
|
private int TABLE_EXISTS = 1;
|
||||||
private int INSERT_DATA = 2;
|
private int INSERT_DATA = 2;
|
||||||
@ -24,32 +30,40 @@ public class DbUtil {
|
|||||||
private int FIND_ALL_DATA = 5;
|
private int FIND_ALL_DATA = 5;
|
||||||
private int DEL_DATA = 6;
|
private int DEL_DATA = 6;
|
||||||
private int ROW_ID = 7;
|
private int ROW_ID = 7;
|
||||||
private static final Object LOCK = new Object();
|
|
||||||
private SQLiteDatabase mDb;
|
private SQLiteDatabase mDb;
|
||||||
|
private SqlHelper mHelper;
|
||||||
|
|
||||||
private DbUtil() {
|
private DbUtil() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private DbUtil(SQLiteDatabase db) {
|
public static DbUtil init(Context context) {
|
||||||
mDb = db;
|
if (context instanceof Application) {
|
||||||
}
|
|
||||||
|
|
||||||
protected static DbUtil getInstance(SQLiteDatabase db) {
|
|
||||||
if (mDbUtil == null) {
|
|
||||||
synchronized (LOCK) {
|
synchronized (LOCK) {
|
||||||
if (mDbUtil == null) {
|
if (INSTANCE == null) {
|
||||||
mDbUtil = new DbUtil(db);
|
INSTANCE = new DbUtil(context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return mDbUtil;
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
private DbUtil(Context context) {
|
||||||
|
mHelper = new SqlHelper(context.getApplicationContext());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static DbUtil getInstance() {
|
||||||
|
if (INSTANCE == null) {
|
||||||
|
throw new NullPointerException("请在Application中调用init进行数据库工具注册注册");
|
||||||
|
}
|
||||||
|
return INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除某条数据
|
* 删除某条数据
|
||||||
*/
|
*/
|
||||||
protected void delData(DbEntity dbEntity, @NonNull Object[] wheres, @NonNull Object[] values) {
|
protected void delData(DbEntity dbEntity, @NonNull Object[] wheres, @NonNull Object[] values) {
|
||||||
|
mDb = mHelper.getWritableDatabase();
|
||||||
if (wheres.length <= 0 || values.length <= 0) {
|
if (wheres.length <= 0 || values.length <= 0) {
|
||||||
Log.e(TAG, "输入删除条件");
|
Log.e(TAG, "输入删除条件");
|
||||||
return;
|
return;
|
||||||
@ -62,19 +76,19 @@ public class DbUtil {
|
|||||||
int i = 0;
|
int i = 0;
|
||||||
for (Object where : wheres) {
|
for (Object where : wheres) {
|
||||||
sb.append(where).append("=").append("'").append(values[i]).append("'");
|
sb.append(where).append("=").append("'").append(values[i]).append("'");
|
||||||
sb.append(i >= wheres.length - 1 ?
|
sb.append(i >= wheres.length - 1 ? "" : ",");
|
||||||
"" :
|
|
||||||
",");
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
print(DEL_DATA, sb.toString());
|
print(DEL_DATA, sb.toString());
|
||||||
mDb.execSQL(sb.toString());
|
mDb.execSQL(sb.toString());
|
||||||
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改某行数据
|
* 修改某行数据
|
||||||
*/
|
*/
|
||||||
protected void modifyData(DbEntity dbEntity) {
|
protected void modifyData(DbEntity dbEntity) {
|
||||||
|
mDb = mHelper.getWritableDatabase();
|
||||||
Class<?> clazz = dbEntity.getClass();
|
Class<?> clazz = dbEntity.getClass();
|
||||||
Field[] fields = Util.getFields(clazz);
|
Field[] fields = Util.getFields(clazz);
|
||||||
if (fields != null && fields.length > 0) {
|
if (fields != null && fields.length > 0) {
|
||||||
@ -87,14 +101,12 @@ public class DbUtil {
|
|||||||
if (ignore != null && ignore.value()) {
|
if (ignore != null && ignore.value()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
sb.append(i > 0 ?
|
sb.append(i > 0 ? ", " : "");
|
||||||
", " :
|
|
||||||
"");
|
|
||||||
try {
|
try {
|
||||||
sb.append(field.getName())
|
sb.append(field.getName())
|
||||||
.append(" = '")
|
.append(" = '")
|
||||||
.append(field.get(dbEntity).toString())
|
.append(field.get(dbEntity).toString())
|
||||||
.append("'");
|
.append("'");
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -103,23 +115,28 @@ public class DbUtil {
|
|||||||
print(MODIFY_DATA, sb.toString());
|
print(MODIFY_DATA, sb.toString());
|
||||||
mDb.execSQL(sb.toString());
|
mDb.execSQL(sb.toString());
|
||||||
}
|
}
|
||||||
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 遍历所有数据
|
* 遍历所有数据
|
||||||
*/
|
*/
|
||||||
protected Cursor findAllData(DbEntity dbEntity) {
|
protected <T extends DbEntity> List<T> findAllData(Class<T> clazz, DbEntity dbEntity) {
|
||||||
|
mDb = mHelper.getReadableDatabase();
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("SELECT rowid, * FROM ").append(Util.getClassName(dbEntity));
|
sb.append("SELECT rowid, * FROM ").append(Util.getClassName(dbEntity));
|
||||||
print(FIND_ALL_DATA, sb.toString());
|
print(FIND_ALL_DATA, sb.toString());
|
||||||
return mDb.rawQuery(sb.toString(), null);
|
Cursor cursor = mDb.rawQuery(sb.toString(), null);
|
||||||
|
return cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 条件查寻数据
|
* 条件查寻数据
|
||||||
*/
|
*/
|
||||||
protected Cursor findData(DbEntity dbEntity, @NonNull Object[] wheres,
|
protected <T extends DbEntity> List<T> findData(Class<T> clazz, DbEntity dbEntity,
|
||||||
@NonNull Object[] values) {
|
@NonNull String[] wheres,
|
||||||
|
@NonNull String[] values) {
|
||||||
|
mDb = mHelper.getReadableDatabase();
|
||||||
if (wheres.length <= 0 || values.length <= 0) {
|
if (wheres.length <= 0 || values.length <= 0) {
|
||||||
Log.e(TAG, "请输入查询条件");
|
Log.e(TAG, "请输入查询条件");
|
||||||
return null;
|
return null;
|
||||||
@ -132,19 +149,22 @@ public class DbUtil {
|
|||||||
int i = 0;
|
int i = 0;
|
||||||
for (Object where : wheres) {
|
for (Object where : wheres) {
|
||||||
sb.append(where).append("=").append("'").append(values[i]).append("'");
|
sb.append(where).append("=").append("'").append(values[i]).append("'");
|
||||||
sb.append(i >= wheres.length - 1 ?
|
sb.append(i >= wheres.length - 1 ? "" : ", ");
|
||||||
"" :
|
|
||||||
", ");
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
print(FIND_DATA, sb.toString());
|
print(FIND_DATA, sb.toString());
|
||||||
return mDb.rawQuery(sb.toString(), null);
|
Cursor cursor = mDb.rawQuery(sb.toString(), null);
|
||||||
|
return cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 插入数据
|
* 插入数据
|
||||||
*/
|
*/
|
||||||
protected void insertData(DbEntity dbEntity) {
|
protected void insertData(DbEntity dbEntity) {
|
||||||
|
mDb = mHelper.getWritableDatabase();
|
||||||
|
if (!tableExists(dbEntity)) {
|
||||||
|
createTable(dbEntity);
|
||||||
|
}
|
||||||
Class<?> clazz = dbEntity.getClass();
|
Class<?> clazz = dbEntity.getClass();
|
||||||
Field[] fields = Util.getFields(clazz);
|
Field[] fields = Util.getFields(clazz);
|
||||||
if (fields != null && fields.length > 0) {
|
if (fields != null && fields.length > 0) {
|
||||||
@ -157,9 +177,7 @@ public class DbUtil {
|
|||||||
if (ignore != null && ignore.value()) {
|
if (ignore != null && ignore.value()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
sb.append(i > 0 ?
|
sb.append(i > 0 ? ", " : "");
|
||||||
", " :
|
|
||||||
"");
|
|
||||||
sb.append(field.getName());
|
sb.append(field.getName());
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
@ -171,9 +189,7 @@ public class DbUtil {
|
|||||||
if (ignore != null && ignore.value()) {
|
if (ignore != null && ignore.value()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
sb.append(i > 0 ?
|
sb.append(i > 0 ? ", " : "");
|
||||||
", " :
|
|
||||||
"");
|
|
||||||
sb.append("'");
|
sb.append("'");
|
||||||
try {
|
try {
|
||||||
sb.append(field.get(dbEntity)).append("'");
|
sb.append(field.get(dbEntity)).append("'");
|
||||||
@ -186,12 +202,13 @@ public class DbUtil {
|
|||||||
print(INSERT_DATA, sb.toString());
|
print(INSERT_DATA, sb.toString());
|
||||||
mDb.execSQL(sb.toString());
|
mDb.execSQL(sb.toString());
|
||||||
}
|
}
|
||||||
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查找某张表是否存在
|
* 查找某张表是否存在
|
||||||
*/
|
*/
|
||||||
protected boolean tableExists(DbEntity dbEntity) {
|
public boolean tableExists(DbEntity dbEntity) {
|
||||||
Cursor cursor = null;
|
Cursor cursor = null;
|
||||||
try {
|
try {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
@ -217,19 +234,15 @@ public class DbUtil {
|
|||||||
/**
|
/**
|
||||||
* 创建表
|
* 创建表
|
||||||
*/
|
*/
|
||||||
protected void createTable(DbEntity dbEntity) {
|
private void createTable(DbEntity dbEntity) {
|
||||||
Field[] fields = Util.getFields(dbEntity.getClass());
|
Field[] fields = Util.getFields(dbEntity.getClass());
|
||||||
if (fields != null && fields.length > 0) {
|
if (fields != null && fields.length > 0) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("create table ").append(Util.getClassName(dbEntity)).append("(");
|
sb.append("create table ").append(Util.getClassName(dbEntity)).append("(");
|
||||||
int i = 0;
|
|
||||||
int ignoreNum = 0;
|
|
||||||
for (Field field : fields) {
|
for (Field field : fields) {
|
||||||
i++;
|
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
Ignore ignore = field.getAnnotation(Ignore.class);
|
Ignore ignore = field.getAnnotation(Ignore.class);
|
||||||
if (ignore != null && ignore.value()) {
|
if (ignore != null && ignore.value()) {
|
||||||
ignoreNum++;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
sb.append(field.getName());
|
sb.append(field.getName());
|
||||||
@ -251,13 +264,12 @@ public class DbUtil {
|
|||||||
} else {
|
} else {
|
||||||
sb.append(" blob");
|
sb.append(" blob");
|
||||||
}
|
}
|
||||||
sb.append(i >= fields.length - ignoreNum - 1 ?
|
sb.append(",");
|
||||||
"" :
|
|
||||||
", ");
|
|
||||||
}
|
}
|
||||||
sb.append(");");
|
String str = sb.toString();
|
||||||
print(CREATE_TABLE, sb.toString());
|
str = str.substring(0, str.length() - 1) + ");";
|
||||||
mDb.execSQL(sb.toString());
|
print(CREATE_TABLE, str);
|
||||||
|
mDb.execSQL(str);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -294,25 +306,34 @@ public class DbUtil {
|
|||||||
/**
|
/**
|
||||||
* 关闭数据库
|
* 关闭数据库
|
||||||
*/
|
*/
|
||||||
protected void close() {
|
private void close() {
|
||||||
if (mDb != null) {
|
if (mDb != null) {
|
||||||
mDb.close();
|
mDb.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取所有行Id
|
* 获取所在行Id
|
||||||
*/
|
*/
|
||||||
protected Cursor getRowId(DbEntity dbEntity) {
|
protected int[] getRowId(DbEntity dbEntity) {
|
||||||
StringBuilder sb = new StringBuilder();
|
mDb = mHelper.getReadableDatabase();
|
||||||
sb.append("SELECT rowid, * FROM ").append(Util.getClassName(dbEntity));
|
Cursor cursor = mDb.rawQuery("SELECT rowid, * FROM " + Util.getClassName(dbEntity), null);
|
||||||
return mDb.rawQuery(sb.toString(), null);
|
int[] ids = new int[cursor.getCount()];
|
||||||
|
int i = 0;
|
||||||
|
while (cursor.moveToNext()) {
|
||||||
|
ids[i] = cursor.getInt(cursor.getColumnIndex("rowid"));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
cursor.close();
|
||||||
|
close();
|
||||||
|
return ids;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取行Id
|
* 获取行Id
|
||||||
*/
|
*/
|
||||||
protected int getRowId(DbEntity dbEntity, Object[] wheres, Object[] values) {
|
protected int getRowId(DbEntity dbEntity, Object[] wheres, Object[] values) {
|
||||||
|
mDb = mHelper.getReadableDatabase();
|
||||||
if (wheres.length <= 0 || values.length <= 0) {
|
if (wheres.length <= 0 || values.length <= 0) {
|
||||||
Log.e(TAG, "请输入删除条件");
|
Log.e(TAG, "请输入删除条件");
|
||||||
return -1;
|
return -1;
|
||||||
@ -325,15 +346,66 @@ public class DbUtil {
|
|||||||
int i = 0;
|
int i = 0;
|
||||||
for (Object where : wheres) {
|
for (Object where : wheres) {
|
||||||
sb.append(where).append("=").append("'").append(values[i]).append("'");
|
sb.append(where).append("=").append("'").append(values[i]).append("'");
|
||||||
sb.append(i >= wheres.length - 1 ?
|
sb.append(i >= wheres.length - 1 ? "" : ",");
|
||||||
"" :
|
|
||||||
",");
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
print(ROW_ID, sb.toString());
|
print(ROW_ID, sb.toString());
|
||||||
Cursor c = mDb.rawQuery(sb.toString(), null);
|
Cursor c = mDb.rawQuery(sb.toString(), null);
|
||||||
int id = c.getColumnIndex("rowid");
|
int id = c.getColumnIndex("rowid");
|
||||||
c.close();
|
c.close();
|
||||||
|
close();
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据数据游标创建一个具体的对象
|
||||||
|
*/
|
||||||
|
private <T extends DbEntity> List<T> newInstanceEntity(Class<T> clazz, Cursor cursor) {
|
||||||
|
Field[] fields = Util.getFields(clazz);
|
||||||
|
List<T> entitys = new ArrayList<>();
|
||||||
|
if (fields != null && fields.length > 0) {
|
||||||
|
try {
|
||||||
|
while (cursor.moveToNext()) {
|
||||||
|
T entity = clazz.newInstance();
|
||||||
|
for (Field field : fields) {
|
||||||
|
field.setAccessible(true);
|
||||||
|
Ignore ignore = field.getAnnotation(Ignore.class);
|
||||||
|
if (ignore != null && ignore.value()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Class<?> type = field.getType();
|
||||||
|
int column = cursor.getColumnIndex(field.getName());
|
||||||
|
if (type == String.class) {
|
||||||
|
field.set(entity, cursor.getString(column));
|
||||||
|
} else if (type == int.class || type == Integer.class) {
|
||||||
|
field.setInt(entity, cursor.getInt(column));
|
||||||
|
} else if (type == float.class || type == Float.class) {
|
||||||
|
field.setFloat(entity, cursor.getFloat(column));
|
||||||
|
} else if (type == double.class || type == Double.class) {
|
||||||
|
field.setDouble(entity, cursor.getDouble(column));
|
||||||
|
} else if (type == long.class || type == Long.class) {
|
||||||
|
field.setLong(entity, cursor.getLong(column));
|
||||||
|
} else if (type == boolean.class || type == Boolean.class) {
|
||||||
|
field.setBoolean(entity,
|
||||||
|
!cursor.getString(column).equalsIgnoreCase("false"));
|
||||||
|
} else if (type == java.util.Date.class || type == java.sql.Date.class) {
|
||||||
|
field.set(entity, new Date(cursor.getString(column)));
|
||||||
|
} else if (type == byte[].class) {
|
||||||
|
field.set(entity, cursor.getBlob(column));
|
||||||
|
}
|
||||||
|
// field.set(entity, cursor.getColumnIndex("entity_id"));
|
||||||
|
}
|
||||||
|
entity.rowID = cursor.getInt(cursor.getColumnIndex("rowid"));
|
||||||
|
entitys.add(entity);
|
||||||
|
}
|
||||||
|
} catch (InstantiationException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cursor.close();
|
||||||
|
close();
|
||||||
|
return entitys;
|
||||||
|
}
|
||||||
}
|
}
|
@ -10,5 +10,5 @@ import java.lang.annotation.Target;
|
|||||||
* 忽略某个字段
|
* 忽略某个字段
|
||||||
*/
|
*/
|
||||||
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Ignore {
|
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Ignore {
|
||||||
boolean value() default false;
|
boolean value() default true;
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@
|
|||||||
// Cursor c = db.query(TABLE_NAME, null, null, null, null, null, null);
|
// Cursor c = db.query(TABLE_NAME, null, null, null, null, null, null);
|
||||||
// if (c.moveToFirst()) {
|
// if (c.moveToFirst()) {
|
||||||
// while (c.moveToNext()) {
|
// while (c.moveToNext()) {
|
||||||
// list.add(cursor2Entity(c));
|
// list.insert(cursor2Entity(c));
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// c.close();
|
// c.close();
|
||||||
|
Reference in New Issue
Block a user