1 /*
2  * Copyright (C) 2023 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 android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.util.AttributeSet;
22 import android.util.Log;
23 import android.view.View;
24 import android.widget.ImageButton;
25 
26 import androidx.preference.PreferenceViewHolder;
27 
28 import com.android.settings.R;
29 import com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsQrCodeFragment;
30 import com.android.settings.core.SubSettingLauncher;
31 import com.android.settings.widget.ValidatedEditTextPreference;
32 
33 public class AudioSharingNamePreference extends ValidatedEditTextPreference {
34     private static final String TAG = "AudioSharingNamePreference";
35     private boolean mShowQrCodeIcon = false;
36 
AudioSharingNamePreference( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)37     public AudioSharingNamePreference(
38             Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
39         super(context, attrs, defStyleAttr, defStyleRes);
40         initialize();
41     }
42 
AudioSharingNamePreference(Context context, AttributeSet attrs, int defStyleAttr)43     public AudioSharingNamePreference(Context context, AttributeSet attrs, int defStyleAttr) {
44         super(context, attrs, defStyleAttr);
45         initialize();
46     }
47 
AudioSharingNamePreference(Context context, AttributeSet attrs)48     public AudioSharingNamePreference(Context context, AttributeSet attrs) {
49         super(context, attrs);
50         initialize();
51     }
52 
AudioSharingNamePreference(Context context)53     public AudioSharingNamePreference(Context context) {
54         super(context);
55         initialize();
56     }
57 
initialize()58     private void initialize() {
59         setLayoutResource(
60                 com.android.settingslib.widget.preference.twotarget.R.layout.preference_two_target);
61         setWidgetLayoutResource(R.layout.preference_widget_qrcode);
62     }
63 
setShowQrCodeIcon(boolean show)64     void setShowQrCodeIcon(boolean show) {
65         mShowQrCodeIcon = show;
66         notifyChanged();
67     }
68 
69     @Override
onBindViewHolder(PreferenceViewHolder holder)70     public void onBindViewHolder(PreferenceViewHolder holder) {
71         super.onBindViewHolder(holder);
72 
73         ImageButton shareButton = (ImageButton) holder.findViewById(R.id.button_icon);
74         View divider =
75                 holder.findViewById(
76                         com.android.settingslib.widget.preference.twotarget.R.id
77                                 .two_target_divider);
78 
79         if (shareButton != null && divider != null) {
80             if (mShowQrCodeIcon) {
81                 configureVisibleStateForQrCodeIcon(shareButton, divider);
82             } else {
83                 configureInvisibleStateForQrCodeIcon(shareButton, divider);
84             }
85         } else {
86             Log.w(TAG, "onBindViewHolder() : shareButton or divider is null!");
87         }
88     }
89 
configureVisibleStateForQrCodeIcon(ImageButton shareButton, View divider)90     private void configureVisibleStateForQrCodeIcon(ImageButton shareButton, View divider) {
91         divider.setVisibility(View.VISIBLE);
92         shareButton.setVisibility(View.VISIBLE);
93         shareButton.setImageDrawable(getContext().getDrawable(R.drawable.ic_qrcode_24dp));
94         shareButton.setOnClickListener(unused -> launchAudioSharingQrCodeFragment());
95     }
96 
configureInvisibleStateForQrCodeIcon(ImageButton shareButton, View divider)97     private void configureInvisibleStateForQrCodeIcon(ImageButton shareButton, View divider) {
98         divider.setVisibility(View.INVISIBLE);
99         shareButton.setVisibility(View.INVISIBLE);
100         shareButton.setOnClickListener(null);
101     }
102 
launchAudioSharingQrCodeFragment()103     private void launchAudioSharingQrCodeFragment() {
104         new SubSettingLauncher(getContext())
105                 .setTitleText(getContext().getString(R.string.audio_streams_qr_code_page_title))
106                 .setDestination(AudioStreamsQrCodeFragment.class.getName())
107                 .setSourceMetricsCategory(SettingsEnums.AUDIO_SHARING_SETTINGS)
108                 .launch();
109     }
110 }
111