Juan Lang | b9f11eb | 2017-05-10 17:04:29 -0700 | [diff] [blame] | 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; |
Salvador Martinez | 3e70811 | 2018-04-13 13:37:44 -0700 | [diff] [blame^] | 31 | private DialogInterface.OnShowListener mOnShowListener; |
Juan Lang | b9f11eb | 2017-05-10 17:04:29 -0700 | [diff] [blame] | 32 | |
| 33 | public CustomDialogPreference(Context context, AttributeSet attrs, int defStyleAttr, |
| 34 | int defStyleRes) { |
| 35 | super(context, attrs, defStyleAttr, defStyleRes); |
| 36 | } |
| 37 | |
| 38 | public CustomDialogPreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 39 | super(context, attrs, defStyleAttr); |
| 40 | } |
| 41 | |
| 42 | public CustomDialogPreference(Context context, AttributeSet attrs) { |
| 43 | super(context, attrs); |
| 44 | } |
| 45 | |
| 46 | public CustomDialogPreference(Context context) { |
| 47 | super(context); |
| 48 | } |
| 49 | |
| 50 | public boolean isDialogOpen() { |
| 51 | return getDialog() != null && getDialog().isShowing(); |
| 52 | } |
| 53 | |
| 54 | public Dialog getDialog() { |
| 55 | return mFragment != null ? mFragment.getDialog() : null; |
| 56 | } |
| 57 | |
Salvador Martinez | 3e70811 | 2018-04-13 13:37:44 -0700 | [diff] [blame^] | 58 | public void setOnShowListener(DialogInterface.OnShowListener listner) { |
| 59 | mOnShowListener = listner; |
| 60 | } |
| 61 | |
Juan Lang | b9f11eb | 2017-05-10 17:04:29 -0700 | [diff] [blame] | 62 | protected void onPrepareDialogBuilder(AlertDialog.Builder builder, |
| 63 | DialogInterface.OnClickListener listener) { |
| 64 | } |
| 65 | |
| 66 | protected void onDialogClosed(boolean positiveResult) { |
| 67 | } |
| 68 | |
| 69 | protected void onClick(DialogInterface dialog, int which) { |
| 70 | } |
| 71 | |
| 72 | protected void onBindDialogView(View view) { |
| 73 | } |
| 74 | |
| 75 | private void setFragment(CustomPreferenceDialogFragment fragment) { |
| 76 | mFragment = fragment; |
| 77 | } |
| 78 | |
Salvador Martinez | 3e70811 | 2018-04-13 13:37:44 -0700 | [diff] [blame^] | 79 | private DialogInterface.OnShowListener getOnShowListener() { |
| 80 | return mOnShowListener; |
| 81 | } |
| 82 | |
Juan Lang | b9f11eb | 2017-05-10 17:04:29 -0700 | [diff] [blame] | 83 | public static class CustomPreferenceDialogFragment extends PreferenceDialogFragment { |
| 84 | |
| 85 | public static CustomPreferenceDialogFragment newInstance(String key) { |
| 86 | final CustomPreferenceDialogFragment fragment = new CustomPreferenceDialogFragment(); |
| 87 | final Bundle b = new Bundle(1); |
| 88 | b.putString(ARG_KEY, key); |
| 89 | fragment.setArguments(b); |
| 90 | return fragment; |
| 91 | } |
| 92 | |
| 93 | private CustomDialogPreference getCustomizablePreference() { |
| 94 | return (CustomDialogPreference) getPreference(); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { |
| 99 | super.onPrepareDialogBuilder(builder); |
| 100 | getCustomizablePreference().setFragment(this); |
| 101 | getCustomizablePreference().onPrepareDialogBuilder(builder, this); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void onDialogClosed(boolean positiveResult) { |
| 106 | getCustomizablePreference().onDialogClosed(positiveResult); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | protected void onBindDialogView(View view) { |
| 111 | super.onBindDialogView(view); |
| 112 | getCustomizablePreference().onBindDialogView(view); |
| 113 | } |
| 114 | |
| 115 | @Override |
Salvador Martinez | 3e70811 | 2018-04-13 13:37:44 -0700 | [diff] [blame^] | 116 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 117 | final Dialog dialog = super.onCreateDialog(savedInstanceState); |
| 118 | dialog.setOnShowListener(getCustomizablePreference().getOnShowListener()); |
| 119 | return dialog; |
| 120 | } |
| 121 | |
| 122 | @Override |
Juan Lang | b9f11eb | 2017-05-10 17:04:29 -0700 | [diff] [blame] | 123 | public void onClick(DialogInterface dialog, int which) { |
| 124 | super.onClick(dialog, which); |
| 125 | getCustomizablePreference().onClick(dialog, which); |
| 126 | } |
| 127 | } |
| 128 | } |