• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     @Nullable private View mEditTextFormatAlert;
45     private boolean mEditable = true;
46 
47     interface OnDialogEventListener {
onBindDialogView()48         void onBindDialogView();
49 
onPreferenceDataChanged(@onNull String editTextValue, boolean checkBoxValue)50         void onPreferenceDataChanged(@NonNull String editTextValue, boolean checkBoxValue);
51     }
52 
setOnDialogEventListener(OnDialogEventListener listener)53     void setOnDialogEventListener(OnDialogEventListener listener) {
54         mOnDialogEventListener = listener;
55     }
56 
AudioSharingPasswordPreference( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)57     public AudioSharingPasswordPreference(
58             Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
59         super(context, attrs, defStyleAttr, defStyleRes);
60     }
61 
AudioSharingPasswordPreference(Context context, AttributeSet attrs, int defStyleAttr)62     public AudioSharingPasswordPreference(Context context, AttributeSet attrs, int defStyleAttr) {
63         super(context, attrs, defStyleAttr);
64     }
65 
AudioSharingPasswordPreference(Context context, AttributeSet attrs)66     public AudioSharingPasswordPreference(Context context, AttributeSet attrs) {
67         super(context, attrs);
68     }
69 
AudioSharingPasswordPreference(Context context)70     public AudioSharingPasswordPreference(Context context) {
71         super(context);
72     }
73 
74     @Override
onBindDialogView(View view)75     protected void onBindDialogView(View view) {
76         super.onBindDialogView(view);
77 
78         mEditText = view.findViewById(android.R.id.edit);
79         mCheckBox = view.findViewById(R.id.audio_sharing_stream_password_checkbox);
80         mDialogMessage = view.findViewById(android.R.id.message);
81         mEditTextFormatAlert = view.findViewById(R.id.edit_alert_message);
82 
83         if (mEditText == null || mCheckBox == null || mDialogMessage == null) {
84             Log.w(TAG, "onBindDialogView() : Invalid layout");
85             return;
86         }
87 
88         mCheckBox.setOnCheckedChangeListener((unused, checked) -> setEditTextEnabled(!checked));
89         if (mOnDialogEventListener != null) {
90             mOnDialogEventListener.onBindDialogView();
91         }
92     }
93 
94     @Override
onPrepareDialogBuilder( AlertDialog.Builder builder, DialogInterface.OnClickListener listener)95     protected void onPrepareDialogBuilder(
96             AlertDialog.Builder builder, DialogInterface.OnClickListener listener) {
97         if (!mEditable) {
98             builder.setPositiveButton(null, null);
99         }
100     }
101 
102     @Override
onClick(DialogInterface dialog, int which)103     protected void onClick(DialogInterface dialog, int which) {
104         if (mEditText == null || mCheckBox == null) {
105             Log.w(TAG, "onClick() : Invalid layout");
106             return;
107         }
108 
109         if (mOnDialogEventListener != null
110                 && which == DialogInterface.BUTTON_POSITIVE
111                 && mEditText.getText() != null) {
112             mOnDialogEventListener.onPreferenceDataChanged(
113                     mEditText.getText().toString(), mCheckBox.isChecked());
114         }
115     }
116 
setEditable(boolean editable)117     void setEditable(boolean editable) {
118         if (mEditText == null || mCheckBox == null || mDialogMessage == null) {
119             Log.w(TAG, "setEditable() : Invalid layout");
120             return;
121         }
122         mEditable = editable;
123         setEditTextEnabled(editable);
124         mCheckBox.setEnabled(editable);
125         mDialogMessage.setVisibility(editable ? GONE : VISIBLE);
126     }
127 
showEditTextFormatAlert(boolean show)128     void showEditTextFormatAlert(boolean show) {
129         if (mEditTextFormatAlert == null) {
130             Log.w(TAG, "showEditTextFormatAlert() : Invalid layout");
131             return;
132         }
133         mEditTextFormatAlert.setVisibility(show ? VISIBLE : GONE);
134     }
135 
setChecked(boolean checked)136     void setChecked(boolean checked) {
137         if (mCheckBox == null) {
138             Log.w(TAG, "setChecked() : Invalid layout");
139             return;
140         }
141         mCheckBox.setChecked(checked);
142     }
143 
setEditTextEnabled(boolean enabled)144     private void setEditTextEnabled(boolean enabled) {
145         if (mEditText == null) {
146             Log.w(TAG, "setEditTextEnabled() : Invalid layout");
147             return;
148         }
149         mEditText.setEnabled(enabled);
150         mEditText.setAlpha(enabled ? 1.0f : ColorUtil.getDisabledAlpha(getContext()));
151     }
152 }
153