OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser.printing; | |
6 | |
7 import android.app.Activity; | |
8 import android.content.ComponentName; | |
9 import android.content.Context; | |
10 import android.content.Intent; | |
11 import android.content.pm.PackageManager; | |
12 import android.os.Bundle; | |
13 import android.support.v7.app.AppCompatActivity; | |
14 | |
15 import org.chromium.base.ActivityState; | |
16 import org.chromium.base.ApplicationStatus; | |
17 import org.chromium.base.ApplicationStatus.ActivityStateListener; | |
18 import org.chromium.base.ContextUtils; | |
19 import org.chromium.base.ThreadUtils; | |
20 import org.chromium.chrome.R; | |
21 import org.chromium.chrome.browser.ChromeActivity; | |
22 | |
23 import java.lang.ref.WeakReference; | |
24 import java.util.HashSet; | |
25 import java.util.List; | |
26 import java.util.Set; | |
27 | |
28 /** | |
29 * A simple activity that allows Chrome to expose print as an option in the shar e menu. | |
30 */ | |
31 public class PrintShareActivity extends AppCompatActivity { | |
32 | |
33 private static Set<Activity> sPendingShareActivities = new HashSet<>(); | |
34 private static ActivityStateListener sStateListener; | |
35 | |
36 /** | |
37 * Enable the print sharing option. | |
38 * | |
39 * @param activity The activity that will be triggering the share action. T he activitiy's | |
40 * state will be tracked to disable the print option when th e share operation | |
41 * has been completed. | |
42 */ | |
43 public static void enablePrintShareOption(Activity activity) { | |
44 ThreadUtils.assertOnUiThread(); | |
45 | |
46 if (sStateListener == null) { | |
47 sStateListener = new ActivityStateListener() { | |
48 @Override | |
49 public void onActivityStateChange(Activity activity, int newStat e) { | |
50 if (newState == ActivityState.PAUSED | |
51 || newState == ActivityState.RESUMED) { | |
Theresa
2016/07/26 18:23:12
This should only be ActivityState.PAUSED since the
| |
52 return; | |
53 } | |
54 unregisterActivity(activity); | |
55 } | |
56 }; | |
57 } | |
58 ApplicationStatus.registerStateListenerForAllActivities(sStateListener); | |
59 boolean wasEmpty = sPendingShareActivities.isEmpty(); | |
60 sPendingShareActivities.add(activity); | |
61 if (wasEmpty) { | |
62 activity.getPackageManager().setComponentEnabledSetting( | |
63 new ComponentName(activity, PrintShareActivity.class), | |
64 PackageManager.COMPONENT_ENABLED_STATE_ENABLED, | |
65 PackageManager.DONT_KILL_APP); | |
66 } | |
67 } | |
68 | |
69 private static void unregisterActivity(Activity activity) { | |
70 sPendingShareActivities.remove(activity); | |
71 if (!sPendingShareActivities.isEmpty()) return; | |
72 ApplicationStatus.unregisterActivityStateListener(sStateListener); | |
73 | |
74 Context context = ContextUtils.getApplicationContext(); | |
75 context.getPackageManager().setComponentEnabledSetting( | |
76 new ComponentName(context, PrintShareActivity.class), | |
77 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, | |
78 PackageManager.DONT_KILL_APP); | |
79 } | |
80 | |
81 @Override | |
82 protected void onCreate(Bundle savedInstanceState) { | |
83 super.onCreate(savedInstanceState); | |
84 | |
85 try { | |
86 Intent intent = getIntent(); | |
87 if (intent == null) return; | |
88 if (!Intent.ACTION_SEND.equals(intent.getAction())) return; | |
89 handlePrintAction(); | |
90 } finally { | |
91 finish(); | |
92 } | |
93 } | |
94 | |
95 private void handlePrintAction() { | |
96 int currentTaskId = getTaskId(); | |
97 List<WeakReference<Activity>> activities = ApplicationStatus.getRunningA ctivities(); | |
98 ChromeActivity triggeringActivity = null; | |
99 for (int i = 0; i < activities.size(); i++) { | |
100 Activity activity = activities.get(i).get(); | |
101 if (activity == null) continue; | |
102 | |
103 // Since the share intent is triggered without NEW_TASK or NEW_DOCUM ENT, the task ID | |
104 // of this activity will match that of the triggering activity. | |
105 if (activity.getTaskId() == currentTaskId | |
106 && activity instanceof ChromeActivity) { | |
107 triggeringActivity = (ChromeActivity) activity; | |
108 break; | |
109 } | |
110 } | |
111 if (triggeringActivity == null) return; | |
112 unregisterActivity(triggeringActivity); | |
113 triggeringActivity.onMenuOrKeyboardAction(R.id.print_id, true); | |
114 } | |
115 | |
116 } | |
OLD | NEW |