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        setCancelable(true);
47        setStyle(STYLE_NORMAL, android.R.style.Theme_DeviceDefault_Dialog);
48    }
49
50    public int getRouteTypes() {
51        Bundle args = getArguments();
52        return args != null ? args.getInt(ARGUMENT_ROUTE_TYPES) : 0;
53    }
54
55    public void setRouteTypes(int types) {
56        if (types != getRouteTypes()) {
57            Bundle args = getArguments();
58            if (args == null) {
59                args = new Bundle();
60            }
61            args.putInt(ARGUMENT_ROUTE_TYPES, types);
62            setArguments(args);
63
64            MediaRouteChooserDialog dialog = (MediaRouteChooserDialog)getDialog();
65            if (dialog != null) {
66                dialog.setRouteTypes(types);
67            }
68        }
69    }
70
71    public void setExtendedSettingsClickListener(View.OnClickListener listener) {
72        if (listener != mExtendedSettingsClickListener) {
73            mExtendedSettingsClickListener = listener;
74
75            MediaRouteChooserDialog dialog = (MediaRouteChooserDialog)getDialog();
76            if (dialog != null) {
77                dialog.setExtendedSettingsClickListener(listener);
78            }
79        }
80    }
81
82    /**
83     * Called when the chooser dialog is being created.
84     * <p>
85     * Subclasses may override this method to customize the dialog.
86     * </p>
87     */
88    public MediaRouteChooserDialog onCreateChooserDialog(
89            Context context, Bundle savedInstanceState) {
90        return new MediaRouteChooserDialog(context, getTheme());
91    }
92
93    @Override
94    public Dialog onCreateDialog(Bundle savedInstanceState) {
95        MediaRouteChooserDialog dialog = onCreateChooserDialog(getActivity(), savedInstanceState);
96        dialog.setRouteTypes(getRouteTypes());
97        dialog.setExtendedSettingsClickListener(mExtendedSettingsClickListener);
98        return dialog;
99    }
100}
101