OLD | NEW |
1 // Copyright 2015 Google Inc. All Rights Reserved. | 1 // Copyright 2015 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 import org.chromium.customtabsclient.shared.CustomTabsHelper; | 54 import org.chromium.customtabsclient.shared.CustomTabsHelper; |
55 import org.chromium.customtabsclient.shared.ServiceConnection; | 55 import org.chromium.customtabsclient.shared.ServiceConnection; |
56 import org.chromium.customtabsclient.shared.ServiceConnectionCallback; | 56 import org.chromium.customtabsclient.shared.ServiceConnectionCallback; |
57 | 57 |
58 import java.util.ArrayList; | 58 import java.util.ArrayList; |
59 import java.util.List; | 59 import java.util.List; |
60 | 60 |
61 /** | 61 /** |
62 * Example client activity for using Chrome Custom Tabs. | 62 * Example client activity for using Chrome Custom Tabs. |
63 */ | 63 */ |
64 public class MainActivity extends Activity | 64 public class MainActivity extends Activity implements OnClickListener, ServiceCo
nnectionCallback { |
65 implements OnClickListener, ServiceConnectionCallback, OnLongClickListen
er { | |
66 private static final String TAG = "CustomTabsClientExample"; | 65 private static final String TAG = "CustomTabsClientExample"; |
67 private static final String TOOLBAR_COLOR = "#ef6c00"; | 66 private static final String TOOLBAR_COLOR = "#ef6c00"; |
68 private static final String URL_STRING = "https://www.google.com"; | |
69 | 67 |
70 private EditText mEditText; | 68 private EditText mEditText; |
71 private CustomTabsSession mCustomTabsSession; | 69 private CustomTabsSession mCustomTabsSession; |
72 private CustomTabsClient mClient; | 70 private CustomTabsClient mClient; |
73 private CustomTabsServiceConnection mConnection; | 71 private CustomTabsServiceConnection mConnection; |
74 private String mPackageNameToBind; | 72 private String mPackageNameToBind; |
75 private Button mConnectButton; | 73 private Button mConnectButton; |
76 private Button mWarmupButton; | 74 private Button mWarmupButton; |
77 private Button mMayLaunchButton; | 75 private Button mMayLaunchButton; |
78 private Button mLaunchButton; | 76 private Button mLaunchButton; |
79 private MediaPlayer mMediaPlayer; | 77 private MediaPlayer mMediaPlayer; |
80 private TextView mUrlLinkText; | 78 private Button mLaunchBrowserActionsButton; |
81 | 79 |
82 /** | 80 /** |
83 * Once per second, asks the framework for the process importance, and logs
any change. | 81 * Once per second, asks the framework for the process importance, and logs
any change. |
84 */ | 82 */ |
85 private Runnable mLogImportance = new Runnable() { | 83 private Runnable mLogImportance = new Runnable() { |
86 private int mPreviousImportance = -1; | 84 private int mPreviousImportance = -1; |
87 private boolean mPreviousServiceInUse = false; | 85 private boolean mPreviousServiceInUse = false; |
88 private Handler mHandler = new Handler(Looper.getMainLooper()); | 86 private Handler mHandler = new Handler(Looper.getMainLooper()); |
89 | 87 |
90 @Override | 88 @Override |
(...skipping 25 matching lines...) Expand all Loading... |
116 @Override | 114 @Override |
117 protected void onCreate(Bundle savedInstanceState) { | 115 protected void onCreate(Bundle savedInstanceState) { |
118 super.onCreate(savedInstanceState); | 116 super.onCreate(savedInstanceState); |
119 setContentView(R.layout.main); | 117 setContentView(R.layout.main); |
120 mEditText = (EditText) findViewById(R.id.edit); | 118 mEditText = (EditText) findViewById(R.id.edit); |
121 mConnectButton = (Button) findViewById(R.id.connect_button); | 119 mConnectButton = (Button) findViewById(R.id.connect_button); |
122 mWarmupButton = (Button) findViewById(R.id.warmup_button); | 120 mWarmupButton = (Button) findViewById(R.id.warmup_button); |
123 mMayLaunchButton = (Button) findViewById(R.id.may_launch_button); | 121 mMayLaunchButton = (Button) findViewById(R.id.may_launch_button); |
124 mLaunchButton = (Button) findViewById(R.id.launch_button); | 122 mLaunchButton = (Button) findViewById(R.id.launch_button); |
125 Spinner spinner = (Spinner) findViewById(R.id.spinner); | 123 Spinner spinner = (Spinner) findViewById(R.id.spinner); |
126 mUrlLinkText = (TextView) findViewById(R.id.url_link); | 124 mLaunchBrowserActionsButton = (Button) findViewById(R.id.launch_browser_
actions_button); |
127 mEditText.requestFocus(); | 125 mEditText.requestFocus(); |
128 mConnectButton.setOnClickListener(this); | 126 mConnectButton.setOnClickListener(this); |
129 mWarmupButton.setOnClickListener(this); | 127 mWarmupButton.setOnClickListener(this); |
130 mMayLaunchButton.setOnClickListener(this); | 128 mMayLaunchButton.setOnClickListener(this); |
131 mLaunchButton.setOnClickListener(this); | 129 mLaunchButton.setOnClickListener(this); |
132 mMediaPlayer = MediaPlayer.create(this, R.raw.amazing_grace); | 130 mMediaPlayer = MediaPlayer.create(this, R.raw.amazing_grace); |
133 mUrlLinkText.setText(URL_STRING); | 131 mLaunchBrowserActionsButton.setOnClickListener(this); |
134 mUrlLinkText.setOnLongClickListener(this); | |
135 | 132 |
136 Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http:/
/www.example.com")); | 133 Intent activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http:/
/www.example.com")); |
137 PackageManager pm = getPackageManager(); | 134 PackageManager pm = getPackageManager(); |
138 List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities( | 135 List<ResolveInfo> resolvedActivityList = pm.queryIntentActivities( |
139 activityIntent, PackageManager.MATCH_ALL); | 136 activityIntent, PackageManager.MATCH_ALL); |
140 List<Pair<String, String>> packagesSupportingCustomTabs = new ArrayList<
>(); | 137 List<Pair<String, String>> packagesSupportingCustomTabs = new ArrayList<
>(); |
141 for (ResolveInfo info : resolvedActivityList) { | 138 for (ResolveInfo info : resolvedActivityList) { |
142 Intent serviceIntent = new Intent(); | 139 Intent serviceIntent = new Intent(); |
143 serviceIntent.setAction("android.support.customtabs.action.CustomTab
sService"); | 140 serviceIntent.setAction("android.support.customtabs.action.CustomTab
sService"); |
144 serviceIntent.setPackage(info.activityInfo.packageName); | 141 serviceIntent.setPackage(info.activityInfo.packageName); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 prepareMenuItems(builder); | 246 prepareMenuItems(builder); |
250 prepareActionButton(builder); | 247 prepareActionButton(builder); |
251 prepareBottombar(builder); | 248 prepareBottombar(builder); |
252 builder.setStartAnimations(this, R.anim.slide_in_right, R.anim.slide
_out_left); | 249 builder.setStartAnimations(this, R.anim.slide_in_right, R.anim.slide
_out_left); |
253 builder.setExitAnimations(this, R.anim.slide_in_left, R.anim.slide_o
ut_right); | 250 builder.setExitAnimations(this, R.anim.slide_in_left, R.anim.slide_o
ut_right); |
254 builder.setCloseButtonIcon( | 251 builder.setCloseButtonIcon( |
255 BitmapFactory.decodeResource(getResources(), R.drawable.ic_a
rrow_back)); | 252 BitmapFactory.decodeResource(getResources(), R.drawable.ic_a
rrow_back)); |
256 CustomTabsIntent customTabsIntent = builder.build(); | 253 CustomTabsIntent customTabsIntent = builder.build(); |
257 CustomTabsHelper.addKeepAliveExtra(this, customTabsIntent.intent); | 254 CustomTabsHelper.addKeepAliveExtra(this, customTabsIntent.intent); |
258 customTabsIntent.launchUrl(this, Uri.parse(url)); | 255 customTabsIntent.launchUrl(this, Uri.parse(url)); |
| 256 } else if (viewId == R.id.launch_browser_actions_button) { |
| 257 Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawabl
e.ic_launcher); |
| 258 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
| 259 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, int
ent, 0); |
| 260 BrowserActionItem item = |
| 261 new BrowserActionItem("Open the link (without icon)", pendin
gIntent); |
| 262 BrowserActionItem item2 = |
| 263 new BrowserActionItem("Open the link (with icon)", pendingIn
tent, icon); |
| 264 ArrayList<BrowserActionItem> items = new ArrayList<>(); |
| 265 items.add(item); |
| 266 items.add(item2); |
| 267 BrowserActionsIntent.openBrowserAction(this, Uri.parse(url), |
| 268 BrowserActionsIntent.URL_TYPE_IMAGE, items); |
259 } | 269 } |
260 } | 270 } |
261 | 271 |
262 private void prepareMenuItems(CustomTabsIntent.Builder builder) { | 272 private void prepareMenuItems(CustomTabsIntent.Builder builder) { |
263 Intent menuIntent = new Intent(); | 273 Intent menuIntent = new Intent(); |
264 menuIntent.setClass(getApplicationContext(), this.getClass()); | 274 menuIntent.setClass(getApplicationContext(), this.getClass()); |
265 // Optional animation configuration when the user clicks menu items. | 275 // Optional animation configuration when the user clicks menu items. |
266 Bundle menuBundle = ActivityOptions.makeCustomAnimation(this, android.R.
anim.slide_in_left, | 276 Bundle menuBundle = ActivityOptions.makeCustomAnimation(this, android.R.
anim.slide_in_left, |
267 android.R.anim.slide_out_right).toBundle(); | 277 android.R.anim.slide_out_right).toBundle(); |
268 PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
menuIntent, 0, | 278 PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
menuIntent, 0, |
(...skipping 28 matching lines...) Expand all Loading... |
297 } | 307 } |
298 | 308 |
299 @Override | 309 @Override |
300 public void onServiceDisconnected() { | 310 public void onServiceDisconnected() { |
301 mConnectButton.setEnabled(true); | 311 mConnectButton.setEnabled(true); |
302 mWarmupButton.setEnabled(false); | 312 mWarmupButton.setEnabled(false); |
303 mMayLaunchButton.setEnabled(false); | 313 mMayLaunchButton.setEnabled(false); |
304 mLaunchButton.setEnabled(false); | 314 mLaunchButton.setEnabled(false); |
305 mClient = null; | 315 mClient = null; |
306 } | 316 } |
307 | |
308 @Override | |
309 public boolean onLongClick(View v) { | |
310 Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic
_launcher); | |
311 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(URL_STRING)); | |
312 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
0); | |
313 BrowserActionItem item = | |
314 new BrowserActionItem("Open the link (without icon)", pendingInt
ent); | |
315 BrowserActionItem item2 = | |
316 new BrowserActionItem("Open the link (with icon)", pendingIntent
, icon); | |
317 ArrayList<BrowserActionItem> items = new ArrayList<>(); | |
318 items.add(item); | |
319 items.add(item2); | |
320 BrowserActionsIntent.openBrowserAction(this, Uri.parse(mUrlLinkText.getT
ext().toString()), | |
321 BrowserActionsIntent.URL_TYPE_IMAGE, items); | |
322 return true; | |
323 } | |
324 } | 317 } |
OLD | NEW |