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