Browse Source

Use Executor thread pool instead of creating my own threads on the fly

master
Anthony Restaino 9 years ago
parent
commit
4a21d3f4f9
  1. 73
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  2. 6
      app/src/main/java/acr/browser/lightning/app/BrowserApp.java
  3. 4
      app/src/main/java/acr/browser/lightning/download/DownloadHandler.java
  4. 2
      app/src/main/java/acr/browser/lightning/fragment/BookmarkSettingsFragment.java
  5. 2
      app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java
  6. 3
      app/src/main/java/acr/browser/lightning/fragment/PrivacySettingsFragment.java
  7. 4
      app/src/main/java/acr/browser/lightning/object/SearchAdapter.java
  8. 6
      app/src/main/java/acr/browser/lightning/utils/AdBlock.java

73
app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java

@ -328,14 +328,14 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mSearch.setOnEditorActionListener(search); mSearch.setOnEditorActionListener(search);
mSearch.setOnTouchListener(search); mSearch.setOnTouchListener(search);
new Thread(new Runnable() { BrowserApp.getTaskThread().execute(new Runnable() {
@Override @Override
public void run() { public void run() {
initializeSearchSuggestions(mSearch); initializeSearchSuggestions(mSearch);
} }
}).run(); });
mDrawerLayout.setDrawerShadow(R.drawable.drawer_right_shadow, GravityCompat.END); mDrawerLayout.setDrawerShadow(R.drawable.drawer_right_shadow, GravityCompat.END);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_left_shadow, GravityCompat.START); mDrawerLayout.setDrawerShadow(R.drawable.drawer_left_shadow, GravityCompat.START);
@ -1480,42 +1480,49 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
* previously searched URLs * previously searched URLs
*/ */
private void initializeSearchSuggestions(final AutoCompleteTextView getUrl) { private void initializeSearchSuggestions(final AutoCompleteTextView getUrl) {
getUrl.setThreshold(1);
getUrl.setDropDownWidth(-1);
getUrl.setDropDownAnchor(R.id.toolbar_layout);
getUrl.setOnItemClickListener(new OnItemClickListener() {
mSearchAdapter = new SearchAdapter(this, mDarkTheme, isIncognito());
getUrl.post(new Runnable() {
@Override @Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) { public void run() {
String url = null; getUrl.setThreshold(1);
CharSequence urlString = ((TextView) view.findViewById(R.id.url)).getText(); getUrl.setDropDownWidth(-1);
if (urlString != null) { getUrl.setDropDownAnchor(R.id.toolbar_layout);
url = urlString.toString(); getUrl.setOnItemClickListener(new OnItemClickListener() {
}
if (url == null || url.startsWith(BrowserActivity.this.getString(R.string.suggestion))) { @Override
CharSequence searchString = ((TextView) view.findViewById(R.id.title)).getText(); public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
if (searchString != null) { String url = null;
url = searchString.toString(); CharSequence urlString = ((TextView) view.findViewById(R.id.url)).getText();
if (urlString != null) {
url = urlString.toString();
}
if (url == null || url.startsWith(BrowserActivity.this.getString(R.string.suggestion))) {
CharSequence searchString = ((TextView) view.findViewById(R.id.title)).getText();
if (searchString != null) {
url = searchString.toString();
}
}
if (url == null) {
return;
}
getUrl.setText(url);
searchTheWeb(url);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getUrl.getWindowToken(), 0);
final LightningView currentTab = mTabsManager.getCurrentTab();
if (currentTab != null) {
currentTab.requestFocus();
}
} }
}
if (url == null) {
return;
}
getUrl.setText(url);
searchTheWeb(url);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getUrl.getWindowToken(), 0);
final LightningView currentTab = mTabsManager.getCurrentTab();
if (currentTab != null) {
currentTab.requestFocus();
}
}
}); });
getUrl.setSelectAllOnFocus(true); getUrl.setSelectAllOnFocus(true);
mSearchAdapter = new SearchAdapter(this, mDarkTheme, isIncognito()); getUrl.setAdapter(mSearchAdapter);
getUrl.setAdapter(mSearchAdapter); }
});
} }
/** /**

6
app/src/main/java/acr/browser/lightning/app/BrowserApp.java

@ -16,6 +16,7 @@ public class BrowserApp extends Application {
private static AppComponent mAppComponent; private static AppComponent mAppComponent;
private static final Executor mIOThread = Executors.newSingleThreadExecutor(); private static final Executor mIOThread = Executors.newSingleThreadExecutor();
private static final Executor mTaskThread = Executors.newCachedThreadPool();
@Inject Bus mBus; @Inject Bus mBus;
@ -41,6 +42,11 @@ public class BrowserApp extends Application {
return mIOThread; return mIOThread;
} }
@NonNull
public static Executor getTaskThread() {
return mTaskThread;
}
public static Bus getBus(@NonNull Context context) { public static Bus getBus(@NonNull Context context) {
return get(context).mBus; return get(context).mBus;
} }

4
app/src/main/java/acr/browser/lightning/download/DownloadHandler.java

@ -228,7 +228,7 @@ public class DownloadHandler {
} else { } else {
final DownloadManager manager = (DownloadManager) context final DownloadManager manager = (DownloadManager) context
.getSystemService(Context.DOWNLOAD_SERVICE); .getSystemService(Context.DOWNLOAD_SERVICE);
new Thread() { BrowserApp.getTaskThread().execute(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -243,7 +243,7 @@ public class DownloadHandler {
eventBus.post(new BrowserEvents.ShowSnackBarMessage(R.string.problem_location_download)); eventBus.post(new BrowserEvents.ShowSnackBarMessage(R.string.problem_location_download));
} }
} }
}.start(); });
eventBus.post(new BrowserEvents.ShowSnackBarMessage( eventBus.post(new BrowserEvents.ShowSnackBarMessage(
context.getString(R.string.download_pending) + ' ' + filename)); context.getString(R.string.download_pending) + ' ' + filename));
} }

2
app/src/main/java/acr/browser/lightning/fragment/BookmarkSettingsFragment.java

@ -137,7 +137,7 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
exportpref.setOnPreferenceClickListener(this); exportpref.setOnPreferenceClickListener(this);
importpref.setOnPreferenceClickListener(this); importpref.setOnPreferenceClickListener(this);
new Thread(mInitializeImportPreference).start(); BrowserApp.getTaskThread().execute(mInitializeImportPreference);
} }
private final Runnable mInitializeImportPreference = new Runnable() { private final Runnable mInitializeImportPreference = new Runnable() {

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

@ -170,7 +170,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
setupNavigationButton(view, R.id.action_toggle_desktop, R.id.icon_desktop); setupNavigationButton(view, R.id.action_toggle_desktop, R.id.icon_desktop);
// Must be called here, only here we have a reference to the ListView // Must be called here, only here we have a reference to the ListView
new Thread(mInitBookmarkManager).run(); BrowserApp.getTaskThread().execute(mInitBookmarkManager);
return view; return view;
} }

3
app/src/main/java/acr/browser/lightning/fragment/PrivacySettingsFragment.java

@ -175,13 +175,12 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface arg0, int arg1) { public void onClick(DialogInterface arg0, int arg1) {
Thread clear = new Thread(new Runnable() { BrowserApp.getTaskThread().execute(new Runnable() {
@Override @Override
public void run() { public void run() {
clearCookies(); clearCookies();
} }
}); });
clear.start();
} }
}) })
.setNegativeButton(getResources().getString(R.string.action_no), null).show(); .setNegativeButton(getResources().getString(R.string.action_no), null).show();

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

@ -96,12 +96,10 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
mSearchSubtitle = mContext.getString(R.string.suggestion); mSearchSubtitle = mContext.getString(R.string.suggestion);
mDarkTheme = dark || incognito; mDarkTheme = dark || incognito;
mIncognito = incognito; mIncognito = incognito;
Thread delete = new Thread(new ClearCacheRunnable(BrowserApp.get(context))); BrowserApp.getTaskThread().execute(new ClearCacheRunnable(BrowserApp.get(context)));
mSearchDrawable = ThemeUtils.getThemedDrawable(context, R.drawable.ic_search, mDarkTheme); mSearchDrawable = ThemeUtils.getThemedDrawable(context, R.drawable.ic_search, mDarkTheme);
mBookmarkDrawable = ThemeUtils.getThemedDrawable(context, R.drawable.ic_bookmark, mDarkTheme); mBookmarkDrawable = ThemeUtils.getThemedDrawable(context, R.drawable.ic_bookmark, mDarkTheme);
mHistoryDrawable = ThemeUtils.getThemedDrawable(context, R.drawable.ic_history, mDarkTheme); mHistoryDrawable = ThemeUtils.getThemedDrawable(context, R.drawable.ic_history, mDarkTheme);
delete.setPriority(Thread.MIN_PRIORITY);
delete.start();
mLanguage = Locale.getDefault().getLanguage(); mLanguage = Locale.getDefault().getLanguage();
if (mLanguage.isEmpty()) { if (mLanguage.isEmpty()) {
mLanguage = DEFAULT_LANGUAGE; mLanguage = DEFAULT_LANGUAGE;

6
app/src/main/java/acr/browser/lightning/utils/AdBlock.java

@ -55,7 +55,7 @@ public class AdBlock {
} }
private void loadBlockedDomainsList(@NonNull final Context context) { private void loadBlockedDomainsList(@NonNull final Context context) {
Thread thread = new Thread(new Runnable() { BrowserApp.getTaskThread().execute(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -77,7 +77,6 @@ public class AdBlock {
} }
} }
}); });
thread.start();
} }
/** /**
@ -140,7 +139,7 @@ public class AdBlock {
* @param context the context needed to read the file * @param context the context needed to read the file
*/ */
private void loadHostsFile(@NonNull final Context context) { private void loadHostsFile(@NonNull final Context context) {
Thread thread = new Thread(new Runnable() { BrowserApp.getTaskThread().execute(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -181,6 +180,5 @@ public class AdBlock {
} }
} }
}); });
thread.start();
} }
} }

Loading…
Cancel
Save