1 /*
<lambda>null2  * 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.connecteddevice.threadnetwork
18 
19 import android.content.Context
20 import android.net.thread.ThreadNetworkController
21 import android.net.thread.ThreadNetworkController.StateCallback
22 import androidx.annotation.VisibleForTesting
23 import androidx.core.content.ContextCompat
24 import androidx.lifecycle.Lifecycle
25 import androidx.lifecycle.LifecycleEventObserver
26 import androidx.lifecycle.LifecycleOwner
27 import androidx.preference.Preference
28 import androidx.preference.PreferenceScreen
29 import com.android.settings.R
30 import com.android.settings.core.BasePreferenceController
31 import com.android.settings.flags.Flags
32 import java.util.concurrent.Executor
33 
34 /**
35  * The fragment controller for Thread settings in
36  * "Connected devices > Connection preferences > Thread".
37  */
38 class ThreadNetworkFragmentController @VisibleForTesting constructor(
39     context: Context,
40     preferenceKey: String,
41     private val executor: Executor,
42     private val threadController: BaseThreadNetworkController?
43 ) : BasePreferenceController(context, preferenceKey), LifecycleEventObserver {
44     private val stateCallback: StateCallback
45     private var threadEnabled = false
46     private var preference: Preference? = null
47 
48     constructor(context: Context, preferenceKey: String) : this(
49         context,
50         preferenceKey,
51         ContextCompat.getMainExecutor(context),
52         ThreadNetworkUtils.getThreadNetworkController(context)
53     )
54 
55     init {
56         stateCallback = newStateCallback()
57     }
58 
59     override fun getAvailabilityStatus(): Int {
60         return if (!Flags.threadSettingsEnabled()) {
61             CONDITIONALLY_UNAVAILABLE
62         } else if (threadController == null) {
63             UNSUPPORTED_ON_DEVICE
64         } else {
65             AVAILABLE
66         }
67     }
68 
69     override fun getSummary(): CharSequence {
70         return if (threadEnabled) {
71             mContext.getText(R.string.switch_on_text)
72         } else {
73             mContext.getText(R.string.switch_off_text)
74         }
75     }
76 
77     override fun displayPreference(screen: PreferenceScreen) {
78         super.displayPreference(screen)
79         preference = screen.findPreference(preferenceKey)
80     }
81 
82     override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
83         if (threadController == null) {
84             return
85         }
86 
87         when (event) {
88             Lifecycle.Event.ON_START ->
89                 threadController.registerStateCallback(executor, stateCallback)
90 
91             Lifecycle.Event.ON_STOP ->
92                 threadController.unregisterStateCallback(stateCallback)
93 
94             else -> {}
95         }
96     }
97 
98     private fun newStateCallback(): StateCallback {
99         return object : StateCallback {
100             override fun onThreadEnableStateChanged(enabledState: Int) {
101                 threadEnabled = enabledState == ThreadNetworkController.STATE_ENABLED
102                 preference?.let { preference -> refreshSummary(preference) }
103             }
104 
105             override fun onDeviceRoleChanged(role: Int) {}
106         }
107     }
108 }
109