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 | |
Adam Powell | 70e11e5 | 2012-06-12 16:59:45 -0700 | [diff] [blame^] | 19 | import com.android.internal.app.MediaRouteChooserDialogFragment; |
| 20 | |
Adam Powell | 690ffb4 | 2012-06-04 19:22:45 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Adam Powell | 70e11e5 | 2012-06-12 16:59:45 -0700 | [diff] [blame^] | 22 | import android.content.ContextWrapper; |
Adam Powell | 690ffb4 | 2012-06-04 19:22:45 -0700 | [diff] [blame] | 23 | import android.media.MediaRouter; |
| 24 | import android.media.MediaRouter.RouteInfo; |
| 25 | import android.util.Log; |
| 26 | import android.view.ActionProvider; |
| 27 | import android.view.MenuItem; |
| 28 | import android.view.View; |
| 29 | |
| 30 | public class MediaRouteActionProvider extends ActionProvider { |
| 31 | private static final String TAG = "MediaRouteActionProvider"; |
| 32 | |
| 33 | private Context mContext; |
| 34 | private MediaRouter mRouter; |
| 35 | private MenuItem mMenuItem; |
| 36 | private MediaRouteButton mView; |
| 37 | private int mRouteTypes; |
| 38 | private final RouterCallback mRouterCallback = new RouterCallback(); |
Adam Powell | b35c445 | 2012-06-12 11:25:54 -0700 | [diff] [blame] | 39 | private View.OnClickListener mExtendedSettingsListener; |
Adam Powell | 690ffb4 | 2012-06-04 19:22:45 -0700 | [diff] [blame] | 40 | |
| 41 | public MediaRouteActionProvider(Context context) { |
| 42 | super(context); |
| 43 | mContext = context; |
Adam Powell | b35c445 | 2012-06-12 11:25:54 -0700 | [diff] [blame] | 44 | mRouter = (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE); |
Adam Powell | 690ffb4 | 2012-06-04 19:22:45 -0700 | [diff] [blame] | 45 | |
| 46 | // Start with live audio by default. |
| 47 | // TODO Update this when new route types are added; segment by API level |
| 48 | // when different route types were added. |
| 49 | setRouteTypes(MediaRouter.ROUTE_TYPE_LIVE_AUDIO); |
| 50 | } |
| 51 | |
| 52 | public void setRouteTypes(int types) { |
| 53 | if (types == mRouteTypes) { |
| 54 | // Already registered; nothing to do. |
| 55 | return; |
| 56 | } |
| 57 | if (mRouteTypes != 0) { |
| 58 | mRouter.removeCallback(mRouterCallback); |
| 59 | } |
| 60 | mRouteTypes = types; |
| 61 | if (mView != null) { |
| 62 | mView.setRouteTypes(mRouteTypes); |
| 63 | } |
| 64 | mRouter.addCallback(types, mRouterCallback); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public View onCreateActionView() { |
| 69 | throw new UnsupportedOperationException("Use onCreateActionView(MenuItem) instead."); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public View onCreateActionView(MenuItem item) { |
| 74 | if (mMenuItem != null || mView != null) { |
| 75 | Log.e(TAG, "onCreateActionView: this ActionProvider is already associated " + |
| 76 | "with a menu item. Don't reuse MediaRouteActionProvider instances! " + |
| 77 | "Abandoning the old one..."); |
| 78 | } |
| 79 | mMenuItem = item; |
| 80 | mView = new MediaRouteButton(mContext); |
| 81 | mMenuItem.setVisible(mRouter.getRouteCount() > 1); |
| 82 | mView.setRouteTypes(mRouteTypes); |
Adam Powell | b35c445 | 2012-06-12 11:25:54 -0700 | [diff] [blame] | 83 | mView.setExtendedSettingsClickListener(mExtendedSettingsListener); |
Adam Powell | 690ffb4 | 2012-06-04 19:22:45 -0700 | [diff] [blame] | 84 | return mView; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public boolean onPerformDefaultAction() { |
Adam Powell | 70e11e5 | 2012-06-12 16:59:45 -0700 | [diff] [blame^] | 89 | final FragmentManager fm = getActivity().getFragmentManager(); |
| 90 | // See if one is already attached to this activity. |
| 91 | MediaRouteChooserDialogFragment dialogFragment = |
| 92 | (MediaRouteChooserDialogFragment) fm.findFragmentByTag( |
| 93 | MediaRouteChooserDialogFragment.FRAGMENT_TAG); |
| 94 | if (dialogFragment != null) { |
| 95 | Log.w(TAG, "onPerformDefaultAction(): Chooser dialog already showing!"); |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | dialogFragment = new MediaRouteChooserDialogFragment(); |
| 100 | dialogFragment.setExtendedSettingsClickListener(mExtendedSettingsListener); |
| 101 | dialogFragment.setRouteTypes(mRouteTypes); |
| 102 | dialogFragment.show(fm, MediaRouteChooserDialogFragment.FRAGMENT_TAG); |
Adam Powell | 690ffb4 | 2012-06-04 19:22:45 -0700 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
Adam Powell | 70e11e5 | 2012-06-12 16:59:45 -0700 | [diff] [blame^] | 106 | private Activity getActivity() { |
| 107 | // Gross way of unwrapping the Activity so we can get the FragmentManager |
| 108 | Context context = mContext; |
| 109 | while (context instanceof ContextWrapper && !(context instanceof Activity)) { |
| 110 | context = ((ContextWrapper) context).getBaseContext(); |
| 111 | } |
| 112 | if (!(context instanceof Activity)) { |
| 113 | throw new IllegalStateException("The MediaRouteActionProvider's Context " + |
| 114 | "is not an Activity."); |
| 115 | } |
| 116 | |
| 117 | return (Activity) context; |
| 118 | } |
| 119 | |
Adam Powell | b35c445 | 2012-06-12 11:25:54 -0700 | [diff] [blame] | 120 | public void setExtendedSettingsClickListener(View.OnClickListener listener) { |
| 121 | mExtendedSettingsListener = listener; |
| 122 | if (mView != null) { |
| 123 | mView.setExtendedSettingsClickListener(listener); |
| 124 | } |
| 125 | } |
| 126 | |
Adam Powell | 690ffb4 | 2012-06-04 19:22:45 -0700 | [diff] [blame] | 127 | private class RouterCallback extends MediaRouter.SimpleCallback { |
| 128 | @Override |
Adam Powell | d0d2cda | 2012-06-08 14:46:35 -0700 | [diff] [blame] | 129 | public void onRouteAdded(MediaRouter router, RouteInfo info) { |
Adam Powell | 690ffb4 | 2012-06-04 19:22:45 -0700 | [diff] [blame] | 130 | mMenuItem.setVisible(mRouter.getRouteCount() > 1); |
| 131 | } |
| 132 | |
| 133 | @Override |
Adam Powell | d0d2cda | 2012-06-08 14:46:35 -0700 | [diff] [blame] | 134 | public void onRouteRemoved(MediaRouter router, RouteInfo info) { |
Adam Powell | 690ffb4 | 2012-06-04 19:22:45 -0700 | [diff] [blame] | 135 | mMenuItem.setVisible(mRouter.getRouteCount() > 1); |
| 136 | } |
| 137 | } |
| 138 | } |