OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.customtabs; | 5 package org.chromium.chrome.browser.customtabs; |
6 | 6 |
7 import android.app.PendingIntent; | 7 import android.app.PendingIntent; |
8 import android.app.PendingIntent.CanceledException; | 8 import android.app.PendingIntent.CanceledException; |
9 import android.content.Intent; | 9 import android.content.Intent; |
10 import android.graphics.Bitmap; | 10 import android.graphics.Bitmap; |
11 import android.net.Uri; | 11 import android.net.Uri; |
12 import android.os.IBinder; | 12 import android.os.IBinder; |
13 import android.os.StrictMode; | 13 import android.os.StrictMode; |
14 import android.support.customtabs.CustomTabsCallback; | 14 import android.support.customtabs.CustomTabsCallback; |
15 import android.support.customtabs.CustomTabsIntent; | 15 import android.support.customtabs.CustomTabsIntent; |
16 import android.text.TextUtils; | 16 import android.text.TextUtils; |
17 import android.view.KeyEvent; | 17 import android.view.KeyEvent; |
18 import android.view.MenuItem; | 18 import android.view.MenuItem; |
19 import android.view.View; | 19 import android.view.View; |
20 import android.view.View.OnClickListener; | 20 import android.view.View.OnClickListener; |
21 import android.view.ViewGroup; | 21 import android.view.ViewGroup; |
22 import android.view.ViewStub; | 22 import android.view.ViewStub; |
23 import android.view.Window; | 23 import android.view.Window; |
24 import android.widget.ImageButton; | 24 import android.widget.ImageButton; |
| 25 import android.widget.RemoteViews; |
25 | 26 |
26 import org.chromium.base.ApiCompatibilityUtils; | 27 import org.chromium.base.ApiCompatibilityUtils; |
27 import org.chromium.base.Log; | 28 import org.chromium.base.Log; |
28 import org.chromium.base.ThreadUtils; | 29 import org.chromium.base.ThreadUtils; |
29 import org.chromium.base.VisibleForTesting; | 30 import org.chromium.base.VisibleForTesting; |
30 import org.chromium.base.metrics.RecordHistogram; | 31 import org.chromium.base.metrics.RecordHistogram; |
31 import org.chromium.base.metrics.RecordUserAction; | 32 import org.chromium.base.metrics.RecordUserAction; |
32 import org.chromium.chrome.R; | 33 import org.chromium.chrome.R; |
33 import org.chromium.chrome.browser.ChromeActivity; | 34 import org.chromium.chrome.browser.ChromeActivity; |
34 import org.chromium.chrome.browser.IntentHandler; | 35 import org.chromium.chrome.browser.IntentHandler; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 */ | 147 */ |
147 static boolean updateCustomButton(IBinder session, int id, Bitmap bitmap, St
ring description) { | 148 static boolean updateCustomButton(IBinder session, int id, Bitmap bitmap, St
ring description) { |
148 ThreadUtils.assertOnUiThread(); | 149 ThreadUtils.assertOnUiThread(); |
149 // Do nothing if there is no activity or the activity does not belong to
this session. | 150 // Do nothing if there is no activity or the activity does not belong to
this session. |
150 if (sActiveContentHandler == null || !sActiveContentHandler.getSession()
.equals(session)) { | 151 if (sActiveContentHandler == null || !sActiveContentHandler.getSession()
.equals(session)) { |
151 return false; | 152 return false; |
152 } | 153 } |
153 return sActiveContentHandler.updateCustomButton(id, bitmap, description)
; | 154 return sActiveContentHandler.updateCustomButton(id, bitmap, description)
; |
154 } | 155 } |
155 | 156 |
| 157 /** |
| 158 * Checks whether the active {@link CustomTabContentHandler} belongs to the
given session, and |
| 159 * if true, updates {@link RemoteViews} on the secondary toolbar. |
| 160 * @return Whether the update is successful. |
| 161 */ |
| 162 static boolean updateRemoteViews(IBinder session, RemoteViews remoteViews) { |
| 163 ThreadUtils.assertOnUiThread(); |
| 164 // Do nothing if there is no activity or the activity does not belong to
this session. |
| 165 if (sActiveContentHandler == null || !sActiveContentHandler.getSession()
.equals(session)) { |
| 166 return false; |
| 167 } |
| 168 return sActiveContentHandler.updateRemoteViews(remoteViews); |
| 169 } |
| 170 |
156 @Override | 171 @Override |
157 public boolean isCustomTab() { | 172 public boolean isCustomTab() { |
158 return true; | 173 return true; |
159 } | 174 } |
160 | 175 |
161 @Override | 176 @Override |
162 public void onStart() { | 177 public void onStart() { |
163 super.onStart(); | 178 super.onStart(); |
164 CustomTabsConnection.getInstance(getApplication()) | 179 CustomTabsConnection.getInstance(getApplication()) |
165 .keepAliveForSession(mIntentDataProvider.getSession(), | 180 .keepAliveForSession(mIntentDataProvider.getSession(), |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 if (params.showOnToolbar()) { | 281 if (params.showOnToolbar()) { |
267 if (!CustomButtonParams.doesIconFitToolbar(CustomTabActivity
.this, bitmap)) { | 282 if (!CustomButtonParams.doesIconFitToolbar(CustomTabActivity
.this, bitmap)) { |
268 return false; | 283 return false; |
269 } | 284 } |
270 showCustomButtonOnToolbar(); | 285 showCustomButtonOnToolbar(); |
271 } else { | 286 } else { |
272 updateBottomBarButton(params); | 287 updateBottomBarButton(params); |
273 } | 288 } |
274 return true; | 289 return true; |
275 } | 290 } |
| 291 |
| 292 @Override |
| 293 public boolean updateRemoteViews(RemoteViews rv) { |
| 294 if (mIntentDataProvider.getBottomBarRemoteViews() == null) { |
| 295 // Update only makes sense if we are already showing a Remot
eViews. |
| 296 return false; |
| 297 } |
| 298 ViewGroup bottomBar = (ViewGroup) findViewById(R.id.bottombar); |
| 299 View view = rv.apply(CustomTabActivity.this, bottomBar); |
| 300 bottomBar.removeAllViews(); |
| 301 bottomBar.addView(view); |
| 302 return true; |
| 303 } |
276 }; | 304 }; |
277 DataUseTabUIManager.onCustomTabInitialNavigation(mainTab, | 305 DataUseTabUIManager.onCustomTabInitialNavigation(mainTab, |
278 CustomTabsConnection.getInstance(getApplication()) | 306 CustomTabsConnection.getInstance(getApplication()) |
279 .getClientPackageNameForSession(mSession), | 307 .getClientPackageNameForSession(mSession), |
280 IntentHandler.getUrlFromIntent(getIntent())); | 308 IntentHandler.getUrlFromIntent(getIntent())); |
281 mainTab.setAppAssociatedWith(CustomTabsConnection.getInstance(getApplica
tion()) | 309 mainTab.setAppAssociatedWith(CustomTabsConnection.getInstance(getApplica
tion()) |
282 .getClientPackageNameForSession(mSession)); | 310 .getClientPackageNameForSession(mSession)); |
283 recordClientPackageName(); | 311 recordClientPackageName(); |
284 loadUrlInCurrentTab(new LoadUrlParams(IntentHandler.getUrlFromIntent(get
Intent())), | 312 loadUrlInCurrentTab(new LoadUrlParams(IntentHandler.getUrlFromIntent(get
Intent())), |
285 IntentHandler.getTimestampFromIntent(getIntent())); | 313 IntentHandler.getTimestampFromIntent(getIntent())); |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
499 if (!mIntentDataProvider.shouldShowBottomBar()) return; | 527 if (!mIntentDataProvider.shouldShowBottomBar()) return; |
500 | 528 |
501 ViewStub bottomBarStub = ((ViewStub) findViewById(R.id.bottombar_stub)); | 529 ViewStub bottomBarStub = ((ViewStub) findViewById(R.id.bottombar_stub)); |
502 bottomBarStub.setLayoutResource(R.layout.custom_tabs_bottombar); | 530 bottomBarStub.setLayoutResource(R.layout.custom_tabs_bottombar); |
503 bottomBarStub.inflate(); | 531 bottomBarStub.inflate(); |
504 | 532 |
505 View shadow = findViewById(R.id.bottombar_shadow); | 533 View shadow = findViewById(R.id.bottombar_shadow); |
506 shadow.setVisibility(View.VISIBLE); | 534 shadow.setVisibility(View.VISIBLE); |
507 | 535 |
508 ViewGroup bottomBar = (ViewGroup) findViewById(R.id.bottombar); | 536 ViewGroup bottomBar = (ViewGroup) findViewById(R.id.bottombar); |
509 bottomBar.setBackgroundColor(mIntentDataProvider.getBottomBarColor()); | 537 RemoteViews remoteViews = mIntentDataProvider.getBottomBarRemoteViews(); |
510 List<CustomButtonParams> items = mIntentDataProvider.getCustomButtonsOnB
ottombar(); | 538 if (remoteViews != null) { |
511 for (CustomButtonParams params : items) { | 539 //TODO(ianwen): add UMA to see the usage of RemoteViews. |
512 if (params.showOnToolbar()) continue; | 540 View inflatedView = remoteViews.apply(this, bottomBar); |
513 final PendingIntent pendingIntent = params.getPendingIntent(); | 541 bottomBar.addView(inflatedView); |
514 OnClickListener clickListener = null; | 542 } else { |
515 if (pendingIntent != null) { | 543 bottomBar.setBackgroundColor(mIntentDataProvider.getBottomBarColor()
); |
516 clickListener = new OnClickListener() { | 544 List<CustomButtonParams> items = mIntentDataProvider.getCustomButton
sOnBottombar(); |
517 @Override | 545 for (CustomButtonParams params : items) { |
518 public void onClick(View v) { | 546 if (params.showOnToolbar()) continue; |
519 Intent addedIntent = new Intent(); | 547 final PendingIntent pendingIntent = params.getPendingIntent(); |
520 addedIntent.setData(Uri.parse(getActivityTab().getUrl())
); | 548 OnClickListener clickListener = null; |
521 try { | 549 if (pendingIntent != null) { |
522 pendingIntent.send(CustomTabActivity.this, 0, addedI
ntent, null, null); | 550 clickListener = new OnClickListener() { |
523 } catch (CanceledException e) { | 551 @Override |
524 Log.e(TAG, | 552 public void onClick(View v) { |
525 "CanceledException while sending pending int
ent in custom tab"); | 553 Intent addedIntent = new Intent(); |
| 554 addedIntent.setData(Uri.parse(getActivityTab().getUr
l())); |
| 555 try { |
| 556 pendingIntent.send(CustomTabActivity.this, 0, ad
dedIntent, null, |
| 557 null); |
| 558 } catch (CanceledException e) { |
| 559 Log.e(TAG, |
| 560 "CanceledException while sending pending
intent."); |
| 561 } |
526 } | 562 } |
527 } | 563 }; |
528 }; | 564 } |
| 565 ImageButton button = params.buildBottomBarButton(this, bottomBar
, clickListener); |
| 566 bottomBar.addView(button); |
529 } | 567 } |
530 ImageButton button = params.buildBottomBarButton(this, bottomBar, cl
ickListener); | |
531 bottomBar.addView(button); | |
532 } | 568 } |
533 } | 569 } |
534 | 570 |
535 @Override | 571 @Override |
536 public boolean shouldShowAppMenu() { | 572 public boolean shouldShowAppMenu() { |
537 return getActivityTab() != null && getToolbarManager().isInitialized(); | 573 return getActivityTab() != null && getToolbarManager().isInitialized(); |
538 } | 574 } |
539 | 575 |
540 @Override | 576 @Override |
541 protected void showAppMenuForKeyboardEvent() { | 577 protected void showAppMenuForKeyboardEvent() { |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
665 case RESULT_CLOSED: | 701 case RESULT_CLOSED: |
666 break; | 702 break; |
667 | 703 |
668 default: | 704 default: |
669 assert false; | 705 assert false; |
670 } | 706 } |
671 | 707 |
672 setResult(result, resultIntent); | 708 setResult(result, resultIntent); |
673 } | 709 } |
674 } | 710 } |
OLD | NEW |