1 /*
2  * Copyright (C) 2013 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 com.android.internal.app;
18 
19 
20 import android.app.Activity;
21 import android.app.Dialog;
22 import android.app.DialogFragment;
23 import android.app.FragmentManager;
24 import android.content.Context;
25 import android.media.MediaRouter;
26 import android.util.Log;
27 import android.view.View;
28 
29 /**
30  * Shows media route dialog as appropriate.
31  * @hide
32  */
33 public abstract class MediaRouteDialogPresenter {
34     private static final String TAG = "MediaRouter";
35 
36     private static final String CHOOSER_FRAGMENT_TAG =
37             "android.app.MediaRouteButton:MediaRouteChooserDialogFragment";
38     private static final String CONTROLLER_FRAGMENT_TAG =
39             "android.app.MediaRouteButton:MediaRouteControllerDialogFragment";
40 
showDialogFragment(Activity activity, int routeTypes, View.OnClickListener extendedSettingsClickListener)41     public static DialogFragment showDialogFragment(Activity activity,
42             int routeTypes, View.OnClickListener extendedSettingsClickListener) {
43         final MediaRouter router = (MediaRouter)activity.getSystemService(
44                 Context.MEDIA_ROUTER_SERVICE);
45         final FragmentManager fm = activity.getFragmentManager();
46 
47         MediaRouter.RouteInfo route = router.getSelectedRoute();
48         if (route.isDefault() || !route.matchesTypes(routeTypes)) {
49             if (fm.findFragmentByTag(CHOOSER_FRAGMENT_TAG) != null) {
50                 Log.w(TAG, "showDialog(): Route chooser dialog already showing!");
51                 return null;
52             }
53             MediaRouteChooserDialogFragment f = new MediaRouteChooserDialogFragment();
54             f.setRouteTypes(routeTypes);
55             f.setExtendedSettingsClickListener(extendedSettingsClickListener);
56             f.show(fm, CHOOSER_FRAGMENT_TAG);
57             return f;
58         } else {
59             if (fm.findFragmentByTag(CONTROLLER_FRAGMENT_TAG) != null) {
60                 Log.w(TAG, "showDialog(): Route controller dialog already showing!");
61                 return null;
62             }
63             MediaRouteControllerDialogFragment f = new MediaRouteControllerDialogFragment();
64             f.show(fm, CONTROLLER_FRAGMENT_TAG);
65             return f;
66         }
67     }
68 
69     /** Create a media route dialog as appropriate. */
createDialog(Context context, int routeTypes, View.OnClickListener extendedSettingsClickListener)70     public static Dialog createDialog(Context context,
71             int routeTypes, View.OnClickListener extendedSettingsClickListener) {
72         int theme = MediaRouteChooserDialog.isLightTheme(context)
73                 ? android.R.style.Theme_DeviceDefault_Light_Dialog
74                 : android.R.style.Theme_DeviceDefault_Dialog;
75         return createDialog(context, routeTypes, extendedSettingsClickListener, theme);
76     }
77 
78     /** Create a media route dialog as appropriate. */
createDialog(Context context, int routeTypes, View.OnClickListener extendedSettingsClickListener, int theme)79     public static Dialog createDialog(Context context,
80             int routeTypes, View.OnClickListener extendedSettingsClickListener, int theme) {
81         return createDialog(context, routeTypes, extendedSettingsClickListener, theme,
82                 true /* showProgressBarWhenEmpty */);
83     }
84 
85     /** Create a media route dialog as appropriate. */
createDialog(Context context, int routeTypes, View.OnClickListener extendedSettingsClickListener, int theme, boolean showProgressBarWhenEmpty)86     public static Dialog createDialog(Context context, int routeTypes,
87             View.OnClickListener extendedSettingsClickListener, int theme,
88             boolean showProgressBarWhenEmpty) {
89         final MediaRouter router = context.getSystemService(MediaRouter.class);
90 
91         MediaRouter.RouteInfo route = router.getSelectedRoute();
92         if (route.isDefault() || !route.matchesTypes(routeTypes)) {
93             final MediaRouteChooserDialog d = new MediaRouteChooserDialog(context, theme,
94                     showProgressBarWhenEmpty);
95             d.setRouteTypes(routeTypes);
96             d.setExtendedSettingsClickListener(extendedSettingsClickListener);
97             return d;
98         } else {
99             return new MediaRouteControllerDialog(context, theme);
100         }
101     }
102 }
103