OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.physicalweb; | 5 package org.chromium.chrome.browser.physicalweb; |
6 | 6 |
7 import android.app.Activity; | |
7 import android.content.Intent; | 8 import android.content.Intent; |
8 import android.os.Build; | 9 import android.os.Build; |
9 import android.os.Bundle; | 10 import android.os.Bundle; |
10 import android.support.v7.app.AppCompatActivity; | 11 import android.support.v7.app.AppCompatActivity; |
11 | 12 |
13 import org.chromium.base.ApplicationStatus; | |
14 import org.chromium.chrome.browser.ChromeActivity; | |
12 import org.chromium.chrome.browser.share.ShareHelper; | 15 import org.chromium.chrome.browser.share.ShareHelper; |
13 import org.chromium.chrome.browser.tab.Tab; | 16 import org.chromium.chrome.browser.tab.Tab; |
14 import org.chromium.chrome.browser.util.IntentUtils; | 17 import org.chromium.chrome.browser.util.IntentUtils; |
15 | 18 |
19 import java.lang.ref.WeakReference; | |
20 import java.util.List; | |
16 /** | 21 /** |
17 * A simple activity that allows Chrome to start the physical web sharing servic e. | 22 * A simple activity that allows Chrome to start the physical web sharing servic e. |
18 */ | 23 */ |
19 public class PhysicalWebShareActivity extends AppCompatActivity { | 24 public class PhysicalWebShareActivity extends AppCompatActivity { |
20 private static final String TAG = "PhysicalWebShareActivity"; | 25 private static final String TAG = "PhysicalWebShareActivity"; |
21 | 26 |
22 @Override | 27 @Override |
23 protected void onCreate(Bundle savedInstanceState) { | 28 protected void onCreate(Bundle savedInstanceState) { |
24 super.onCreate(savedInstanceState); | 29 super.onCreate(savedInstanceState); |
25 | 30 |
26 try { | 31 try { |
27 Intent intent = getIntent(); | 32 Intent intent = getIntent(); |
28 if (intent == null) return; | 33 if (intent == null) return; |
29 if (!Intent.ACTION_SEND.equals(intent.getAction())) return; | 34 if (!Intent.ACTION_SEND.equals(intent.getAction())) return; |
30 if (!IntentUtils.safeHasExtra(getIntent(), ShareHelper.EXTRA_TASK_ID )) return; | 35 if (!IntentUtils.safeHasExtra(getIntent(), ShareHelper.EXTRA_TASK_ID )) return; |
31 handleShareAction(); | 36 handleShareAction(); |
32 } finally { | 37 } finally { |
33 finish(); | 38 finish(); |
34 } | 39 } |
35 } | 40 } |
36 | 41 |
37 private void handleShareAction() { | 42 private void handleShareAction() { |
38 // TODO(iankc): implement sharing | 43 ChromeActivity openPage = getTriggeringActivity(); |
44 if (openPage == null) return; | |
45 String url = openPage.getActivityTab().getUrl(); | |
46 | |
47 Intent intent = new Intent(this, PhysicalWebBroadcastService.class); | |
48 intent.putExtra(PhysicalWebBroadcastService.DISPLAY_URL_KEY, url); | |
49 startService(intent); | |
39 } | 50 } |
40 | 51 |
52 /** | |
53 * Pre-conditions for Physical Web Sharing to be enabled: | |
54 * Device has hardware BLE advertising capabilities. | |
55 * Device is Marshmallow or above. | |
56 * Device has sharing feature enabled. | |
57 */ | |
41 public static boolean sharingIsEnabled(Tab currentTab) { | 58 public static boolean sharingIsEnabled(Tab currentTab) { |
42 return PhysicalWeb.sharingIsEnabled() && Build.VERSION.SDK_INT >= Build. VERSION_CODES.M; | 59 return PhysicalWeb.sharingIsEnabled() && Build.VERSION.SDK_INT >= Build. VERSION_CODES.M; |
43 } | 60 } |
61 | |
62 /** | |
63 * Returns the ChromeActivity that called the share intent picker. | |
64 */ | |
65 protected ChromeActivity getTriggeringActivity() { | |
cco3
2017/03/02 19:01:08
Since we don't have an abstract class, could we tu
| |
66 int triggeringTaskId = | |
67 IntentUtils.safeGetIntExtra(getIntent(), ShareHelper.EXTRA_TASK_ ID, 0); | |
68 List<WeakReference<Activity>> activities = ApplicationStatus.getRunningA ctivities(); | |
69 ChromeActivity triggeringActivity = null; | |
70 for (int i = 0; i < activities.size(); i++) { | |
71 Activity activity = activities.get(i).get(); | |
72 if (activity == null) continue; | |
73 | |
74 // Since the share intent is triggered without NEW_TASK or NEW_DOCUM ENT, the task ID | |
75 // of this activity will match that of the triggering activity. | |
76 if (activity.getTaskId() == triggeringTaskId && activity instanceof ChromeActivity) { | |
77 return (ChromeActivity) activity; | |
78 } | |
79 } | |
80 return null; | |
81 } | |
44 } | 82 } |
OLD | NEW |