1 /*
2  * Copyright (C) 2024 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 
17 package com.android.settings.connecteddevice.audiosharing;
18 
19 import static android.view.View.GONE;
20 import static android.view.View.VISIBLE;
21 
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.util.AttributeSet;
25 import android.util.Log;
26 import android.view.View;
27 import android.widget.CheckBox;
28 import android.widget.EditText;
29 
30 import androidx.annotation.NonNull;
31 import androidx.annotation.Nullable;
32 import androidx.appcompat.app.AlertDialog;
33 
34 import com.android.settings.R;
35 import com.android.settings.widget.ValidatedEditTextPreference;
36 import com.android.settingslib.utils.ColorUtil;
37 
38 public class AudioSharingPasswordPreference extends ValidatedEditTextPreference {
39     private static final String TAG = "AudioSharingPasswordPreference";
40     @Nullable private OnDialogEventListener mOnDialogEventListener;
41     @Nullable private EditText mEditText;
42     @Nullable private CheckBox mCheckBox;
43     @Nullable private View mDialogMessage;
44     private boolean mEditable = true;
45 
46     interface OnDialogEventListener {
onBindDialogView()47         void onBindDialogView();
48 
onPreferenceDataChanged(@onNull String editTextValue, boolean checkBoxValue)49         void onPreferenceDataChanged(@NonNull String editTextValue, boolean checkBoxValue);
50     }
51 
setOnDialogEventListener(OnDialogEventListener listener)52     void setOnDialogEventListener(OnDialogEventListener listener) {
53         mOnDialogEventListener = listener;
54     }
55 
AudioSharingPasswordPreference( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)56     public AudioSharingPasswordPreference(
57             Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
58         super(context, attrs, defStyleAttr, defStyleRes);
59     }
60 
AudioSharingPasswordPreference(Context context, AttributeSet attrs, int defStyleAttr)61     public AudioSharingPasswordPreference(Context context, AttributeSet attrs, int defStyleAttr) {
62         super(context, attrs, defStyleAttr);
63     }
64 
AudioSharingPasswordPreference(Context context, AttributeSet attrs)65     public AudioSharingPasswordPreference(Context context, AttributeSet attrs) {
66         super(context, attrs);
67     }
68 
AudioSharingPasswordPreference(Context context)69     public AudioSharingPasswordPreference(Context context) {
70         super(context);
71     }
72 
73     @Override
onBindDialogView(View view)74     protected void onBindDialogView(View view) {
75         super.onBindDialogView(view);
76 
77         mEditText = view.findViewById(android.R.id.edit);
78         mCheckBox = view.findViewById(R.id.audio_sharing_stream_password_checkbox);
79         mDialogMessage = view.findViewById(android.R.id.message);
80 
81         if (mEditText == null || mCheckBox == null || mDialogMessage == null) {
82             Log.w(TAG, "onBindDialogView() : Invalid layout");
83             return;
84         }
85 
86         mCheckBox.setOnCheckedChangeListener((unused, checked) -> setEditTextEnabled(!checked));
87         if (mOnDialogEventListener != null) {
88             mOnDialogEventListener.onBindDialogView();
89         }
90     }
91 
92     @Override
onPrepareDialogBuilder( AlertDialog.Builder builder, DialogInterface.OnClickListener listener)93     protected void onPrepareDialogBuilder(
94             AlertDialog.Builder builder, DialogInterface.OnClickListener listener) {
95         if (!mEditable) {
96             builder.setPositiveButton(null, null);
97         }
98     }
99 
100     @Override
onClick(DialogInterface dialog, int which)101     protected void onClick(DialogInterface dialog, int which) {
102         if (mEditText == null || mCheckBox == null) {
103             Log.w(TAG, "onClick() : Invalid layout");
104             return;
105         }
106 
107         if (mOnDialogEventListener != null
108                 && which == DialogInterface.BUTTON_POSITIVE
109                 && mEditText.getText() != null) {
110             mOnDialogEventListener.onPreferenceDataChanged(
111                     mEditText.getText().toString(), mCheckBox.isChecked());
112         }
113     }
114 
setEditable(boolean editable)115     void setEditable(boolean editable) {
116         if (mEditText == null || mCheckBox == null || mDialogMessage == null) {
117             Log.w(TAG, "setEditable() : Invalid layout");
118             return;
119         }
120         mEditable = editable;
121         setEditTextEnabled(editable);
122         mCheckBox.setEnabled(editable);
123         mDialogMessage.setVisibility(editable ? GONE : VISIBLE);
124     }
125 
setChecked(boolean checked)126     void setChecked(boolean checked) {
127         if (mCheckBox == null) {
128             Log.w(TAG, "setChecked() : Invalid layout");
129             return;
130         }
131         mCheckBox.setChecked(checked);
132     }
133 
setEditTextEnabled(boolean enabled)134     private void setEditTextEnabled(boolean enabled) {
135         if (mEditText == null) {
136             Log.w(TAG, "setEditTextEnabled() : Invalid layout");
137             return;
138         }
139         mEditText.setEnabled(enabled);
140         mEditText.setAlpha(enabled ? 1.0f : ColorUtil.getDisabledAlpha(getContext()));
141     }
142 }
143