Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/externalnav/ExternalNavigationDelegateImpl.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/externalnav/ExternalNavigationDelegateImpl.java b/chrome/android/java/src/org/chromium/chrome/browser/externalnav/ExternalNavigationDelegateImpl.java |
| index 21d792c8662636ed0b1fe78407cd04820781784e..3e69716a969695d10190fde8cd8fd75a3b33d4ef 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/externalnav/ExternalNavigationDelegateImpl.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/externalnav/ExternalNavigationDelegateImpl.java |
| @@ -41,6 +41,7 @@ import org.chromium.chrome.browser.util.UrlUtilities; |
| import org.chromium.content_public.browser.LoadUrlParams; |
| import org.chromium.content_public.common.Referrer; |
| import org.chromium.ui.base.PageTransition; |
| +import org.chromium.ui.base.WindowAndroid; |
| import org.chromium.ui.base.WindowAndroid.PermissionCallback; |
| import java.util.List; |
| @@ -53,17 +54,25 @@ public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat |
| private static final String PDF_VIEWER = "com.google.android.apps.docs"; |
| private static final String PDF_MIME = "application/pdf"; |
| private static final String PDF_SUFFIX = ".pdf"; |
| - private final ChromeActivity mActivity; |
| + private final Tab mTab; |
| + protected final Context mApplicationContext; |
| - public ExternalNavigationDelegateImpl(ChromeActivity activity) { |
| - mActivity = activity; |
| + public ExternalNavigationDelegateImpl(Tab tab) { |
| + mTab = tab; |
| + // At the point of creation the tab should have a valid WindowAndroid. |
|
gone
2016/01/25 23:42:43
Add an assert?
Yusuf
2016/01/25 23:55:59
Done.
|
| + mApplicationContext = tab.getWindowAndroid().getContext().get().getApplicationContext(); |
| } |
| /** |
| - * @return The activity that this delegate is associated with. |
| + * @return The activity {@link Context} if it can be reached. |
|
gone
2016/01/25 23:42:43
nit: Explicitly specify when you'd need this funct
Yusuf
2016/01/25 23:55:59
Done.
|
| + * Application {@link Context} if not. |
| */ |
| - protected final Activity getActivity() { |
| - return mActivity; |
| + protected final Context getActivityContextIfAvailable() { |
| + if (mTab.getWindowAndroid() == null) return mApplicationContext; |
| + Context activityContext = WindowAndroid.activityFromContext( |
| + mTab.getWindowAndroid().getContext().get()); |
| + if (activityContext == null) return mApplicationContext; |
| + return activityContext; |
| } |
| /** |
| @@ -189,17 +198,17 @@ public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat |
| @Override |
| public List<ComponentName> queryIntentActivities(Intent intent) { |
| - return IntentUtils.getIntentHandlers(mActivity, intent); |
| + return IntentUtils.getIntentHandlers(mApplicationContext, intent); |
| } |
| @Override |
| public boolean willChromeHandleIntent(Intent intent) { |
| - return willChromeHandleIntent(mActivity, intent, false); |
| + return willChromeHandleIntent(mApplicationContext, intent, false); |
| } |
| @Override |
| public boolean isSpecializedHandlerAvailable(Intent intent) { |
| - return isPackageSpecializedHandler(mActivity, null, intent); |
| + return isPackageSpecializedHandler(mApplicationContext, null, intent); |
| } |
| /** |
| @@ -242,14 +251,16 @@ public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat |
| @Override |
| public String getPackageName() { |
| - return mActivity.getPackageName(); |
| + return mApplicationContext.getPackageName(); |
| } |
| @Override |
| public void startActivity(Intent intent) { |
| try { |
| - forcePdfViewerAsIntentHandlerIfNeeded(mActivity, intent); |
| - mActivity.startActivity(intent); |
| + forcePdfViewerAsIntentHandlerIfNeeded(mApplicationContext, intent); |
| + Context context = getActivityContextIfAvailable(); |
| + if (!(context instanceof Activity)) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| + context.startActivity(intent); |
| } catch (RuntimeException e) { |
| logTransactionTooLargeOrRethrow(e, intent); |
| } |
| @@ -258,8 +269,10 @@ public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat |
| @Override |
| public boolean startActivityIfNeeded(Intent intent) { |
| try { |
| - forcePdfViewerAsIntentHandlerIfNeeded(mActivity, intent); |
| - return mActivity.startActivityIfNeeded(intent, -1); |
| + forcePdfViewerAsIntentHandlerIfNeeded(mApplicationContext, intent); |
| + Context context = getActivityContextIfAvailable(); |
| + if (!(context instanceof Activity)) return false; |
| + return ((Activity) context).startActivityIfNeeded(intent, -1); |
|
gone
2016/01/25 23:42:43
if (context instanceof Activity) {
return ((Ac
Yusuf
2016/01/25 23:55:59
Done.
|
| } catch (RuntimeException e) { |
| logTransactionTooLargeOrRethrow(e, intent); |
| return false; |
| @@ -269,7 +282,10 @@ public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat |
| @Override |
| public void startIncognitoIntent(final Intent intent, final String referrerUrl, |
| final String fallbackUrl, final Tab tab, final boolean needsToCloseTab) { |
| - new AlertDialog.Builder(mActivity, R.style.AlertDialogTheme) |
| + Context context = tab.getWindowAndroid().getContext().get(); |
| + if (!(context instanceof Activity)) return; |
|
gone
2016/01/25 23:42:43
nit newline
Yusuf
2016/01/25 23:55:59
Done.
|
| + Activity activity = (Activity) context; |
| + new AlertDialog.Builder(activity, R.style.AlertDialogTheme) |
| .setTitle(R.string.external_app_leave_incognito_warning_title) |
| .setMessage(R.string.external_app_leave_incognito_warning) |
| .setPositiveButton(R.string.ok, new OnClickListener() { |
| @@ -304,8 +320,7 @@ public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat |
| // If the url points inside of Chromium's data directory, no permissions are necessary. |
| // This is required to prevent permission prompt when uses wants to access offline pages. |
| - if (url.startsWith("file://" + PathUtils.getDataDirectory( |
| - mActivity.getApplicationContext()))) { |
| + if (url.startsWith("file://" + PathUtils.getDataDirectory(mApplicationContext))) { |
| return false; |
| } |
| @@ -359,7 +374,7 @@ public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat |
| intent.addCategory(Intent.CATEGORY_BROWSABLE); |
| intent.setClassName(getPackageName(), ChromeLauncherActivity.class.getName()); |
| intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| - IntentHandler.addTrustedIntentExtras(intent, mActivity); |
| + IntentHandler.addTrustedIntentExtras(intent, mApplicationContext); |
| startActivity(intent); |
| if (needsToCloseTab) closeTab(tab); |
| @@ -406,13 +421,13 @@ public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat |
| @Override |
| public boolean isDocumentMode() { |
| - return FeatureUtilities.isDocumentMode(mActivity); |
| + return FeatureUtilities.isDocumentMode(mApplicationContext); |
| } |
| @Override |
| public String getDefaultSmsPackageName() { |
| if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return null; |
| - return Telephony.Sms.getDefaultSmsPackage(mActivity); |
| + return Telephony.Sms.getDefaultSmsPackage(mApplicationContext); |
| } |
| private static void logTransactionTooLargeOrRethrow(RuntimeException e, Intent intent) { |
| @@ -425,6 +440,8 @@ public class ExternalNavigationDelegateImpl implements ExternalNavigationDelegat |
| } |
| private void closeTab(Tab tab) { |
| - mActivity.getTabModelSelector().closeTab(tab); |
| + Context context = tab.getWindowAndroid().getContext().get(); |
| + if (!(context instanceof ChromeActivity)) return; |
|
gone
2016/01/25 23:42:43
if (context instanceof ChromeActivity) {
((Chr
Yusuf
2016/01/25 23:55:59
Done.
|
| + ((ChromeActivity) context).getTabModelSelector().closeTab(tab); |
| } |
| } |