| Index: chrome/android/java/src/org/chromium/chrome/browser/share/OptionalShareTargetsManager.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/printing/PrintShareActivity.java b/chrome/android/java/src/org/chromium/chrome/browser/share/OptionalShareTargetsManager.java
|
| similarity index 52%
|
| copy from chrome/android/java/src/org/chromium/chrome/browser/printing/PrintShareActivity.java
|
| copy to chrome/android/java/src/org/chromium/chrome/browser/share/OptionalShareTargetsManager.java
|
| index 243cf45b931d1ad8286fc20631dea9f80c3f3697..411aa6eaf0a60fabb4d427073edfef4288383506 100644
|
| --- a/chrome/android/java/src/org/chromium/chrome/browser/printing/PrintShareActivity.java
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/share/OptionalShareTargetsManager.java
|
| @@ -1,29 +1,22 @@
|
| -// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Copyright 2017 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.printing;
|
| +package org.chromium.chrome.browser.share;
|
|
|
| import android.app.Activity;
|
| import android.content.ComponentName;
|
| -import android.content.Intent;
|
| import android.content.pm.PackageManager;
|
| import android.os.AsyncTask;
|
| -import android.os.Bundle;
|
| import android.os.StrictMode;
|
| -import android.support.v7.app.AppCompatActivity;
|
|
|
| import org.chromium.base.ActivityState;
|
| import org.chromium.base.ApplicationStatus;
|
| import org.chromium.base.ApplicationStatus.ActivityStateListener;
|
| import org.chromium.base.Log;
|
| import org.chromium.base.ThreadUtils;
|
| -import org.chromium.chrome.R;
|
| -import org.chromium.chrome.browser.ChromeActivity;
|
| -import org.chromium.chrome.browser.share.ShareHelper;
|
| -import org.chromium.chrome.browser.util.IntentUtils;
|
|
|
| -import java.lang.ref.WeakReference;
|
| +import java.util.ArrayList;
|
| import java.util.Collections;
|
| import java.util.HashSet;
|
| import java.util.List;
|
| @@ -31,54 +24,60 @@ import java.util.Set;
|
| import java.util.concurrent.ExecutionException;
|
|
|
| /**
|
| - * A simple activity that allows Chrome to expose print as an option in the share menu.
|
| + * A manager for optional share activities in the share picker intent.
|
| */
|
| -public class PrintShareActivity extends AppCompatActivity {
|
| -
|
| - private static final String TAG = "cr_printing";
|
| +public class OptionalShareTargetsManager {
|
| + private static final String TAG = "share_manager";
|
|
|
| private static Set<Activity> sPendingShareActivities =
|
| Collections.synchronizedSet(new HashSet<Activity>());
|
| private static ActivityStateListener sStateListener;
|
| private static AsyncTask<Void, Void, Void> sStateChangeTask;
|
| + private static List<ComponentName> sEnabledComponents;
|
|
|
| /**
|
| - * Enable the print sharing option.
|
| - *
|
| - * @param activity The activity that will be triggering the share action. The activitiy's
|
| - * state will be tracked to disable the print option when the share operation
|
| - * has been completed.
|
| - * @param callback The callback to be triggered after the print option has been enabled. This
|
| + * Enables sharing options.
|
| + * @param triggeringActivity The activity that will be triggering the share action. The
|
| + * activity's state will be tracked to disable the options when
|
| + * the share operation has been completed.
|
| + * @param enabledClasses classes to be enabled.
|
| + * @param callback The callback to be triggered after the options have been enabled. This
|
| * may or may not be synchronous depending on whether this will require
|
| * interacting with the Android framework.
|
| */
|
| - public static void enablePrintShareOption(final Activity activity, final Runnable callback) {
|
| + public static void enableOptionalShareActivities(final Activity triggeringActivity,
|
| + final List<Class<? extends Activity>> enabledClasses, final Runnable callback) {
|
| ThreadUtils.assertOnUiThread();
|
|
|
| if (sStateListener == null) {
|
| sStateListener = new ActivityStateListener() {
|
| @Override
|
| - public void onActivityStateChange(Activity activity, int newState) {
|
| + public void onActivityStateChange(Activity triggeringActivity, int newState) {
|
| if (newState == ActivityState.PAUSED) return;
|
| - unregisterActivity(activity);
|
| + handleShareFinish(triggeringActivity);
|
| }
|
| };
|
| }
|
| ApplicationStatus.registerStateListenerForAllActivities(sStateListener);
|
| boolean wasEmpty = sPendingShareActivities.isEmpty();
|
| - sPendingShareActivities.add(activity);
|
| + sPendingShareActivities.add(triggeringActivity);
|
|
|
| waitForPendingStateChangeTask();
|
| if (wasEmpty) {
|
| + // Note: possible race condition if two calls to this method happen simultaneously.
|
| sStateChangeTask = new AsyncTask<Void, Void, Void>() {
|
| @Override
|
| protected Void doInBackground(Void... params) {
|
| if (sPendingShareActivities.isEmpty()) return null;
|
| -
|
| - activity.getPackageManager().setComponentEnabledSetting(
|
| - new ComponentName(activity, PrintShareActivity.class),
|
| - PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
|
| - PackageManager.DONT_KILL_APP);
|
| + sEnabledComponents = new ArrayList<>(enabledClasses.size());
|
| + for (int i = 0; i < enabledClasses.size(); i++) {
|
| + ComponentName newEnabledComponent =
|
| + new ComponentName(triggeringActivity, enabledClasses.get(i));
|
| + triggeringActivity.getPackageManager().setComponentEnabledSetting(
|
| + newEnabledComponent, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
|
| + PackageManager.DONT_KILL_APP);
|
| + sEnabledComponents.add(newEnabledComponent);
|
| + }
|
| return null;
|
| }
|
|
|
| @@ -97,10 +96,15 @@ public class PrintShareActivity extends AppCompatActivity {
|
| }
|
| }
|
|
|
| - private static void unregisterActivity(final Activity activity) {
|
| + /**
|
| + * Handles when the triggering activity has finished the sharing operation. If all
|
| + * pending shares have been complete then it will disable all enabled components.
|
| + * @param triggeringActivity The activity that is triggering the share action.
|
| + */
|
| + public static void handleShareFinish(final Activity triggeringActivity) {
|
| ThreadUtils.assertOnUiThread();
|
|
|
| - sPendingShareActivities.remove(activity);
|
| + sPendingShareActivities.remove(triggeringActivity);
|
| if (!sPendingShareActivities.isEmpty()) return;
|
| ApplicationStatus.unregisterActivityStateListener(sStateListener);
|
|
|
| @@ -109,11 +113,12 @@ public class PrintShareActivity extends AppCompatActivity {
|
| @Override
|
| protected Void doInBackground(Void... params) {
|
| if (!sPendingShareActivities.isEmpty()) return null;
|
| -
|
| - activity.getPackageManager().setComponentEnabledSetting(
|
| - new ComponentName(activity, PrintShareActivity.class),
|
| - PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
| - PackageManager.DONT_KILL_APP);
|
| + for (int i = 0; i < sEnabledComponents.size(); i++) {
|
| + triggeringActivity.getPackageManager().setComponentEnabledSetting(
|
| + sEnabledComponents.get(i),
|
| + PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
| + PackageManager.DONT_KILL_APP);
|
| + }
|
| return null;
|
| }
|
|
|
| @@ -138,47 +143,9 @@ public class PrintShareActivity extends AppCompatActivity {
|
| sStateChangeTask.get();
|
| sStateChangeTask = null;
|
| } catch (InterruptedException | ExecutionException e) {
|
| - Log.e(TAG, "Print state change task did not complete as expected");
|
| + Log.e(TAG, "State change task did not complete as expected");
|
| } finally {
|
| StrictMode.setThreadPolicy(oldPolicy);
|
| }
|
| }
|
| -
|
| - @Override
|
| - protected void onCreate(Bundle savedInstanceState) {
|
| - super.onCreate(savedInstanceState);
|
| -
|
| - try {
|
| - Intent intent = getIntent();
|
| - if (intent == null) return;
|
| - if (!Intent.ACTION_SEND.equals(intent.getAction())) return;
|
| - if (!IntentUtils.safeHasExtra(getIntent(), ShareHelper.EXTRA_TASK_ID)) return;
|
| - handlePrintAction();
|
| - } finally {
|
| - finish();
|
| - }
|
| - }
|
| -
|
| - private void handlePrintAction() {
|
| - int triggeringTaskId =
|
| - IntentUtils.safeGetIntExtra(getIntent(), ShareHelper.EXTRA_TASK_ID, 0);
|
| - List<WeakReference<Activity>> activities = ApplicationStatus.getRunningActivities();
|
| - ChromeActivity triggeringActivity = null;
|
| - for (int i = 0; i < activities.size(); i++) {
|
| - Activity activity = activities.get(i).get();
|
| - if (activity == null) continue;
|
| -
|
| - // Since the share intent is triggered without NEW_TASK or NEW_DOCUMENT, the task ID
|
| - // of this activity will match that of the triggering activity.
|
| - if (activity.getTaskId() == triggeringTaskId
|
| - && activity instanceof ChromeActivity) {
|
| - triggeringActivity = (ChromeActivity) activity;
|
| - break;
|
| - }
|
| - }
|
| - if (triggeringActivity == null) return;
|
| - unregisterActivity(triggeringActivity);
|
| - triggeringActivity.onMenuOrKeyboardAction(R.id.print_id, true);
|
| - }
|
| -
|
| -}
|
| +}
|
|
|