Home | History | Annotate | Download | only in settingslib
      1 /*
      2  * Copyright (C) 2018 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.Dialog;
     19 import android.content.Context;
     20 import android.content.DialogInterface;
     21 import android.os.Bundle;
     22 import android.util.AttributeSet;
     23 import android.view.View;
     24 
     25 import androidx.appcompat.app.AlertDialog;
     26 import androidx.preference.DialogPreference;
     27 import androidx.preference.PreferenceDialogFragmentCompat;
     28 
     29 public class CustomDialogPreferenceCompat extends DialogPreference {
     30 
     31     private CustomPreferenceDialogFragment mFragment;
     32     private DialogInterface.OnShowListener mOnShowListener;
     33 
     34     public CustomDialogPreferenceCompat(Context context, AttributeSet attrs, int defStyleAttr,
     35             int defStyleRes) {
     36         super(context, attrs, defStyleAttr, defStyleRes);
     37     }
     38 
     39     public CustomDialogPreferenceCompat(Context context, AttributeSet attrs, int defStyleAttr) {
     40         super(context, attrs, defStyleAttr);
     41     }
     42 
     43     public CustomDialogPreferenceCompat(Context context, AttributeSet attrs) {
     44         super(context, attrs);
     45     }
     46 
     47     public CustomDialogPreferenceCompat(Context context) {
     48         super(context);
     49     }
     50 
     51     public boolean isDialogOpen() {
     52         return getDialog() != null && getDialog().isShowing();
     53     }
     54 
     55     public Dialog getDialog() {
     56         return mFragment != null ? mFragment.getDialog() : null;
     57     }
     58 
     59     public void setOnShowListener(DialogInterface.OnShowListener listner) {
     60         mOnShowListener = listner;
     61     }
     62 
     63     protected void onPrepareDialogBuilder(AlertDialog.Builder builder,
     64             DialogInterface.OnClickListener listener) {
     65     }
     66 
     67     protected void onDialogClosed(boolean positiveResult) {
     68     }
     69 
     70     protected void onClick(DialogInterface dialog, int which) {
     71     }
     72 
     73     protected void onBindDialogView(View view) {
     74     }
     75 
     76     private void setFragment(CustomPreferenceDialogFragment fragment) {
     77         mFragment = fragment;
     78     }
     79 
     80     private DialogInterface.OnShowListener getOnShowListener() {
     81         return mOnShowListener;
     82     }
     83 
     84     public static class CustomPreferenceDialogFragment extends PreferenceDialogFragmentCompat {
     85 
     86         public static CustomPreferenceDialogFragment newInstance(String key) {
     87             final CustomPreferenceDialogFragment fragment = new CustomPreferenceDialogFragment();
     88             final Bundle b = new Bundle(1);
     89             b.putString(ARG_KEY, key);
     90             fragment.setArguments(b);
     91             return fragment;
     92         }
     93 
     94         private CustomDialogPreferenceCompat getCustomizablePreference() {
     95             return (CustomDialogPreferenceCompat) getPreference();
     96         }
     97 
     98         @Override
     99         protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
    100             super.onPrepareDialogBuilder(builder);
    101             getCustomizablePreference().setFragment(this);
    102             getCustomizablePreference().onPrepareDialogBuilder(builder, this);
    103         }
    104 
    105         @Override
    106         public void onDialogClosed(boolean positiveResult) {
    107             getCustomizablePreference().onDialogClosed(positiveResult);
    108         }
    109 
    110         @Override
    111         protected void onBindDialogView(View view) {
    112             super.onBindDialogView(view);
    113             getCustomizablePreference().onBindDialogView(view);
    114         }
    115 
    116         @Override
    117         public Dialog onCreateDialog(Bundle savedInstanceState) {
    118             final Dialog dialog = super.onCreateDialog(savedInstanceState);
    119             dialog.setOnShowListener(getCustomizablePreference().getOnShowListener());
    120             return dialog;
    121         }
    122 
    123         @Override
    124         public void onClick(DialogInterface dialog, int which) {
    125             super.onClick(dialog, which);
    126             getCustomizablePreference().onClick(dialog, which);
    127         }
    128     }
    129 }
    130