Removing some unused code, breaking out strings into constants, improving network receiver
This commit is contained in:
parent
84dd37d51d
commit
438662bffd
@ -2073,27 +2073,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO Check if all the calls are relative to TabsFragement
|
||||
|
||||
/**
|
||||
* A utility method that creates a FrameLayout button with the given ID and
|
||||
* sets the image of the button to the given image ID. The OnClick and OnLongClick
|
||||
* listeners are set to this class, so BrowserActivity should handle those events
|
||||
* there. Additionally, it tints the images according to the current theme.
|
||||
* This method only is a convenience so that this code does not have to be repeated
|
||||
* for the several "Buttons" that use this.
|
||||
*
|
||||
* @param buttonId the view id of the button
|
||||
* @param imageId the image to set as the button image
|
||||
*/
|
||||
private void setupFrameLayoutButton(@IdRes int buttonId, @IdRes int imageId) {
|
||||
final View frameButton = findViewById(buttonId);
|
||||
final ImageView buttonImage = (ImageView) findViewById(imageId);
|
||||
frameButton.setOnClickListener(this);
|
||||
frameButton.setOnLongClickListener(this);
|
||||
buttonImage.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN);
|
||||
}
|
||||
|
||||
/**
|
||||
* This NetworkReceiver notifies each of the WebViews in the browser whether
|
||||
* the network is currently connected or not. This is important because some
|
||||
@ -2102,10 +2081,8 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
|
||||
*/
|
||||
private final NetworkReceiver mNetworkReceiver = new NetworkReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
super.onReceive(context, intent);
|
||||
boolean isConnected = isConnected(context);
|
||||
Log.d(TAG, "Network Connected: " + String.valueOf(isConnected));
|
||||
public void onConnectivityChange(boolean isConnected) {
|
||||
Log.d(TAG, "Network Connected: " + isConnected);
|
||||
mTabsManager.notifyConnectionStatus(isConnected);
|
||||
}
|
||||
};
|
||||
|
@ -1,5 +1,6 @@
|
||||
package acr.browser.lightning.async;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
@ -26,7 +27,7 @@ public class ImageDownloadTask extends AsyncTask<Void, Void, Bitmap> {
|
||||
|
||||
private static final String TAG = ImageDownloadTask.class.getSimpleName();
|
||||
@NonNull private final WeakReference<ImageView> mFaviconImage;
|
||||
@NonNull private final WeakReference<Context> mContextReference;
|
||||
@NonNull private final Application mContext;
|
||||
@NonNull private final HistoryItem mWeb;
|
||||
private final String mUrl;
|
||||
@NonNull private final Bitmap mDefaultBitmap;
|
||||
@ -34,7 +35,7 @@ public class ImageDownloadTask extends AsyncTask<Void, Void, Bitmap> {
|
||||
public ImageDownloadTask(@NonNull ImageView bmImage,
|
||||
@NonNull HistoryItem web,
|
||||
@NonNull Bitmap defaultBitmap,
|
||||
@NonNull Context context) {
|
||||
@NonNull Application context) {
|
||||
// Set a tag on the ImageView so we know if the view
|
||||
// has gone out of scope and should not be used
|
||||
bmImage.setTag(web.getUrl().hashCode());
|
||||
@ -42,7 +43,7 @@ public class ImageDownloadTask extends AsyncTask<Void, Void, Bitmap> {
|
||||
this.mWeb = web;
|
||||
this.mUrl = web.getUrl();
|
||||
this.mDefaultBitmap = defaultBitmap;
|
||||
this.mContextReference = new WeakReference<>(context.getApplicationContext());
|
||||
this.mContext = context;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@ -53,11 +54,7 @@ public class ImageDownloadTask extends AsyncTask<Void, Void, Bitmap> {
|
||||
if (mUrl == null) {
|
||||
return mDefaultBitmap;
|
||||
}
|
||||
Context context = mContextReference.get();
|
||||
if (context == null) {
|
||||
return mDefaultBitmap;
|
||||
}
|
||||
File cache = context.getCacheDir();
|
||||
File cache = mContext.getCacheDir();
|
||||
final Uri uri = Uri.parse(mUrl);
|
||||
if (uri.getHost() == null || uri.getScheme() == null) {
|
||||
return mDefaultBitmap;
|
||||
|
@ -12,8 +12,11 @@ public final class Constants {
|
||||
|
||||
public static final boolean FULL_VERSION = BuildConfig.FULL_VERSION;
|
||||
|
||||
// Hardcoded user agents
|
||||
public static final String DESKTOP_USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36";
|
||||
public static final String MOBILE_USER_AGENT = "Mozilla/5.0 (Linux; U; Android 4.4; en-us; Nexus 4 Build/JOP24G) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30";
|
||||
|
||||
// Search query URLs
|
||||
public static final String YAHOO_SEARCH = "https://search.yahoo.com/search?p=";
|
||||
public static final String GOOGLE_SEARCH = "https://www.google.com/search?client=lightning&ie=UTF-8&oe=UTF-8&q=";
|
||||
public static final String BING_SEARCH = "https://www.bing.com/search?q=";
|
||||
@ -21,36 +24,45 @@ public final class Constants {
|
||||
public static final String DUCK_LITE_SEARCH = "https://duckduckgo.com/lite/?t=lightning&q=";
|
||||
public static final String STARTPAGE_MOBILE_SEARCH = "https://startpage.com/do/m/mobilesearch?language=english&query=";
|
||||
public static final String STARTPAGE_SEARCH = "https://startpage.com/do/search?language=english&query=";
|
||||
public static final String ASK_SEARCH = "http://www.ask.com/web?qsrc=0&o=0&l=dir&qo=lightningBrowser&q=";
|
||||
public static final String HOMEPAGE = "about:home";
|
||||
public static final String ASK_SEARCH = "http://www.ask.com/web?qsrc=0&o=0&l=dir&qo=LightningBrowser&q=";
|
||||
public static final String BAIDU_SEARCH = "https://www.baidu.com/s?wd=";
|
||||
public static final String YANDEX_SEARCH = "https://yandex.ru/yandsearch?lr=21411&text=";
|
||||
|
||||
// Custom local page schemes
|
||||
public static final String SCHEME_HOMEPAGE = "about:home";
|
||||
public static final String SCHEME_BLANK = "about:blank";
|
||||
public static final String SCHEME_BOOKMARKS = "about:bookmarks";
|
||||
|
||||
// Miscellaneous JavaScript
|
||||
public static final String JAVASCRIPT_INVERT_PAGE = "javascript:(function(){var e='img {-webkit-filter: invert(100%);'+'-moz-filter: invert(100%);'+'-o-filter: invert(100%);'+'-ms-filter: invert(100%); }',t=document.getElementsByTagName('head')[0],n=document.createElement('style');if(!window.counter){window.counter=1}else{window.counter++;if(window.counter%2==0){var e='html {-webkit-filter: invert(0%); -moz-filter: invert(0%); -o-filter: invert(0%); -ms-filter: invert(0%); }'}}n.type='text/css';if(n.styleSheet){n.styleSheet.cssText=e}else{n.appendChild(document.createTextNode(e))}t.appendChild(n)})();";
|
||||
public static final String JAVASCRIPT_TEXT_REFLOW = "javascript:document.getElementsByTagName('body')[0].style.width=window.innerWidth+'px';";
|
||||
public static final String JAVASCRIPT_THEME_COLOR = "(function () {\n" +
|
||||
" \"use strict\";\n" +
|
||||
" var metas, i, tag;\n" +
|
||||
" metas = document.getElementsByTagName('meta');\n" +
|
||||
" if (metas !== null) {\n" +
|
||||
" for (i = 0; i < metas.length; i++) {\n" +
|
||||
" tag = metas[i].getAttribute('name');\n" +
|
||||
" if (tag !== null && tag.toLowerCase() === 'theme-color') {\n" +
|
||||
" return metas[i].getAttribute('content');\n" +
|
||||
" }\n" +
|
||||
" console.log(tag);\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
'\n' +
|
||||
" return '';\n" +
|
||||
"}());";
|
||||
" \"use strict\";\n" +
|
||||
" var metas, i, tag;\n" +
|
||||
" metas = document.getElementsByTagName('meta');\n" +
|
||||
" if (metas !== null) {\n" +
|
||||
" for (i = 0; i < metas.length; i++) {\n" +
|
||||
" tag = metas[i].getAttribute('name');\n" +
|
||||
" if (tag !== null && tag.toLowerCase() === 'theme-color') {\n" +
|
||||
" return metas[i].getAttribute('content');\n" +
|
||||
" }\n" +
|
||||
" console.log(tag);\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
'\n' +
|
||||
" return '';\n" +
|
||||
"}());";
|
||||
|
||||
public static final String LOAD_READING_URL = "ReadingUrl";
|
||||
|
||||
public static final String SEPARATOR = "\\|\\$\\|SEPARATOR\\|\\$\\|";
|
||||
// URL Schemes
|
||||
public static final String HTTP = "http://";
|
||||
public static final String HTTPS = "https://";
|
||||
public static final String FILE = "file://";
|
||||
public static final String ABOUT = "about:";
|
||||
public static final String FOLDER = "folder://";
|
||||
|
||||
// Application log tag
|
||||
public static final String TAG = "Lightning";
|
||||
|
||||
// These should match the order of @array/proxy_choices_array
|
||||
@ -59,8 +71,10 @@ public final class Constants {
|
||||
public static final int PROXY_I2P = 2;
|
||||
public static final int PROXY_MANUAL = 3;
|
||||
|
||||
// Default text encoding we will use
|
||||
public static final String DEFAULT_ENCODING = "UTF-8";
|
||||
|
||||
// Allowable text encodings for the WebView
|
||||
public static final String[] TEXT_ENCODINGS = {"ISO-8859-1", "UTF-8", "GBK", "Big5", "ISO-2022-JP", "SHIFT_JS", "EUC-JP", "EUC-KR"};
|
||||
|
||||
public static final String INTENT_ORIGIN = "URL_INTENT_ORIGIN";
|
||||
|
@ -7,6 +7,7 @@ import android.app.Application;
|
||||
import android.os.AsyncTask;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
@ -24,6 +25,8 @@ import acr.browser.lightning.view.LightningView;
|
||||
|
||||
public class HistoryPage extends AsyncTask<Void, Void, Void> {
|
||||
|
||||
private static final String TAG = HistoryPage.class.getSimpleName();
|
||||
|
||||
public static final String FILENAME = "history.html";
|
||||
|
||||
private static final String HEADING_1 = "<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta content=\"en-us\" http-equiv=\"Content-Language\" /><meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\" /><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\"><title>";
|
||||
@ -95,7 +98,7 @@ public class HistoryPage extends AsyncTask<Void, Void, Void> {
|
||||
historyWriter = new FileWriter(historyWebPage, false);
|
||||
historyWriter.write(historyBuilder.toString());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
Log.e(TAG, "Unable to write history page to disk", e);
|
||||
} finally {
|
||||
Utils.close(historyWriter);
|
||||
}
|
||||
|
@ -399,7 +399,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
|
||||
holder.favicon.setImageBitmap(mFolderBitmap);
|
||||
} else if (web.getBitmap() == null) {
|
||||
holder.favicon.setImageBitmap(mWebpageBitmap);
|
||||
new ImageDownloadTask(holder.favicon, web, mWebpageBitmap, context)
|
||||
new ImageDownloadTask(holder.favicon, web, mWebpageBitmap, BrowserApp.get(context))
|
||||
.executeOnExecutor(AsyncExecutor.getInstance());
|
||||
} else {
|
||||
holder.favicon.setImageBitmap(web.getBitmap());
|
||||
|
@ -124,11 +124,11 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme
|
||||
}
|
||||
|
||||
|
||||
if (mHomepage.contains("about:home")) {
|
||||
if (mHomepage.contains(Constants.SCHEME_HOMEPAGE)) {
|
||||
home.setSummary(getResources().getString(R.string.action_homepage));
|
||||
} else if (mHomepage.contains("about:blank")) {
|
||||
} else if (mHomepage.contains(Constants.SCHEME_BLANK)) {
|
||||
home.setSummary(getResources().getString(R.string.action_blank));
|
||||
} else if (mHomepage.contains("about:bookmarks")) {
|
||||
} else if (mHomepage.contains(Constants.SCHEME_BOOKMARKS)) {
|
||||
home.setSummary(getResources().getString(R.string.action_bookmarks));
|
||||
} else {
|
||||
home.setSummary(mHomepage);
|
||||
@ -316,11 +316,11 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme
|
||||
picker.setTitle(getResources().getString(R.string.home));
|
||||
mHomepage = mPreferenceManager.getHomepage();
|
||||
int n;
|
||||
if (mHomepage.contains("about:home")) {
|
||||
if (mHomepage.startsWith(Constants.SCHEME_HOMEPAGE)) {
|
||||
n = 1;
|
||||
} else if (mHomepage.contains("about:blank")) {
|
||||
} else if (mHomepage.startsWith(Constants.SCHEME_BLANK)) {
|
||||
n = 2;
|
||||
} else if (mHomepage.contains("about:bookmarks")) {
|
||||
} else if (mHomepage.startsWith(Constants.SCHEME_BOOKMARKS)) {
|
||||
n = 3;
|
||||
} else {
|
||||
n = 4;
|
||||
@ -332,15 +332,15 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
switch (which + 1) {
|
||||
case 1:
|
||||
mPreferenceManager.setHomepage("about:home");
|
||||
mPreferenceManager.setHomepage(Constants.SCHEME_HOMEPAGE);
|
||||
home.setSummary(getResources().getString(R.string.action_homepage));
|
||||
break;
|
||||
case 2:
|
||||
mPreferenceManager.setHomepage("about:blank");
|
||||
mPreferenceManager.setHomepage(Constants.SCHEME_BLANK);
|
||||
home.setSummary(getResources().getString(R.string.action_blank));
|
||||
break;
|
||||
case 3:
|
||||
mPreferenceManager.setHomepage("about:bookmarks");
|
||||
mPreferenceManager.setHomepage(Constants.SCHEME_BOOKMARKS);
|
||||
home.setSummary(getResources().getString(R.string.action_bookmarks));
|
||||
break;
|
||||
case 4:
|
||||
@ -402,7 +402,7 @@ public class GeneralSettingsFragment extends LightningPreferenceFragment impleme
|
||||
homePicker.setTitle(getResources().getString(R.string.title_custom_homepage));
|
||||
final EditText getHome = new EditText(mActivity);
|
||||
mHomepage = mPreferenceManager.getHomepage();
|
||||
if (!mHomepage.startsWith("about:")) {
|
||||
if (!mHomepage.startsWith(Constants.ABOUT)) {
|
||||
getHome.setText(mHomepage);
|
||||
} else {
|
||||
String defaultUrl = "https://www.google.com";
|
||||
|
@ -2,14 +2,9 @@ package acr.browser.lightning.preference;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.support.annotation.IntDef;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
@ -170,7 +165,7 @@ public class PreferenceManager {
|
||||
|
||||
@NonNull
|
||||
public String getHomepage() {
|
||||
return mPrefs.getString(Name.HOMEPAGE, Constants.HOMEPAGE);
|
||||
return mPrefs.getString(Name.HOMEPAGE, Constants.SCHEME_HOMEPAGE);
|
||||
}
|
||||
|
||||
public boolean getIncognitoCookiesEnabled() {
|
||||
|
@ -6,13 +6,18 @@ import android.content.Intent;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
|
||||
public class NetworkReceiver extends BroadcastReceiver {
|
||||
public abstract class NetworkReceiver extends BroadcastReceiver {
|
||||
|
||||
public abstract void onConnectivityChange(boolean isConnected);
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {}
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
onConnectivityChange(isConnected(context));
|
||||
}
|
||||
|
||||
public static boolean isConnected(@NonNull Context context) {
|
||||
private static boolean isConnected(@NonNull Context context) {
|
||||
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
if (cm == null)
|
||||
return false;
|
||||
|
@ -118,6 +118,7 @@ abstract class BaseSuggestionsTask {
|
||||
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
|
||||
connection.setDoInput(true);
|
||||
connection.setRequestProperty("Accept-Encoding", "gzip");
|
||||
connection.setRequestProperty("Accept-Charset", getEncoding());
|
||||
connection.connect();
|
||||
if (connection.getResponseCode() >= HttpURLConnection.HTTP_MULT_CHOICE ||
|
||||
connection.getResponseCode() < HttpURLConnection.HTTP_OK) {
|
||||
|
@ -216,10 +216,6 @@ public final class Utils {
|
||||
return domain.startsWith("www.") ? domain.substring(4) : domain;
|
||||
}
|
||||
|
||||
public static String[] getArray(@NonNull String input) {
|
||||
return input.split(Constants.SEPARATOR);
|
||||
}
|
||||
|
||||
public static void trimCache(@NonNull Context context) {
|
||||
try {
|
||||
File dir = context.getCacheDir();
|
||||
|
@ -193,10 +193,10 @@ public class LightningView {
|
||||
return;
|
||||
}
|
||||
switch (sHomepage) {
|
||||
case "about:home":
|
||||
case Constants.SCHEME_HOMEPAGE:
|
||||
loadStartpage();
|
||||
break;
|
||||
case "about:bookmarks":
|
||||
case Constants.SCHEME_BOOKMARKS:
|
||||
loadBookmarkpage();
|
||||
break;
|
||||
default:
|
||||
|
@ -295,7 +295,7 @@ public class LightningWebClient extends WebViewClient {
|
||||
// If we are in incognito, immediately load, we don't want the url to leave the app
|
||||
return super.shouldOverrideUrlLoading(view, url);
|
||||
}
|
||||
if (url.startsWith("about:")) {
|
||||
if (url.startsWith(Constants.ABOUT)) {
|
||||
// If this is an about page, immediately load, we don't need to leave the app
|
||||
return super.shouldOverrideUrlLoading(view, url);
|
||||
}
|
||||
@ -310,7 +310,7 @@ public class LightningWebClient extends WebViewClient {
|
||||
view.loadUrl(url, headers);
|
||||
return true;
|
||||
}
|
||||
if (url.startsWith("about:") && Utils.doesSupportHeaders()) {
|
||||
if (url.startsWith(Constants.ABOUT) && Utils.doesSupportHeaders()) {
|
||||
// If this is an about page, immediately load, we don't need to leave the app
|
||||
view.loadUrl(url, headers);
|
||||
return true;
|
||||
|
Loading…
x
Reference in New Issue
Block a user