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.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(R.id.two_target_divider);
83 
84         if (mVisible) {
85             shareButton.setOnClickListener(mClickListener);
86             shareButton.setVisibility(View.VISIBLE);
87             shareButton.setContentDescription(
88                     getContext().getString(R.string.wifi_dpp_share_hotspot));
89             shareButton.setImageDrawable(mShareIconDrawable);
90 
91             dividerView.setVisibility(View.VISIBLE);
92         } else {
93             shareButton.setVisibility(View.GONE);
94 
95             dividerView.setVisibility(View.GONE);
96         }
97     }
98 
setButtonOnClickListener(View.OnClickListener listener)99     public void setButtonOnClickListener(View.OnClickListener listener) {
100         mClickListener = listener;
101     }
102 
setButtonVisible(boolean visible)103     public void setButtonVisible(boolean visible) {
104         mVisible = visible;
105     }
106 
getDrawable(@rawableRes int iconResId)107     private Drawable getDrawable(@DrawableRes int iconResId) {
108         Drawable buttonIcon = null;
109 
110         try {
111             buttonIcon = getContext().getDrawable(iconResId);
112         } catch (Resources.NotFoundException exception) {
113             Log.e(TAG, "Resource does not exist: " + iconResId);
114         }
115         return buttonIcon;
116     }
117 
118     @VisibleForTesting
isQrCodeButtonAvailable()119     boolean isQrCodeButtonAvailable() {
120         return mVisible && mClickListener != null;
121     }
122 }
123