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;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.graphics.drawable.Drawable;
22 import android.os.UserManager;
23 import android.util.AttributeSet;
24 import android.util.Log;
25 import android.widget.ImageButton;
26 
27 import androidx.annotation.DrawableRes;
28 import androidx.annotation.Nullable;
29 import androidx.preference.PreferenceViewHolder;
30 
31 import com.android.settings.R;
32 import com.android.settings.wifi.dpp.WifiDppUtils;
33 import com.android.settingslib.RestrictedPreference;
34 
35 /**
36  * The Preference for users to add Wi-Fi networks in WifiSettings
37  */
38 public class AddWifiNetworkPreference extends RestrictedPreference {
39 
40     private static final String TAG = "AddWifiNetworkPreference";
41 
42     private final Drawable mScanIconDrawable;
43 
AddWifiNetworkPreference(Context context)44     public AddWifiNetworkPreference(Context context) {
45         this(context, null);
46     }
47 
AddWifiNetworkPreference(Context context, @Nullable AttributeSet attrs)48     public AddWifiNetworkPreference(Context context, @Nullable AttributeSet attrs) {
49         super(context, attrs);
50         setLayoutResource(com.android.settingslib.R.layout.preference_access_point);
51         setWidgetLayoutResource(R.layout.wifi_button_preference_widget);
52         setIcon(R.drawable.ic_add_24dp);
53         setTitle(R.string.wifi_add_network);
54 
55         mScanIconDrawable = getDrawable(R.drawable.ic_scan_24dp);
56         checkRestrictionAndSetDisabled(UserManager.DISALLOW_ADD_WIFI_CONFIG);
57     }
58 
59     @Override
onBindViewHolder(PreferenceViewHolder holder)60     public void onBindViewHolder(PreferenceViewHolder holder) {
61         super.onBindViewHolder(holder);
62 
63         final ImageButton scanButton = (ImageButton) holder.findViewById(R.id.button_icon);
64         scanButton.setImageDrawable(mScanIconDrawable);
65         scanButton.setContentDescription(
66                 getContext().getString(R.string.wifi_dpp_scan_qr_code));
67         scanButton.setOnClickListener(view -> {
68             getContext().startActivity(
69                     WifiDppUtils.getEnrolleeQrCodeScannerIntent(getContext(), /* ssid */ null));
70         });
71     }
72 
getDrawable(@rawableRes int iconResId)73     private Drawable getDrawable(@DrawableRes int iconResId) {
74         Drawable buttonIcon = null;
75 
76         try {
77             buttonIcon = getContext().getDrawable(iconResId);
78         } catch (Resources.NotFoundException exception) {
79             Log.e(TAG, "Resource does not exist: " + iconResId);
80         }
81         return buttonIcon;
82     }
83 }
84