MediaRouteChooserDialog.java revision 2ef36d857302c5cd738c7c8bdec53d31feebebba
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), 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 } 94 95 refreshRoutes(); 96 } 97 } 98 99 @Override 100 protected void onCreate(Bundle savedInstanceState) { 101 super.onCreate(savedInstanceState); 102 103 getWindow().requestFeature(Window.FEATURE_LEFT_ICON); 104 105 setContentView(R.layout.media_route_chooser_dialog); 106 setTitle(R.string.media_route_chooser_title); 107 108 // Must be called after setContentView. 109 getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, 110 MediaRouterThemeHelper.getThemeResource( 111 getContext(), R.attr.mediaRouteOffDrawable)); 112 113 mAdapter = new RouteAdapter(getContext()); 114 mListView = (ListView)findViewById(R.id.media_route_list); 115 mListView.setAdapter(mAdapter); 116 mListView.setOnItemClickListener(mAdapter); 117 } 118 119 @Override 120 public void onAttachedToWindow() { 121 super.onAttachedToWindow(); 122 123 mAttachedToWindow = true; 124 mRouter.addCallback(mSelector, mCallback, MediaRouter.CALLBACK_FLAG_ACTIVE_SCAN); 125 refreshRoutes(); 126 } 127 128 @Override 129 public void onDetachedFromWindow() { 130 mAttachedToWindow = false; 131 mRouter.removeCallback(mCallback); 132 133 super.onDetachedFromWindow(); 134 } 135 136 private void refreshRoutes() { 137 if (mAttachedToWindow) { 138 mAdapter.update(); 139 } 140 } 141 142 private final class RouteAdapter extends ArrayAdapter<MediaRouter.RouteInfo> 143 implements ListView.OnItemClickListener { 144 private final LayoutInflater mInflater; 145 146 public RouteAdapter(Context context) { 147 super(context, 0); 148 mInflater = LayoutInflater.from(context); 149 } 150 151 public void update() { 152 clear(); 153 final List<MediaRouter.RouteInfo> routes = mRouter.getRoutes(); 154 final int count = routes.size(); 155 for (int i = 0; i < count; i++) { 156 MediaRouter.RouteInfo route = routes.get(i); 157 if (!route.isDefault() && route.matchesSelector(mSelector)) { 158 add(route); 159 } 160 } 161 sort(RouteComparator.sInstance); 162 notifyDataSetChanged(); 163 } 164 165 @Override 166 public boolean areAllItemsEnabled() { 167 return false; 168 } 169 170 @Override 171 public boolean isEnabled(int position) { 172 return getItem(position).isEnabled(); 173 } 174 175 @Override 176 public View getView(int position, View convertView, ViewGroup parent) { 177 View view = convertView; 178 if (view == null) { 179 view = mInflater.inflate(R.layout.media_route_list_item, parent, false); 180 } 181 MediaRouter.RouteInfo route = getItem(position); 182 TextView text1 = (TextView)view.findViewById(android.R.id.text1); 183 TextView text2 = (TextView)view.findViewById(android.R.id.text2); 184 text1.setText(route.getName()); 185 String description = route.getDescription(); 186 if (TextUtils.isEmpty(description)) { 187 text2.setVisibility(View.GONE); 188 text2.setText(""); 189 } else { 190 text2.setVisibility(View.VISIBLE); 191 text2.setText(description); 192 } 193 view.setEnabled(route.isEnabled()); 194 return view; 195 } 196 197 @Override 198 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 199 MediaRouter.RouteInfo route = getItem(position); 200 if (route.isEnabled()) { 201 route.select(); 202 dismiss(); 203 } 204 } 205 } 206 207 private final class MediaRouterCallback extends MediaRouter.Callback { 208 @Override 209 public void onRouteAdded(MediaRouter router, MediaRouter.RouteInfo info) { 210 refreshRoutes(); 211 } 212 213 @Override 214 public void onRouteRemoved(MediaRouter router, MediaRouter.RouteInfo info) { 215 refreshRoutes(); 216 } 217 218 @Override 219 public void onRouteChanged(MediaRouter router, MediaRouter.RouteInfo info) { 220 refreshRoutes(); 221 } 222 223 @Override 224 public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo route) { 225 dismiss(); 226 } 227 } 228 229 private static final class RouteComparator implements Comparator<MediaRouter.RouteInfo> { 230 public static final RouteComparator sInstance = new RouteComparator(); 231 232 @Override 233 public int compare(MediaRouter.RouteInfo lhs, MediaRouter.RouteInfo rhs) { 234 return lhs.getName().compareTo(rhs.getName()); 235 } 236 } 237} 238