Formatting

This commit is contained in:
Anthony Restaino 2014-07-28 17:09:03 -04:00
parent 1e2908ccd6
commit b94cdc40af
8 changed files with 97 additions and 96 deletions

View File

@ -11,6 +11,7 @@ import java.io.InputStreamReader;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.HashSet; import java.util.HashSet;
import java.util.Locale;
import java.util.Set; import java.util.Set;
public class AdBlock { public class AdBlock {
@ -25,19 +26,18 @@ public class AdBlock {
private boolean mBlockAds; private boolean mBlockAds;
private static final Locale mLocale = Locale.getDefault();
public AdBlock(Context context) { public AdBlock(Context context) {
if (mBlockedDomainsList.isEmpty()) { if (mBlockedDomainsList.isEmpty()) {
loadBlockedDomainsList(context); loadBlockedDomainsList(context);
} }
mPreferences = context.getSharedPreferences( mPreferences = context.getSharedPreferences(PreferenceConstants.PREFERENCES, 0);
PreferenceConstants.PREFERENCES, 0); mBlockAds = mPreferences.getBoolean(PreferenceConstants.BLOCK_ADS, false);
mBlockAds = mPreferences.getBoolean(PreferenceConstants.BLOCK_ADS,
false);
} }
public void updatePreference() { public void updatePreference() {
mBlockAds = mPreferences.getBoolean(PreferenceConstants.BLOCK_ADS, mBlockAds = mPreferences.getBoolean(PreferenceConstants.BLOCK_ADS, false);
false);
} }
private void loadBlockedDomainsList(final Context context) { private void loadBlockedDomainsList(final Context context) {
@ -47,15 +47,15 @@ public class AdBlock {
public void run() { public void run() {
AssetManager asset = context.getAssets(); AssetManager asset = context.getAssets();
try { try {
BufferedReader reader = new BufferedReader( BufferedReader reader = new BufferedReader(new InputStreamReader(
new InputStreamReader(asset.open(BLOCKED_DOMAINS_LIST_FILE_NAME))); asset.open(BLOCKED_DOMAINS_LIST_FILE_NAME)));
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
mBlockedDomainsList.add(line.trim().toLowerCase()); mBlockedDomainsList.add(line.trim().toLowerCase(mLocale));
} }
} catch (IOException e) { } catch (IOException e) {
Log.wtf(TAG, "Reading blocked domains list from file '" + Log.wtf(TAG, "Reading blocked domains list from file '"
BLOCKED_DOMAINS_LIST_FILE_NAME + "' failed.", e); + BLOCKED_DOMAINS_LIST_FILE_NAME + "' failed.", e);
} }
} }
}); });
@ -75,7 +75,7 @@ public class AdBlock {
return false; return false;
} }
boolean isOnBlacklist = mBlockedDomainsList.contains(domain.toLowerCase()); boolean isOnBlacklist = mBlockedDomainsList.contains(domain.toLowerCase(mLocale));
if (isOnBlacklist) { if (isOnBlacklist) {
Log.d(TAG, "URL '" + url + "' is an ad"); Log.d(TAG, "URL '" + url + "' is an ad");
} }

View File

