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 @@ -328,14 +328,14 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mSearch.setOnEditorActionListener(search);
mSearch.setOnTouchListener(search);
new Thread(new Runnable() {
BrowserApp.getTaskThread().execute(new Runnable() {
@Override
public void run() {
initializeSearchSuggestions(mSearch);
}
}).run();
});
mDrawerLayout.setDrawerShadow(R.drawable.drawer_right_shadow, GravityCompat.END);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_left_shadow, GravityCompat.START);
@ -1480,42 +1480,49 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1480,42 +1480,49 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
* previously searched URLs
*/
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
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
String url = null;
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();
public void run() {
getUrl.setThreshold(1);
getUrl.setDropDownWidth(-1);
getUrl.setDropDownAnchor(R.id.toolbar_layout);
getUrl.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
String url = null;
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);
mSearchAdapter = new SearchAdapter(this, mDarkTheme, isIncognito());
getUrl.setAdapter(mSearchAdapter);
getUrl.setSelectAllOnFocus(true);
getUrl.setAdapter(mSearchAdapter);
}
});
}
/**

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

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

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

@ -228,7 +228,7 @@ public class DownloadHandler { @@ -228,7 +228,7 @@ public class DownloadHandler {
} else {
final DownloadManager manager = (DownloadManager) context
.getSystemService(Context.DOWNLOAD_SERVICE);
new Thread() {
BrowserApp.getTaskThread().execute(new Runnable() {
@Override
public void run() {
try {
@ -243,7 +243,7 @@ public class DownloadHandler { @@ -243,7 +243,7 @@ public class DownloadHandler {
eventBus.post(new BrowserEvents.ShowSnackBarMessage(R.string.problem_location_download));
}
}
}.start();
});
eventBus.post(new BrowserEvents.ShowSnackBarMessage(
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 @@ -137,7 +137,7 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
exportpref.setOnPreferenceClickListener(this);
importpref.setOnPreferenceClickListener(this);
new Thread(mInitializeImportPreference).start();
BrowserApp.getTaskThread().execute(mInitializeImportPreference);
}
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, @@ -170,7 +170,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
setupNavigationButton(view, R.id.action_toggle_desktop, R.id.icon_desktop);
// Must be called here, only here we have a reference to the ListView
new Thread(mInitBookmarkManager).run();
BrowserApp.getTaskThread().execute(mInitBookmarkManager);
return view;
}

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

@ -175,13 +175,12 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme @@ -175,13 +175,12 @@ public class PrivacySettingsFragment extends LightningPreferenceFragment impleme
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Thread clear = new Thread(new Runnable() {
BrowserApp.getTaskThread().execute(new Runnable() {
@Override
public void run() {
clearCookies();
}
});
clear.start();
}
})
.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 { @@ -96,12 +96,10 @@ public class SearchAdapter extends BaseAdapter implements Filterable {
mSearchSubtitle = mContext.getString(R.string.suggestion);
mDarkTheme = dark || 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);
mBookmarkDrawable = ThemeUtils.getThemedDrawable(context, R.drawable.ic_bookmark, mDarkTheme);
mHistoryDrawable = ThemeUtils.getThemedDrawable(context, R.drawable.ic_history, mDarkTheme);
delete.setPriority(Thread.MIN_PRIORITY);
delete.start();
mLanguage = Locale.getDefault().getLanguage();
if (mLanguage.isEmpty()) {
mLanguage = DEFAULT_LANGUAGE;

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

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

Loading…
Cancel
Save