blob: 3ecafc3bc9b90ab5e826755c0092d5e0b4a29587 [file] [log] [blame]
Adam Powell690ffb42012-06-04 19:22:45 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
19import com.android.internal.R;
Adam Powell70e11e52012-06-12 16:59:45 -070020import com.android.internal.app.MediaRouteChooserDialogFragment;
Adam Powell690ffb42012-06-04 19:22:45 -070021
22import android.content.Context;
Adam Powell70e11e52012-06-12 16:59:45 -070023import android.content.ContextWrapper;
Adam Powell690ffb42012-06-04 19:22:45 -070024import android.content.res.TypedArray;
25import android.graphics.Canvas;
Adam Powelle4681872012-08-02 10:31:30 -070026import android.graphics.Rect;
Adam Powell690ffb42012-06-04 19:22:45 -070027import android.graphics.drawable.Drawable;
28import android.media.MediaRouter;
Adam Powellf7e0a322012-06-21 15:29:40 -070029import android.media.MediaRouter.RouteGroup;
Adam Powell690ffb42012-06-04 19:22:45 -070030import android.media.MediaRouter.RouteInfo;
Adam Powelle4681872012-08-02 10:31:30 -070031import android.text.TextUtils;
Adam Powell690ffb42012-06-04 19:22:45 -070032import android.util.AttributeSet;
33import android.util.Log;
Adam Powelle4681872012-08-02 10:31:30 -070034import android.view.Gravity;
35import android.view.HapticFeedbackConstants;
Adam Powell690ffb42012-06-04 19:22:45 -070036import android.view.SoundEffectConstants;
37import android.view.View;
Adam Powelle4681872012-08-02 10:31:30 -070038import android.widget.Toast;
Adam Powell690ffb42012-06-04 19:22:45 -070039
40public class MediaRouteButton extends View {
41 private static final String TAG = "MediaRouteButton";
42
43 private MediaRouter mRouter;
44 private final MediaRouteCallback mRouterCallback = new MediaRouteCallback();
45 private int mRouteTypes;
46
Jack Palevich101c4492012-06-22 16:06:57 +080047 private boolean mAttachedToWindow;
48
Adam Powell690ffb42012-06-04 19:22:45 -070049 private Drawable mRemoteIndicator;
50 private boolean mRemoteActive;
51 private boolean mToggleMode;
Adam Powelle4681872012-08-02 10:31:30 -070052 private boolean mCheatSheetEnabled;
Adam Powell2ee6a2a2012-10-01 14:02:13 -070053 private boolean mIsConnecting;
Adam Powell690ffb42012-06-04 19:22:45 -070054
55 private int mMinWidth;
56 private int mMinHeight;
57
Adam Powellb35c4452012-06-12 11:25:54 -070058 private OnClickListener mExtendedSettingsClickListener;
Adam Powell70e11e52012-06-12 16:59:45 -070059 private MediaRouteChooserDialogFragment mDialogFragment;
Adam Powellb35c4452012-06-12 11:25:54 -070060
Adam Powell2ee6a2a2012-10-01 14:02:13 -070061 private static final int[] CHECKED_STATE_SET = {
62 R.attr.state_checked
63 };
64
Adam Powell690ffb42012-06-04 19:22:45 -070065 private static final int[] ACTIVATED_STATE_SET = {
66 R.attr.state_activated
67 };
68
69 public MediaRouteButton(Context context) {
70 this(context, null);
71 }
72
73 public MediaRouteButton(Context context, AttributeSet attrs) {
Adam Powell74d762a2012-06-27 16:19:45 -070074 this(context, attrs, com.android.internal.R.attr.mediaRouteButtonStyle);
Adam Powell690ffb42012-06-04 19:22:45 -070075 }
76
77 public MediaRouteButton(Context context, AttributeSet attrs, int defStyleAttr) {
78 super(context, attrs, defStyleAttr);
79
Dianne Hackbornb58b8f82012-06-11 15:08:39 -070080 mRouter = (MediaRouter)context.getSystemService(Context.MEDIA_ROUTER_SERVICE);
Adam Powell690ffb42012-06-04 19:22:45 -070081
82 TypedArray a = context.obtainStyledAttributes(attrs,
83 com.android.internal.R.styleable.MediaRouteButton, defStyleAttr, 0);
84 setRemoteIndicatorDrawable(a.getDrawable(
85 com.android.internal.R.styleable.MediaRouteButton_externalRouteEnabledDrawable));
86 mMinWidth = a.getDimensionPixelSize(
87 com.android.internal.R.styleable.MediaRouteButton_minWidth, 0);
88 mMinHeight = a.getDimensionPixelSize(
89 com.android.internal.R.styleable.MediaRouteButton_minHeight, 0);
Adam Powell849df0b2012-06-19 17:47:13 -070090 final int routeTypes = a.getInteger(
91 com.android.internal.R.styleable.MediaRouteButton_mediaRouteTypes,
92 MediaRouter.ROUTE_TYPE_LIVE_AUDIO);
Adam Powell690ffb42012-06-04 19:22:45 -070093 a.recycle();
94
95 setClickable(true);
Adam Powelle4681872012-08-02 10:31:30 -070096 setLongClickable(true);
Adam Powell849df0b2012-06-19 17:47:13 -070097
98 setRouteTypes(routeTypes);
Adam Powell690ffb42012-06-04 19:22:45 -070099 }
100
101 private void setRemoteIndicatorDrawable(Drawable d) {
102 if (mRemoteIndicator != null) {
103 mRemoteIndicator.setCallback(null);
104 unscheduleDrawable(mRemoteIndicator);
105 }
106 mRemoteIndicator = d;
107 if (d != null) {
108 d.setCallback(this);
109 d.setState(getDrawableState());
110 d.setVisible(getVisibility() == VISIBLE, false);
111 }
112
113 refreshDrawableState();
114 }
115
116 @Override
117 public boolean performClick() {
118 // Send the appropriate accessibility events and call listeners
119 boolean handled = super.performClick();
120 if (!handled) {
121 playSoundEffect(SoundEffectConstants.CLICK);
122 }
123
124 if (mToggleMode) {
125 if (mRemoteActive) {
Adam Powell0d03c042012-06-14 16:04:12 -0700126 mRouter.selectRouteInt(mRouteTypes, mRouter.getSystemAudioRoute());
Adam Powell690ffb42012-06-04 19:22:45 -0700127 } else {
128 final int N = mRouter.getRouteCount();
129 for (int i = 0; i < N; i++) {
130 final RouteInfo route = mRouter.getRouteAt(i);
131 if ((route.getSupportedTypes() & mRouteTypes) != 0 &&
132 route != mRouter.getSystemAudioRoute()) {
Adam Powelleba1b702012-06-19 15:58:38 -0700133 mRouter.selectRouteInt(mRouteTypes, route);
Adam Powell690ffb42012-06-04 19:22:45 -0700134 }
135 }
136 }
137 } else {
Adam Powell70e11e52012-06-12 16:59:45 -0700138 showDialog();
Adam Powell690ffb42012-06-04 19:22:45 -0700139 }
140
141 return handled;
142 }
143
Adam Powelle4681872012-08-02 10:31:30 -0700144 void setCheatSheetEnabled(boolean enable) {
145 mCheatSheetEnabled = enable;
146 }
147
148 @Override
149 public boolean performLongClick() {
150 if (super.performLongClick()) {
151 return true;
152 }
153
154 if (!mCheatSheetEnabled) {
155 return false;
156 }
157
158 final CharSequence contentDesc = getContentDescription();
159 if (TextUtils.isEmpty(contentDesc)) {
160 // Don't show the cheat sheet if we have no description
161 return false;
162 }
163
164 final int[] screenPos = new int[2];
165 final Rect displayFrame = new Rect();
166 getLocationOnScreen(screenPos);
167 getWindowVisibleDisplayFrame(displayFrame);
168
169 final Context context = getContext();
170 final int width = getWidth();
171 final int height = getHeight();
172 final int midy = screenPos[1] + height / 2;
173 final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
174
175 Toast cheatSheet = Toast.makeText(context, contentDesc, Toast.LENGTH_SHORT);
176 if (midy < displayFrame.height()) {
177 // Show along the top; follow action buttons
178 cheatSheet.setGravity(Gravity.TOP | Gravity.END,
179 screenWidth - screenPos[0] - width / 2, height);
180 } else {
181 // Show along the bottom center
182 cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);
183 }
184 cheatSheet.show();
185 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
186
187 return true;
188 }
189
Adam Powell690ffb42012-06-04 19:22:45 -0700190 public void setRouteTypes(int types) {
191 if (types == mRouteTypes) {
192 // Already registered; nothing to do.
193 return;
194 }
Jack Palevich101c4492012-06-22 16:06:57 +0800195
196 if (mAttachedToWindow && mRouteTypes != 0) {
Adam Powell690ffb42012-06-04 19:22:45 -0700197 mRouter.removeCallback(mRouterCallback);
198 }
Jack Palevich101c4492012-06-22 16:06:57 +0800199
Adam Powell690ffb42012-06-04 19:22:45 -0700200 mRouteTypes = types;
Jack Palevich101c4492012-06-22 16:06:57 +0800201
202 if (mAttachedToWindow) {
203 updateRouteInfo();
204 mRouter.addCallback(types, mRouterCallback);
205 }
206 }
207
208 private void updateRouteInfo() {
Adam Powell690ffb42012-06-04 19:22:45 -0700209 updateRemoteIndicator();
210 updateRouteCount();
Adam Powell690ffb42012-06-04 19:22:45 -0700211 }
212
213 public int getRouteTypes() {
214 return mRouteTypes;
215 }
216
217 void updateRemoteIndicator() {
Adam Powell2ee6a2a2012-10-01 14:02:13 -0700218 final RouteInfo selected = mRouter.getSelectedRoute(mRouteTypes);
219 final boolean isRemote = selected != mRouter.getSystemAudioRoute();
220 final boolean isConnecting = selected.getStatusCode() == RouteInfo.STATUS_CONNECTING;
221
222 boolean needsRefresh = false;
Adam Powell690ffb42012-06-04 19:22:45 -0700223 if (mRemoteActive != isRemote) {
224 mRemoteActive = isRemote;
Adam Powell2ee6a2a2012-10-01 14:02:13 -0700225 needsRefresh = true;
226 }
227 if (mIsConnecting != isConnecting) {
228 mIsConnecting = isConnecting;
229 needsRefresh = true;
230 }
231
232 if (needsRefresh) {
Adam Powell690ffb42012-06-04 19:22:45 -0700233 refreshDrawableState();
234 }
235 }
236
237 void updateRouteCount() {
238 final int N = mRouter.getRouteCount();
239 int count = 0;
Adam Powell705ab802012-09-17 13:30:51 -0700240 boolean hasVideoRoutes = false;
Adam Powell690ffb42012-06-04 19:22:45 -0700241 for (int i = 0; i < N; i++) {
Adam Powellf7e0a322012-06-21 15:29:40 -0700242 final RouteInfo route = mRouter.getRouteAt(i);
Adam Powell705ab802012-09-17 13:30:51 -0700243 final int routeTypes = route.getSupportedTypes();
244 if ((routeTypes & mRouteTypes) != 0) {
Adam Powellf7e0a322012-06-21 15:29:40 -0700245 if (route instanceof RouteGroup) {
246 count += ((RouteGroup) route).getRouteCount();
247 } else {
248 count++;
249 }
Adam Powell705ab802012-09-17 13:30:51 -0700250 if ((routeTypes & MediaRouter.ROUTE_TYPE_LIVE_VIDEO) != 0) {
251 hasVideoRoutes = true;
252 }
Adam Powell690ffb42012-06-04 19:22:45 -0700253 }
254 }
255
256 setEnabled(count != 0);
257
Adam Powell705ab802012-09-17 13:30:51 -0700258 // Only allow toggling if we have more than just user routes.
259 // Don't toggle if we support video routes, we may have to let the dialog scan.
260 mToggleMode = count == 2 && (mRouteTypes & MediaRouter.ROUTE_TYPE_LIVE_AUDIO) != 0 &&
261 !hasVideoRoutes;
Adam Powell690ffb42012-06-04 19:22:45 -0700262 }
263
264 @Override
265 protected int[] onCreateDrawableState(int extraSpace) {
266 final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
Adam Powell2ee6a2a2012-10-01 14:02:13 -0700267
268 // Technically we should be handling this more completely, but these
269 // are implementation details here. Checked is used to express the connecting
270 // drawable state and it's mutually exclusive with activated for the purposes
271 // of state selection here.
272 if (mIsConnecting) {
273 mergeDrawableStates(drawableState, CHECKED_STATE_SET);
274 } else if (mRemoteActive) {
Adam Powell690ffb42012-06-04 19:22:45 -0700275 mergeDrawableStates(drawableState, ACTIVATED_STATE_SET);
276 }
277 return drawableState;
278 }
279
280 @Override
281 protected void drawableStateChanged() {
282 super.drawableStateChanged();
283
284 if (mRemoteIndicator != null) {
285 int[] myDrawableState = getDrawableState();
286 mRemoteIndicator.setState(myDrawableState);
287 invalidate();
288 }
289 }
290
291 @Override
292 protected boolean verifyDrawable(Drawable who) {
293 return super.verifyDrawable(who) || who == mRemoteIndicator;
294 }
295
296 @Override
297 public void jumpDrawablesToCurrentState() {
298 super.jumpDrawablesToCurrentState();
299 if (mRemoteIndicator != null) mRemoteIndicator.jumpToCurrentState();
300 }
301
302 @Override
303 public void setVisibility(int visibility) {
304 super.setVisibility(visibility);
305 if (mRemoteIndicator != null) {
306 mRemoteIndicator.setVisible(getVisibility() == VISIBLE, false);
307 }
308 }
309
310 @Override
Jack Palevich101c4492012-06-22 16:06:57 +0800311 public void onAttachedToWindow() {
312 super.onAttachedToWindow();
313 mAttachedToWindow = true;
314 if (mRouteTypes != 0) {
315 mRouter.addCallback(mRouteTypes, mRouterCallback);
316 updateRouteInfo();
317 }
318 }
319
320 @Override
321 public void onDetachedFromWindow() {
322 if (mRouteTypes != 0) {
323 mRouter.removeCallback(mRouterCallback);
324 }
325 mAttachedToWindow = false;
326 super.onDetachedFromWindow();
327 }
328
329 @Override
Adam Powell690ffb42012-06-04 19:22:45 -0700330 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
331 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
332 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
333 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
334 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
335
336 final int minWidth = Math.max(mMinWidth,
337 mRemoteIndicator != null ? mRemoteIndicator.getIntrinsicWidth() : 0);
338 final int minHeight = Math.max(mMinHeight,
339 mRemoteIndicator != null ? mRemoteIndicator.getIntrinsicHeight() : 0);
340
341 int width;
342 switch (widthMode) {
343 case MeasureSpec.EXACTLY:
344 width = widthSize;
345 break;
346 case MeasureSpec.AT_MOST:
347 width = Math.min(widthSize, minWidth + getPaddingLeft() + getPaddingRight());
348 break;
349 default:
350 case MeasureSpec.UNSPECIFIED:
351 width = minWidth + getPaddingLeft() + getPaddingRight();
352 break;
353 }
354
355 int height;
356 switch (heightMode) {
357 case MeasureSpec.EXACTLY:
358 height = heightSize;
359 break;
360 case MeasureSpec.AT_MOST:
361 height = Math.min(heightSize, minHeight + getPaddingTop() + getPaddingBottom());
362 break;
363 default:
364 case MeasureSpec.UNSPECIFIED:
365 height = minHeight + getPaddingTop() + getPaddingBottom();
366 break;
367 }
368
369 setMeasuredDimension(width, height);
370 }
371
372 @Override
373 protected void onDraw(Canvas canvas) {
374 super.onDraw(canvas);
375
376 if (mRemoteIndicator == null) return;
377
378 final int left = getPaddingLeft();
379 final int right = getWidth() - getPaddingRight();
380 final int top = getPaddingTop();
381 final int bottom = getHeight() - getPaddingBottom();
382
383 final int drawWidth = mRemoteIndicator.getIntrinsicWidth();
384 final int drawHeight = mRemoteIndicator.getIntrinsicHeight();
385 final int drawLeft = left + (right - left - drawWidth) / 2;
386 final int drawTop = top + (bottom - top - drawHeight) / 2;
387
388 mRemoteIndicator.setBounds(drawLeft, drawTop, drawLeft + drawWidth, drawTop + drawHeight);
389 mRemoteIndicator.draw(canvas);
390 }
391
Adam Powellb35c4452012-06-12 11:25:54 -0700392 public void setExtendedSettingsClickListener(OnClickListener listener) {
Adam Powellb35c4452012-06-12 11:25:54 -0700393 mExtendedSettingsClickListener = listener;
Adam Powell70e11e52012-06-12 16:59:45 -0700394 if (mDialogFragment != null) {
395 mDialogFragment.setExtendedSettingsClickListener(listener);
396 }
397 }
398
399 /**
400 * Asynchronously show the route chooser dialog.
401 * This will attach a {@link DialogFragment} to the containing Activity.
402 */
403 public void showDialog() {
404 final FragmentManager fm = getActivity().getFragmentManager();
405 if (mDialogFragment == null) {
406 // See if one is already attached to this activity.
407 mDialogFragment = (MediaRouteChooserDialogFragment) fm.findFragmentByTag(
408 MediaRouteChooserDialogFragment.FRAGMENT_TAG);
409 }
410 if (mDialogFragment != null) {
411 Log.w(TAG, "showDialog(): Already showing!");
412 return;
413 }
414
415 mDialogFragment = new MediaRouteChooserDialogFragment();
416 mDialogFragment.setExtendedSettingsClickListener(mExtendedSettingsClickListener);
417 mDialogFragment.setLauncherListener(new MediaRouteChooserDialogFragment.LauncherListener() {
418 @Override
419 public void onDetached(MediaRouteChooserDialogFragment detachedFragment) {
420 mDialogFragment = null;
421 }
422 });
423 mDialogFragment.setRouteTypes(mRouteTypes);
424 mDialogFragment.show(fm, MediaRouteChooserDialogFragment.FRAGMENT_TAG);
425 }
426
427 private Activity getActivity() {
428 // Gross way of unwrapping the Activity so we can get the FragmentManager
429 Context context = getContext();
430 while (context instanceof ContextWrapper && !(context instanceof Activity)) {
431 context = ((ContextWrapper) context).getBaseContext();
432 }
433 if (!(context instanceof Activity)) {
434 throw new IllegalStateException("The MediaRouteButton's Context is not an Activity.");
435 }
436
437 return (Activity) context;
Adam Powellb35c4452012-06-12 11:25:54 -0700438 }
439
Adam Powell690ffb42012-06-04 19:22:45 -0700440 private class MediaRouteCallback extends MediaRouter.SimpleCallback {
441 @Override
Adam Powelld0d2cda2012-06-08 14:46:35 -0700442 public void onRouteSelected(MediaRouter router, int type, RouteInfo info) {
Adam Powell690ffb42012-06-04 19:22:45 -0700443 updateRemoteIndicator();
444 }
445
446 @Override
Adam Powelld0d2cda2012-06-08 14:46:35 -0700447 public void onRouteUnselected(MediaRouter router, int type, RouteInfo info) {
Adam Powell690ffb42012-06-04 19:22:45 -0700448 updateRemoteIndicator();
449 }
450
451 @Override
Adam Powell2ee6a2a2012-10-01 14:02:13 -0700452 public void onRouteChanged(MediaRouter router, RouteInfo info) {
453 updateRemoteIndicator();
454 }
455
456 @Override
Adam Powelld0d2cda2012-06-08 14:46:35 -0700457 public void onRouteAdded(MediaRouter router, RouteInfo info) {
Adam Powell690ffb42012-06-04 19:22:45 -0700458 updateRouteCount();
459 }
460
461 @Override
Adam Powelld0d2cda2012-06-08 14:46:35 -0700462 public void onRouteRemoved(MediaRouter router, RouteInfo info) {
Adam Powell690ffb42012-06-04 19:22:45 -0700463 updateRouteCount();
464 }
Adam Powellf3b653a2012-06-22 18:19:08 -0700465
466 @Override
467 public void onRouteGrouped(MediaRouter router, RouteInfo info, RouteGroup group,
468 int index) {
469 updateRouteCount();
470 }
471
472 @Override
473 public void onRouteUngrouped(MediaRouter router, RouteInfo info, RouteGroup group) {
474 updateRouteCount();
475 }
Adam Powell690ffb42012-06-04 19:22:45 -0700476 }
477}