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