Cleaning up lint warnings and making some performance improvements on string builders

This commit is contained in:
Anthony Restaino 2015-09-07 20:31:59 -04:00
parent 3b75765d92
commit 732d309888
17 changed files with 35 additions and 46 deletions

View File

@ -21,10 +21,10 @@ import info.guardianproject.netcipher.web.WebkitProxy;
*/ */
public class ProxyUtils { public class ProxyUtils {
// Helper // Helper
private I2PAndroidHelper mI2PHelper; private final I2PAndroidHelper mI2PHelper;
private static boolean mI2PHelperBound; private static boolean mI2PHelperBound;
private static boolean mI2PProxyInitialized; private static boolean mI2PProxyInitialized;
private PreferenceManager mPreferences; private final PreferenceManager mPreferences;
private static ProxyUtils mInstance; private static ProxyUtils mInstance;
private ProxyUtils(Context context) { private ProxyUtils(Context context) {
@ -191,7 +191,7 @@ public class ProxyUtils {
} }
} }
public int setProxyChoice(int choice, Activity activity) { public static int setProxyChoice(int choice, Activity activity) {
switch (choice) { switch (choice) {
case Constants.PROXY_ORBOT: case Constants.PROXY_ORBOT:
if (!OrbotHelper.isOrbotInstalled(activity)) { if (!OrbotHelper.isOrbotInstalled(activity)) {

View File

@ -1022,7 +1022,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
public void onTrimMemory(int level) { public void onTrimMemory(int level) {
if (level > TRIM_MEMORY_MODERATE && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { if (level > TRIM_MEMORY_MODERATE && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
Log.d(Constants.TAG, "Low Memory, Free Memory"); Log.d(Constants.TAG, "Low Memory, Free Memory");
for (int n = 0; n < mWebViewList.size(); n++) { for (int n = 0, size = mWebViewList.size(); n < size; n++) {
mWebViewList.get(n).freeMemory(); mWebViewList.get(n).freeMemory();
} }
} }
@ -1164,7 +1164,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
performExitCleanUp(); performExitCleanUp();
mCurrentView = null; mCurrentView = null;
mWebView = null; mWebView = null;
for (int n = 0; n < mWebViewList.size(); n++) { for (int n = 0, size = mWebViewList.size(); n < size; n++) {
if (mWebViewList.get(n) != null) { if (mWebViewList.get(n) != null) {
mWebViewList.get(n).onDestroy(); mWebViewList.get(n).onDestroy();
} }
@ -1225,7 +1225,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
void saveOpenTabs() { void saveOpenTabs() {
if (mPreferences.getRestoreLostTabsEnabled()) { if (mPreferences.getRestoreLostTabsEnabled()) {
String s = ""; String s = "";
for (int n = 0; n < mWebViewList.size(); n++) { for (int n = 0, size = mWebViewList.size(); n < size; n++) {
if (!mWebViewList.get(n).getUrl().isEmpty()) { if (!mWebViewList.get(n).getUrl().isEmpty()) {
s = s + mWebViewList.get(n).getUrl() + "|$|SEPARATOR|$|"; s = s + mWebViewList.get(n).getUrl() + "|$|SEPARATOR|$|";
} }
@ -1270,7 +1270,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
} }
mHistoryDatabase = HistoryDatabase.getInstance(getApplicationContext()); mHistoryDatabase = HistoryDatabase.getInstance(getApplicationContext());
initializePreferences(); initializePreferences();
for (int n = 0; n < mWebViewList.size(); n++) { for (int n = 0, size = mWebViewList.size(); n < size; n++) {
if (mWebViewList.get(n) != null) { if (mWebViewList.get(n) != null) {
mWebViewList.get(n).initializePreferences(null, this); mWebViewList.get(n).initializePreferences(null, this);
} else { } else {
@ -2358,7 +2358,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
super.onReceive(context, intent); super.onReceive(context, intent);
boolean isConnected = isConnected(context); boolean isConnected = isConnected(context);
Log.d(Constants.TAG, "Network Connected: " + String.valueOf(isConnected)); Log.d(Constants.TAG, "Network Connected: " + String.valueOf(isConnected));
for (int n = 0; n < mWebViewList.size(); n++) { for (int n = 0, size = mWebViewList.size(); n < size; n++) {
WebView view = mWebViewList.get(n).getWebView(); WebView view = mWebViewList.get(n).getWebView();
if (view != null) if (view != null)
view.setNetworkAvailable(isConnected); view.setNetworkAvailable(isConnected);
@ -2431,7 +2431,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
/** /**
* This is received when the user edit a bookmark * This is received when the user edit a bookmark
* *
* @param event * @param event the event that the bookmark has changed
*/ */
@Subscribe @Subscribe
public void bookmarkChanged(final BookmarkEvents.BookmarkChanged event) { public void bookmarkChanged(final BookmarkEvents.BookmarkChanged event) {
@ -2446,7 +2446,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
/** /**
* Notify the browser that a bookmark was deleted * Notify the browser that a bookmark was deleted
* *
* @param event * @param event the event that the bookmark has been deleted
*/ */
@Subscribe @Subscribe
public void bookmarkDeleted(final BookmarkEvents.Deleted event) { public void bookmarkDeleted(final BookmarkEvents.Deleted event) {

View File

@ -6,10 +6,7 @@ import android.view.Menu;
import android.webkit.CookieManager; import android.webkit.CookieManager;
import android.webkit.CookieSyncManager; import android.webkit.CookieSyncManager;
import com.squareup.otto.Subscribe;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.bus.BookmarkEvents;
import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.preference.PreferenceManager;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")

View File

@ -21,9 +21,6 @@ import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView; import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import acr.browser.lightning.R; import acr.browser.lightning.R;
import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.preference.PreferenceManager;

View File

@ -72,7 +72,7 @@ public final class BookmarkPage {
final StringBuilder bookmarkBuilder = new StringBuilder(BookmarkPage.HEADING); final StringBuilder bookmarkBuilder = new StringBuilder(BookmarkPage.HEADING);
final String folderIconPath = Constants.FILE + CACHE_DIR + "/folder.png"; final String folderIconPath = Constants.FILE + CACHE_DIR + "/folder.png";
for (int n = 0; n < list.size(); n++) { for (int n = 0, size = list.size(); n < size; n++) {
final HistoryItem item = list.get(n); final HistoryItem item = list.get(n);
bookmarkBuilder.append(BookmarkPage.PART1); bookmarkBuilder.append(BookmarkPage.PART1);
if (item.isFolder()) { if (item.isFolder()) {

View File

@ -34,7 +34,7 @@ public class BookmarkSettingsFragment extends PreferenceFragment implements Pref
private File[] mFileList; private File[] mFileList;
private String[] mFileNameList; private String[] mFileNameList;
private PermissionsManager mPermissionsManager; private PermissionsManager mPermissionsManager;
private static String[] REQUIRED_PERMISSIONS = new String[]{ private static final String[] REQUIRED_PERMISSIONS = new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE Manifest.permission.WRITE_EXTERNAL_STORAGE
}; };

View File

@ -171,7 +171,7 @@ public class DisplaySettingsFragment extends PreferenceFragment implements Prefe
builder.show(); builder.show();
} }
private float getTextSize(int size) { private static float getTextSize(int size) {
switch (size) { switch (size) {
case 0: case 0:
return XSMALL; return XSMALL;

View File

@ -231,10 +231,10 @@ public class GeneralSettingsFragment extends PreferenceFragment implements Prefe
ProxyUtils utils = ProxyUtils.getInstance(mActivity); ProxyUtils utils = ProxyUtils.getInstance(mActivity);
switch (choice) { switch (choice) {
case Constants.PROXY_ORBOT: case Constants.PROXY_ORBOT:
choice = utils.setProxyChoice(choice, mActivity); choice = ProxyUtils.setProxyChoice(choice, mActivity);
break; break;
case Constants.PROXY_I2P: case Constants.PROXY_I2P:
choice = utils.setProxyChoice(choice, mActivity); choice = ProxyUtils.setProxyChoice(choice, mActivity);
break; break;
case Constants.PROXY_MANUAL: case Constants.PROXY_MANUAL:
manualProxyPicker(); manualProxyPicker();

View File

@ -57,7 +57,7 @@ public class PreferenceManager {
} }
private static PreferenceManager mInstance; private static PreferenceManager mInstance;
private SharedPreferences mPrefs; private final SharedPreferences mPrefs;
private static final String PREFERENCES = "settings"; private static final String PREFERENCES = "settings";

View File

@ -385,16 +385,16 @@ public class ArticleTextExtractor {
// System.out.println("date modified element " + elem.toString()); // System.out.println("date modified element " + elem.toString());
} }
if ("".equals(dateStr)) { if (dateStr != null && dateStr.isEmpty()) {
dateStr = SHelper.innerTrim(doc.select("meta[name=utime]").attr("content")); dateStr = SHelper.innerTrim(doc.select("meta[name=utime]").attr("content"));
} }
if ("".equals(dateStr)) { if (dateStr != null && dateStr.isEmpty()) {
dateStr = SHelper.innerTrim(doc.select("meta[name=pdate]").attr("content")); dateStr = SHelper.innerTrim(doc.select("meta[name=pdate]").attr("content"));
} }
if ("".equals(dateStr)) { if (dateStr != null && dateStr.isEmpty()) {
dateStr = SHelper.innerTrim(doc.select("meta[property=article:published]").attr("content")); dateStr = SHelper.innerTrim(doc.select("meta[property=article:published]").attr("content"));
} }
if ("".equals(dateStr)) { if (dateStr != null && dateStr.isEmpty()) {
return parseDate(dateStr); return parseDate(dateStr);
} }
@ -1143,13 +1143,14 @@ public class ArticleTextExtractor {
} }
private static String cleanTitle(String title) { private static String cleanTitle(String title) {
StringBuilder res = new StringBuilder();
// int index = title.lastIndexOf("|"); // int index = title.lastIndexOf("|");
// if (index > 0 && title.length() / 2 < index) // if (index > 0 && title.length() / 2 < index)
// title = title.substring(0, index + 1); // title = title.substring(0, index + 1);
int counter = 0; int counter = 0;
String[] strs = title.split("\\|"); String[] strs = title.split("\\|");
StringBuilder res = new StringBuilder(strs.length);
for (String part : strs) { for (String part : strs) {
if (IGNORED_TITLE_PARTS.contains(part.toLowerCase().trim())) if (IGNORED_TITLE_PARTS.contains(part.toLowerCase().trim()))
continue; continue;

View File

@ -203,6 +203,7 @@ public class HtmlFetcher {
} }
// main workhorse to call externally // main workhorse to call externally
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
public JResult fetchAndExtract(String url, int timeout, boolean resolve, public JResult fetchAndExtract(String url, int timeout, boolean resolve,
int maxContentSize, boolean forceReload) throws Exception { int maxContentSize, boolean forceReload) throws Exception {
String originalUrl = url; String originalUrl = url;
@ -325,13 +326,13 @@ public class HtmlFetcher {
} }
public String fetchAsString(String urlAsString, int timeout) public String fetchAsString(String urlAsString, int timeout)
throws MalformedURLException, IOException { throws IOException {
return fetchAsString(urlAsString, timeout, true); return fetchAsString(urlAsString, timeout, true);
} }
// main routine to get raw webpage content // main routine to get raw webpage content
public String fetchAsString(String urlAsString, int timeout, boolean includeSomeGooseOptions) public String fetchAsString(String urlAsString, int timeout, boolean includeSomeGooseOptions)
throws MalformedURLException, IOException { throws IOException {
HttpURLConnection hConn = createUrlConnection(urlAsString, timeout, includeSomeGooseOptions); HttpURLConnection hConn = createUrlConnection(urlAsString, timeout, includeSomeGooseOptions);
hConn.setInstanceFollowRedirects(true); hConn.setInstanceFollowRedirects(true);
String encoding = hConn.getContentEncoding(); String encoding = hConn.getContentEncoding();
@ -413,7 +414,7 @@ public class HtmlFetcher {
* UTF-8 in the Location: header. * UTF-8 in the Location: header.
*/ */
static String encodeUriFromHeader(String badLocation) { static String encodeUriFromHeader(String badLocation) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(badLocation.length());
for (char ch : badLocation.toCharArray()) { for (char ch : badLocation.toCharArray()) {
if (ch < (char) 128) { if (ch < (char) 128) {
@ -428,7 +429,7 @@ public class HtmlFetcher {
} }
protected HttpURLConnection createUrlConnection(String urlAsStr, int timeout, protected HttpURLConnection createUrlConnection(String urlAsStr, int timeout,
boolean includeSomeGooseOptions) throws MalformedURLException, IOException { boolean includeSomeGooseOptions) throws IOException {
URL url = new URL(urlAsStr); URL url = new URL(urlAsStr);
//using proxy may increase latency //using proxy may increase latency
HttpURLConnection hConn = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY); HttpURLConnection hConn = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);

View File

@ -72,9 +72,9 @@ public class SHelper {
if (str.isEmpty()) if (str.isEmpty())
return ""; return "";
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(str.length());
boolean previousSpace = false; boolean previousSpace = false;
for (int i = 0; i < str.length(); i++) { for (int i = 0, length = str.length(); i < length; i++) {
char c = str.charAt(i); char c = str.charAt(i);
if (c == ' ' || (int) c == 9 || c == '\n') { if (c == ' ' || (int) c == 9 || c == '\n') {
previousSpace = true; previousSpace = true;
@ -95,7 +95,7 @@ public class SHelper {
* invalid encoding character occurs. * invalid encoding character occurs.
*/ */
public static String encodingCleanup(String str) { public static String encodingCleanup(String str) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(str.length());
boolean startedWithCorrectString = false; boolean startedWithCorrectString = false;
for (int i = 0; i < str.length(); i++) { for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i); char c = str.charAt(i);
@ -301,7 +301,7 @@ public class SHelper {
} }
public static String printNode(Element root, int indentation) { public static String printNode(Element root, int indentation) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(indentation);
for (int i = 0; i < indentation; i++) { for (int i = 0; i < indentation; i++) {
sb.append(' '); sb.append(' ');
} }

View File

@ -12,7 +12,6 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.Constants;
@ -46,7 +45,7 @@ public class DownloadImageTask extends AsyncTask<Void, Void, Bitmap> {
// unique path for each url that is bookmarked. // unique path for each url that is bookmarked.
final Uri uri = Uri.parse(mUrl); final Uri uri = Uri.parse(mUrl);
final String hash = "" + uri.getHost().hashCode(); final String hash = String.valueOf(uri.getHost().hashCode());
final File image = new File(mCacheDir, hash + ".png"); final File image = new File(mCacheDir, hash + ".png");
final Uri urldisplay = Uri.fromParts(uri.getScheme(), uri.getHost(), "favicon.ico"); final Uri urldisplay = Uri.fromParts(uri.getScheme(), uri.getHost(), "favicon.ico");
// checks to see if the image exists // checks to see if the image exists

View File

@ -15,7 +15,6 @@
*/ */
package acr.browser.lightning.utils; package acr.browser.lightning.utils;
import android.net.Uri;
import android.util.Patterns; import android.util.Patterns;
import android.webkit.URLUtil; import android.webkit.URLUtil;
@ -28,11 +27,11 @@ import java.util.regex.Pattern;
public class UrlUtils { public class UrlUtils {
static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile( static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile(
"(?i)" + // switch on case insensitive matching "(?i)" + // switch on case insensitive matching
"(" + // begin group for schema '(' + // begin group for schema
"(?:http|https|file):\\/\\/" + "(?:http|https|file):\\/\\/" +
"|(?:inline|data|about|javascript):" + "|(?:inline|data|about|javascript):" +
"|(?:.*:.*@)" + "|(?:.*:.*@)" +
")" + ')' +
"(.*)"); "(.*)");
// Google search // Google search
public final static String QUERY_PLACE_HOLDER = "%s"; public final static String QUERY_PLACE_HOLDER = "%s";

View File

@ -34,11 +34,8 @@ import android.webkit.URLUtil;
import java.io.Closeable; import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.math.BigInteger;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;

View File

@ -2,7 +2,6 @@ package acr.browser.lightning.utils;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.provider.Browser;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.webkit.CookieManager; import android.webkit.CookieManager;
import android.webkit.CookieSyncManager; import android.webkit.CookieSyncManager;

View File

@ -51,7 +51,6 @@ import android.widget.LinearLayout;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
@ -95,7 +94,7 @@ public class LightningView {
0, 0, -1.0f, 0, 255, // blue 0, 0, -1.0f, 0, 255, // blue
0, 0, 0, 1.0f, 0 // alpha 0, 0, 0, 1.0f, 0 // alpha
}; };
private PermissionsManager mPermissionsManager; private final PermissionsManager mPermissionsManager;
private static final String[] PERMISSIONS = new String[]{Manifest.permission.ACCESS_FINE_LOCATION}; private static final String[] PERMISSIONS = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
@SuppressLint("NewApi") @SuppressLint("NewApi")