Browse Source

Browser now handles all intents correctly

master
Anthony Restaino 11 years ago
parent
commit
6698e2e023
  1. 50
      src/acr/browser/lightning/BrowserActivity.java
  2. 2
      src/acr/browser/lightning/BrowserController.java
  3. 101
      src/acr/browser/lightning/IntentUtils.java
  4. 70
      src/acr/browser/lightning/LightningView.java

50
src/acr/browser/lightning/BrowserActivity.java

@ -530,7 +530,7 @@ public class BrowserActivity extends Activity implements BrowserController {
wkp.setProxy("acr.browser.lightning.BrowserApp", wkp.setProxy("acr.browser.lightning.BrowserApp",
getApplicationContext(), host, port); getApplicationContext(), host, port);
} catch (Exception e) { } catch (Exception e) {
Log.d(Constants.LOGTAG, "error enabling web proxying", e); Log.d(Constants.TAG, "error enabling web proxying", e);
} }
} }
@ -734,7 +734,7 @@ public class BrowserActivity extends Activity implements BrowserController {
newTab(null, true); newTab(null, true);
return true; return true;
case R.id.action_incognito: case R.id.action_incognito:
startActivity(new Intent(Constants.INCOGNITO_INTENT)); startActivity(new Intent(this, IncognitoActivity.class));
return true; return true;
case R.id.action_share: case R.id.action_share:
if (!mCurrentView.getUrl().startsWith(Constants.FILE)) { if (!mCurrentView.getUrl().startsWith(Constants.FILE)) {
@ -766,7 +766,7 @@ public class BrowserActivity extends Activity implements BrowserController {
} }
return true; return true;
case R.id.action_settings: case R.id.action_settings:
startActivity(new Intent(Constants.SETTINGS_INTENT)); startActivity(new Intent(this, SettingsActivity.class));
return true; return true;
case R.id.action_history: case R.id.action_history:
openHistory(); openHistory();
@ -1053,6 +1053,18 @@ public class BrowserActivity extends Activity implements BrowserController {
} }
} }
@Override
public void closeEmptyTab() {
if (mCurrentView != null
&& mCurrentView.getWebView().copyBackForwardList().getSize() == 0) {
closeCurrentTab();
}
}
private void closeCurrentTab() {
//don't delete the tab because the browser will close and mess stuff up
}
private void selectItem(final int position) { private void selectItem(final int position) {
// update selected item and title, then close the drawer // update selected item and title, then close the drawer
@ -1150,21 +1162,21 @@ public class BrowserActivity extends Activity implements BrowserController {
PreferenceConstants.CLEAR_CACHE_EXIT, false) PreferenceConstants.CLEAR_CACHE_EXIT, false)
&& mCurrentView != null && !isIncognito()) { && mCurrentView != null && !isIncognito()) {
mCurrentView.clearCache(true); mCurrentView.clearCache(true);
Log.i(Constants.LOGTAG, "Cache Cleared"); Log.i(Constants.TAG, "Cache Cleared");
} }
if (mPreferences.getBoolean( if (mPreferences.getBoolean(
PreferenceConstants.CLEAR_HISTORY_EXIT, false) PreferenceConstants.CLEAR_HISTORY_EXIT, false)
&& !isIncognito()) { && !isIncognito()) {
clearHistory(); clearHistory();
Log.i(Constants.LOGTAG, "History Cleared"); Log.i(Constants.TAG, "History Cleared");
} }
if (mPreferences.getBoolean( if (mPreferences.getBoolean(
PreferenceConstants.CLEAR_COOKIES_EXIT, false) PreferenceConstants.CLEAR_COOKIES_EXIT, false)
&& !isIncognito()) { && !isIncognito()) {
clearCookies(); clearCookies();
Log.i(Constants.LOGTAG, "Cookies Cleared"); Log.i(Constants.TAG, "Cookies Cleared");
} }
if (reference != null) { if (reference != null) {
@ -1186,7 +1198,7 @@ public class BrowserActivity extends Activity implements BrowserController {
closeActivity(); closeActivity();
} }
Log.i(Constants.LOGTAG, "deleted tab"); Log.i(Constants.TAG, "deleted tab");
} }
@Override @Override
@ -1195,19 +1207,19 @@ public class BrowserActivity extends Activity implements BrowserController {
if (mPreferences.getBoolean(PreferenceConstants.CLEAR_CACHE_EXIT, if (mPreferences.getBoolean(PreferenceConstants.CLEAR_CACHE_EXIT,
false) && mCurrentView != null && !isIncognito()) { false) && mCurrentView != null && !isIncognito()) {
mCurrentView.clearCache(true); mCurrentView.clearCache(true);
Log.i(Constants.LOGTAG, "Cache Cleared"); Log.i(Constants.TAG, "Cache Cleared");
} }
if (mPreferences.getBoolean(PreferenceConstants.CLEAR_HISTORY_EXIT, if (mPreferences.getBoolean(PreferenceConstants.CLEAR_HISTORY_EXIT,
false) && !isIncognito()) { false) && !isIncognito()) {
clearHistory(); clearHistory();
Log.i(Constants.LOGTAG, "History Cleared"); Log.i(Constants.TAG, "History Cleared");
} }
if (mPreferences.getBoolean(PreferenceConstants.CLEAR_COOKIES_EXIT, if (mPreferences.getBoolean(PreferenceConstants.CLEAR_COOKIES_EXIT,
false) && !isIncognito()) { false) && !isIncognito()) {
clearCookies(); clearCookies();
Log.i(Constants.LOGTAG, "Cookies Cleared"); Log.i(Constants.TAG, "Cookies Cleared");
} }
mCurrentView = null; mCurrentView = null;
@ -1258,7 +1270,7 @@ public class BrowserActivity extends Activity implements BrowserController {
mDrawerLayout.closeDrawer(mDrawerRight); mDrawerLayout.closeDrawer(mDrawerRight);
} else { } else {
if (mCurrentView != null) { if (mCurrentView != null) {
Log.i(Constants.LOGTAG, "onBackPressed"); Log.i(Constants.TAG, "onBackPressed");
if (mCurrentView.canGoBack()) { if (mCurrentView.canGoBack()) {
if (!mCurrentView.isShown()) { if (!mCurrentView.isShown()) {
onHideCustomView(); onHideCustomView();
@ -1269,7 +1281,7 @@ public class BrowserActivity extends Activity implements BrowserController {
deleteTab(mDrawerList.getCheckedItemPosition()); deleteTab(mDrawerList.getCheckedItemPosition());
} }
} else { } else {
Log.e(Constants.LOGTAG, Log.e(Constants.TAG,
"So madness. Much confusion. Why happen."); "So madness. Much confusion. Why happen.");
super.onBackPressed(); super.onBackPressed();
} }
@ -1279,7 +1291,7 @@ public class BrowserActivity extends Activity implements BrowserController {
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
Log.i(Constants.LOGTAG, "onPause"); Log.i(Constants.TAG, "onPause");
if (mCurrentView != null) { if (mCurrentView != null) {
mCurrentView.pauseTimers(); mCurrentView.pauseTimers();
mCurrentView.onPause(); mCurrentView.onPause();
@ -1311,7 +1323,7 @@ public class BrowserActivity extends Activity implements BrowserController {
@Override @Override
protected void onDestroy() { protected void onDestroy() {
Log.i(Constants.LOGTAG, "onDestroy"); Log.i(Constants.TAG, "onDestroy");
if (mHistoryDatabase != null) { if (mHistoryDatabase != null) {
if (mHistoryDatabase.isOpen()) if (mHistoryDatabase.isOpen())
mHistoryDatabase.close(); mHistoryDatabase.close();
@ -1326,7 +1338,7 @@ public class BrowserActivity extends Activity implements BrowserController {
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
Log.i(Constants.LOGTAG, "onResume"); Log.i(Constants.TAG, "onResume");
if (SettingsController.getClearHistory()) { if (SettingsController.getClearHistory()) {
} }
if (mSearchAdapter != null) { if (mSearchAdapter != null) {
@ -1790,13 +1802,13 @@ public class BrowserActivity extends Activity implements BrowserController {
cursor.close(); cursor.close();
cursor = null; cursor = null;
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
Log.e(Constants.LOGTAG, Log.e(Constants.TAG,
"IllegalStateException in updateHistory"); "IllegalStateException in updateHistory");
} catch (NullPointerException e) { } catch (NullPointerException e) {
Log.e(Constants.LOGTAG, Log.e(Constants.TAG,
"NullPointerException in updateHistory"); "NullPointerException in updateHistory");
} catch (SQLiteException e) { } catch (SQLiteException e) {
Log.e(Constants.LOGTAG, "SQLiteException in updateHistory"); Log.e(Constants.TAG, "SQLiteException in updateHistory");
} }
} }
}; };
@ -2146,7 +2158,7 @@ public class BrowserActivity extends Activity implements BrowserController {
if (mCustomView == null || mCustomViewCallback == null if (mCustomView == null || mCustomViewCallback == null
|| mCurrentView == null) || mCurrentView == null)
return; return;
Log.i(Constants.LOGTAG, "onHideCustomView"); Log.i(Constants.TAG, "onHideCustomView");
mCurrentView.setVisibility(View.VISIBLE); mCurrentView.setVisibility(View.VISIBLE);
mCustomView.setKeepScreenOn(false); mCustomView.setKeepScreenOn(false);
setFullscreen(mPreferences.getBoolean( setFullscreen(mPreferences.getBoolean(

2
src/acr/browser/lightning/BrowserController.java

@ -46,4 +46,6 @@ public interface BrowserController {
public void openBookmarkPage(WebView view); public void openBookmarkPage(WebView view);
public boolean isActionBarShowing(); public boolean isActionBarShowing();
public void closeEmptyTab();
} }

101
src/acr/browser/lightning/IntentUtils.java

@ -0,0 +1,101 @@
package acr.browser.lightning;
import java.net.URISyntaxException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.util.Log;
import android.webkit.WebView;
public class IntentUtils {
private Activity mActivity;
private BrowserController mController;
static final Pattern ACCEPTED_URI_SCHEMA = Pattern.compile("(?i)"
+ // switch on case insensitive matching
"("
+ // begin group for schema
"(?:http|https|file):\\/\\/" + "|(?:inline|data|about|javascript):"
+ "|(?:.*:.*@)" + ")" + "(.*)");
public IntentUtils(BrowserController controller) {
mController = controller;
mActivity = mController.getActivity();
}
public boolean startActivityForUrl(WebView tab, String url) {
Intent intent;
try {
intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
} catch (URISyntaxException ex) {
Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
return false;
}
if (mActivity.getPackageManager().resolveActivity(intent, 0) == null) {
String packagename = intent.getPackage();
if (packagename != null) {
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("market://search?q=pname:" + packagename));
intent.addCategory(Intent.CATEGORY_BROWSABLE);
mActivity.startActivity(intent);
return true;
} else {
return false;
}
}
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setComponent(null);
if (tab != null) {
intent.putExtra(mActivity.getPackageName() + ".Origin", 1);
}
Matcher m = ACCEPTED_URI_SCHEMA.matcher(url);
if (m.matches() && !isSpecializedHandlerAvailable(intent)) {
return false;
}
try {
if (mActivity.startActivityIfNeeded(intent, -1)) {
return true;
}
} catch (ActivityNotFoundException ex) {
}
return false;
}
/**
* Search for intent handlers that are specific to this URL aka, specialized
* apps like google maps or youtube
*/
private boolean isSpecializedHandlerAvailable(Intent intent) {
PackageManager pm = mActivity.getPackageManager();
List<ResolveInfo> handlers = pm.queryIntentActivities(intent,
PackageManager.GET_RESOLVED_FILTER);
if (handlers == null || handlers.size() == 0) {
return false;
}
for (ResolveInfo resolveInfo : handlers) {
IntentFilter filter = resolveInfo.filter;
if (filter == null) {
// No intent filter matches this intent?
// Error on the side of staying in the browser, ignore
continue;
}
if (filter.countDataAuthorities() == 0
|| filter.countDataPaths() == 0) {
// Generic handler, skip
continue;
}
return true;
}
return false;
}
}

70
src/acr/browser/lightning/LightningView.java

@ -67,7 +67,6 @@ public class LightningView {
private Activity mActivity; private Activity mActivity;
private WebSettings mSettings; private WebSettings mSettings;
private static int API = android.os.Build.VERSION.SDK_INT; private static int API = android.os.Build.VERSION.SDK_INT;
private static String mPackageName;
private static String mHomepage; private static String mHomepage;
private static String mDefaultUserAgent; private static String mDefaultUserAgent;
private static Bitmap mWebpageBitmap; private static Bitmap mWebpageBitmap;
@ -76,16 +75,18 @@ public class LightningView {
private static AdBlock mAdBlock; private static AdBlock mAdBlock;
private CookieManager mCookieManager; private CookieManager mCookieManager;
private boolean isForgroundTab = false; private boolean isForgroundTab = false;
private IntentUtils mIntentUtils = null;
@SuppressLint("NewApi") @SuppressLint("NewApi")
public LightningView(Activity activity, String url, public LightningView(Activity activity, String url,
CookieManager cookieManager) { CookieManager cookieManager) {
mActivity = activity; mActivity = activity;
mCookieManager = cookieManager; mCookieManager = cookieManager;
mWebView = new WebView(activity); mWebView = new WebView(activity);
mTitle = new Title(activity); mTitle = new Title(activity);
mAdBlock = new AdBlock(activity); mAdBlock = new AdBlock(activity);
mPackageName = activity.getPackageName(); activity.getPackageName();
mWebpageBitmap = BitmapFactory.decodeResource(activity.getResources(), mWebpageBitmap = BitmapFactory.decodeResource(activity.getResources(),
R.drawable.ic_webpage); R.drawable.ic_webpage);
@ -95,6 +96,7 @@ public class LightningView {
throw new ClassCastException(activity.toString() throw new ClassCastException(activity.toString()
+ " must implement BrowserController"); + " must implement BrowserController");
} }
mIntentUtils = new IntentUtils(mBrowserController);
mWebView.setDrawingCacheBackgroundColor(0x00000000); mWebView.setDrawingCacheBackgroundColor(0x00000000);
mWebView.setFocusableInTouchMode(true); mWebView.setFocusableInTouchMode(true);
mWebView.setFocusable(true); mWebView.setFocusable(true);
@ -488,6 +490,10 @@ public class LightningView {
} }
} }
public Activity getActivity() {
return mActivity;
}
public synchronized void onDestroy() { public synchronized void onDestroy() {
if (mWebView != null) { if (mWebView != null) {
mWebView.stopLoading(); mWebView.stopLoading();
@ -686,7 +692,7 @@ public class LightningView {
return null; // let webkit handle it return null; // let webkit handle it
} }
} catch (Exception e) { } catch (Exception e) {
Log.e(Constants.LOGTAG, "Error filtering stream", e); Log.e(Constants.TAG, "Error filtering stream", e);
ByteArrayInputStream EMPTY = new ByteArrayInputStream( ByteArrayInputStream EMPTY = new ByteArrayInputStream(
"".getBytes()); "".getBytes());
WebResourceResponse response = new WebResourceResponse( WebResourceResponse response = new WebResourceResponse(
@ -701,7 +707,13 @@ public class LightningView {
if (view.isShown()) { if (view.isShown()) {
view.invalidate(); view.invalidate();
} }
mTitle.setTitle(view.getTitle()); if (view.getTitle() == null) {
mTitle.setTitle(mActivity.getString(R.string.untitled));
} else if (view.getTitle().length() > 0) {
mTitle.setTitle(view.getTitle());
} else {
mTitle.setTitle(mActivity.getString(R.string.untitled));
}
mBrowserController.update(); mBrowserController.update();
} }
@ -748,7 +760,7 @@ public class LightningView {
String user = name.getText().toString(); String user = name.getText().toString();
String pass = password.getText().toString(); String pass = password.getText().toString();
handler.proceed(user.trim(), pass.trim()); handler.proceed(user.trim(), pass.trim());
Log.i(Constants.LOGTAG, "Request Login"); Log.i(Constants.TAG, "Request Login");
} }
}) })
@ -842,46 +854,16 @@ public class LightningView {
@Override @Override
public boolean shouldOverrideUrlLoading(WebView view, String url) { public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("market://") if (url.startsWith("about:")) {
|| url.startsWith("http://play.google.com/store/apps") return super.shouldOverrideUrlLoading(view, url);
|| url.startsWith("https://play.google.com/store/apps")) { }
Intent urlIntent = new Intent(Intent.ACTION_VIEW, if (url.contains("mailto:")) {
Uri.parse(url));
urlIntent.putExtra(mPackageName + ".Origin", 1);
mActivity.startActivity(urlIntent);
return true;
} else if (url.startsWith("http://www.youtube.com")
|| url.startsWith("https://www.youtube.com")
|| url.startsWith("http://m.youtube.com")
|| url.startsWith("https://m.youtube.com")) {
Intent urlIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(url));
urlIntent.putExtra(mPackageName + ".Origin", 1);
mActivity.startActivity(urlIntent);
return true;
} else if (url.startsWith("http://maps.google.com")
|| url.startsWith("https://maps.google.com")) {
Intent urlIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(url));
urlIntent.putExtra(mPackageName + ".Origin", 1);
mActivity.startActivity(urlIntent);
return true;
} else if (url.contains("tel:") || TextUtils.isDigitsOnly(url)) {
mActivity.startActivity(new Intent(Intent.ACTION_DIAL, Uri
.parse(url)));
return true;
} else if (url.contains("mailto:")) {
MailTo mailTo = MailTo.parse(url); MailTo mailTo = MailTo.parse(url);
Intent i = Utils.newEmailIntent(mActivity, mailTo.getTo(), Intent i = Utils.newEmailIntent(mActivity, mailTo.getTo(),
mailTo.getSubject(), mailTo.getBody(), mailTo.getCc()); mailTo.getSubject(), mailTo.getBody(), mailTo.getCc());
mActivity.startActivity(i); mActivity.startActivity(i);
view.reload(); view.reload();
return true; return true;
} else if (url.startsWith("magnet:?")) {
Intent urlIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(url));
urlIntent.putExtra(mPackageName + ".Origin", 1);
mActivity.startActivity(urlIntent);
} else if (url.startsWith("intent://")) { } else if (url.startsWith("intent://")) {
Intent intent = null; Intent intent = null;
try { try {
@ -893,12 +875,12 @@ public class LightningView {
try { try {
mActivity.startActivity(intent); mActivity.startActivity(intent);
} catch (ActivityNotFoundException e) { } catch (ActivityNotFoundException e) {
Log.e(Constants.LOGTAG, "ActivityNotFoundException"); Log.e(Constants.TAG, "ActivityNotFoundException");
} }
return true; return true;
} }
} }
return super.shouldOverrideUrlLoading(view, url); return mIntentUtils.startActivityForUrl(mWebView, url);
} }
} }
@ -925,7 +907,11 @@ public class LightningView {
@Override @Override
public void onReceivedTitle(WebView view, String title) { public void onReceivedTitle(WebView view, String title) {
mTitle.setTitle(title); if (title.length() > 0) {
mTitle.setTitle(title);
} else {
mTitle.setTitle(mActivity.getString(R.string.untitled));
}
mBrowserController.update(); mBrowserController.update();
mBrowserController.updateHistory(title, view.getUrl()); mBrowserController.updateHistory(title, view.getUrl());
} }

Loading…
Cancel
Save