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; | |
9 import android.content.Context; | 8 import android.content.Context; |
10 import android.content.Intent; | 9 import android.content.Intent; |
11 import android.content.res.Resources; | 10 import android.content.res.Resources; |
12 import android.graphics.Bitmap; | 11 import android.graphics.Bitmap; |
13 import android.graphics.drawable.BitmapDrawable; | 12 import android.graphics.drawable.BitmapDrawable; |
14 import android.graphics.drawable.Drawable; | 13 import android.graphics.drawable.Drawable; |
15 import android.os.Bundle; | 14 import android.os.Bundle; |
16 import android.support.annotation.NonNull; | 15 import android.support.annotation.NonNull; |
17 import android.support.customtabs.CustomTabsIntent; | 16 import android.support.customtabs.CustomTabsIntent; |
18 import android.text.TextUtils; | 17 import android.text.TextUtils; |
18 import android.view.Gravity; | |
19 import android.view.LayoutInflater; | 19 import android.view.LayoutInflater; |
20 import android.view.View; | 20 import android.view.View; |
21 import android.view.View.OnClickListener; | |
22 import android.view.View.OnLongClickListener; | |
21 import android.view.ViewGroup; | 23 import android.view.ViewGroup; |
22 import android.widget.ImageButton; | 24 import android.widget.ImageButton; |
23 | 25 |
24 import org.chromium.base.Log; | 26 import org.chromium.base.Log; |
25 import org.chromium.chrome.R; | 27 import org.chromium.chrome.R; |
26 import org.chromium.chrome.browser.util.IntentUtils; | 28 import org.chromium.chrome.browser.util.IntentUtils; |
27 import org.chromium.chrome.browser.widget.TintedDrawable; | 29 import org.chromium.chrome.browser.widget.TintedDrawable; |
30 import org.chromium.ui.widget.Toast; | |
28 | 31 |
29 import java.util.ArrayList; | 32 import java.util.ArrayList; |
30 import java.util.HashSet; | 33 import java.util.HashSet; |
31 import java.util.List; | 34 import java.util.List; |
32 import java.util.Set; | 35 import java.util.Set; |
33 | 36 |
34 import javax.annotation.Nullable; | 37 import javax.annotation.Nullable; |
35 | 38 |
36 /** | 39 /** |
37 * Container for all parameters related to creating a customizable button. | 40 * Container for all parameters related to creating a customizable button. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 * @return The {@link PendingIntent} that will be sent when user clicks the c ustomized button. | 104 * @return The {@link PendingIntent} that will be sent when user clicks the c ustomized button. |
102 */ | 105 */ |
103 PendingIntent getPendingIntent() { | 106 PendingIntent getPendingIntent() { |
104 return mPendingIntent; | 107 return mPendingIntent; |
105 } | 108 } |
106 | 109 |
107 /** | 110 /** |
108 * Builds an {@link ImageButton} from the data in this params. Generated but tons should be | 111 * Builds an {@link ImageButton} from the data in this params. Generated but tons should be |
109 * placed on the bottom bar. The button's tag will be its id. | 112 * placed on the bottom bar. The button's tag will be its id. |
110 * @param parent The parent that the inflated {@link ImageButton}. | 113 * @param parent The parent that the inflated {@link ImageButton}. |
114 * @param listener {@link OnClickListener} that should be used with the butt on. | |
111 * @return Parsed list of {@link CustomButtonParams}, which is empty if the input is invalid. | 115 * @return Parsed list of {@link CustomButtonParams}, which is empty if the input is invalid. |
112 */ | 116 */ |
113 ImageButton buildBottomBarButton(Context context, ViewGroup parent) { | 117 ImageButton buildBottomBarButton(Context context, ViewGroup parent, OnClickL istener listener) { |
114 if (mIsOnToolbar) return null; | 118 if (mIsOnToolbar) return null; |
115 | 119 |
116 ImageButton button = (ImageButton) LayoutInflater.from(context) | 120 ImageButton button = (ImageButton) LayoutInflater.from(context) |
117 .inflate(R.layout.custom_tabs_bottombar_item, parent, false); | 121 .inflate(R.layout.custom_tabs_bottombar_item, parent, false); |
118 button.setId(mId); | 122 button.setId(mId); |
119 button.setImageBitmap(mIcon); | 123 button.setImageBitmap(mIcon); |
120 button.setContentDescription(mDescription); | 124 button.setContentDescription(mDescription); |
121 if (mPendingIntent == null) { | 125 if (mPendingIntent == null) { |
122 button.setEnabled(false); | 126 button.setEnabled(false); |
123 } else { | 127 } else { |
124 // TODO(ianwen): add UMA for button clicking. | 128 button.setOnClickListener(listener); |
125 button.setOnClickListener(new View.OnClickListener() { | |
126 @Override | |
127 public void onClick(View v) { | |
128 try { | |
129 mPendingIntent.send(); | |
130 } catch (CanceledException e) { | |
131 Log.e(TAG, "CanceledException while sending pending inte nt in custom tab"); | |
132 } | |
133 } | |
134 }); | |
135 } | 129 } |
130 button.setOnLongClickListener(new OnLongClickListener() { | |
131 @Override | |
132 public boolean onLongClick(View view) { | |
133 final int screenWidth = view.getResources().getDisplayMetrics(). widthPixels; | |
134 final int[] screenPos = new int[2]; | |
135 view.getLocationOnScreen(screenPos); | |
136 final int width = view.getWidth(); | |
137 | |
138 Toast toast = Toast.makeText( | |
139 view.getContext(), view.getContentDescription(), Toast.L ENGTH_SHORT); | |
140 toast.setGravity(Gravity.BOTTOM | Gravity.END, | |
141 screenWidth - screenPos[0] - width / 2, | |
142 view.getResources().getDimensionPixelSize( | |
Ted C
2016/01/15 23:28:05
FYI, this will be wrong in multiwindow
| |
143 R.dimen.toolbar_height_no_shadow)); | |
144 toast.show(); | |
145 return true; | |
146 } | |
147 }); | |
136 return button; | 148 return button; |
137 } | 149 } |
138 | 150 |
139 /** | 151 /** |
140 * Parses a list of {@link CustomButtonParams} from the intent sent by clien ts. | 152 * Parses a list of {@link CustomButtonParams} from the intent sent by clien ts. |
141 * @param intent The intent sent by the client. | 153 * @param intent The intent sent by the client. |
142 * @return A list of parsed {@link CustomButtonParams}. Return an empty list if input is invalid | 154 * @return A list of parsed {@link CustomButtonParams}. Return an empty list if input is invalid |
143 */ | 155 */ |
144 static List<CustomButtonParams> fromIntent(Context context, Intent intent) { | 156 static List<CustomButtonParams> fromIntent(Context context, Intent intent) { |
145 List<CustomButtonParams> paramsList = new ArrayList<>(1); | 157 List<CustomButtonParams> paramsList = new ArrayList<>(1); |
146 if (intent == null) return paramsList; | 158 if (intent == null) return paramsList; |
147 | 159 |
148 Bundle singleBundle = IntentUtils.safeGetBundleExtra(intent, | 160 Bundle singleBundle = IntentUtils.safeGetBundleExtra(intent, |
149 CustomTabsIntent.EXTRA_ACTION_BUTTON_BUNDLE); | 161 CustomTabsIntent.EXTRA_ACTION_BUTTON_BUNDLE); |
150 ArrayList<Bundle> bundleList = IntentUtils.getParcelableArrayListExtra(i ntent, | 162 ArrayList<Bundle> bundleList = IntentUtils.getParcelableArrayListExtra(i ntent, |
151 CustomTabsIntent.EXTRA_ACTION_BUTTON_BUNDLE); | 163 CustomTabsIntent.EXTRA_ACTION_BAR_ITEMS); |
152 boolean tinted = IntentUtils.safeGetBooleanExtra(intent, | 164 boolean tinted = IntentUtils.safeGetBooleanExtra(intent, |
153 CustomTabsIntent.EXTRA_TINT_ACTION_BUTTON, false); | 165 CustomTabsIntent.EXTRA_TINT_ACTION_BUTTON, false); |
154 if (singleBundle != null) { | 166 if (singleBundle != null) paramsList.add(fromBundle(context, singleBundl e, tinted, false)); |
155 CustomButtonParams params = fromBundle(context, singleBundle, tinted , false); | 167 if (bundleList != null) { |
156 paramsList.add(params); | |
157 } else if (bundleList != null) { | |
158 Set<Integer> ids = new HashSet<>(); | 168 Set<Integer> ids = new HashSet<>(); |
159 for (Bundle bundle : bundleList) { | 169 for (Bundle bundle : bundleList) { |
160 CustomButtonParams params = fromBundle(context, bundle, tinted, true); | 170 CustomButtonParams params = fromBundle(context, bundle, tinted, true); |
161 if (params == null) { | 171 if (params == null) { |
162 continue; | 172 continue; |
163 } else if (ids.contains(params.getId())) { | 173 } else if (ids.contains(params.getId())) { |
164 Log.e(TAG, "Bottom bar items contain duplicate id: " + param s.getId()); | 174 Log.e(TAG, "Bottom bar items contain duplicate id: " + param s.getId()); |
165 continue; | 175 continue; |
166 } | 176 } |
167 ids.add(params.getId()); | 177 ids.add(params.getId()); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
244 * @return Whether the given icon's size is suitable to put on toolbar. | 254 * @return Whether the given icon's size is suitable to put on toolbar. |
245 */ | 255 */ |
246 static boolean doesIconFitToolbar(Context context, Bitmap bitmap) { | 256 static boolean doesIconFitToolbar(Context context, Bitmap bitmap) { |
247 int height = context.getResources().getDimensionPixelSize(R.dimen.toolba r_icon_height); | 257 int height = context.getResources().getDimensionPixelSize(R.dimen.toolba r_icon_height); |
248 if (bitmap.getHeight() < height) return false; | 258 if (bitmap.getHeight() < height) return false; |
249 int scaledWidth = bitmap.getWidth() / bitmap.getHeight() * height; | 259 int scaledWidth = bitmap.getWidth() / bitmap.getHeight() * height; |
250 if (scaledWidth > 2 * height) return false; | 260 if (scaledWidth > 2 * height) return false; |
251 return true; | 261 return true; |
252 } | 262 } |
253 } | 263 } |
OLD | NEW |