Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/ServiceTabLauncher.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ServiceTabLauncher.java b/chrome/android/java/src/org/chromium/chrome/browser/ServiceTabLauncher.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f0eda8d0722c5b98f886d6974ec1acdccec2fba1 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/ServiceTabLauncher.java |
| @@ -0,0 +1,81 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser; |
| + |
| +import android.content.Context; |
| +import android.content.Intent; |
| +import android.content.pm.ApplicationInfo; |
| +import android.content.pm.PackageManager; |
| +import android.content.pm.PackageManager.NameNotFoundException; |
| +import android.net.Uri; |
| +import android.util.Log; |
| + |
| +import org.chromium.base.CalledByNative; |
| + |
| +/** |
| + * Tab Launcher to be used to launch new tabs from background Android Services, when it is not |
| + * known whether an activity is available. It will send an intent to launch the activity. |
| + * |
| + * TODO(peter): Have the activity confirm that the tab has been launched. |
| + */ |
| +public class ServiceTabLauncher { |
| + private static final String TAG = ServiceTabLauncher.class.getSimpleName(); |
| + |
| + private static final String BROWSER_ACTIVITY_KEY = |
| + "org.chromium.chrome.browser.BROWSER_ACTIVITY"; |
| + |
| + private final long mNativeServiceTabLauncher; |
| + private final Context mContext; |
| + |
| + @CalledByNative |
| + private static ServiceTabLauncher create(long nativeServiceTabLauncher, Context context) { |
| + return new ServiceTabLauncher(nativeServiceTabLauncher, context); |
| + } |
| + |
| + private ServiceTabLauncher(long nativeServiceTabLauncher, Context context) { |
| + mNativeServiceTabLauncher = nativeServiceTabLauncher; |
| + mContext = context; |
| + } |
| + |
| + /** |
| + * Launches the browser activity and launches a tab for |url|. |
| + * |
| + * @param url The URL to be opened in a new tab. |
| + */ |
| + @CalledByNative |
| + private void launchTab(String url) { |
| + Intent intent = new Intent(mContext, getBrowserActivityClassFromManifest()); |
| + intent.setAction(Intent.ACTION_MAIN); |
| + intent.addCategory(Intent.CATEGORY_LAUNCHER); |
| + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| + intent.setData(Uri.parse(url)); |
| + |
| + // TODO(peter): Add the referer. |
| + |
| + mContext.startActivity(intent); |
| + } |
| + |
| + /** |
| + * Reads the BROWSER_ACTIVITY name from the meta data in the Android Manifest file. |
| + * |
| + * @return Class for the browser activity to use when launching tabs. |
| + */ |
| + private Class<?> getBrowserActivityClassFromManifest() { |
| + try { |
| + ApplicationInfo info = mContext.getPackageManager().getApplicationInfo( |
| + mContext.getPackageName(), PackageManager.GET_META_DATA); |
| + String className = info.metaData.getString(BROWSER_ACTIVITY_KEY); |
| + |
| + return Class.forName(className); |
| + |
|
mlamouri (slow - plz ping)
2015/02/11 13:50:23
nit: empty line
Peter Beverloo
2015/02/11 16:18:27
Done.
|
| + } catch (NameNotFoundException e) { |
| + Log.e(TAG, "Context.getPackage() refers to an invalid package name."); |
| + } catch (ClassNotFoundException e) { |
| + Log.e(TAG, "Invalid value for BROWSER_ACTIVITY in the Android manifest file."); |
| + } |
| + |
| + return null; |
| + } |
| +} |