From 0cf218018cbee17cff6b03f0d1caae6cdcbafab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sieradzki?= Date: Sat, 19 Jul 2014 18:22:33 +0200 Subject: [PATCH] Clean up SettingsActivity and AdvancedSettingsActivity * field that are overwritten from every class instance SHOULD NOT be "static" * some field could and was converted to local variables * add missing "private" qualifier here and there * action bar may be null, added checking for this * remove wrapping all method body in try-catch, moved it to method call * removed redundant field and local variable initializations * single-char string replaced with char * redundant qualifiers ("SettingsActivity.this" etc.) was removed --- .../lightning/AdvancedSettingsActivity.java | 177 +++++++++--------- .../browser/lightning/SettingsActivity.java | 58 +++--- 2 files changed, 120 insertions(+), 115 deletions(-) diff --git a/src/acr/browser/lightning/AdvancedSettingsActivity.java b/src/acr/browser/lightning/AdvancedSettingsActivity.java index be7e3bd..0a16f67 100644 --- a/src/acr/browser/lightning/AdvancedSettingsActivity.java +++ b/src/acr/browser/lightning/AdvancedSettingsActivity.java @@ -3,6 +3,7 @@ */ package acr.browser.lightning; +import android.app.ActionBar; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; @@ -29,18 +30,14 @@ public class AdvancedSettingsActivity extends Activity { // mPreferences variables private static final int API = android.os.Build.VERSION.SDK_INT; - private static SharedPreferences mPreferences; + private SharedPreferences mPreferences; - private static SharedPreferences.Editor mEditPrefs; + private SharedPreferences.Editor mEditPrefs; - private static RelativeLayout r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, - r12, r13, r14, r15, rIncognitoCookies, rClearCache, - rSearchSuggestions, rClearHistoryExit, rClearCookiesExit; - - private static CheckBox cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9, cb10, + private CheckBox cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9, cb10, cb11, cbIncognitoCookies, cbSearchSuggestions, cbClearHistoryExit, cbClearCookiesExit; - private static Context mContext; + private Context mContext; private boolean mSystemBrowser; @@ -50,19 +47,26 @@ public class AdvancedSettingsActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.advanced_settings); - getActionBar().setHomeButtonEnabled(true); - getActionBar().setDisplayHomeAsUpEnabled(true); + + ActionBar actionBar = getActionBar(); + if (actionBar != null) { + actionBar.setHomeButtonEnabled(true); + actionBar.setDisplayHomeAsUpEnabled(true); + } + mPreferences = getSharedPreferences(PreferenceConstants.PREFERENCES, 0); if (mPreferences.getBoolean(PreferenceConstants.HIDE_STATUS_BAR, false)) { getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } + + // TODO WARNING: SharedPreferences.edit() without a corresponding commit() or apply() call mEditPrefs = mPreferences.edit(); + mSystemBrowser = mPreferences.getBoolean( PreferenceConstants.SYSTEM_BROWSER_PRESENT, false); mContext = this; initialize(); - } @Override @@ -71,7 +75,11 @@ public class AdvancedSettingsActivity extends Activity { return true; } - void initialize() { + private void initialize() { + + RelativeLayout r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, + r12, r13, r14, r15, rIncognitoCookies, rClearCache, + rSearchSuggestions, rClearHistoryExit, rClearCookiesExit; r1 = (RelativeLayout) findViewById(R.id.r1); r2 = (RelativeLayout) findViewById(R.id.r2); @@ -195,7 +203,7 @@ public class AdvancedSettingsActivity extends Activity { messageHandler = new MessageHandler(); } - static class MessageHandler extends Handler { + private class MessageHandler extends Handler { @Override public void handleMessage(Message msg) { @@ -215,10 +223,9 @@ public class AdvancedSettingsActivity extends Activity { } super.handleMessage(msg); } - } - static void cb1(CheckBox view) { + private void cb1(CheckBox view) { view.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override @@ -232,7 +239,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void cb2(CheckBox view) { + private void cb2(CheckBox view) { view.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override @@ -246,7 +253,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void cbClearHistoryExit(CheckBox view) { + private void cbClearHistoryExit(CheckBox view) { view.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override @@ -260,7 +267,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void cbClearCookiesExit(CheckBox view) { + private void cbClearCookiesExit(CheckBox view) { view.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override @@ -274,7 +281,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void cb3(CheckBox view) { + private void cb3(CheckBox view) { view.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override @@ -288,7 +295,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void cb4(CheckBox view) { + private void cb4(CheckBox view) { view.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override @@ -301,7 +308,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void cb5(CheckBox view) { + private void cb5(CheckBox view) { view.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override @@ -315,7 +322,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void cb6(CheckBox view) { + private void cb6(CheckBox view) { view.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override @@ -328,7 +335,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void cb7(CheckBox view) { + private void cb7(CheckBox view) { view.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override @@ -341,7 +348,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void cb8(CheckBox view) { + private void cb8(CheckBox view) { view.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override @@ -355,7 +362,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void cb9(CheckBox view) { + private void cb9(CheckBox view) { view.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override @@ -369,7 +376,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void cb10(CheckBox view) { + private void cb10(CheckBox view) { view.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override @@ -383,7 +390,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void cb11(CheckBox view) { + private void cb11(CheckBox view) { view.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override @@ -397,7 +404,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void cbIncognitoCookies(CheckBox view) { + private void cbIncognitoCookies(CheckBox view) { view.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override @@ -411,7 +418,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void cbSearchSuggestions(CheckBox view) { + private void cbSearchSuggestions(CheckBox view) { view.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override @@ -424,7 +431,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void r1(RelativeLayout view) { + private void r1(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override @@ -436,7 +443,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void r2(RelativeLayout view) { + private void r2(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override @@ -448,7 +455,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void rClearHistoryExit(RelativeLayout view) { + private void rClearHistoryExit(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override @@ -460,7 +467,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void rClearCookiesExit(RelativeLayout view) { + private void rClearCookiesExit(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override @@ -472,7 +479,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void r3(RelativeLayout view) { + private void r3(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override @@ -484,7 +491,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void r4(RelativeLayout view) { + private void r4(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override @@ -502,7 +509,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void r5(RelativeLayout view) { + private void r5(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override @@ -514,7 +521,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void r6(RelativeLayout view) { + private void r6(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override @@ -526,7 +533,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void r7(RelativeLayout view) { + private void r7(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override @@ -538,7 +545,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void r8(RelativeLayout view) { + private void r8(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override @@ -586,7 +593,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void r11(RelativeLayout view) { + private void r11(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override @@ -599,7 +606,7 @@ public class AdvancedSettingsActivity extends Activity { } - void r12(RelativeLayout view) { + private void r12(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override @@ -611,7 +618,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void r13(RelativeLayout view) { + private void r13(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override @@ -622,7 +629,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void r14(RelativeLayout view) { + private void r14(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override @@ -633,7 +640,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void r15(RelativeLayout view) { + private void r15(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override @@ -680,7 +687,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void rIncognitoCookies(RelativeLayout view) { + private void rIncognitoCookies(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override @@ -693,7 +700,7 @@ public class AdvancedSettingsActivity extends Activity { } - void rSearchSuggestions(RelativeLayout view) { + private void rSearchSuggestions(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override @@ -704,7 +711,7 @@ public class AdvancedSettingsActivity extends Activity { }); } - void rClearCache(RelativeLayout view) { + private void rClearCache(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override @@ -725,11 +732,10 @@ public class AdvancedSettingsActivity extends Activity { getResources().getString(R.string.message_cache_cleared)); } + @SuppressWarnings("deprecation") public void clearHistory() { - AdvancedSettingsActivity.this - .deleteDatabase(DatabaseHandler.DATABASE_NAME); - WebViewDatabase m = WebViewDatabase - .getInstance(AdvancedSettingsActivity.this); + deleteDatabase(DatabaseHandler.DATABASE_NAME); + WebViewDatabase m = WebViewDatabase.getInstance(this); m.clearFormData(); m.clearHttpAuthUsernamePassword(); if (API < 18) { @@ -739,11 +745,11 @@ public class AdvancedSettingsActivity extends Activity { if (mSystemBrowser) { try { Browser.clearHistory(getContentResolver()); - } catch (NullPointerException ignored) { + } catch (Exception ignored) { } } SettingsController.setClearHistory(true); - Utils.trimCache(AdvancedSettingsActivity.this); + Utils.trimCache(this); messageHandler.sendEmptyMessage(1); } @@ -754,21 +760,23 @@ public class AdvancedSettingsActivity extends Activity { messageHandler.sendEmptyMessage(2); } - void r9(RelativeLayout view) { + private void r9(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - - importFromStockBrowser(); - + try { + importFromStockBrowser(); + } catch (Exception e) { + // ignored exception + // TODO add logging + } } - }); } - void r10(RelativeLayout view) { + private void r10(RelativeLayout view) { view.setOnClickListener(new OnClickListener() { @Override @@ -811,41 +819,36 @@ public class AdvancedSettingsActivity extends Activity { public void importFromStockBrowser() { if (mSystemBrowser) { - try { - String[] proj = new String[]{Browser.BookmarkColumns.TITLE, - Browser.BookmarkColumns.URL}; - // use 0 for history, 1 for bookmarks - String sel = Browser.BookmarkColumns.BOOKMARK + " = 1"; - Cursor mCur; - mCur = getContentResolver().query(Browser.BOOKMARKS_URI, proj, - sel, null, null); - - String title = ""; - String url = ""; - int number = 0; - if (mCur.moveToFirst() && mCur.getCount() > 0) { - while (mCur.isAfterLast() == false) { - number++; - title = mCur.getString(mCur - .getColumnIndex(Browser.BookmarkColumns.TITLE)); - url = mCur.getString(mCur - .getColumnIndex(Browser.BookmarkColumns.URL)); - if (title.length() < 1) { - title = Utils.getDomainName(url); - } - Utils.addBookmark(mContext, title, url); - mCur.moveToNext(); + String[] proj = new String[]{Browser.BookmarkColumns.TITLE, + Browser.BookmarkColumns.URL}; + // use 0 for history, 1 for bookmarks + String sel = Browser.BookmarkColumns.BOOKMARK + " = 1"; + Cursor mCur; + mCur = getContentResolver().query(Browser.BOOKMARKS_URI, proj, + sel, null, null); + + String title, url; + int number = 0; + if (mCur.moveToFirst() && mCur.getCount() > 0) { + while (!mCur.isAfterLast()) { + number++; + title = mCur.getString(mCur + .getColumnIndex(Browser.BookmarkColumns.TITLE)); + url = mCur.getString(mCur + .getColumnIndex(Browser.BookmarkColumns.URL)); + if (title.length() < 1) { + title = Utils.getDomainName(url); } + Utils.addBookmark(mContext, title, url); + mCur.moveToNext(); } - Utils.showToast(mContext, number + " " - + getResources().getString(R.string.message_import)); - } catch (NullPointerException ignored) { } + Utils.showToast(mContext, number + " " + + getResources().getString(R.string.message_import)); } else { Utils.createInformativeDialog(mContext, getResources().getString(R.string.title_error), getResources().getString(R.string.dialog_import_error)); } } - } diff --git a/src/acr/browser/lightning/SettingsActivity.java b/src/acr/browser/lightning/SettingsActivity.java index c2bc201..efe51a1 100644 --- a/src/acr/browser/lightning/SettingsActivity.java +++ b/src/acr/browser/lightning/SettingsActivity.java @@ -4,6 +4,7 @@ package acr.browser.lightning; import android.annotation.SuppressLint; +import android.app.ActionBar; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; @@ -33,27 +34,25 @@ public class SettingsActivity extends Activity { private static int API = android.os.Build.VERSION.SDK_INT; - private static SharedPreferences.Editor mEditPrefs; + private SharedPreferences.Editor mEditPrefs; - private static int mAgentChoice; + private int mAgentChoice; - private static String mHomepage; + private String mHomepage; - private static TextView mAgentTextView; + private TextView mAgentTextView; - private static TextView mDownloadTextView; + private TextView mDownloadTextView; - private static int mEasterEggCounter = 0; + private int mEasterEggCounter; - private static String mSearchUrl; + private String mDownloadLocation; - private static String mDownloadLocation; + private TextView mHomepageText; - private static TextView mHomepageText; + private SharedPreferences mPreferences; - private static SharedPreferences mPreferences; - - private static TextView mSearchText; + private TextView mSearchText; private Context mContext; @@ -77,8 +76,12 @@ public class SettingsActivity extends Activity { @SuppressLint("NewApi") public void init() { // mPreferences storage - getActionBar().setHomeButtonEnabled(true); - getActionBar().setDisplayHomeAsUpEnabled(true); + ActionBar actionBar = getActionBar(); + if (actionBar != null) { + actionBar.setHomeButtonEnabled(true); + actionBar.setDisplayHomeAsUpEnabled(true); + } + mPreferences = getSharedPreferences(PreferenceConstants.PREFERENCES, 0); if (mPreferences.getBoolean(PreferenceConstants.HIDE_STATUS_BAR, false)) { getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, @@ -151,7 +154,7 @@ public class SettingsActivity extends Activity { PreferenceConstants.DOWNLOAD_DIRECTORY, Environment.DIRECTORY_DOWNLOADS); - mDownloadTextView.setText(Constants.EXTERNAL_STORAGE + "/" + mDownloadTextView.setText(Constants.EXTERNAL_STORAGE + '/' + mDownloadLocation); String code = "HOLO"; @@ -161,7 +164,7 @@ public class SettingsActivity extends Activity { getPackageName(), 0); code = p.versionName; } catch (NameNotFoundException e) { - // TODO Auto-generated catch block + // TODO add logging e.printStackTrace(); } @@ -341,13 +344,12 @@ public class SettingsActivity extends Activity { } public void searchUrlPicker() { - final AlertDialog.Builder urlPicker = new AlertDialog.Builder( - SettingsActivity.this); + final AlertDialog.Builder urlPicker = new AlertDialog.Builder(this); urlPicker.setTitle(getResources().getString(R.string.custom_url)); - final EditText getSearchUrl = new EditText(SettingsActivity.this); + final EditText getSearchUrl = new EditText(this); - mSearchUrl = mPreferences.getString(PreferenceConstants.SEARCH_URL, + String mSearchUrl = mPreferences.getString(PreferenceConstants.SEARCH_URL, Constants.GOOGLE_SEARCH); getSearchUrl.setText(mSearchUrl); urlPicker.setView(getSearchUrl); @@ -737,7 +739,7 @@ public class SettingsActivity extends Activity { agentStringPicker.setTitle(getResources().getString( R.string.title_user_agent)); - final EditText getAgent = new EditText(SettingsActivity.this); + final EditText getAgent = new EditText(this); agentStringPicker.setView(getAgent); agentStringPicker.setPositiveButton( getResources().getString(R.string.action_ok), @@ -768,7 +770,7 @@ public class SettingsActivity extends Activity { mDownloadLocation = mPreferences.getString( PreferenceConstants.DOWNLOAD_DIRECTORY, Environment.DIRECTORY_DOWNLOADS); - int n = -1; + int n; if (mDownloadLocation.contains(Environment.DIRECTORY_DOWNLOADS)) { n = 1; } else { @@ -791,7 +793,7 @@ public class SettingsActivity extends Activity { mEditPrefs.commit(); mDownloadTextView .setText(Constants.EXTERNAL_STORAGE - + "/" + + '/' + Environment.DIRECTORY_DOWNLOADS); break; case 2: @@ -822,7 +824,7 @@ public class SettingsActivity extends Activity { mActivity); homePicker.setTitle(getResources().getString( R.string.title_custom_homepage)); - final EditText getHome = new EditText(SettingsActivity.this); + final EditText getHome = new EditText(this); mHomepage = mPreferences.getString(PreferenceConstants.HOMEPAGE, Constants.HOMEPAGE); if (!mHomepage.startsWith("about:")) { @@ -854,7 +856,7 @@ public class SettingsActivity extends Activity { LinearLayout layout = new LinearLayout(this); downLocationPicker.setTitle(getResources().getString( R.string.title_download_location)); - final EditText getDownload = new EditText(SettingsActivity.this); + final EditText getDownload = new EditText(this); getDownload.setBackgroundResource(0); mDownloadLocation = mPreferences.getString( PreferenceConstants.DOWNLOAD_DIRECTORY, @@ -873,7 +875,7 @@ public class SettingsActivity extends Activity { TextView v = new TextView(this); v.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18); v.setTextColor(Color.DKGRAY); - v.setText(Constants.EXTERNAL_STORAGE + "/"); + v.setText(Constants.EXTERNAL_STORAGE + '/'); v.setPadding(padding, padding, 0, padding); layout.addView(v); layout.addView(getDownload); @@ -896,7 +898,7 @@ public class SettingsActivity extends Activity { PreferenceConstants.DOWNLOAD_DIRECTORY, text); mEditPrefs.commit(); mDownloadTextView.setText(Constants.EXTERNAL_STORAGE - + "/" + text); + + '/' + text); } }); downLocationPicker.show(); @@ -911,7 +913,7 @@ public class SettingsActivity extends Activity { picker.setTitle(getResources().getString(R.string.home)); mHomepage = mPreferences.getString( PreferenceConstants.HOMEPAGE, Constants.HOMEPAGE); - int n = -1; + int n; if (mHomepage.contains("about:home")) { n = 1; } else if (mHomepage.contains("about:blank")) {