Debian Codegraph @

core/java/com/android/internal/app/MediaRouteChooserDialog.java

raw symbols SCIP
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
17 package com.android.internal.app;
18
19 import android.app.AlertDialog;
20 import android.content.Context;
21 import android.media.MediaRouter;
22 import android.media.MediaRouter.RouteInfo;
23 import android.os.Bundle;
24 import android.text.TextUtils;
25 import android.util.TypedValue;
26 import android.view.Gravity;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.AdapterView;
31 import android.widget.ArrayAdapter;
32 import android.widget.Button;
33 import android.widget.LinearLayout;
34 import android.widget.ListView;
35 import android.widget.TextView;
36
37 import com.android.internal.R;
38
39 import java.util.Comparator;
40
41 /**
42 * This class implements the route chooser dialog for {@link MediaRouter}.
43 * <p>
44 * This dialog allows the user to choose a route that matches a given selector.
45 * </p>
46 *
47 * @see MediaRouteButton
48 * @see MediaRouteActionProvider
49 *
50 * TODO: Move this back into the API, as in the support library media router.
51 */
52 public class MediaRouteChooserDialog extends AlertDialog {
53 private final MediaRouter mRouter;
54 private final MediaRouterCallback mCallback;
55 private final boolean mShowProgressBarWhenEmpty;
56
57 private int mRouteTypes;
58 private View.OnClickListener mExtendedSettingsClickListener;
59 private RouteAdapter mAdapter;
60 private ListView mListView;
61 private Button mExtendedSettingsButton;
62 private boolean mAttachedToWindow;
63
64 public MediaRouteChooserDialog(Context context, int theme) {
65 this(context, theme, true);
66 }
67
68 public MediaRouteChooserDialog(Context context, int theme, boolean showProgressBarWhenEmpty) {
69 super(context, theme);
70
71 mRouter = (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE);
72 mCallback = new MediaRouterCallback();
73 mShowProgressBarWhenEmpty = showProgressBarWhenEmpty;
74 }
75
76 /**
77 * Gets the media route types for filtering the routes that the user can
78 * select using the media route chooser dialog.
79 *
80 * @return The route types.
81 */
82 public int getRouteTypes() {
83 return mRouteTypes;
84 }
85
86 /**
87 * Sets the types of routes that will be shown in the media route chooser dialog
88 * launched by this button.
89 *
90 * @param types The route types to match.
91 */
92 public void setRouteTypes(int types) {
93 if (mRouteTypes != types) {
94 mRouteTypes = types;
95
96 if (mAttachedToWindow) {
97 mRouter.removeCallback(mCallback);
98 mRouter.addCallback(types, mCallback,
99 MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
100 }
101
102 refreshRoutes();
103 }
104 }
105
106 public void setExtendedSettingsClickListener(View.OnClickListener listener) {
107 if (listener != mExtendedSettingsClickListener) {
108 mExtendedSettingsClickListener = listener;
109 updateExtendedSettingsButton();
110 }
111 }
112
113 /**
114 * Returns true if the route should be included in the list.
115 * <p>
116 * The default implementation returns true for enabled non-default routes that
117 * match the route types. Subclasses can override this method to filter routes
118 * differently.
119 * </p>
120 *
121 * @param route The route to consider, never null.
122 * @return True if the route should be included in the chooser dialog.
123 */
124 public boolean onFilterRoute(MediaRouter.RouteInfo route) {
125 return !route.isDefault() && route.isEnabled() && route.matchesTypes(mRouteTypes);
126 }
127
128 @Override
129 protected void onCreate(Bundle savedInstanceState) {
130 // Note: setView must be called before super.onCreate().
131 setView(LayoutInflater.from(getContext()).inflate(R.layout.media_route_chooser_dialog,
132 null));
133
134 setTitle(mRouteTypes == MediaRouter.ROUTE_TYPE_REMOTE_DISPLAY
135 ? R.string.media_route_chooser_title_for_remote_display
136 : R.string.media_route_chooser_title);
137
138 setIcon(isLightTheme(getContext()) ? R.drawable.ic_media_route_off_holo_light
139 : R.drawable.ic_media_route_off_holo_dark);
140
141 super.onCreate(savedInstanceState);
142
143 View emptyView = findViewById(android.R.id.empty);
144 mAdapter = new RouteAdapter(getContext());
145 mListView = (ListView) findViewById(R.id.media_route_list);
146 mListView.setAdapter(mAdapter);
147 mListView.setOnItemClickListener(mAdapter);
148 mListView.setEmptyView(emptyView);
149
150 mExtendedSettingsButton = (Button) findViewById(R.id.media_route_extended_settings_button);
151 updateExtendedSettingsButton();
152
153 if (!mShowProgressBarWhenEmpty) {
154 findViewById(R.id.media_route_progress_bar).setVisibility(View.GONE);
155
156 // Center the empty view when the progress bar is not shown.
157 LinearLayout.LayoutParams params =
158 (LinearLayout.LayoutParams) emptyView.getLayoutParams();
159 params.gravity = Gravity.CENTER;
160 emptyView.setLayoutParams(params);
161 }
162 }
163
164 private void updateExtendedSettingsButton() {
165 if (mExtendedSettingsButton != null) {
166 mExtendedSettingsButton.setOnClickListener(mExtendedSettingsClickListener);
167 mExtendedSettingsButton.setVisibility(
168 mExtendedSettingsClickListener != null ? View.VISIBLE : View.GONE);
169 }
170 }
171
172 @Override
173 public void onAttachedToWindow() {
174 super.onAttachedToWindow();
175
176 mAttachedToWindow = true;
177 mRouter.addCallback(mRouteTypes, mCallback, MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
178 refreshRoutes();
179 }
180
181 @Override
182 public void onDetachedFromWindow() {
183 mAttachedToWindow = false;
184 mRouter.removeCallback(mCallback);
185
186 super.onDetachedFromWindow();
187 }
188
189 /**
190 * Refreshes the list of routes that are shown in the chooser dialog.
191 */
192 public void refreshRoutes() {
193 if (mAttachedToWindow) {
194 mAdapter.update();
195 }
196 }
197
198 static boolean isLightTheme(Context context) {
199 TypedValue value = new TypedValue();
200 return context.getTheme().resolveAttribute(R.attr.isLightTheme, value, true)
201 && value.data != 0;
202 }
203
204 private final class RouteAdapter extends ArrayAdapter<MediaRouter.RouteInfo>
205 implements ListView.OnItemClickListener {
206 private final LayoutInflater mInflater;
207
208 public RouteAdapter(Context context) {
209 super(context, 0);
210 mInflater = LayoutInflater.from(context);
211 }
212
213 public void update() {
214 clear();
215 final int count = mRouter.getRouteCount();
216 for (int i = 0; i < count; i++) {
217 MediaRouter.RouteInfo route = mRouter.getRouteAt(i);
218 if (onFilterRoute(route)) {
219 add(route);
220 }
221 }
222 sort(RouteComparator.sInstance);
223 notifyDataSetChanged();
224 }
225
226 @Override
227 public boolean areAllItemsEnabled() {
228 return false;
229 }
230
231 @Override
232 public boolean isEnabled(int position) {
233 return getItem(position).isEnabled();
234 }
235
236 @Override
237 public View getView(int position, View convertView, ViewGroup parent) {
238 View view = convertView;
239 if (view == null) {
240 view = mInflater.inflate(R.layout.media_route_list_item, parent, false);
241 }
242 MediaRouter.RouteInfo route = getItem(position);
243 TextView text1 = (TextView)view.findViewById(android.R.id.text1);
244 TextView text2 = (TextView)view.findViewById(android.R.id.text2);
245 text1.setText(route.getName());
246 CharSequence description = route.getDescription();
247 if (TextUtils.isEmpty(description)) {
248 text2.setVisibility(View.GONE);
249 text2.setText("");
250 } else {
251 text2.setVisibility(View.VISIBLE);
252 text2.setText(description);
253 }
254 view.setEnabled(route.isEnabled());
255 return view;
256 }
257
258 @Override
259 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
260 MediaRouter.RouteInfo route = getItem(position);
261 if (route.isEnabled()) {
262 route.select();
263 dismiss();
264 }
265 }
266 }
267
268 private final class MediaRouterCallback extends MediaRouter.SimpleCallback {
269 @Override
270 public void onRouteAdded(MediaRouter router, MediaRouter.RouteInfo info) {
271 refreshRoutes();
272 }
273
274 @Override
275 public void onRouteRemoved(MediaRouter router, MediaRouter.RouteInfo info) {
276 refreshRoutes();
277 }
278
279 @Override
280 public void onRouteChanged(MediaRouter router, MediaRouter.RouteInfo info) {
281 refreshRoutes();
282 }
283
284 @Override
285 public void onRouteSelected(MediaRouter router, int type, RouteInfo info) {
286 dismiss();
287 }
288 }
289
290 private static final class RouteComparator implements Comparator<MediaRouter.RouteInfo> {
291 public static final RouteComparator sInstance = new RouteComparator();
292
293 @Override
294 public int compare(MediaRouter.RouteInfo lhs, MediaRouter.RouteInfo rhs) {
295 return lhs.getName().toString().compareTo(rhs.getName().toString());
296 }
297 }
298 }