1 /* 2 * Copyright (C) 2020 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.development; 18 19 import android.content.Context; 20 import android.debug.PairDevice; 21 import android.os.Bundle; 22 import android.view.View; 23 24 import androidx.preference.Preference; 25 import androidx.preference.PreferenceViewHolder; 26 27 import com.android.settings.R; 28 29 /** 30 * An AP preference for the currently connected AP 31 */ 32 public class AdbPairedDevicePreference extends Preference { 33 private static final String TAG = "AdbPairedDevicePref"; 34 35 private PairDevice mPairedDevice; 36 37 // Extract using getSerializable(PAIRED_DEVICE_EXTRA) 38 public static final String PAIRED_DEVICE_EXTRA = "paired_device"; 39 AdbPairedDevicePreference(PairDevice pairedDevice, Context context)40 public AdbPairedDevicePreference(PairDevice pairedDevice, Context context) { 41 super(context); 42 43 mPairedDevice = pairedDevice; 44 setWidgetLayoutResource(getWidgetLayoutResourceId()); 45 refresh(); 46 } 47 getWidgetLayoutResourceId()48 protected int getWidgetLayoutResourceId() { 49 return R.layout.preference_widget_gear_optional_background; 50 } 51 52 /** 53 * Refreshes the preference bound to the paired device previously passed in. 54 */ refresh()55 public void refresh() { 56 setTitle(this, mPairedDevice); 57 } 58 setPairedDevice(PairDevice pairedDevice)59 public void setPairedDevice(PairDevice pairedDevice) { 60 mPairedDevice = pairedDevice; 61 } 62 getPairedDevice()63 public PairDevice getPairedDevice() { 64 return mPairedDevice; 65 } 66 67 @Override onBindViewHolder(PreferenceViewHolder holder)68 public void onBindViewHolder(PreferenceViewHolder holder) { 69 super.onBindViewHolder(holder); 70 71 final View gear = holder.findViewById(R.id.settings_button); 72 final View gearNoBg = holder.findViewById(R.id.settings_button_no_background); 73 74 gear.setVisibility(View.INVISIBLE); 75 gearNoBg.setVisibility(View.VISIBLE); 76 } 77 setTitle(AdbPairedDevicePreference preference, PairDevice pairedDevice)78 static void setTitle(AdbPairedDevicePreference preference, 79 PairDevice pairedDevice) { 80 preference.setTitle(pairedDevice.getDeviceName()); 81 preference.setSummary(pairedDevice.isConnected() 82 ? preference.getContext().getText(R.string.adb_wireless_device_connected_summary) 83 : ""); 84 } 85 86 /** 87 * Writes the paired devices bound to this preference to the bundle. 88 * 89 * @param bundle the bundle to write the paired device to 90 */ savePairedDeviceToExtras(Bundle bundle)91 public void savePairedDeviceToExtras(Bundle bundle) { 92 bundle.putParcelable(PAIRED_DEVICE_EXTRA, mPairedDevice); 93 } 94 } 95 96