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.connecteddevice.threadnetwork 18 19 import android.content.Context 20 import android.content.pm.PackageManager 21 import android.net.thread.ThreadNetworkController 22 import android.net.thread.ThreadNetworkController.StateCallback 23 import android.net.thread.ThreadNetworkException 24 import android.net.thread.ThreadNetworkManager 25 import android.os.OutcomeReceiver 26 import androidx.annotation.VisibleForTesting 27 import java.util.concurrent.Executor 28 29 /** Common utilities for Thread settings classes. */ 30 object ThreadNetworkUtils { 31 /** 32 * Retrieves the [BaseThreadNetworkController] instance that is backed by the Android 33 * [ThreadNetworkController]. 34 */ getThreadNetworkControllernull35 fun getThreadNetworkController(context: Context): BaseThreadNetworkController? { 36 if (!context.packageManager.hasSystemFeature(PackageManager.FEATURE_THREAD_NETWORK)) { 37 return null 38 } 39 val manager = context.getSystemService(ThreadNetworkManager::class.java) ?: return null 40 val controller = manager.allThreadNetworkControllers[0] 41 return object : BaseThreadNetworkController { 42 override fun setEnabled( 43 enabled: Boolean, 44 executor: Executor, 45 receiver: OutcomeReceiver<Void?, ThreadNetworkException> 46 ) { 47 controller.setEnabled(enabled, executor, receiver) 48 } 49 50 override fun registerStateCallback(executor: Executor, callback: StateCallback) { 51 controller.registerStateCallback(executor, callback) 52 } 53 54 override fun unregisterStateCallback(callback: StateCallback) { 55 controller.unregisterStateCallback(callback) 56 } 57 } 58 } 59 }