1 /*
2  * Copyright (C) 2024 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.system
18 
19 import android.content.Context
20 import android.content.Intent
21 import android.content.pm.ResolveInfo
22 
23 import androidx.preference.Preference
24 
25 import com.android.settings.R
26 import com.android.settings.core.BasePreferenceController
27 import com.android.settings.flags.Flags
28 
29 open class DeviceDiagnosticsPreferenceController(context: Context, preferenceKey: String) :
30     BasePreferenceController(context, preferenceKey) {
31 
getAvailabilityStatusnull32     override fun getAvailabilityStatus(): Int {
33         if (!Flags.enableDeviceDiagnosticsInSettings()) {
34             return UNSUPPORTED_ON_DEVICE
35         }
36         if (getIntent() == null) {
37             return UNSUPPORTED_ON_DEVICE
38         }
39         return AVAILABLE
40     }
41 
handlePreferenceTreeClicknull42     override fun handlePreferenceTreeClick(preference: Preference): Boolean {
43         if (preferenceKey != preference.key) {
44             return false
45         }
46 
47         val intent = getIntent()
48         if (intent == null) {
49             return false
50         }
51 
52         preference.getContext().startActivity(intent)
53         return true
54     }
55 
getIntentnull56     private fun getIntent(): Intent? {
57         val intent = Intent(Intent.ACTION_MAIN)
58 
59         val packageName = mContext.getResources().getString(
60                 R.string.config_device_diagnostics_package_name)
61         intent.setPackage(packageName)
62 
63         val info = mContext.getPackageManager().resolveActivity(intent, 0)
64         if (info == null) {
65             return null
66         }
67 
68         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
69         return intent
70     }
71 }
72