Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

put db/disk accesses into background thread? #109

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions source/src/cn/eoe/app/ui/DetailsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public class DetailsActivity extends BaseActivity implements OnClickListener {
private DetailDB mDetailDB;
private String mKey;
private int mDBID = -1;

private AsyncTask<Void, Void, Void> initAppraiseTask;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -85,7 +87,13 @@ protected void onCreate(Bundle savedInstanceState) {
mKey = sharePre.getString(UserLoginUidActivity.KEY, "");
initData();
initControl();
initAppraise();
initAppraiseTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
initAppraise();
return null;
}
}.execute();
}

private void initData() {
Expand Down Expand Up @@ -193,16 +201,32 @@ protected void onPause() {
if (mKey.equals(null) && mKey.equals("")) {
return;
}
if (mDBID == -1 && (IsGood || IsBed || IsCollect)) {
// 添加
mDetailDB.insertSQL(mUrl, mKey, IsGood ? 1 : 0, IsBed ? 1 : 0,
IsCollect ? 1 : 0);
} else if (mDBID != -1) {
// 修改
mDetailDB.updateSQL(mDBID, IsGood ? 1 : 0, IsBed ? 1 : 0,
IsCollect ? 1 : 0);
}
mDetailDB.dbClose();
new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
// synchronize with initAppraiseTask
try {
initAppraiseTask.get();
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
protected Void doInBackground(Void... params) {
if (mDBID == -1 && (IsGood || IsBed || IsCollect)) {
// 添加
mDetailDB.insertSQL(mUrl, mKey, IsGood ? 1 : 0, IsBed ? 1 : 0,
IsCollect ? 1 : 0);
} else if (mDBID != -1) {
// 修改
mDetailDB.updateSQL(mDBID, IsGood ? 1 : 0, IsBed ? 1 : 0,
IsCollect ? 1 : 0);
}
mDetailDB.dbClose();
return null;
}
}.execute();
}

class MyTask extends AsyncTask<String, Integer, String> {
Expand Down Expand Up @@ -277,6 +301,14 @@ public void onClick(View v) {
showLongToast(getResources().getString(R.string.user_login_prompt));
return;
}

// synchronize with initAppraiseTask
try {
initAppraiseTask.get();
} catch (Exception e) {
e.printStackTrace();
}

String url = null;
switch (v.getId()) {
case R.id.rlGood:
Expand Down
32 changes: 21 additions & 11 deletions source/src/cn/eoe/app/utils/ImageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
Expand Down Expand Up @@ -116,16 +118,24 @@ public static boolean hasExternalCacheDir() {
private static void setThumbnailImage(ImageView view, String imageUrl,
String cachePath, DBHelper dbHelper, ImageCallback callback,
boolean b) {
Bitmap bitmap = null;
bitmap = ImageUtil.loadThumbnailImage(cachePath, imageUrl, dbHelper,
callback, b);
if (bitmap == null) {// 先查找数据库,再查找本地sd卡,若没有.再从网站加载,若网站上没有图片或错误时返回null
// 设置默认图片
view.setImageResource(Default_Img);
} else {
// 设置本地SD卡缓存图片
view.setImageBitmap(bitmap);
}
new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... params) {
return ImageUtil.loadThumbnailImage(cachePath, imageUrl, dbHelper,
callback, b);
}

@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap == null) {// 先查找数据库,再查找本地sd卡,若没有.再从网站加载,若网站上没有图片或错误时返回null
// 设置默认图片
view.setImageResource(Default_Img);
} else {
// 设置本地SD卡缓存图片
view.setImageBitmap(bitmap);
}
}
}.execute();
}

private static Bitmap getImageFromDB(String imagePath, String imageUrl,
Expand Down Expand Up @@ -297,7 +307,7 @@ public static Bitmap loadThumbnailImage(final String imagePath,
return bitmap;
} else {
// 从网上加载
final Handler handler = new Handler() {
final Handler handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
if (msg.obj != null) {
Expand Down