MediaRouteChooserDialogFragment.java revision 0abd3a6ce83ed23abe614155e776b600ef2a66c3
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 com.android.internal.app;
18
19import android.app.Dialog;
20import android.app.DialogFragment;
21import android.content.Context;
22import android.os.Bundle;
23import android.view.View;
24import android.view.View.OnClickListener;
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 * TODO: Move this back into the API, as in the support library media router.
34 */
35public class MediaRouteChooserDialogFragment extends DialogFragment {
36    private final String ARGUMENT_ROUTE_TYPES = "routeTypes";
37
38    private View.OnClickListener mExtendedSettingsClickListener;
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        setStyle(STYLE_NORMAL, android.R.style.Theme_DeviceDefault_Dialog);
49    }
50
51    public int getRouteTypes() {
52        Bundle args = getArguments();
53        return args != null ? args.getInt(ARGUMENT_ROUTE_TYPES) : 0;
54    }
55
56    public void setRouteTypes(int types) {
57        if (types != getRouteTypes()) {
58            Bundle args = getArguments();
59            if (args == null) {
60                args = new Bundle();
61            }
62            args.putInt(ARGUMENT_ROUTE_TYPES, types);
63            setArguments(args);
64
65            MediaRouteChooserDialog dialog = (MediaRouteChooserDialog)getDialog();
66            if (dialog != null) {
67                dialog.setRouteTypes(types);
68            }
69        }
70    }
71
72    public void setExtendedSettingsClickListener(View.OnClickListener listener) {
73        if (listener != mExtendedSettingsClickListener) {
74            mExtendedSettingsClickListener = listener;
75
76            MediaRouteChooserDialog dialog = (MediaRouteChooserDialog)getDialog();
77            if (dialog != null) {
78                dialog.setExtendedSettingsClickListener(listener);
79            }
80        }
81    }
82
83    /**
84     * Called when the chooser dialog is being created.
85     * <p>
86     * Subclasses may override this method to customize the dialog.
87     * </p>
88     */
89    public MediaRouteChooserDialog onCreateChooserDialog(
90            Context context, Bundle savedInstanceState) {
91        return new MediaRouteChooserDialog(context, getTheme());
92    }
93
94    @Override
95    public Dialog onCreateDialog(Bundle savedInstanceState) {
96        MediaRouteChooserDialog dialog = onCreateChooserDialog(getActivity(), savedInstanceState);
97        dialog.setRouteTypes(getRouteTypes());
98        dialog.setExtendedSettingsClickListener(mExtendedSettingsClickListener);
99        return dialog;
100    }
101}
102