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 package com.android.settings.development;
17 
18 import android.app.Activity;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.debug.PairDevice;
22 
23 import androidx.annotation.VisibleForTesting;
24 import androidx.fragment.app.Fragment;
25 import androidx.preference.PreferenceScreen;
26 
27 import com.android.settings.R;
28 import com.android.settingslib.core.AbstractPreferenceController;
29 import com.android.settingslib.widget.ActionButtonsPreference;
30 
31 /**
32  * Controller for logic pertaining to displaying adb device information for the
33  * {@link AdbDeviceDetailsFragment}.
34  */
35 public class AdbDeviceDetailsActionController extends AbstractPreferenceController {
36     private static final String TAG = "AdbDeviceDetailsAction";
37 
38     @VisibleForTesting
39     static final String KEY_BUTTONS_PREF = "buttons";
40 
41     private PairDevice mPairedDevice;
42     private final Fragment mFragment;
43 
44     private ActionButtonsPreference mButtonsPref;
45 
AdbDeviceDetailsActionController( PairDevice pairedDevice, Context context, Fragment fragment)46     public AdbDeviceDetailsActionController(
47             PairDevice pairedDevice,
48             Context context,
49             Fragment fragment) {
50         super(context);
51 
52         mPairedDevice = pairedDevice;
53         mFragment = fragment;
54     }
55 
56     @Override
isAvailable()57     public boolean isAvailable() {
58         return true;
59     }
60 
61     @Override
getPreferenceKey()62     public String getPreferenceKey() {
63         return KEY_BUTTONS_PREF;
64     }
65 
66     @Override
displayPreference(PreferenceScreen screen)67     public void displayPreference(PreferenceScreen screen) {
68         super.displayPreference(screen);
69 
70         mButtonsPref = ((ActionButtonsPreference) screen.findPreference(getPreferenceKey()))
71                 .setButton1Visible(false)
72                 .setButton2Icon(R.drawable.ic_settings_delete)
73                 .setButton2Text(R.string.adb_device_forget)
74                 .setButton2OnClickListener(view -> forgetDevice());
75     }
76 
77     /**
78      * Forgets the device.
79      */
forgetDevice()80     private void forgetDevice() {
81         Intent intent = new Intent();
82         intent.putExtra(
83                 WirelessDebuggingFragment.PAIRED_DEVICE_REQUEST_TYPE,
84                 WirelessDebuggingFragment.FORGET_ACTION);
85         intent.putExtra(
86                 WirelessDebuggingFragment.PAIRED_DEVICE_EXTRA,
87                 mPairedDevice);
88         mFragment.getActivity().setResult(Activity.RESULT_OK, intent);
89         mFragment.getActivity().finish();
90     }
91 }
92 
93