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