1 /* 2 * Copyright (C) 2015 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 package com.android.settingslib; 17 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; 27 28 public class CustomDialogPreference extends DialogPreference { 29 30 private CustomPreferenceDialogFragment mFragment; 31 CustomDialogPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)32 public CustomDialogPreference(Context context, AttributeSet attrs, int defStyleAttr, 33 int defStyleRes) { 34 super(context, attrs, defStyleAttr, defStyleRes); 35 } 36 CustomDialogPreference(Context context, AttributeSet attrs, int defStyleAttr)37 public CustomDialogPreference(Context context, AttributeSet attrs, int defStyleAttr) { 38 super(context, attrs, defStyleAttr); 39 } 40 CustomDialogPreference(Context context, AttributeSet attrs)41 public CustomDialogPreference(Context context, AttributeSet attrs) { 42 super(context, attrs); 43 } 44 CustomDialogPreference(Context context)45 public CustomDialogPreference(Context context) { 46 super(context); 47 } 48 isDialogOpen()49 public boolean isDialogOpen() { 50 return getDialog() != null && getDialog().isShowing(); 51 } 52 getDialog()53 public Dialog getDialog() { 54 return mFragment != null ? mFragment.getDialog() : null; 55 } 56 onPrepareDialogBuilder(AlertDialog.Builder builder, DialogInterface.OnClickListener listener)57 protected void onPrepareDialogBuilder(AlertDialog.Builder builder, 58 DialogInterface.OnClickListener listener) { 59 } 60 onDialogClosed(boolean positiveResult)61 protected void onDialogClosed(boolean positiveResult) { 62 } 63 onClick(DialogInterface dialog, int which)64 protected void onClick(DialogInterface dialog, int which) { 65 } 66 onBindDialogView(View view)67 protected void onBindDialogView(View view) { 68 } 69 setFragment(CustomPreferenceDialogFragment fragment)70 private void setFragment(CustomPreferenceDialogFragment fragment) { 71 mFragment = fragment; 72 } 73 74 public static class CustomPreferenceDialogFragment extends PreferenceDialogFragment { 75 newInstance(String key)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); 81 return fragment; 82 } 83 getCustomizablePreference()84 private CustomDialogPreference getCustomizablePreference() { 85 return (CustomDialogPreference) getPreference(); 86 } 87 88 @Override onPrepareDialogBuilder(AlertDialog.Builder builder)89 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { 90 super.onPrepareDialogBuilder(builder); 91 getCustomizablePreference().setFragment(this); 92 getCustomizablePreference().onPrepareDialogBuilder(builder, this); 93 } 94 95 @Override onDialogClosed(boolean positiveResult)96 public void onDialogClosed(boolean positiveResult) { 97 getCustomizablePreference().onDialogClosed(positiveResult); 98 } 99 100 @Override onBindDialogView(View view)101 protected void onBindDialogView(View view) { 102 super.onBindDialogView(view); 103 getCustomizablePreference().onBindDialogView(view); 104 } 105 106 @Override onClick(DialogInterface dialog, int which)107 public void onClick(DialogInterface dialog, int which) { 108 super.onClick(dialog, which); 109 getCustomizablePreference().onClick(dialog, which); 110 } 111 } 112 } 113