From d6529b5646850e06b174905594f38876caccd7fc Mon Sep 17 00:00:00 2001 From: yulin2 Date: Sat, 18 Oct 2014 13:01:48 -0500 Subject: [PATCH 01/13] Update NotificationHandler.java put bitmap decode from file into background thread. --- .../sync/NotificationHandler.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/com/github/andlyticsproject/sync/NotificationHandler.java b/src/com/github/andlyticsproject/sync/NotificationHandler.java index c570ef57..dbbc3fca 100644 --- a/src/com/github/andlyticsproject/sync/NotificationHandler.java +++ b/src/com/github/andlyticsproject/sync/NotificationHandler.java @@ -12,6 +12,7 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; +import android.os.AsyncTask; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat.BigTextStyle; import android.support.v4.app.NotificationCompat.Builder; @@ -121,14 +122,24 @@ public static void handleNotificaions(Context context, List diffs, Preferences.NOTIFICATION_WHEN_ACCOUNT_VISISBLE)) { // The user can choose not to see notifications if the current account is visible - Builder builder = new NotificationCompat.Builder(context); + final Builder builder = new NotificationCompat.Builder(context); builder.setSmallIcon(R.drawable.statusbar_andlytics); builder.setContentTitle(contentTitle); builder.setContentText(contentText); File iconFilePath = new File(context.getCacheDir(), iconName); if (iconFilePath.exists()) { - Bitmap bm = BitmapFactory.decodeFile(iconFilePath.getAbsolutePath()); - builder.setLargeIcon(bm); + new AsyncTask() { + @Override + protected Bitmap doInBackground(File... args) { + Bitmap bm = BitmapFactory.decodeFile(args[0].getAbsolutePath()); + return bm; + } + + @Override + protected void onPostExecute(Bitmap bm) { + builder.setLargeIcon(bm); + } + }.execute(iconFilePath); } BigTextStyle style = new BigTextStyle(builder); style.bigText(contentText); From 52d407fc416134d1fdbd20b375818aee8d5c9947 Mon Sep 17 00:00:00 2001 From: yulin2 Date: Sat, 18 Oct 2014 13:06:07 -0500 Subject: [PATCH 02/13] Update AppInfoActivity.java put bitmap decode from file into background thread. --- .../andlyticsproject/AppInfoActivity.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/com/github/andlyticsproject/AppInfoActivity.java b/src/com/github/andlyticsproject/AppInfoActivity.java index 25446bd0..bfb9d3a8 100644 --- a/src/com/github/andlyticsproject/AppInfoActivity.java +++ b/src/com/github/andlyticsproject/AppInfoActivity.java @@ -10,6 +10,7 @@ import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.net.Uri; +import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; @@ -78,9 +79,19 @@ public void onCreate(Bundle savedInstanceState) { } if (iconFilePath != null) { - Bitmap bm = BitmapFactory.decodeFile(iconFilePath); - BitmapDrawable icon = new BitmapDrawable(getResources(), bm); - getSupportActionBar().setIcon(icon); + new AsyncTask() { + @Override + protected Bitmap doInBackground(Void... params) { + Bitmap bm = BitmapFactory.decodeFile(iconFilePath); + return bm; + } + + @Override + protected void onPostExecute(Bitmap bm) { + BitmapDrawable icon = new BitmapDrawable(getResources(), bm); + getSupportActionBar().setIcon(icon); + } + }.execute(); } LayoutInflater layoutInflater = getLayoutInflater(); From ae12ede5804075080fd371fe6b8b16e2df810e20 Mon Sep 17 00:00:00 2001 From: yulin2 Date: Sat, 18 Oct 2014 13:09:37 -0500 Subject: [PATCH 03/13] Update DetailsActivity.java put bitmap decode from file into background thread. --- .../andlyticsproject/DetailsActivity.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/com/github/andlyticsproject/DetailsActivity.java b/src/com/github/andlyticsproject/DetailsActivity.java index 10fc6e99..7b6b2d92 100755 --- a/src/com/github/andlyticsproject/DetailsActivity.java +++ b/src/com/github/andlyticsproject/DetailsActivity.java @@ -5,6 +5,7 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; +import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; @@ -95,12 +96,22 @@ protected void onCreate(Bundle savedInstanceState) { appName = getDbAdapter().getAppName(packageName); hasRevenue = getIntent().getBooleanExtra(EXTRA_HAS_REVENUE, true); - ActionBar actionBar = getSupportActionBar(); + final ActionBar actionBar = getSupportActionBar(); if (iconFilePath != null) { - Bitmap bm = BitmapFactory.decodeFile(iconFilePath); - BitmapDrawable icon = new BitmapDrawable(getResources(), bm); - actionBar.setIcon(icon); + new AsyncTask() { + @Override + protected Bitmap doInBackground(Void... params) { + Bitmap bm = BitmapFactory.decodeFile(iconFilePath); + return bm; + } + + @Override + protected void onPostExecute(Bitmap bm) { + BitmapDrawable icon = new BitmapDrawable(getResources(), bm); + actionBar.setIcon(icon); + } + }.execute(); } actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); From 042eae79a973b21792e0516c44102d0366e5d4ff Mon Sep 17 00:00:00 2001 From: yulin2 Date: Sat, 18 Oct 2014 13:18:37 -0500 Subject: [PATCH 04/13] Update FileUtils.java put write to files into background thread. --- .../andlyticsproject/util/FileUtils.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/com/github/andlyticsproject/util/FileUtils.java b/src/com/github/andlyticsproject/util/FileUtils.java index fd8d5057..f459bee4 100755 --- a/src/com/github/andlyticsproject/util/FileUtils.java +++ b/src/com/github/andlyticsproject/util/FileUtils.java @@ -23,14 +23,18 @@ private FileUtils() { } public static void writeToFile(File file, String str) { - try { - FileOutputStream out = new FileOutputStream(file); - out.write(str.getBytes("UTF-8")); - out.flush(); - FileUtils.closeSilently(out); - } catch (IOException e) { - throw new RuntimeException(e); - } + new Thread() { + public void run() { + try { + FileOutputStream out = new FileOutputStream(file); + out.write(str.getBytes("UTF-8")); + out.flush(); + FileUtils.closeSilently(out); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + }.start(); } public static void writeToExternalStorage(String filename, String str) { From 4530f8171af4d12dbb07dcb3d4f8d0317b3947ab Mon Sep 17 00:00:00 2001 From: yulin2 Date: Sun, 19 Oct 2014 11:46:04 -0500 Subject: [PATCH 05/13] remove asynctask in NotificationHandler --- .../sync/NotificationHandler.java | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/com/github/andlyticsproject/sync/NotificationHandler.java b/src/com/github/andlyticsproject/sync/NotificationHandler.java index dbbc3fca..f9bc4e3f 100644 --- a/src/com/github/andlyticsproject/sync/NotificationHandler.java +++ b/src/com/github/andlyticsproject/sync/NotificationHandler.java @@ -12,7 +12,6 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; -import android.os.AsyncTask; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat.BigTextStyle; import android.support.v4.app.NotificationCompat.Builder; @@ -128,18 +127,8 @@ public static void handleNotificaions(Context context, List diffs, builder.setContentText(contentText); File iconFilePath = new File(context.getCacheDir(), iconName); if (iconFilePath.exists()) { - new AsyncTask() { - @Override - protected Bitmap doInBackground(File... args) { - Bitmap bm = BitmapFactory.decodeFile(args[0].getAbsolutePath()); - return bm; - } - - @Override - protected void onPostExecute(Bitmap bm) { - builder.setLargeIcon(bm); - } - }.execute(iconFilePath); + Bitmap bm = BitmapFactory.decodeFile(iconFilePath.getAbsolutePath()); + builder.setLargeIcon(bm); } BigTextStyle style = new BigTextStyle(builder); style.bigText(contentText); From 992ae8e7019aa275e4f611c31450e2d2871c34ea Mon Sep 17 00:00:00 2001 From: yulin2 Date: Sun, 19 Oct 2014 11:47:10 -0500 Subject: [PATCH 06/13] remove final --- src/com/github/andlyticsproject/sync/NotificationHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/github/andlyticsproject/sync/NotificationHandler.java b/src/com/github/andlyticsproject/sync/NotificationHandler.java index f9bc4e3f..c570ef57 100644 --- a/src/com/github/andlyticsproject/sync/NotificationHandler.java +++ b/src/com/github/andlyticsproject/sync/NotificationHandler.java @@ -121,7 +121,7 @@ public static void handleNotificaions(Context context, List diffs, Preferences.NOTIFICATION_WHEN_ACCOUNT_VISISBLE)) { // The user can choose not to see notifications if the current account is visible - final Builder builder = new NotificationCompat.Builder(context); + Builder builder = new NotificationCompat.Builder(context); builder.setSmallIcon(R.drawable.statusbar_andlytics); builder.setContentTitle(contentTitle); builder.setContentText(contentText); From c8f5fc0d77ebd88f2027b54fae7eb0b42714e202 Mon Sep 17 00:00:00 2001 From: yulin2 Date: Sun, 19 Oct 2014 12:08:53 -0500 Subject: [PATCH 07/13] use DetachableAsyncTask instead of AsyncTask --- .../andlyticsproject/AppInfoActivity.java | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/com/github/andlyticsproject/AppInfoActivity.java b/src/com/github/andlyticsproject/AppInfoActivity.java index bfb9d3a8..7b65ed69 100644 --- a/src/com/github/andlyticsproject/AppInfoActivity.java +++ b/src/com/github/andlyticsproject/AppInfoActivity.java @@ -10,7 +10,6 @@ import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.net.Uri; -import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; @@ -79,19 +78,7 @@ public void onCreate(Bundle savedInstanceState) { } if (iconFilePath != null) { - new AsyncTask() { - @Override - protected Bitmap doInBackground(Void... params) { - Bitmap bm = BitmapFactory.decodeFile(iconFilePath); - return bm; - } - - @Override - protected void onPostExecute(Bitmap bm) { - BitmapDrawable icon = new BitmapDrawable(getResources(), bm); - getSupportActionBar().setIcon(icon); - } - }.execute(); + Utils.execute(new LoadBitmap(this)); } LayoutInflater layoutInflater = getLayoutInflater(); @@ -269,6 +256,32 @@ protected void onPostExecute(Void result) { } } + + private class LoadBitmap extends DetachableAsyncTask { + + LoadBitmap(AppInfoActivity activity) { + super(activity); + } + + @Override + protected Bitmap doInBackground(Void... params) { + if (activity == null) { + return null; + } + Bitmap bm = BitmapFactory.decodeFile(iconFilePath); + return bm; + } + + @Override + protected void onPostExecute(Bitmap bm) { + if (activity == null) { + return; + } + + BitmapDrawable icon = new BitmapDrawable(activity.getResources(), bm); + activity.getSupportActionBar().setIcon(icon); + } + } private void getLinksFromDb() { appInfo = db.findAppByPackageName(packageName); From 4f73d287f2f0dee4bad32d8f971fbd8bb0791367 Mon Sep 17 00:00:00 2001 From: yulin2 Date: Sun, 19 Oct 2014 12:15:14 -0500 Subject: [PATCH 08/13] use DetachableAsyncTask instead of AsyncTask --- .../andlyticsproject/DetailsActivity.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/com/github/andlyticsproject/DetailsActivity.java b/src/com/github/andlyticsproject/DetailsActivity.java index 7b6b2d92..e12c0b0f 100755 --- a/src/com/github/andlyticsproject/DetailsActivity.java +++ b/src/com/github/andlyticsproject/DetailsActivity.java @@ -5,7 +5,6 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; -import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; @@ -96,22 +95,30 @@ protected void onCreate(Bundle savedInstanceState) { appName = getDbAdapter().getAppName(packageName); hasRevenue = getIntent().getBooleanExtra(EXTRA_HAS_REVENUE, true); - final ActionBar actionBar = getSupportActionBar(); + ActionBar actionBar = getSupportActionBar(); if (iconFilePath != null) { - new AsyncTask() { + Utils.execute(new DetachableAsyncTask(this) { @Override protected Bitmap doInBackground(Void... params) { + if (activity == null) { + return null; + } + Bitmap bm = BitmapFactory.decodeFile(iconFilePath); return bm; } @Override protected void onPostExecute(Bitmap bm) { - BitmapDrawable icon = new BitmapDrawable(getResources(), bm); - actionBar.setIcon(icon); + if (activity == null) { + return; + } + + BitmapDrawable icon = new BitmapDrawable(activity.getResources(), bm); + activity.getSupportActionBar().setIcon(icon); } - }.execute(); + }); } actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); From fc04b82db0d8bdeca82c083a92cb1f309d000963 Mon Sep 17 00:00:00 2001 From: yulin2 Date: Sun, 19 Oct 2014 12:20:41 -0500 Subject: [PATCH 09/13] remove thread. writeToFile is already invoked in background thread. --- .../andlyticsproject/util/FileUtils.java | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/com/github/andlyticsproject/util/FileUtils.java b/src/com/github/andlyticsproject/util/FileUtils.java index f459bee4..fd8d5057 100755 --- a/src/com/github/andlyticsproject/util/FileUtils.java +++ b/src/com/github/andlyticsproject/util/FileUtils.java @@ -23,18 +23,14 @@ private FileUtils() { } public static void writeToFile(File file, String str) { - new Thread() { - public void run() { - try { - FileOutputStream out = new FileOutputStream(file); - out.write(str.getBytes("UTF-8")); - out.flush(); - FileUtils.closeSilently(out); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - }.start(); + try { + FileOutputStream out = new FileOutputStream(file); + out.write(str.getBytes("UTF-8")); + out.flush(); + FileUtils.closeSilently(out); + } catch (IOException e) { + throw new RuntimeException(e); + } } public static void writeToExternalStorage(String filename, String str) { From f87c98ca6578900f9341c3e9df18e7f5d654044e Mon Sep 17 00:00:00 2001 From: yulin2 Date: Mon, 20 Oct 2014 10:38:25 -0500 Subject: [PATCH 10/13] detach/attach LoadBitmap task --- .../andlyticsproject/AppInfoActivity.java | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/com/github/andlyticsproject/AppInfoActivity.java b/src/com/github/andlyticsproject/AppInfoActivity.java index 7b65ed69..afface4c 100644 --- a/src/com/github/andlyticsproject/AppInfoActivity.java +++ b/src/com/github/andlyticsproject/AppInfoActivity.java @@ -78,7 +78,14 @@ public void onCreate(Bundle savedInstanceState) { } if (iconFilePath != null) { - Utils.execute(new LoadBitmap(this)); + if (getLastCustomNonConfigurationInstance() != null) { + loadBitmap = (LoadBitmap) getLastCustomNonConfigurationInstance(); + loadBitmap.attach(this); + setSupportActionBarIcon(loadBitmap.bitmap); + } else { + loadBitmap = new LoadBitmap(this); + Utils.execute(loadBitmap, iconFilePath); + } } LayoutInflater layoutInflater = getLayoutInflater(); @@ -257,18 +264,21 @@ protected void onPostExecute(Void result) { } - private class LoadBitmap extends DetachableAsyncTask { + private static class LoadBitmap extends DetachableAsyncTask { + + Bitmap bitmap; LoadBitmap(AppInfoActivity activity) { super(activity); } @Override - protected Bitmap doInBackground(Void... params) { + protected Bitmap doInBackground(String... params) { if (activity == null) { return null; } - Bitmap bm = BitmapFactory.decodeFile(iconFilePath); + Bitmap bm = BitmapFactory.decodeFile(params[0]); + bitmap = bm; return bm; } @@ -278,10 +288,14 @@ protected void onPostExecute(Bitmap bm) { return; } - BitmapDrawable icon = new BitmapDrawable(activity.getResources(), bm); - activity.getSupportActionBar().setIcon(icon); + activity.setSupportActionBarIcon(bm); } } + + private void setSupportActionBarIcon(Bitmap bm) { + BitmapDrawable icon = new BitmapDrawable(getResources(), bm); + getSupportActionBar().setIcon(icon); + } private void getLinksFromDb() { appInfo = db.findAppByPackageName(packageName); From 5f6741d77ec6f75378a0f5d4ac262bf6a337bbc6 Mon Sep 17 00:00:00 2001 From: yulin2 Date: Mon, 20 Oct 2014 10:45:29 -0500 Subject: [PATCH 11/13] add loadBitmap field --- src/com/github/andlyticsproject/AppInfoActivity.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/com/github/andlyticsproject/AppInfoActivity.java b/src/com/github/andlyticsproject/AppInfoActivity.java index afface4c..70c57665 100644 --- a/src/com/github/andlyticsproject/AppInfoActivity.java +++ b/src/com/github/andlyticsproject/AppInfoActivity.java @@ -45,6 +45,7 @@ public class AppInfoActivity extends SherlockFragmentActivity implements private LinksListAdapter linksListAdapter; private LoadLinksDb loadLinksDb; + private LoadBitmap loadBitmap; private AppInfo appInfo; private List links; From 77e325586ddcf2de3bb4a812e35f8648c30fd571 Mon Sep 17 00:00:00 2001 From: yulin2 Date: Mon, 20 Oct 2014 10:58:05 -0500 Subject: [PATCH 12/13] attach/detach LoadBitmap --- .../andlyticsproject/DetailsActivity.java | 65 +++++++++++++------ 1 file changed, 45 insertions(+), 20 deletions(-) diff --git a/src/com/github/andlyticsproject/DetailsActivity.java b/src/com/github/andlyticsproject/DetailsActivity.java index e12c0b0f..09ef30a9 100755 --- a/src/com/github/andlyticsproject/DetailsActivity.java +++ b/src/com/github/andlyticsproject/DetailsActivity.java @@ -45,6 +45,8 @@ public class DetailsActivity extends BaseActivity implements DetailedStatsActivi private String appName; private boolean hasRevenue; + + private LoadBitmap loadBitmap; public static class TabListener> implements ActionBar.TabListener { @@ -98,27 +100,16 @@ protected void onCreate(Bundle savedInstanceState) { ActionBar actionBar = getSupportActionBar(); if (iconFilePath != null) { - Utils.execute(new DetachableAsyncTask(this) { - @Override - protected Bitmap doInBackground(Void... params) { - if (activity == null) { - return null; - } - - Bitmap bm = BitmapFactory.decodeFile(iconFilePath); - return bm; + if (getLastCustomNonConfigurationInstance() != null) { + loadBitmap = (LoadBitmap) getLastCustomNonConfigurationInstance(); + loadBitmap.attach(this); + if (loadBitmap.bitmap != null) { + setSupportActionBarIcon(loadBitmap.bitmap); } - - @Override - protected void onPostExecute(Bitmap bm) { - if (activity == null) { - return; - } - - BitmapDrawable icon = new BitmapDrawable(activity.getResources(), bm); - activity.getSupportActionBar().setIcon(icon); - } - }); + } else { + loadBitmap = new LoadBitmap(this); + Utils.execute(loadBitmap, iconFilePath); + } } actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); @@ -323,5 +314,39 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { } } } + + private static class LoadBitmap extends DetachableAsyncTask { + + Bitmap bitmap; + + LoadBitmap(DetailsActivity activity) { + super(activity); + } + + @Override + protected Bitmap doInBackground(String... params) { + if (activity == null) { + return null; + } + + Bitmap bm = BitmapFactory.decodeFile(params[0]); + bitmap = bm; + return bm; + } + + @Override + protected void onPostExecute(Bitmap bm) { + if (activity == null) { + return; + } + + activity.setSupportActionBarIcon(bm); + } + } + + private void setSupportActionBarIcon(Bitmap bm) { + BitmapDrawable icon = new BitmapDrawable(getResources(), bm); + getSupportActionBar().setIcon(icon); + } } From 5ef6eb98850eb248855aed67bfb6c5416c36ef1d Mon Sep 17 00:00:00 2001 From: yulin2 Date: Mon, 20 Oct 2014 10:58:50 -0500 Subject: [PATCH 13/13] add null check for attach --- src/com/github/andlyticsproject/AppInfoActivity.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/com/github/andlyticsproject/AppInfoActivity.java b/src/com/github/andlyticsproject/AppInfoActivity.java index 70c57665..f42e2897 100644 --- a/src/com/github/andlyticsproject/AppInfoActivity.java +++ b/src/com/github/andlyticsproject/AppInfoActivity.java @@ -82,7 +82,9 @@ public void onCreate(Bundle savedInstanceState) { if (getLastCustomNonConfigurationInstance() != null) { loadBitmap = (LoadBitmap) getLastCustomNonConfigurationInstance(); loadBitmap.attach(this); - setSupportActionBarIcon(loadBitmap.bitmap); + if (loadBitmap.bitmap != null) { + setSupportActionBarIcon(loadBitmap.bitmap); + } } else { loadBitmap = new LoadBitmap(this); Utils.execute(loadBitmap, iconFilePath);