@ -200,24 +200,30 @@ public class AdvancedSettingsActivity extends Activity {
R.string.stock_browser_available)); R.string.stock_browser_available));
} }
messageHandler = new MessageHandler(); messageHandler = new MessageHandler(mContext);
} }
private class MessageHandler extends Handler { private static class MessageHandler extends Handler {
Context mHandlerContext;
public MessageHandler(Context context){
this.mHandlerContext = context;
}
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
switch (msg.what) { switch (msg.what) {
case 1: case 1:
Utils.showToast( Utils.showToast(
mContext, mHandlerContext,
mContext.getResources().getString( mHandlerContext.getResources().getString(
R.string.message_clear_history)); R.string.message_clear_history));
break; break;
case 2: case 2:
Utils.showToast( Utils.showToast(
mContext, mHandlerContext,
mContext.getResources().getString( mHandlerContext.getResources().getString(
R.string.message_cookies_cleared)); R.string.message_cookies_cleared));
break; break;
} }

View File

@ -34,17 +34,15 @@ public class DatabaseHandler extends SQLiteOpenHelper {
public static SQLiteDatabase mDatabase; public static SQLiteDatabase mDatabase;
public DatabaseHandler(Context context) { public DatabaseHandler(Context context) {
super(context.getApplicationContext(), DATABASE_NAME, null, super(context.getApplicationContext(), DATABASE_NAME, null, DATABASE_VERSION);
DATABASE_VERSION);
mDatabase = this.getWritableDatabase(); mDatabase = this.getWritableDatabase();
} }
// Creating Tables // Creating Tables
@Override @Override
public void onCreate(SQLiteDatabase db) { public void onCreate(SQLiteDatabase db) {
String CREATE_HISTORY_TABLE = "CREATE TABLE " + TABLE_HISTORY + "(" String CREATE_HISTORY_TABLE = "CREATE TABLE " + TABLE_HISTORY + "(" + KEY_ID
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_URL + " TEXT," + " INTEGER PRIMARY KEY," + KEY_URL + " TEXT," + KEY_TITLE + " TEXT" + ")";
+ KEY_TITLE + " TEXT" + ")";
db.execSQL(CREATE_HISTORY_TABLE); db.execSQL(CREATE_HISTORY_TABLE);
} }
@ -96,9 +94,8 @@ public class DatabaseHandler extends SQLiteOpenHelper {
// Getting single item // Getting single item
String getHistoryItem(String url) { String getHistoryItem(String url) {
Cursor cursor = mDatabase.query(TABLE_HISTORY, new String[]{KEY_ID, KEY_URL, Cursor cursor = mDatabase.query(TABLE_HISTORY, new String[] { KEY_ID, KEY_URL, KEY_TITLE },
KEY_TITLE}, KEY_URL + "=?", new String[]{url}, null, null, KEY_URL + "=?", new String[] { url }, null, null, null, null);
null, null);
String m = null; String m = null;
if (cursor != null) { if (cursor != null) {
cursor.moveToFirst(); cursor.moveToFirst();
@ -112,8 +109,9 @@ public class DatabaseHandler extends SQLiteOpenHelper {
public List<HistoryItem> findItemsContaining(String search) { public List<HistoryItem> findItemsContaining(String search) {
List<HistoryItem> itemList = new ArrayList<HistoryItem>(); List<HistoryItem> itemList = new ArrayList<HistoryItem>();
//select query // select query
String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " WHERE " + KEY_TITLE + " LIKE '%" + search + "%'"; String selectQuery = "SELECT * FROM " + TABLE_HISTORY + " WHERE " + KEY_TITLE + " LIKE '%"
+ search + "%'";
Cursor cursor = mDatabase.rawQuery(selectQuery, null); Cursor cursor = mDatabase.rawQuery(selectQuery, null);
// looping through all rows and adding to list // looping through all rows and adding to list
@ -181,15 +179,14 @@ public class DatabaseHandler extends SQLiteOpenHelper {
values.put(KEY_URL, item.getUrl()); values.put(KEY_URL, item.getUrl());
values.put(KEY_TITLE, item.getTitle()); values.put(KEY_TITLE, item.getTitle());
int n = mDatabase.update(TABLE_HISTORY, values, KEY_ID + " = ?", int n = mDatabase.update(TABLE_HISTORY, values, KEY_ID + " = ?",
new String[]{String.valueOf(item.getId())}); new String[] { String.valueOf(item.getId()) });
// updating row // updating row
return n; return n;
} }
// Deleting single item // Deleting single item
public synchronized void deleteHistoryItem(String id) { public synchronized void deleteHistoryItem(String id) {
mDatabase.delete(TABLE_HISTORY, KEY_ID + " = ?", mDatabase.delete(TABLE_HISTORY, KEY_ID + " = ?", new String[] { String.valueOf(id) });
new String[]{String.valueOf(id)});
} }
// Getting items Count // Getting items Count

View File

@ -30,25 +30,29 @@ public class DownloadHandler {
private static Activity mActivity; private static Activity mActivity;
/** /**
* Notify the host application a download should be done, or that the data should be streamed if a streaming viewer * Notify the host application a download should be done, or that the data
* is available. * should be streamed if a streaming viewer is available.
* *
* @param activity Activity requesting the download. * @param activity
* @param url The full url to the content that should be downloaded * Activity requesting the download.
* @param userAgent User agent of the downloading application. * @param url
* @param contentDisposition Content-disposition http header, if present. * The full url to the content that should be downloaded
* @param mimetype The mimetype of the content reported by the server * @param userAgent
* @param privateBrowsing If the request is coming from a private browsing tab. * User agent of the downloading application.
* @param contentDisposition
* Content-disposition http header, if present.
* @param mimetype
* The mimetype of the content reported by the server
* @param privateBrowsing
* If the request is coming from a private browsing tab.
*/ */
public static void onDownloadStart(Activity activity, String url, public static void onDownloadStart(Activity activity, String url, String userAgent,
String userAgent, String contentDisposition, String mimetype, String contentDisposition, String mimetype, boolean privateBrowsing) {
boolean privateBrowsing) {
mActivity = activity; mActivity = activity;
// if we're dealing wih A/V content that's not explicitly marked // if we're dealing wih A/V content that's not explicitly marked
// for download, check if it's streamable. // for download, check if it's streamable.
if (contentDisposition == null if (contentDisposition == null
|| !contentDisposition.regionMatches( || !contentDisposition.regionMatches(true, 0, "attachment", 0, 10)) {
true, 0, "attachment", 0, 10)) {
// query the package manager to see if there's a registered handler // query the package manager to see if there's a registered handler
// that matches. // that matches.
Intent intent = new Intent(Intent.ACTION_VIEW); Intent intent = new Intent(Intent.ACTION_VIEW);
@ -60,10 +64,8 @@ public class DownloadHandler {
ComponentName myName = activity.getComponentName(); ComponentName myName = activity.getComponentName();
// If we resolved to ourselves, we don't want to attempt to // If we resolved to ourselves, we don't want to attempt to
// load the url only to try and download it again. // load the url only to try and download it again.
if (!myName.getPackageName().equals( if (!myName.getPackageName().equals(info.activityInfo.packageName)
info.activityInfo.packageName) || !myName.getClassName().equals(info.activityInfo.name)) {
|| !myName.getClassName().equals(
info.activityInfo.name)) {
// someone (other than us) knows how to handle this mime // someone (other than us) knows how to handle this mime
// type with this scheme, don't download. // type with this scheme, don't download.
try { try {
@ -76,8 +78,8 @@ public class DownloadHandler {
} }
} }
} }
onDownloadStartNoStream(activity, url, userAgent, contentDisposition, onDownloadStartNoStream(activity, url, userAgent, contentDisposition, mimetype,
mimetype, privateBrowsing); privateBrowsing);
} }
// This is to work around the fact that java.net.URI throws Exceptions // This is to work around the fact that java.net.URI throws Exceptions
@ -111,23 +113,27 @@ public class DownloadHandler {
} }
/** /**
* Notify the host application a download should be done, even if there is a streaming viewer available for thise * Notify the host application a download should be done, even if there is a
* type. * streaming viewer available for thise type.
* *
* @param activity Activity requesting the download. * @param activity
* @param url The full url to the content that should be downloaded * Activity requesting the download.
* @param userAgent User agent of the downloading application. * @param url
* @param contentDisposition Content-disposition http header, if present. * The full url to the content that should be downloaded
* @param mimetype The mimetype of the content reported by the server * @param userAgent
* @param privateBrowsing If the request is coming from a private browsing tab. * User agent of the downloading application.
* @param contentDisposition
* Content-disposition http header, if present.
* @param mimetype
* The mimetype of the content reported by the server
* @param privateBrowsing
* If the request is coming from a private browsing tab.
*/ */
/*package */ /* package */
static void onDownloadStartNoStream(Activity activity, static void onDownloadStartNoStream(Activity activity, String url, String userAgent,
String url, String userAgent, String contentDisposition, String contentDisposition, String mimetype, boolean privateBrowsing) {
String mimetype, boolean privateBrowsing) {
String filename = URLUtil.guessFileName(url, String filename = URLUtil.guessFileName(url, contentDisposition, mimetype);
contentDisposition, mimetype);
// Check to see if we have an SDCard // Check to see if we have an SDCard
String status = Environment.getExternalStorageState(); String status = Environment.getExternalStorageState();
@ -144,12 +150,9 @@ public class DownloadHandler {
title = R.string.download_no_sdcard_dlg_title; title = R.string.download_no_sdcard_dlg_title;
} }
new AlertDialog.Builder(activity) new AlertDialog.Builder(activity).setTitle(title)
.setTitle(title) .setIcon(android.R.drawable.ic_dialog_alert).setMessage(msg)
.setIcon(android.R.drawable.ic_dialog_alert) .setPositiveButton(R.string.action_ok, null).show();
.setMessage(msg)
.setPositiveButton(R.string.action_ok, null)
.show();
return; return;
} }
@ -177,7 +180,8 @@ public class DownloadHandler {
} }
request.setMimeType(mimetype); request.setMimeType(mimetype);
// set downloaded file destination to /sdcard/Download. // set downloaded file destination to /sdcard/Download.
// or, should it be set to one of several Environment.DIRECTORY* dirs depending on mimetype? // or, should it be set to one of several Environment.DIRECTORY* dirs
// depending on mimetype?
String location = mActivity.getSharedPreferences(PreferenceConstants.PREFERENCES, 0) String location = mActivity.getSharedPreferences(PreferenceConstants.PREFERENCES, 0)
.getString(PreferenceConstants.DOWNLOAD_DIRECTORY, Environment.DIRECTORY_DOWNLOADS); .getString(PreferenceConstants.DOWNLOAD_DIRECTORY, Environment.DIRECTORY_DOWNLOADS);
@ -190,19 +194,17 @@ public class DownloadHandler {
// old percent-encoded url. // old percent-encoded url.
String cookies = CookieManager.getInstance().getCookie(url); String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies); request.addRequestHeader("cookie", cookies);
request.setNotificationVisibility( request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
if (mimetype == null) { if (mimetype == null) {
if (TextUtils.isEmpty(addressString)) { if (TextUtils.isEmpty(addressString)) {
return; return;
} }
// We must have long pressed on a link or image to download it. We // We must have long pressed on a link or image to download it. We
// are not sure of the mimetype in this case, so do a head request // are not sure of the mimetype in this case, so do a head request
new FetchUrlMimeType(activity, request, addressString, cookies, new FetchUrlMimeType(activity, request, addressString, cookies, userAgent).start();
userAgent).start();
} else { } else {
final DownloadManager manager final DownloadManager manager = (DownloadManager) activity
= (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE); .getSystemService(Context.DOWNLOAD_SERVICE);
new Thread("Browser download") { new Thread("Browser download") {
@Override @Override
public void run() { public void run() {
@ -210,7 +212,6 @@ public class DownloadHandler {
} }
}.start(); }.start();
} }
Toast.makeText(activity, R.string.download_pending, Toast.LENGTH_SHORT) Toast.makeText(activity, R.string.download_pending, Toast.LENGTH_SHORT).show();
.show();
} }
} }

View File

@ -100,8 +100,8 @@ public class FetchUrlMimeType extends Thread {
} }
// Start the download // Start the download
DownloadManager manager = (DownloadManager) mContext.getSystemService( DownloadManager manager = (DownloadManager) mContext
Context.DOWNLOAD_SERVICE); .getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(mRequest); manager.enqueue(mRequest);
} }
} }

View File

@ -22,8 +22,7 @@ public class IncognitoActivity extends BrowserActivity {
@Override @Override
public void updateCookiePreference() { public void updateCookiePreference() {
if (mPreferences == null) { if (mPreferences == null) {
mPreferences = getSharedPreferences( mPreferences = getSharedPreferences(PreferenceConstants.PREFERENCES, 0);
PreferenceConstants.PREFERENCES, 0);
} }
mCookieManager = CookieManager.getInstance(); mCookieManager = CookieManager.getInstance();
CookieSyncManager.createInstance(this); CookieSyncManager.createInstance(this);
@ -35,9 +34,9 @@ public class IncognitoActivity extends BrowserActivity {
@Override @Override
public synchronized void initializeTabs() { public synchronized void initializeTabs() {
super.initializeTabs(); super.initializeTabs();
//restoreOrNewTab(); // restoreOrNewTab();
newTab(null, true); newTab(null, true);
//if incognito mode use newTab(null, true); instead // if incognito mode use newTab(null, true); instead
} }
@Override @Override
@ -48,20 +47,20 @@ public class IncognitoActivity extends BrowserActivity {
@Override @Override
protected void onNewIntent(Intent intent) { protected void onNewIntent(Intent intent) {
//handleNewIntent(intent); // handleNewIntent(intent);
super.onNewIntent(intent); super.onNewIntent(intent);
} }
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
//saveOpenTabs(); // saveOpenTabs();
} }
@Override @Override
public void updateHistory(String title, String url) { public void updateHistory(String title, String url) {
super.updateHistory(title, url); super.updateHistory(title, url);
//addItemToHistory(title, url); // addItemToHistory(title, url);
} }
@Override @Override

View File

@ -94,8 +94,7 @@ public class IntentUtils {
// to launch a new intent for every URL, using OR only // to launch a new intent for every URL, using OR only
// launches a new one if there is a non-browser app that // launches a new one if there is a non-browser app that
// can handle it. // can handle it.
if (filter.countDataAuthorities() == 0 if (filter.countDataAuthorities() == 0 || filter.countDataPaths() == 0) {
|| filter.countDataPaths() == 0) {
// Generic handler, skip // Generic handler, skip
continue; continue;
} }

View File

@ -44,8 +44,7 @@ public class LicenseActivity extends Activity implements View.OnClickListener {
} }
private void actionView(String url) { private void actionView(String url) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url), this, startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url), this, MainActivity.class));
MainActivity.class));
finish(); finish();
} }