Adam Powell | 690ffb4 | 2012-06-04 19:22:45 -0700 | [diff] [blame] | 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 | |
| 17 | package android.app; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.media.MediaRouter; |
| 21 | import android.media.MediaRouter.RouteInfo; |
| 22 | import android.util.Log; |
| 23 | import android.view.ActionProvider; |
| 24 | import android.view.MenuItem; |
| 25 | import android.view.View; |
| 26 | |
| 27 | public 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 | |
| 37 | public MediaRouteActionProvider(Context context) { |
| 38 | super(context); |
| 39 | mContext = context; |
| 40 | mRouter = MediaRouter.forApplication(context); |
| 41 | |
| 42 | // Start with live audio by default. |
| 43 | // TODO Update this when new route types are added; segment by API level |
| 44 | // when different route types were added. |
| 45 | setRouteTypes(MediaRouter.ROUTE_TYPE_LIVE_AUDIO); |
| 46 | } |
| 47 | |
| 48 | public void setRouteTypes(int types) { |
| 49 | if (types == mRouteTypes) { |
| 50 | // Already registered; nothing to do. |
| 51 | return; |
| 52 | } |
| 53 | if (mRouteTypes != 0) { |
| 54 | mRouter.removeCallback(mRouterCallback); |
| 55 | } |
| 56 | mRouteTypes = types; |
| 57 | if (mView != null) { |
| 58 | mView.setRouteTypes(mRouteTypes); |
| 59 | } |
| 60 | mRouter.addCallback(types, mRouterCallback); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public View onCreateActionView() { |
| 65 | throw new UnsupportedOperationException("Use onCreateActionView(MenuItem) instead."); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public View onCreateActionView(MenuItem item) { |
| 70 | if (mMenuItem != null || mView != null) { |
| 71 | Log.e(TAG, "onCreateActionView: this ActionProvider is already associated " + |
| 72 | "with a menu item. Don't reuse MediaRouteActionProvider instances! " + |
| 73 | "Abandoning the old one..."); |
| 74 | } |
| 75 | mMenuItem = item; |
| 76 | mView = new MediaRouteButton(mContext); |
| 77 | mMenuItem.setVisible(mRouter.getRouteCount() > 1); |
| 78 | mView.setRouteTypes(mRouteTypes); |
| 79 | return mView; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public boolean onPerformDefaultAction() { |
| 84 | // Show routing dialog |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | private class RouterCallback extends MediaRouter.SimpleCallback { |
| 89 | @Override |
Adam Powell | d0d2cda | 2012-06-08 14:46:35 -0700 | [diff] [blame^] | 90 | public void onRouteAdded(MediaRouter router, RouteInfo info) { |
Adam Powell | 690ffb4 | 2012-06-04 19:22:45 -0700 | [diff] [blame] | 91 | mMenuItem.setVisible(mRouter.getRouteCount() > 1); |
| 92 | } |
| 93 | |
| 94 | @Override |
Adam Powell | d0d2cda | 2012-06-08 14:46:35 -0700 | [diff] [blame^] | 95 | public void onRouteRemoved(MediaRouter router, RouteInfo info) { |
Adam Powell | 690ffb4 | 2012-06-04 19:22:45 -0700 | [diff] [blame] | 96 | mMenuItem.setVisible(mRouter.getRouteCount() > 1); |
| 97 | } |
| 98 | } |
| 99 | } |