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