From d60fe82b4adcbb53e92b1130471bf3d786ddc723 Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Mon, 9 Feb 2015 15:45:40 -0500 Subject: [PATCH] Fix lint problems and other code style problems, also fixed sluggish navigation drawer issues --- res/layout/search.xml | 2 +- res/layout/tab_drawer.xml | 12 +- .../lightning/AnimatedProgressBar.java | 2 +- .../browser/lightning/BookmarkManager.java | 3 +- .../browser/lightning/BrowserActivity.java | 142 +++++++++--------- .../lightning/HistoryDatabaseHandler.java | 10 +- src/acr/browser/lightning/HistoryItem.java | 6 +- src/acr/browser/lightning/IntentUtils.java | 6 +- src/acr/browser/lightning/LightningView.java | 36 +++-- src/acr/browser/lightning/MainActivity.java | 1 - src/acr/browser/lightning/SearchAdapter.java | 8 +- 11 files changed, 105 insertions(+), 123 deletions(-) diff --git a/res/layout/search.xml b/res/layout/search.xml index 3bfa575..a2dd47d 100644 --- a/res/layout/search.xml +++ b/res/layout/search.xml @@ -21,7 +21,7 @@ android:layout_height="wrap_content" android:background="@null" android:gravity="left|center_vertical" - android:hint="@+string/search_hint" + android:hint="@string/search_hint" android:imeOptions="actionGo" android:inputType="textUri" android:paddingBottom="1dp" diff --git a/res/layout/tab_drawer.xml b/res/layout/tab_drawer.xml index 7811f05..c1b963b 100644 --- a/res/layout/tab_drawer.xml +++ b/res/layout/tab_drawer.xml @@ -1,5 +1,5 @@ - @@ -49,14 +48,13 @@ android:id="@+id/view" android:layout_width="fill_parent" android:layout_height="1dp" - android:layout_below="@+id/new_tab_button" android:background="@color/gray_list_bg" /> @@ -119,4 +115,4 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/src/acr/browser/lightning/AnimatedProgressBar.java b/src/acr/browser/lightning/AnimatedProgressBar.java index 95b61be..fbe1663 100644 --- a/src/acr/browser/lightning/AnimatedProgressBar.java +++ b/src/acr/browser/lightning/AnimatedProgressBar.java @@ -61,7 +61,7 @@ public class AnimatedProgressBar extends LinearLayout { * @param attrs is the attribute set passed by the constructor */ private void init(final Context context, AttributeSet attrs) { - this.setLayerType(LAYER_TYPE_HARDWARE, null); + this.setLayerType(LAYER_TYPE_NONE, null); TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AnimatedProgressBar, 0, 0); int backgroundColor; int progressColor; diff --git a/src/acr/browser/lightning/BookmarkManager.java b/src/acr/browser/lightning/BookmarkManager.java index 9904000..6bba8dc 100644 --- a/src/acr/browser/lightning/BookmarkManager.java +++ b/src/acr/browser/lightning/BookmarkManager.java @@ -360,10 +360,9 @@ public class BookmarkManager { if (file == null) { return; } - File bookmarksImport = file; List list = new ArrayList(); try { - BufferedReader bookmarksReader = new BufferedReader(new FileReader(bookmarksImport)); + BufferedReader bookmarksReader = new BufferedReader(new FileReader(file)); String line; int number = 0; while ((line = bookmarksReader.readLine()) != null) { diff --git a/src/acr/browser/lightning/BrowserActivity.java b/src/acr/browser/lightning/BrowserActivity.java index 72cd860..70901af 100644 --- a/src/acr/browser/lightning/BrowserActivity.java +++ b/src/acr/browser/lightning/BrowserActivity.java @@ -77,7 +77,7 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl private DrawerLayout mDrawerLayout; private ListView mDrawerListLeft; - private RelativeLayout mDrawerLeft; + private LinearLayout mDrawerLeft; private LinearLayout mDrawerRight; private ListView mDrawerListRight; private RelativeLayout mNewTab; @@ -171,15 +171,20 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl // mProgressBar.setVisibility(View.GONE); // TODO mNewTab = (RelativeLayout) findViewById(R.id.new_tab_button); - mDrawerLeft = (RelativeLayout) findViewById(R.id.left_drawer); + mDrawerLeft = (LinearLayout) findViewById(R.id.left_drawer); + mDrawerLeft.setLayerType(View.LAYER_TYPE_HARDWARE, null); // Drawer + // stutters + // otherwise mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerListLeft = (ListView) findViewById(R.id.left_drawer_list); mDrawerListLeft.setDivider(null); mDrawerListLeft.setDividerHeight(0); mDrawerRight = (LinearLayout) findViewById(R.id.right_drawer); + mDrawerRight.setLayerType(View.LAYER_TYPE_HARDWARE, null); mDrawerListRight = (ListView) findViewById(R.id.right_drawer_list); mDrawerListRight.setDivider(null); mDrawerListRight.setDividerHeight(0); + setNavigationDrawerWidth(); mDrawerLayout.setDrawerListener(new DrawerLocker()); @@ -232,6 +237,11 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl mArrowDrawable = new DrawerArrowDrawable(this); mArrowImage = (ImageView) mActionBar.getCustomView().findViewById(R.id.arrow); + mArrowImage.setLayerType(View.LAYER_TYPE_HARDWARE, null); // Use a + // hardware + // layer for + // the + // animation mArrowImage.setImageDrawable(mArrowDrawable); LinearLayout arrowButton = (LinearLayout) mActionBar.getCustomView().findViewById( R.id.arrow_button); @@ -399,17 +409,22 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl @Override public void onFocusChange(View v, final boolean hasFocus) { if (!hasFocus && mCurrentView != null) { - if (mCurrentView != null) { - if (mCurrentView.getProgress() < 100) { - setIsLoading(); - } else { - setIsFinishedLoading(); - } + if (mCurrentView.getProgress() < 100) { + setIsLoading(); + } else { + setIsFinishedLoading(); } updateUrl(mCurrentView.getUrl(), true); } else if (hasFocus) { - updateUrl(mCurrentView.getUrl(), false); - mSearch.selectAll(); + String url = mCurrentView.getUrl(); + if (url == null || url.startsWith(Constants.FILE)) { + mSearch.setText(""); + } else { + mSearch.setText(url); + } + ((AutoCompleteTextView) v).selectAll(); // Hack to make sure + // the text gets + // selected mIcon = mCopyIcon; mSearch.setCompoundDrawables(null, null, mCopyIcon, null); } @@ -578,8 +593,7 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl if (mCurrentView != null) { if (!mCurrentView.getUrl().startsWith(Constants.FILE)) { ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); - ClipData clip = ClipData.newPlainText("label", mCurrentView.getUrl() - .toString()); + ClipData clip = ClipData.newPlainText("label", mCurrentView.getUrl()); clipboard.setPrimaryClip(clip); Utils.showToast(mContext, mContext.getResources().getString(R.string.message_link_copied)); @@ -653,7 +667,7 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl .setNegativeButton(R.string.no, dialogClickListener).show(); return true; - } else if (oh.isOrbotInstalled() & useProxy == true) { + } else if (oh.isOrbotInstalled() & useProxy) { initializeTor(); return true; } else { @@ -691,19 +705,23 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl .getLayoutParams(); params.width = maxWidth; mDrawerLeft.setLayoutParams(params); + mDrawerLeft.requestLayout(); DrawerLayout.LayoutParams paramsRight = (android.support.v4.widget.DrawerLayout.LayoutParams) mDrawerRight .getLayoutParams(); paramsRight.width = maxWidth; mDrawerRight.setLayoutParams(paramsRight); + mDrawerRight.requestLayout(); } else { DrawerLayout.LayoutParams params = (android.support.v4.widget.DrawerLayout.LayoutParams) mDrawerLeft .getLayoutParams(); params.width = width; mDrawerLeft.setLayoutParams(params); + mDrawerLeft.requestLayout(); DrawerLayout.LayoutParams paramsRight = (android.support.v4.widget.DrawerLayout.LayoutParams) mDrawerRight .getLayoutParams(); paramsRight.width = width; mDrawerRight.setLayoutParams(paramsRight); + mDrawerRight.requestLayout(); } } @@ -729,7 +747,7 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl } if (mPreferences.getBoolean(PreferenceConstants.RESTORE_LOST_TABS, true)) { String mem = mPreferences.getString(PreferenceConstants.URL_MEMORY, ""); - mEditPrefs.putString(PreferenceConstants.URL_MEMORY, ""); + mEditPrefs.putString(PreferenceConstants.URL_MEMORY, "").apply(); String[] array = Utils.getArray(mem); int count = 0; for (int n = 0; n < array.length; n++) { @@ -899,8 +917,7 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl if (mCurrentView != null) { if (!mCurrentView.getUrl().startsWith(Constants.FILE)) { ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); - ClipData clip = ClipData.newPlainText("label", mCurrentView.getUrl() - .toString()); + ClipData clip = ClipData.newPlainText("label", mCurrentView.getUrl()); clipboard.setPrimaryClip(clip); Utils.showToast(mContext, mContext.getResources().getString(R.string.message_link_copied)); @@ -1006,6 +1023,31 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl }); } + private void showCloseDialog(final int position) { + AlertDialog.Builder builder = new AlertDialog.Builder(mContext); + ArrayAdapter adapter = new ArrayAdapter<>(mContext, + android.R.layout.simple_dropdown_item_1line); + adapter.add(mContext.getString(R.string.close_tab)); + adapter.add(mContext.getString(R.string.close_all_tabs)); + builder.setAdapter(adapter, new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + switch (which) { + case 0: + deleteTab(position); + break; + case 1: + closeBrowser(); + break; + default: + break; + } + } + }); + builder.show(); + } + /** * The click listener for ListView in the navigation drawer */ @@ -1025,28 +1067,7 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl @Override public boolean onItemLongClick(AdapterView arg0, View arg1, final int position, long arg3) { - AlertDialog.Builder builder = new AlertDialog.Builder(mContext); - ArrayAdapter adapter = new ArrayAdapter<>(mContext, - android.R.layout.simple_dropdown_item_1line); - adapter.add(mContext.getString(R.string.close_tab)); - adapter.add(mContext.getString(R.string.close_all_tabs)); - builder.setAdapter(adapter, new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - switch (which) { - case 0: - deleteTab(position); - break; - case 1: - closeBrowser(); - break; - default: - break; - } - } - }); - builder.show(); + showCloseDialog(position); return true; } } @@ -1339,10 +1360,8 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl Log.d(Constants.TAG, "Cookies Cleared"); } - if (reference != null) { - reference.pauseTimers(); - reference.onDestroy(); - } + reference.pauseTimers(); + reference.onDestroy(); mCurrentView = null; mTitleAdapter.notifyDataSetChanged(); finish(); @@ -1362,33 +1381,7 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl @Override public boolean onKeyLongPress(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { - if (mPreferences.getBoolean(PreferenceConstants.CLEAR_CACHE_EXIT, false) - && mCurrentView != null && !isIncognito()) { - mCurrentView.clearCache(true); - Log.d(Constants.TAG, "Cache Cleared"); - - } - if (mPreferences.getBoolean(PreferenceConstants.CLEAR_HISTORY_EXIT, false) - && !isIncognito()) { - clearHistory(); - Log.d(Constants.TAG, "History Cleared"); - - } - if (mPreferences.getBoolean(PreferenceConstants.CLEAR_COOKIES_EXIT, false) - && !isIncognito()) { - clearCookies(); - Log.d(Constants.TAG, "Cookies Cleared"); - - } - mCurrentView = null; - for (int n = 0; n < mWebViews.size(); n++) { - if (mWebViews.get(n) != null) { - mWebViews.get(n).onDestroy(); - } - } - mWebViews.clear(); - mTitleAdapter.notifyDataSetChanged(); - finish(); + showCloseDialog(mDrawerListLeft.getCheckedItemPosition()); } return true; } @@ -1502,8 +1495,7 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl s = s + mWebViews.get(n).getUrl() + "|$|SEPARATOR|$|"; } } - mEditPrefs.putString(PreferenceConstants.URL_MEMORY, s); - mEditPrefs.commit(); + mEditPrefs.putString(PreferenceConstants.URL_MEMORY, s).commit(); } } @@ -1822,8 +1814,7 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl } } catch (Exception e) { - } finally { - + e.printStackTrace(); } } else { // if it exists, retrieve it from the cache @@ -1850,6 +1841,7 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl } } catch (Exception e) { + e.printStackTrace(); } } if (mIcon == null) { @@ -1878,7 +1870,7 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl @Override public void updateUrl(String url, boolean shortUrl) { - if (url == null) { + if (url == null || mSearch == null || mSearch.hasFocus()) { return; } if (shortUrl) { @@ -1940,7 +1932,7 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl } } try { - if (mHistoryHandler == null && !mHistoryHandler.isOpen()) { + if (mHistoryHandler == null || !mHistoryHandler.isOpen()) { mHistoryHandler = new HistoryDatabaseHandler(mContext); } mHistoryHandler.visitHistoryItem(url, title); diff --git a/src/acr/browser/lightning/HistoryDatabaseHandler.java b/src/acr/browser/lightning/HistoryDatabaseHandler.java index 749b184..9341527 100644 --- a/src/acr/browser/lightning/HistoryDatabaseHandler.java +++ b/src/acr/browser/lightning/HistoryDatabaseHandler.java @@ -57,11 +57,7 @@ public class HistoryDatabaseHandler extends SQLiteOpenHelper { } public boolean isOpen() { - if (mDatabase != null) { - return mDatabase.isOpen(); - } else { - return false; - } + return mDatabase != null && mDatabase.isOpen(); } @Override @@ -184,10 +180,8 @@ public class HistoryDatabaseHandler extends SQLiteOpenHelper { ContentValues values = new ContentValues(); values.put(KEY_URL, item.getUrl()); values.put(KEY_TITLE, item.getTitle()); - int n = mDatabase.update(TABLE_HISTORY, values, KEY_ID + " = ?", + return mDatabase.update(TABLE_HISTORY, values, KEY_ID + " = ?", new String[] { String.valueOf(item.getId()) }); - // updating row - return n; } // Getting items Count diff --git a/src/acr/browser/lightning/HistoryItem.java b/src/acr/browser/lightning/HistoryItem.java index 2b5c770..e106508 100644 --- a/src/acr/browser/lightning/HistoryItem.java +++ b/src/acr/browser/lightning/HistoryItem.java @@ -140,11 +140,7 @@ public class HistoryItem implements Comparable { if (!mTitle.equals(that.mTitle)) { return false; } - if (!mUrl.equals(that.mUrl)) { - return false; - } - - return true; + return mUrl.equals(that.mUrl); } @Override diff --git a/src/acr/browser/lightning/IntentUtils.java b/src/acr/browser/lightning/IntentUtils.java index 7652839..e43f275 100644 --- a/src/acr/browser/lightning/IntentUtils.java +++ b/src/acr/browser/lightning/IntentUtils.java @@ -19,8 +19,6 @@ public class IntentUtils { private Activity mActivity; - private BrowserController mController; - static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile("(?i)" + // switch on case insensitive matching "(" @@ -29,8 +27,7 @@ public class IntentUtils { + ")" + "(.*)"); public IntentUtils(BrowserController controller) { - mController = controller; - mActivity = mController.getActivity(); + mActivity = controller.getActivity(); } public boolean startActivityForUrl(WebView tab, String url) { @@ -69,6 +66,7 @@ public class IntentUtils { return true; } } catch (ActivityNotFoundException ex) { + ex.printStackTrace(); } return false; } diff --git a/src/acr/browser/lightning/LightningView.java b/src/acr/browser/lightning/LightningView.java index edadd03..e0dde12 100644 --- a/src/acr/browser/lightning/LightningView.java +++ b/src/acr/browser/lightning/LightningView.java @@ -90,14 +90,13 @@ public class LightningView { if (API > 15) { mWebView.setBackground(null); mWebView.getRootView().setBackground(null); - } else { + } else if (mWebView.getRootView() != null) { mWebView.getRootView().setBackgroundDrawable(null); } mWebView.setWillNotCacheDrawing(false); mWebView.setAlwaysDrawnWithCacheEnabled(true); mWebView.setScrollbarFadingEnabled(true); mWebView.setSaveEnabled(true); - mWebView.setWebChromeClient(new LightningChromeClient(activity)); mWebView.setWebViewClient(new LightningWebClient(activity)); mWebView.setDownloadListener(new LightningDownloadListener(activity)); @@ -395,9 +394,6 @@ public class LightningView { if (API > 16) { settings.setMediaPlaybackRequiresUserGesture(true); } - if (API < 19) { - settings.setDatabasePath(context.getCacheDir() + "/databases"); - } if (API >= Build.VERSION_CODES.LOLLIPOP && !mBrowserController.isIncognito()) { settings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE); } else if (API >= Build.VERSION_CODES.LOLLIPOP) { @@ -411,20 +407,24 @@ public class LightningView { settings.setDomStorageEnabled(false); } settings.setAppCacheEnabled(true); - settings.setAppCachePath(context.getCacheDir().toString()); settings.setCacheMode(WebSettings.LOAD_DEFAULT); - settings.setGeolocationDatabasePath(context.getFilesDir().toString()); - settings.setAllowFileAccess(true); settings.setDatabaseEnabled(true); settings.setSupportZoom(true); settings.setBuiltInZoomControls(true); settings.setDisplayZoomControls(false); settings.setAllowContentAccess(true); + settings.setAllowFileAccess(true); settings.setDefaultTextEncodingName("utf-8"); if (API > 16) { settings.setAllowFileAccessFromFileURLs(false); settings.setAllowUniversalAccessFromFileURLs(false); } + + settings.setAppCachePath(context.getDir("appcache", 0).getPath()); + settings.setGeolocationDatabasePath(context.getDir("geolocation", 0).getPath()); + if (API < Build.VERSION_CODES.KITKAT) { + settings.setDatabasePath(context.getDir("databases", 0).getPath()); + } } public boolean isShown() { @@ -471,11 +471,11 @@ public class LightningView { } public void setNormalRendering() { - mWebView.setLayerType(View.LAYER_TYPE_NONE, mPaint); + mWebView.setLayerType(View.LAYER_TYPE_NONE, null); } public void setSoftwareRendering() { - mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, mPaint); + mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); } public void setColorMode(int mode) { @@ -483,6 +483,9 @@ public class LightningView { switch (mode) { case 0: mPaint.setColorFilter(null); + // setSoftwareRendering(); // Some devices get segfaults + // in the WebView with Hardware Acceleration enabled, + // the only fix is to disable hardware rendering setNormalRendering(); mInvertPage = false; break; @@ -652,6 +655,16 @@ public class LightningView { mActivity = context; } + @Override + public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { + if (mAdBlock.isAd(request.getUrl().getHost())) { + ByteArrayInputStream EMPTY = new ByteArrayInputStream("".getBytes()); + return new WebResourceResponse("text/plain", "utf-8", EMPTY); + } + + return super.shouldInterceptRequest(view, request); + } + @Override public WebResourceResponse shouldInterceptRequest(WebView view, String url) { if (mAdBlock.isAd(url)) { @@ -766,7 +779,7 @@ public class LightningView { public void onPageFinished(WebView view, String url) { if (view.isShown()) { mBrowserController.updateUrl(url, true); - view.invalidate(); + view.postInvalidate(); } if (view.getTitle() == null || view.getTitle().isEmpty()) { mTitle.setTitle(mActivity.getString(R.string.untitled)); @@ -785,7 +798,6 @@ public class LightningView { mBrowserController.updateUrl(url, false); mBrowserController.showActionBar(); } - mTitle.setFavicon(mWebpageBitmap); mBrowserController.update(); } diff --git a/src/acr/browser/lightning/MainActivity.java b/src/acr/browser/lightning/MainActivity.java index 7045026..a391219 100644 --- a/src/acr/browser/lightning/MainActivity.java +++ b/src/acr/browser/lightning/MainActivity.java @@ -36,7 +36,6 @@ public class MainActivity extends BrowserActivity { @Override public synchronized void initializeTabs() { - super.initializeTabs(); restoreOrNewTab(); // if incognito mode use newTab(null, true); instead } diff --git a/src/acr/browser/lightning/SearchAdapter.java b/src/acr/browser/lightning/SearchAdapter.java index 1b14acf..eb4610c 100644 --- a/src/acr/browser/lightning/SearchAdapter.java +++ b/src/acr/browser/lightning/SearchAdapter.java @@ -162,13 +162,10 @@ public class SearchAdapter extends BaseAdapter implements Filterable { @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults results = new FilterResults(); - if (constraint == null) { + if (constraint == null || constraint.toString() == null) { return results; } String query = constraint.toString().toLowerCase(Locale.getDefault()); - if (query == null) { - return results; - } if (mUseGoogle && !mIncognito && !mIsExecuting) { new RetrieveSearchSuggestions().execute(query); } @@ -269,11 +266,10 @@ public class SearchAdapter extends BaseAdapter implements Filterable { try { download.close(); } catch (IOException e) { - return filter; + e.printStackTrace(); } } } - return filter; }