MediaRouteActionProvider.java revision b35c445f34e1a18e17aef3e3dfbc1c39b4d1815c
1/* 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 android.content.Context; 20import android.media.MediaRouter; 21import android.media.MediaRouter.RouteInfo; 22import android.util.Log; 23import android.view.ActionProvider; 24import android.view.MenuItem; 25import android.view.View; 26 27public class MediaRouteActionProvider extends ActionProvider { 28 private static final String TAG = "MediaRouteActionProvider"; 29 30 private Context mContext; 31 private MediaRouter mRouter; 32 private MenuItem mMenuItem; 33 private MediaRouteButton mView; 34 private int mRouteTypes; 35 private final RouterCallback mRouterCallback = new RouterCallback(); 36 private View.OnClickListener mExtendedSettingsListener; 37 38 public MediaRouteActionProvider(Context context) { 39 super(context); 40 mContext = context; 41 mRouter = (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE); 42 43 // Start with live audio by default. 44 // TODO Update this when new route types are added; segment by API level 45 // when different route types were added. 46 setRouteTypes(MediaRouter.ROUTE_TYPE_LIVE_AUDIO); 47 } 48 49 public void setRouteTypes(int types) { 50 if (types == mRouteTypes) { 51 // Already registered; nothing to do. 52 return; 53 } 54 if (mRouteTypes != 0) { 55 mRouter.removeCallback(mRouterCallback); 56 } 57 mRouteTypes = types; 58 if (mView != null) { 59 mView.setRouteTypes(mRouteTypes); 60 } 61 mRouter.addCallback(types, mRouterCallback); 62 } 63 64 @Override 65 public View onCreateActionView() { 66 throw new UnsupportedOperationException("Use onCreateActionView(MenuItem) instead."); 67 } 68 69 @Override 70 public View onCreateActionView(MenuItem item) { 71 if (mMenuItem != null || mView != null) { 72 Log.e(TAG, "onCreateActionView: this ActionProvider is already associated " + 73 "with a menu item. Don't reuse MediaRouteActionProvider instances! " + 74 "Abandoning the old one..."); 75 } 76 mMenuItem = item; 77 mView = new MediaRouteButton(mContext); 78 mMenuItem.setVisible(mRouter.getRouteCount() > 1); 79 mView.setRouteTypes(mRouteTypes); 80 mView.setExtendedSettingsClickListener(mExtendedSettingsListener); 81 return mView; 82 } 83 84 @Override 85 public boolean onPerformDefaultAction() { 86 // Show routing dialog 87 return true; 88 } 89 90 public void setExtendedSettingsClickListener(View.OnClickListener listener) { 91 mExtendedSettingsListener = listener; 92 if (mView != null) { 93 mView.setExtendedSettingsClickListener(listener); 94 } 95 } 96 97 private class RouterCallback extends MediaRouter.SimpleCallback { 98 @Override 99 public void onRouteAdded(MediaRouter router, RouteInfo info) { 100 mMenuItem.setVisible(mRouter.getRouteCount() > 1); 101 } 102 103 @Override 104 public void onRouteRemoved(MediaRouter router, RouteInfo info) { 105 mMenuItem.setVisible(mRouter.getRouteCount() > 1); 106 } 107 } 108} 109