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.content.Intent;
21 import android.debug.IAdbManager;
22 import android.os.RemoteException;
23 import android.os.ServiceManager;
24 import android.text.TextUtils;
25 import android.util.Log;
26 
27 import androidx.fragment.app.Fragment;
28 import androidx.preference.Preference;
29 
30 import com.android.settings.core.BasePreferenceController;
31 
32 /**
33  * Controller for the "Pair device with QR code" preference in the Wireless debugging
34  * fragment.
35  */
36 public class AdbQrCodePreferenceController extends BasePreferenceController {
37     private static final String TAG = "AdbQrCodePrefCtrl";
38 
39     private IAdbManager mAdbManager;
40     private Fragment mParentFragment;
41 
AdbQrCodePreferenceController(Context context, String key)42     public AdbQrCodePreferenceController(Context context, String key) {
43         super(context, key);
44 
45         mAdbManager = IAdbManager.Stub.asInterface(ServiceManager.getService(
46                 Context.ADB_SERVICE));
47     }
48 
setParentFragment(Fragment parent)49     public void setParentFragment(Fragment parent) {
50         mParentFragment = parent;
51     }
52 
53     @Override
getAvailabilityStatus()54     public int getAvailabilityStatus() {
55         try {
56             return mAdbManager.isAdbWifiQrSupported() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
57         } catch (RemoteException e) {
58             Log.e(TAG, "Unable to check if adb wifi QR code scanning is supported.", e);
59         }
60 
61         return UNSUPPORTED_ON_DEVICE;
62     }
63 
64     @Override
handlePreferenceTreeClick(Preference preference)65     public boolean handlePreferenceTreeClick(Preference preference) {
66         if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
67             return false;
68         }
69 
70         final Intent intent = new Intent(mContext, AdbQrCodeActivity.class);
71         mParentFragment.startActivityForResult(intent,
72                 WirelessDebuggingFragment.PAIRING_DEVICE_REQUEST);
73         return true;
74     }
75 }
76