Browse Source

Use thread pool executors on AsyncTasks to increase performance

master
Anthony Restaino 9 years ago
parent
commit
5fd401c2c0
  1. 2
      app/src/main/java/acr/browser/lightning/activity/ReadingActivity.java
  2. 4
      app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java
  3. 25
      app/src/main/java/acr/browser/lightning/object/SearchAdapter.java

2
app/src/main/java/acr/browser/lightning/activity/ReadingActivity.java

@ -129,7 +129,7 @@ public class ReadingActivity extends AppCompatActivity {
} }
if (getSupportActionBar() != null) if (getSupportActionBar() != null)
getSupportActionBar().setTitle(Utils.getDomainName(mUrl)); getSupportActionBar().setTitle(Utils.getDomainName(mUrl));
new PageLoader(this).execute(mUrl); new PageLoader(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mUrl);
return true; return true;
} }

4
app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java

@ -3,6 +3,7 @@ package acr.browser.lightning.fragment;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.PorterDuff; import android.graphics.PorterDuff;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.IdRes; import android.support.annotation.IdRes;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@ -344,7 +345,8 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
if (web.isFolder()) { if (web.isFolder()) {
holder.favicon.setImageBitmap(mFolderBitmap); holder.favicon.setImageBitmap(mFolderBitmap);
} else if (web.getBitmap() == null) { } else if (web.getBitmap() == null) {
new DownloadImageTask(holder.favicon, web, mWebpageBitmap).execute(); new DownloadImageTask(holder.favicon, web, mWebpageBitmap)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else { } else {
holder.favicon.setImageBitmap(web.getBitmap()); holder.favicon.setImageBitmap(web.getBitmap());
} }

25
app/src/main/java/acr/browser/lightning/object/SearchAdapter.java

@ -1,6 +1,5 @@
package acr.browser.lightning.object; package acr.browser.lightning.object;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
@ -66,7 +65,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
private static final String ENCODING = "ISO-8859-1"; private static final String ENCODING = "ISO-8859-1";
private static final long INTERVAL_DAY = 86400000; private static final long INTERVAL_DAY = 86400000;
private static final int MAX_SUGGESTIONS = 5; private static final int MAX_SUGGESTIONS = 5;
private final SuggestionsComparator mComparator = new SuggestionsComparator(); private static final SuggestionsComparator mComparator = new SuggestionsComparator();
private final String mSearchSubtitle; private final String mSearchSubtitle;
private SearchFilter mFilter; private SearchFilter mFilter;
private final Drawable mSearchDrawable; private final Drawable mSearchDrawable;
@ -86,7 +85,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
@Override @Override
public void run() { public void run() {
deleteOldCacheFiles(mContext); deleteOldCacheFiles();
} }
}); });
@ -97,8 +96,8 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
delete.start(); delete.start();
} }
private void deleteOldCacheFiles(Context context) { private void deleteOldCacheFiles() {
File dir = new File(context.getCacheDir().toString()); File dir = new File(BrowserApp.getAppContext().getCacheDir().toString());
String[] fileList = dir.list(new NameFilter()); String[] fileList = dir.list(new NameFilter());
long earliestTimeAllowed = System.currentTimeMillis() - INTERVAL_DAY; long earliestTimeAllowed = System.currentTimeMillis() - INTERVAL_DAY;
for (String fileName : fileList) { for (String fileName : fileList) {
@ -155,7 +154,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
SuggestionHolder holder; SuggestionHolder holder;
if (convertView == null) { if (convertView == null) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater(); LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.two_line_autocomplete, parent, false); convertView = inflater.inflate(R.layout.two_line_autocomplete, parent, false);
holder = new SuggestionHolder(); holder = new SuggestionHolder();
@ -221,7 +220,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
} }
String query = constraint.toString().toLowerCase(Locale.getDefault()); String query = constraint.toString().toLowerCase(Locale.getDefault());
if (mUseGoogle && !mIncognito && !mIsExecuting) { if (mUseGoogle && !mIncognito && !mIsExecuting) {
new RetrieveSearchSuggestions().execute(query); new RetrieveSearchSuggestions().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, query);
} }
int counter = 0; int counter = 0;
@ -352,7 +351,13 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
} }
private File downloadSuggestionsForQuery(String query) { /**
* This method downloads the search suggestions for the specific query.
* NOTE: This is a blocking operation, do not run on the UI thread.
* @param query the query to get suggestions for
* @return the cache file containing the suggestions
*/
private static File downloadSuggestionsForQuery(String query) {
File cacheFile = new File(BrowserApp.getAppContext().getCacheDir(), query.hashCode() + CACHE_FILE_TYPE); File cacheFile = new File(BrowserApp.getAppContext().getCacheDir(), query.hashCode() + CACHE_FILE_TYPE);
if (System.currentTimeMillis() - INTERVAL_DAY < cacheFile.lastModified()) { if (System.currentTimeMillis() - INTERVAL_DAY < cacheFile.lastModified()) {
return cacheFile; return cacheFile;
@ -429,7 +434,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
// } // }
private List<HistoryItem> getFilteredList() { private List<HistoryItem> getFilteredList() {
List<HistoryItem> list = new ArrayList<>(); List<HistoryItem> list = new ArrayList<>(5);
synchronized (mBookmarks) { synchronized (mBookmarks) {
synchronized (mHistory) { synchronized (mHistory) {
synchronized (mSuggestions) { synchronized (mSuggestions) {
@ -456,7 +461,7 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
return list; return list;
} }
private class SuggestionsComparator implements Comparator<HistoryItem> { private static class SuggestionsComparator implements Comparator<HistoryItem> {
@Override @Override
public int compare(HistoryItem lhs, HistoryItem rhs) { public int compare(HistoryItem lhs, HistoryItem rhs) {

Loading…
Cancel
Save