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