MediaRouteActionProvider.java revision b35c445f34e1a18e17aef3e3dfbc1c39b4d1815c
1690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell/*
2690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell * Copyright (C) 2012 The Android Open Source Project
3690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell *
4690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell * Licensed under the Apache License, Version 2.0 (the "License");
5690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell * you may not use this file except in compliance with the License.
6690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell * You may obtain a copy of the License at
7690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell *
8690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell *      http://www.apache.org/licenses/LICENSE-2.0
9690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell *
10690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell * Unless required by applicable law or agreed to in writing, software
11690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell * distributed under the License is distributed on an "AS IS" BASIS,
12690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell * See the License for the specific language governing permissions and
14690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell * limitations under the License.
15690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell */
16690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
17690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellpackage android.app;
18690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
19690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellimport android.content.Context;
20690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellimport android.media.MediaRouter;
21690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellimport android.media.MediaRouter.RouteInfo;
22690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellimport android.util.Log;
23690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellimport android.view.ActionProvider;
24690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellimport android.view.MenuItem;
25690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellimport android.view.View;
26690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
27690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powellpublic class MediaRouteActionProvider extends ActionProvider {
28690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private static final String TAG = "MediaRouteActionProvider";
29690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
30690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private Context mContext;
31690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private MediaRouter mRouter;
32690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private MenuItem mMenuItem;
33690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private MediaRouteButton mView;
34690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private int mRouteTypes;
35690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private final RouterCallback mRouterCallback = new RouterCallback();
36b35c445f34e1a18e17aef3e3dfbc1c39b4d1815cAdam Powell    private View.OnClickListener mExtendedSettingsListener;
37690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
38690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    public MediaRouteActionProvider(Context context) {
39690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        super(context);
40690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        mContext = context;
41b35c445f34e1a18e17aef3e3dfbc1c39b4d1815cAdam Powell        mRouter = (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE);
42690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
43690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        // Start with live audio by default.
44690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        // TODO Update this when new route types are added; segment by API level
45690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        // when different route types were added.
46690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        setRouteTypes(MediaRouter.ROUTE_TYPE_LIVE_AUDIO);
47690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
48690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
49690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    public void setRouteTypes(int types) {
50690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        if (types == mRouteTypes) {
51690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            // Already registered; nothing to do.
52690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            return;
53690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
54690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        if (mRouteTypes != 0) {
55690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            mRouter.removeCallback(mRouterCallback);
56690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
57690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        mRouteTypes = types;
58690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        if (mView != null) {
59690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            mView.setRouteTypes(mRouteTypes);
60690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
61690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        mRouter.addCallback(types, mRouterCallback);
62690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
63690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
64690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    @Override
65690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    public View onCreateActionView() {
66690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        throw new UnsupportedOperationException("Use onCreateActionView(MenuItem) instead.");
67690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
68690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
69690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    @Override
70690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    public View onCreateActionView(MenuItem item) {
71690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        if (mMenuItem != null || mView != null) {
72690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            Log.e(TAG, "onCreateActionView: this ActionProvider is already associated " +
73690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                    "with a menu item. Don't reuse MediaRouteActionProvider instances! " +
74690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell                    "Abandoning the old one...");
75690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
76690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        mMenuItem = item;
77690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        mView = new MediaRouteButton(mContext);
78690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        mMenuItem.setVisible(mRouter.getRouteCount() > 1);
79690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        mView.setRouteTypes(mRouteTypes);
80b35c445f34e1a18e17aef3e3dfbc1c39b4d1815cAdam Powell        mView.setExtendedSettingsClickListener(mExtendedSettingsListener);
81690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        return mView;
82690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
83690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
84690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    @Override
85690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    public boolean onPerformDefaultAction() {
86690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        // Show routing dialog
87690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        return true;
88690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
89690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
90b35c445f34e1a18e17aef3e3dfbc1c39b4d1815cAdam Powell    public void setExtendedSettingsClickListener(View.OnClickListener listener) {
91b35c445f34e1a18e17aef3e3dfbc1c39b4d1815cAdam Powell        mExtendedSettingsListener = listener;
92b35c445f34e1a18e17aef3e3dfbc1c39b4d1815cAdam Powell        if (mView != null) {
93b35c445f34e1a18e17aef3e3dfbc1c39b4d1815cAdam Powell            mView.setExtendedSettingsClickListener(listener);
94b35c445f34e1a18e17aef3e3dfbc1c39b4d1815cAdam Powell        }
95b35c445f34e1a18e17aef3e3dfbc1c39b4d1815cAdam Powell    }
96b35c445f34e1a18e17aef3e3dfbc1c39b4d1815cAdam Powell
97690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    private class RouterCallback extends MediaRouter.SimpleCallback {
98690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        @Override
99d0d2cda9d414da73773285d7fee9e13aef3495e9Adam Powell        public void onRouteAdded(MediaRouter router, RouteInfo info) {
100690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            mMenuItem.setVisible(mRouter.getRouteCount() > 1);
101690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
102690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell
103690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        @Override
104d0d2cda9d414da73773285d7fee9e13aef3495e9Adam Powell        public void onRouteRemoved(MediaRouter router, RouteInfo info) {
105690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell            mMenuItem.setVisible(mRouter.getRouteCount() > 1);
106690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell        }
107690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell    }
108690ffb4e1f735148a15f2036d9a3c1962fba188cAdam Powell}
109