1 /*
2  * Copyright (C) 2019 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.wifi.tether;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.graphics.drawable.Drawable;
22 import android.util.AttributeSet;
23 import android.util.Log;
24 import android.view.View;
25 import android.widget.ImageButton;
26 
27 import androidx.annotation.DrawableRes;
28 import androidx.preference.PreferenceViewHolder;
29 
30 import com.android.internal.annotations.VisibleForTesting;
31 import com.android.settings.R;
32 import com.android.settings.widget.ValidatedEditTextPreference;
33 
34 /**
35  * Support a QR code share button for {@code EditTextPreference} that supports input validation.
36  */
37 public class WifiTetherSsidPreference extends ValidatedEditTextPreference {
38     private static final String TAG = "WifiTetherSsidPreference";
39 
40     private Drawable mShareIconDrawable;
41     private View.OnClickListener mClickListener;
42     private boolean mVisible;
43 
WifiTetherSsidPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)44     public WifiTetherSsidPreference(Context context, AttributeSet attrs,
45             int defStyleAttr, int defStyleRes) {
46         super(context, attrs, defStyleAttr, defStyleRes);
47 
48         initialize();
49     }
50 
WifiTetherSsidPreference(Context context, AttributeSet attrs, int defStyleAttr)51     public WifiTetherSsidPreference(Context context, AttributeSet attrs, int defStyleAttr) {
52         super(context, attrs, defStyleAttr);
53 
54         initialize();
55     }
56 
WifiTetherSsidPreference(Context context, AttributeSet attrs)57     public WifiTetherSsidPreference(Context context, AttributeSet attrs) {
58         super(context, attrs);
59 
60         initialize();
61     }
62 
WifiTetherSsidPreference(Context context)63     public WifiTetherSsidPreference(Context context) {
64         super(context);
65 
66         initialize();
67     }
68 
initialize()69     private void initialize() {
70         // TODO(b/129019971): use methods of divider line in parent object
71         setLayoutResource(com.android.settingslib.widget.preference.twotarget.R.layout.preference_two_target);
72         setWidgetLayoutResource(R.layout.wifi_button_preference_widget);
73 
74         mShareIconDrawable = getDrawable(R.drawable.ic_qrcode_24dp);
75     }
76 
77     @Override
onBindViewHolder(PreferenceViewHolder holder)78     public void onBindViewHolder(PreferenceViewHolder holder) {
79         super.onBindViewHolder(holder);
80 
81         final ImageButton shareButton = (ImageButton) holder.findViewById(R.id.button_icon);
82         final View dividerView = holder.findViewById(
83                 com.android.settingslib.widget.preference.twotarget.R.id.two_target_divider);
84 
85         if (mVisible) {
86             shareButton.setOnClickListener(mClickListener);
87             shareButton.setVisibility(View.VISIBLE);
88             shareButton.setContentDescription(
89                     getContext().getString(R.string.wifi_dpp_share_hotspot));
90             shareButton.setImageDrawable(mShareIconDrawable);
91 
92             dividerView.setVisibility(View.VISIBLE);
93         } else {
94             shareButton.setVisibility(View.GONE);
95 
96             dividerView.setVisibility(View.GONE);
97         }
98     }
99 
setButtonOnClickListener(View.OnClickListener listener)100     public void setButtonOnClickListener(View.OnClickListener listener) {
101         mClickListener = listener;
102     }
103 
setButtonVisible(boolean visible)104     public void setButtonVisible(boolean visible) {
105         mVisible = visible;
106     }
107 
getDrawable(@rawableRes int iconResId)108     private Drawable getDrawable(@DrawableRes int iconResId) {
109         Drawable buttonIcon = null;
110 
111         try {
112             buttonIcon = getContext().getDrawable(iconResId);
113         } catch (Resources.NotFoundException exception) {
114             Log.e(TAG, "Resource does not exist: " + iconResId);
115         }
116         return buttonIcon;
117     }
118 
119     @VisibleForTesting
isQrCodeButtonAvailable()120     boolean isQrCodeButtonAvailable() {
121         return mVisible && mClickListener != null;
122     }
123 }
124