blob: 4860182aa4d5ec3d5b8ef44dae236bc6aefcb94f [file] [log] [blame]
Adam Powell690ffb42012-06-04 19:22:45 -07001/*
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
Adam Powell70e11e52012-06-12 16:59:45 -070019import com.android.internal.app.MediaRouteChooserDialogFragment;
20
Adam Powell690ffb42012-06-04 19:22:45 -070021import android.content.Context;
Adam Powell70e11e52012-06-12 16:59:45 -070022import android.content.ContextWrapper;
Adam Powell690ffb42012-06-04 19:22:45 -070023import android.media.MediaRouter;
24import android.media.MediaRouter.RouteInfo;
25import android.util.Log;
26import android.view.ActionProvider;
27import android.view.MenuItem;
28import android.view.View;
29
30public 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 Powellb35c4452012-06-12 11:25:54 -070039 private View.OnClickListener mExtendedSettingsListener;
Adam Powell690ffb42012-06-04 19:22:45 -070040
41 public MediaRouteActionProvider(Context context) {
42 super(context);
43 mContext = context;
Adam Powellb35c4452012-06-12 11:25:54 -070044 mRouter = (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE);
Adam Powell690ffb42012-06-04 19:22:45 -070045
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 Powellb35c4452012-06-12 11:25:54 -070083 mView.setExtendedSettingsClickListener(mExtendedSettingsListener);
Adam Powell690ffb42012-06-04 19:22:45 -070084 return mView;
85 }
86
87 @Override
88 public boolean onPerformDefaultAction() {
Adam Powell70e11e52012-06-12 16:59:45 -070089 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 Powell690ffb42012-06-04 19:22:45 -0700103 return true;
104 }
105
Adam Powell70e11e52012-06-12 16:59:45 -0700106 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 Powellb35c4452012-06-12 11:25:54 -0700120 public void setExtendedSettingsClickListener(View.OnClickListener listener) {
121 mExtendedSettingsListener = listener;
122 if (mView != null) {
123 mView.setExtendedSettingsClickListener(listener);
124 }
125 }
126
Adam Powell690ffb42012-06-04 19:22:45 -0700127 private class RouterCallback extends MediaRouter.SimpleCallback {
128 @Override
Adam Powelld0d2cda2012-06-08 14:46:35 -0700129 public void onRouteAdded(MediaRouter router, RouteInfo info) {
Adam Powell690ffb42012-06-04 19:22:45 -0700130 mMenuItem.setVisible(mRouter.getRouteCount() > 1);
131 }
132
133 @Override
Adam Powelld0d2cda2012-06-08 14:46:35 -0700134 public void onRouteRemoved(MediaRouter router, RouteInfo info) {
Adam Powell690ffb42012-06-04 19:22:45 -0700135 mMenuItem.setVisible(mRouter.getRouteCount() > 1);
136 }
137 }
138}