MediaRouteButton.java revision b58b8f832d06b0ffa8886eba5a4916578a3b8743
1690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell/*
2690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell * Copyright (C) 2012 The Android Open Source Project
3690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell *
4690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell * Licensed under the Apache License, Version 2.0 (the "License");
5690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell * you may not use this file except in compliance with the License.
6690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell * You may obtain a copy of the License at
7690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell *
8690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell *      http://www.apache.org/licenses/LICENSE-2.0
9690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell *
10690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell * Unless required by applicable law or agreed to in writing, software
11690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell * distributed under the License is distributed on an "AS IS" BASIS,
12690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell * See the License for the specific language governing permissions and
14690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell * limitations under the License.
15690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell */
16690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
17690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellpackage android.app;
18690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
19690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellimport com.android.internal.R;
20690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
21690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellimport android.content.Context;
22690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellimport android.content.res.TypedArray;
23690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellimport android.graphics.Canvas;
24690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellimport android.graphics.drawable.Drawable;
25690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellimport android.media.MediaRouter;
26690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellimport android.media.MediaRouter.RouteInfo;
27690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellimport android.util.AttributeSet;
28690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellimport android.util.Log;
29690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellimport android.view.SoundEffectConstants;
30690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellimport android.view.View;
31690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
32690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellpublic class MediaRouteButton extends View {
33690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private static final String TAG = "MediaRouteButton";
34690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
35690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private MediaRouter mRouter;
36690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private final MediaRouteCallback mRouterCallback = new MediaRouteCallback();
37690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private int mRouteTypes;
38690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
39690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private Drawable mRemoteIndicator;
40690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private boolean mRemoteActive;
41690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private boolean mToggleMode;
42690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
43690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private int mMinWidth;
44690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private int mMinHeight;
45690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
46690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private static final int[] ACTIVATED_STATE_SET = {
47690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        R.attr.state_activated
48690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    };
49690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
50690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    public MediaRouteButton(Context context) {
51690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        this(context, null);
52690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
53690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
54690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    public MediaRouteButton(Context context, AttributeSet attrs) {
55690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        this(context, null, com.android.internal.R.attr.mediaRouteButtonStyle);
56690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
57690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
58690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    public MediaRouteButton(Context context, AttributeSet attrs, int defStyleAttr) {
59690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        super(context, attrs, defStyleAttr);
60690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
61b58b8f832d06b0ffa8886eba5a4916578a3b8743Dianne Hackborn        mRouter = (MediaRouter)context.getSystemService(Context.MEDIA_ROUTER_SERVICE);
62690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
63690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        TypedArray a = context.obtainStyledAttributes(attrs,
64690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                com.android.internal.R.styleable.MediaRouteButton, defStyleAttr, 0);
65690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        setRemoteIndicatorDrawable(a.getDrawable(
66690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                com.android.internal.R.styleable.MediaRouteButton_externalRouteEnabledDrawable));
67690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        mMinWidth = a.getDimensionPixelSize(
68690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                com.android.internal.R.styleable.MediaRouteButton_minWidth, 0);
69690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        mMinHeight = a.getDimensionPixelSize(
70690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                com.android.internal.R.styleable.MediaRouteButton_minHeight, 0);
71690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        a.recycle();
72690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
73690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        setClickable(true);
74690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
75690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
76690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private void setRemoteIndicatorDrawable(Drawable d) {
77690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        if (mRemoteIndicator != null) {
78690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            mRemoteIndicator.setCallback(null);
79690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            unscheduleDrawable(mRemoteIndicator);
80690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
81690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        mRemoteIndicator = d;
82690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        if (d != null) {
83690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            d.setCallback(this);
84690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            d.setState(getDrawableState());
85690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            d.setVisible(getVisibility() == VISIBLE, false);
86690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
87690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
88690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        refreshDrawableState();
89690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
90690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
91690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    @Override
92690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    public boolean performClick() {
93690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        // Send the appropriate accessibility events and call listeners
94690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        boolean handled = super.performClick();
95690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        if (!handled) {
96690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            playSoundEffect(SoundEffectConstants.CLICK);
97690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
98690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
99690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        if (mToggleMode) {
100690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            if (mRemoteActive) {
101690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                mRouter.selectRoute(mRouteTypes, mRouter.getSystemAudioRoute());
102690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            } else {
103690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                final int N = mRouter.getRouteCount();
104690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                for (int i = 0; i < N; i++) {
105690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                    final RouteInfo route = mRouter.getRouteAt(i);
106690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                    if ((route.getSupportedTypes() & mRouteTypes) != 0 &&
107690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                            route != mRouter.getSystemAudioRoute()) {
108690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                        mRouter.selectRoute(mRouteTypes, route);
109690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                    }
110690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                }
111690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            }
112690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        } else {
113690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            Log.d(TAG, "TODO: Implement the dialog!");
114690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
115690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
116690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        return handled;
117690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
118690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
119690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    public void setRouteTypes(int types) {
120690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        if (types == mRouteTypes) {
121690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            // Already registered; nothing to do.
122690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            return;
123690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
124690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        if (mRouteTypes != 0) {
125690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            mRouter.removeCallback(mRouterCallback);
126690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
127690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        mRouteTypes = types;
128690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        updateRemoteIndicator();
129690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        updateRouteCount();
130690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        mRouter.addCallback(types, mRouterCallback);
131690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
132690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
133690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    public int getRouteTypes() {
134690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        return mRouteTypes;
135690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
136690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
137690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    void updateRemoteIndicator() {
138690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        final boolean isRemote =
139690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                mRouter.getSelectedRoute(mRouteTypes) != mRouter.getSystemAudioRoute();
140690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        if (mRemoteActive != isRemote) {
141690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            mRemoteActive = isRemote;
142690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            refreshDrawableState();
143690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
144690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
145690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
146690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    void updateRouteCount() {
147690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        final int N = mRouter.getRouteCount();
148690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        int count = 0;
149690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        for (int i = 0; i < N; i++) {
150690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            if ((mRouter.getRouteAt(i).getSupportedTypes() & mRouteTypes) != 0) {
151690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                count++;
152690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            }
153690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
154690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
155690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        setEnabled(count != 0);
156690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
157690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        // Only allow toggling if we have more than just user routes
158690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        mToggleMode = count == 2 && (mRouteTypes & MediaRouter.ROUTE_TYPE_LIVE_AUDIO) != 0;
159690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
160690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
161690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    @Override
162690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    protected int[] onCreateDrawableState(int extraSpace) {
163690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
164690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        if (mRemoteActive) {
165690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            mergeDrawableStates(drawableState, ACTIVATED_STATE_SET);
166690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
167690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        return drawableState;
168690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
169690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
170690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    @Override
171690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    protected void drawableStateChanged() {
172690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        super.drawableStateChanged();
173690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
174690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        if (mRemoteIndicator != null) {
175690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            int[] myDrawableState = getDrawableState();
176690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            mRemoteIndicator.setState(myDrawableState);
177690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            invalidate();
178690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
179690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
180690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
181690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    @Override
182690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    protected boolean verifyDrawable(Drawable who) {
183690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        return super.verifyDrawable(who) || who == mRemoteIndicator;
184690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
185690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
186690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    @Override
187690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    public void jumpDrawablesToCurrentState() {
188690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        super.jumpDrawablesToCurrentState();
189690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        if (mRemoteIndicator != null) mRemoteIndicator.jumpToCurrentState();
190690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
191690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
192690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    @Override
193690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    public void setVisibility(int visibility) {
194690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        super.setVisibility(visibility);
195690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        if (mRemoteIndicator != null) {
196690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            mRemoteIndicator.setVisible(getVisibility() == VISIBLE, false);
197690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
198690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
199690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
200690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    @Override
201690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
202690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
203690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
204690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
205690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
206690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
207690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        final int minWidth = Math.max(mMinWidth,
208690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                mRemoteIndicator != null ? mRemoteIndicator.getIntrinsicWidth() : 0);
209690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        final int minHeight = Math.max(mMinHeight,
210690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                mRemoteIndicator != null ? mRemoteIndicator.getIntrinsicHeight() : 0);
211690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
212690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        int width;
213690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        switch (widthMode) {
214690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            case MeasureSpec.EXACTLY:
215690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                width = widthSize;
216690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                break;
217690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            case MeasureSpec.AT_MOST:
218690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                width = Math.min(widthSize, minWidth + getPaddingLeft() + getPaddingRight());
219690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                break;
220690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            default:
221690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            case MeasureSpec.UNSPECIFIED:
222690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                width = minWidth + getPaddingLeft() + getPaddingRight();
223690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                break;
224690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
225690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
226690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        int height;
227690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        switch (heightMode) {
228690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            case MeasureSpec.EXACTLY:
229690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                height = heightSize;
230690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                break;
231690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            case MeasureSpec.AT_MOST:
232690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                height = Math.min(heightSize, minHeight + getPaddingTop() + getPaddingBottom());
233690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                break;
234690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            default:
235690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            case MeasureSpec.UNSPECIFIED:
236690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                height = minHeight + getPaddingTop() + getPaddingBottom();
237690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                break;
238690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
239690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
240690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        setMeasuredDimension(width, height);
241690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
242690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
243690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    @Override
244690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    protected void onDraw(Canvas canvas) {
245690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        super.onDraw(canvas);
246690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
247690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        if (mRemoteIndicator == null) return;
248690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
249690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        final int left = getPaddingLeft();
250690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        final int right = getWidth() - getPaddingRight();
251690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        final int top = getPaddingTop();
252690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        final int bottom = getHeight() - getPaddingBottom();
253690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
254690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        final int drawWidth = mRemoteIndicator.getIntrinsicWidth();
255690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        final int drawHeight = mRemoteIndicator.getIntrinsicHeight();
256690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        final int drawLeft = left + (right - left - drawWidth) / 2;
257690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        final int drawTop = top + (bottom - top - drawHeight) / 2;
258690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
259690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        mRemoteIndicator.setBounds(drawLeft, drawTop, drawLeft + drawWidth, drawTop + drawHeight);
260690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        mRemoteIndicator.draw(canvas);
261690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
262690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
263690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private class MediaRouteCallback extends MediaRouter.SimpleCallback {
264690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        @Override
265d0d2cda9d414da73773285d7fee9e13aef3495e9Adam Powell        public void onRouteSelected(MediaRouter router, int type, RouteInfo info) {
266690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            updateRemoteIndicator();
267690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
268690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
269690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        @Override
270d0d2cda9d414da73773285d7fee9e13aef3495e9Adam Powell        public void onRouteUnselected(MediaRouter router, int type, RouteInfo info) {
271690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            updateRemoteIndicator();
272690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
273690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
274690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        @Override
275d0d2cda9d414da73773285d7fee9e13aef3495e9Adam Powell        public void onRouteAdded(MediaRouter router, RouteInfo info) {
276690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            updateRouteCount();
277690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
278690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
279690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        @Override
280d0d2cda9d414da73773285d7fee9e13aef3495e9Adam Powell        public void onRouteRemoved(MediaRouter router, RouteInfo info) {
281690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            updateRouteCount();
282690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
283690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
284690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell}
285