Home | History | Annotate | Download | only in app
      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 
     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     public static Dialog createDialog(Context context,
     70             int routeTypes, View.OnClickListener extendedSettingsClickListener) {
     71         final MediaRouter router = (MediaRouter)context.getSystemService(
     72                 Context.MEDIA_ROUTER_SERVICE);
     73 
     74         MediaRouter.RouteInfo route = router.getSelectedRoute();
     75         if (route.isDefault() || !route.matchesTypes(routeTypes)) {
     76             final MediaRouteChooserDialog d = new MediaRouteChooserDialog(
     77                     context, android.R.style.Theme_DeviceDefault_Dialog);
     78             d.setRouteTypes(routeTypes);
     79             d.setExtendedSettingsClickListener(extendedSettingsClickListener);
     80             return d;
     81         } else {
     82             MediaRouteControllerDialog d = new MediaRouteControllerDialog(
     83                     context, android.R.style.Theme_DeviceDefault_Dialog);
     84             return d;
     85         }
     86     }
     87 }
     88