MediaRouteChooserDialog.java revision 02acfc96bf4b4108f76f465f7895c7bbd60c0253
1/*
2 * Copyright (C) 2013 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.support.v7.app;
18
19import android.app.Dialog;
20import android.content.Context;
21import android.os.Bundle;
22import android.support.v7.media.MediaRouter;
23import android.support.v7.media.MediaRouteSelector;
24import android.support.v7.mediarouter.R;
25import android.text.TextUtils;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29import android.view.Window;
30import android.widget.AdapterView;
31import android.widget.ArrayAdapter;
32import android.widget.ListView;
33import android.widget.TextView;
34
35import java.util.Comparator;
36import java.util.List;
37
38/**
39 * This class implements the route chooser dialog for {@link MediaRouter}.
40 * <p>
41 * This dialog allows the user to choose a route that matches a given selector.
42 * </p>
43 *
44 * @see MediaRouteButton
45 * @see MediaRouteActionProvider
46 */
47public class MediaRouteChooserDialog extends Dialog {
48    private final MediaRouter mRouter;
49    private final MediaRouterCallback mCallback;
50
51    private MediaRouteSelector mSelector = MediaRouteSelector.EMPTY;
52    private RouteAdapter mAdapter;
53    private ListView mListView;
54    private boolean mAttachedToWindow;
55
56    public MediaRouteChooserDialog(Context context) {
57        this(context, 0);
58    }
59
60    public MediaRouteChooserDialog(Context context, int theme) {
61        super(MediaRouterThemeHelper.createThemedContext(context, true), theme);
62        context = getContext();
63
64        mRouter = MediaRouter.getInstance(context);
65        mCallback = new MediaRouterCallback();
66    }
67
68    /**
69     * Gets the media route selector for filtering the routes that the user can select.
70     *
71     * @return The selector, never null.
72     */
73    public MediaRouteSelector getRouteSelector() {
74        return mSelector;
75    }
76
77    /**
78     * Sets the media route selector for filtering the routes that the user can select.
79     *
80     * @param selector The selector, must not be null.
81     */
82    public void setRouteSelector(MediaRouteSelector selector) {
83        if (selector == null) {
84            throw new IllegalArgumentException("selector must not be null");
85        }
86
87        if (!mSelector.equals(selector)) {
88            mSelector = selector;
89
90            if (mAttachedToWindow) {
91                mRouter.removeCallback(mCallback);
92                mRouter.addCallback(selector, mCallback,
93                        MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
94            }
95
96            refreshRoutes();
97        }
98    }
99
100    /**
101     * Returns true if the route should be included in the list.
102     * <p>
103     * The default implementation returns true for enabled non-default routes that
104     * match the selector.  Subclasses can override this method to filter routes
105     * differently.
106     * </p>
107     *
108     * @param route The route to consider, never null.
109     * @return True if the route should be included in the chooser dialog.
110     */
111    public boolean onFilterRoute(MediaRouter.RouteInfo route) {
112        return !route.isDefault() && route.isEnabled() && route.matchesSelector(mSelector);
113    }
114
115    @Override
116    protected void onCreate(Bundle savedInstanceState) {
117        super.onCreate(savedInstanceState);
118
119        getWindow().requestFeature(Window.FEATURE_LEFT_ICON);
120
121        setContentView(R.layout.mr_media_route_chooser_dialog);
122        setTitle(R.string.mr_media_route_chooser_title);
123
124        // Must be called after setContentView.
125        getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
126                MediaRouterThemeHelper.getThemeResource(
127                        getContext(), R.attr.mediaRouteOffDrawable));
128
129        mAdapter = new RouteAdapter(getContext());
130        mListView = (ListView)findViewById(R.id.media_route_list);
131        mListView.setAdapter(mAdapter);
132        mListView.setOnItemClickListener(mAdapter);
133        mListView.setEmptyView(findViewById(android.R.id.empty));
134    }
135
136    @Override
137    public void onAttachedToWindow() {
138        super.onAttachedToWindow();
139
140        mAttachedToWindow = true;
141        mRouter.addCallback(mSelector, mCallback, MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
142        refreshRoutes();
143    }
144
145    @Override
146    public void onDetachedFromWindow() {
147        mAttachedToWindow = false;
148        mRouter.removeCallback(mCallback);
149
150        super.onDetachedFromWindow();
151    }
152
153    /**
154     * Refreshes the list of routes that are shown in the chooser dialog.
155     */
156    public void refreshRoutes() {
157        if (mAttachedToWindow) {
158            mAdapter.update();
159        }
160    }
161
162    private final class RouteAdapter extends ArrayAdapter<MediaRouter.RouteInfo>
163            implements ListView.OnItemClickListener {
164        private final LayoutInflater mInflater;
165
166        public RouteAdapter(Context context) {
167            super(context, 0);
168            mInflater = LayoutInflater.from(context);
169        }
170
171        public void update() {
172            clear();
173            final List<MediaRouter.RouteInfo> routes = mRouter.getRoutes();
174            final int count = routes.size();
175            for (int i = 0; i < count; i++) {
176                MediaRouter.RouteInfo route = routes.get(i);
177                if (onFilterRoute(route)) {
178                    add(route);
179                }
180            }
181            sort(RouteComparator.sInstance);
182            notifyDataSetChanged();
183        }
184
185        @Override
186        public boolean areAllItemsEnabled() {
187            return false;
188        }
189
190        @Override
191        public boolean isEnabled(int position) {
192            return getItem(position).isEnabled();
193        }
194
195        @Override
196        public View getView(int position, View convertView, ViewGroup parent) {
197            View view = convertView;
198            if (view == null) {
199                view = mInflater.inflate(R.layout.mr_media_route_list_item, parent, false);
200            }
201            MediaRouter.RouteInfo route = getItem(position);
202            TextView text1 = (TextView)view.findViewById(android.R.id.text1);
203            TextView text2 = (TextView)view.findViewById(android.R.id.text2);
204            text1.setText(route.getName());
205            String description = route.getDescription();
206            if (TextUtils.isEmpty(description)) {
207                text2.setVisibility(View.GONE);
208                text2.setText("");
209            } else {
210                text2.setVisibility(View.VISIBLE);
211                text2.setText(description);
212            }
213            view.setEnabled(route.isEnabled());
214            return view;
215        }
216
217        @Override
218        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
219            MediaRouter.RouteInfo route = getItem(position);
220            if (route.isEnabled()) {
221                route.select();
222                dismiss();
223            }
224        }
225    }
226
227    private final class MediaRouterCallback extends MediaRouter.Callback {
228        @Override
229        public void onRouteAdded(MediaRouter router, MediaRouter.RouteInfo info) {
230            refreshRoutes();
231        }
232
233        @Override
234        public void onRouteRemoved(MediaRouter router, MediaRouter.RouteInfo info) {
235            refreshRoutes();
236        }
237
238        @Override
239        public void onRouteChanged(MediaRouter router, MediaRouter.RouteInfo info) {
240            refreshRoutes();
241        }
242
243        @Override
244        public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo route) {
245            dismiss();
246        }
247    }
248
249    private static final class RouteComparator implements Comparator<MediaRouter.RouteInfo> {
250        public static final RouteComparator sInstance = new RouteComparator();
251
252        @Override
253        public int compare(MediaRouter.RouteInfo lhs, MediaRouter.RouteInfo rhs) {
254            return lhs.getName().compareTo(rhs.getName());
255        }
256    }
257}
258