Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/ServiceTabCreator.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ServiceTabCreator.java b/chrome/android/java/src/org/chromium/chrome/browser/ServiceTabCreator.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2feb4d098beb499dcf2755550336906388a79a02 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/ServiceTabCreator.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 Creator to be used from background Android Services, when it is not known whether an |
| + * Activity is available. It works by sending an intent to launch a new tab, and then getting |
|
Miguel Garcia
2015/02/03 16:58:40
It does no work like this yet right? Perhaps keep
Peter Beverloo
2015/02/03 17:24:54
Done. I added a TODO.
|
| + * confirmation from the Activity that the tab has been created. |
| + */ |
| +public class ServiceTabCreator { |
| + private static final String TAG = ServiceTabCreator.class.getSimpleName(); |
| + |
| + private static final String BROWSER_ACTIVITY_KEY = |
| + "org.chromium.chrome.browser.BROWSER_ACTIVITY"; |
| + |
| + private final long mNativeServiceTabCreator; |
| + private final Context mContext; |
| + |
| + @CalledByNative |
| + private static ServiceTabCreator create(long nativeServiceTabCreator, Context context) { |
|
Miguel Garcia
2015/02/03 16:58:40
shouldn't we make this a singleton so that we have
Peter Beverloo
2015/02/03 17:24:54
What else would initialize it, given @CalledByNati
|
| + return new ServiceTabCreator(nativeServiceTabCreator, context); |
| + } |
| + |
| + private ServiceTabCreator(long nativeServiceTabCreator, Context context) { |
| + mNativeServiceTabCreator = nativeServiceTabCreator; |
| + mContext = context; |
| + } |
| + |
| + /** |
| + * Launches the browser activity and creates a new tab for |url|. |
| + * |
| + * @param url The URL to be opened in a new tab. |
| + */ |
| + @CalledByNative |
| + private void createTab(String url) { |
|
Miguel Garcia
2015/02/03 16:58:40
perhaps rename it to launchTab, it would be mode d
Peter Beverloo
2015/02/03 17:24:54
Done.
|
| + Intent intent = new Intent(mContext, getBrowserActivityClassFromManifest()); |
|
Miguel Garcia
2015/02/03 16:58:40
perhaps get the class first and fail gracefully if
Peter Beverloo
2015/02/03 17:24:54
The only realistic failure-case for gBACFM() is th
|
| + 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 creating 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); |
| + |
| + } catch (NameNotFoundException e) { |
| + // Context.getPackageName() refers to an invalid package name, which is not possible. |
|
Miguel Garcia
2015/02/03 16:58:40
You might still want to log it just in case.
Peter Beverloo
2015/02/03 17:24:54
Done. I also removed the comments.
|
| + } catch (ClassNotFoundException e) { |
| + // The value of BROWSER_ACTIVITY does not refer to an existing class. |
| + Log.e(TAG, "Invalid value for BROWSER_ACTIVITY in the Android manifest file."); |
| + } |
| + |
| + return null; |
| + } |
| +} |