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