• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.support.v7.app;
18 
19 import android.app.Dialog;
20 import android.content.Context;
21 import android.content.res.Configuration;
22 import android.os.Bundle;
23 import android.support.v4.app.DialogFragment;
24 import android.support.v7.media.MediaRouteSelector;
25 
26 /**
27  * Media route chooser dialog fragment.
28  * <p>
29  * Creates a {@link MediaRouteChooserDialog}.  The application may subclass
30  * this dialog fragment to customize the media route chooser dialog.
31  * </p>
32  */
33 public class MediaRouteChooserDialogFragment extends DialogFragment {
34     private final String ARGUMENT_SELECTOR = "selector";
35 
36     private MediaRouteChooserDialog mDialog;
37     private MediaRouteSelector mSelector;
38 
39     /**
40      * Creates a media route chooser dialog fragment.
41      * <p>
42      * All subclasses of this class must also possess a default constructor.
43      * </p>
44      */
MediaRouteChooserDialogFragment()45     public MediaRouteChooserDialogFragment() {
46         setCancelable(true);
47     }
48 
49     /**
50      * Gets the media route selector for filtering the routes that the user can select.
51      *
52      * @return The selector, never null.
53      */
getRouteSelector()54     public MediaRouteSelector getRouteSelector() {
55         ensureRouteSelector();
56         return mSelector;
57     }
58 
ensureRouteSelector()59     private void ensureRouteSelector() {
60         if (mSelector == null) {
61             Bundle args = getArguments();
62             if (args != null) {
63                 mSelector = MediaRouteSelector.fromBundle(args.getBundle(ARGUMENT_SELECTOR));
64             }
65             if (mSelector == null) {
66                 mSelector = MediaRouteSelector.EMPTY;
67             }
68         }
69     }
70 
71     /**
72      * Sets the media route selector for filtering the routes that the user can select.
73      * This method must be called before the fragment is added.
74      *
75      * @param selector The selector to set.
76      */
setRouteSelector(MediaRouteSelector selector)77     public void setRouteSelector(MediaRouteSelector selector) {
78         if (selector == null) {
79             throw new IllegalArgumentException("selector must not be null");
80         }
81 
82         ensureRouteSelector();
83         if (!mSelector.equals(selector)) {
84             mSelector = selector;
85 
86             Bundle args = getArguments();
87             if (args == null) {
88                 args = new Bundle();
89             }
90             args.putBundle(ARGUMENT_SELECTOR, selector.asBundle());
91             setArguments(args);
92 
93             MediaRouteChooserDialog dialog = (MediaRouteChooserDialog)getDialog();
94             if (dialog != null) {
95                 dialog.setRouteSelector(selector);
96             }
97         }
98     }
99 
100     /**
101      * Called when the chooser dialog is being created.
102      * <p>
103      * Subclasses may override this method to customize the dialog.
104      * </p>
105      */
onCreateChooserDialog( Context context, Bundle savedInstanceState)106     public MediaRouteChooserDialog onCreateChooserDialog(
107             Context context, Bundle savedInstanceState) {
108         return new MediaRouteChooserDialog(context);
109     }
110 
111     @Override
onCreateDialog(Bundle savedInstanceState)112     public Dialog onCreateDialog(Bundle savedInstanceState) {
113         mDialog = onCreateChooserDialog(getContext(), savedInstanceState);
114         mDialog.setRouteSelector(getRouteSelector());
115         return mDialog;
116     }
117 
118     @Override
onConfigurationChanged(Configuration newConfig)119     public void onConfigurationChanged(Configuration newConfig) {
120         super.onConfigurationChanged(newConfig);
121         if (mDialog != null) {
122             mDialog.updateLayout();
123         }
124     }
125 }
126