2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.settings;
18 import android.app.AlertDialog;
19 import android.app.Dialog;
20 import android.content.Context;
21 import android.content.DialogInterface;
22 import android.os.Bundle;
23 import android.support.v14.preference.PreferenceDialogFragment;
24 import android.support.v7.preference.DialogPreference;
25 import android.util.AttributeSet;
26 import android.view.View;
28 public class CustomDialogPreference extends DialogPreference {
30 private CustomPreferenceDialogFragment mFragment;
32 public CustomDialogPreference(Context context, AttributeSet attrs, int defStyleAttr,
34 super(context, attrs, defStyleAttr, defStyleRes);
37 public CustomDialogPreference(Context context, AttributeSet attrs, int defStyleAttr) {
38 super(context, attrs, defStyleAttr);
41 public CustomDialogPreference(Context context, AttributeSet attrs) {
42 super(context, attrs);
45 public CustomDialogPreference(Context context) {
49 public boolean isDialogOpen() {
50 return getDialog() != null && getDialog().isShowing();
53 public Dialog getDialog() {
54 return mFragment != null ? mFragment.getDialog() : null;
57 protected void onPrepareDialogBuilder(AlertDialog.Builder builder,
58 DialogInterface.OnClickListener listener) {
61 protected void onDialogClosed(boolean positiveResult) {
64 protected void onClick(DialogInterface dialog, int which) {
67 protected void onBindDialogView(View view) {
70 private void setFragment(CustomPreferenceDialogFragment fragment) {
74 public static class CustomPreferenceDialogFragment extends PreferenceDialogFragment {
76 public static CustomPreferenceDialogFragment newInstance(String key) {
77 final CustomPreferenceDialogFragment fragment = new CustomPreferenceDialogFragment();
78 final Bundle b = new Bundle(1);
79 b.putString(ARG_KEY, key);
80 fragment.setArguments(b);
84 private CustomDialogPreference getCustomizablePreference() {
85 return (CustomDialogPreference) getPreference();
89 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
90 super.onPrepareDialogBuilder(builder);
91 getCustomizablePreference().setFragment(this);
92 getCustomizablePreference().onPrepareDialogBuilder(builder, this);
96 public void onDialogClosed(boolean positiveResult) {
97 getCustomizablePreference().onDialogClosed(positiveResult);
101 protected void onBindDialogView(View view) {
102 super.onBindDialogView(view);
103 getCustomizablePreference().onBindDialogView(view);
107 public void onClick(DialogInterface dialog, int which) {
108 super.onClick(dialog, which);
109 getCustomizablePreference().onClick(dialog, which);