1 /* 2 * Copyright (C) 2023 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 android.net.thread; 18 19 import static java.util.Objects.requireNonNull; 20 21 import android.annotation.FlaggedApi; 22 import android.annotation.NonNull; 23 import android.annotation.SystemApi; 24 import android.annotation.SystemService; 25 import android.content.Context; 26 import android.os.RemoteException; 27 28 import com.android.net.module.util.CollectionUtils; 29 30 import java.util.Collections; 31 import java.util.List; 32 33 /** 34 * Provides the primary API for managing app aspects of Thread network connectivity. 35 * 36 * @hide 37 */ 38 @FlaggedApi(ThreadNetworkFlags.FLAG_THREAD_ENABLED) 39 @SystemApi 40 @SystemService(ThreadNetworkManager.SERVICE_NAME) 41 public final class ThreadNetworkManager { 42 /** 43 * This value tracks {@link Context#THREAD_NETWORK_SERVICE}. 44 * 45 * <p>This is needed because at the time this service is created, it needs to support both 46 * Android U and V but {@link Context#THREAD_NETWORK_SERVICE} Is only available on the V branch. 47 * 48 * <p>Note that this is not added to NetworkStack ConstantsShim because we need this constant in 49 * the framework library while ConstantsShim is only linked against the service library. 50 * 51 * @hide 52 */ 53 public static final String SERVICE_NAME = "thread_network"; 54 55 /** 56 * This value tracks {@link PackageManager#FEATURE_THREAD_NETWORK}. 57 * 58 * <p>This is needed because at the time this service is created, it needs to support both 59 * Android U and V but {@link PackageManager#FEATURE_THREAD_NETWORK} Is only available on the V 60 * branch. 61 * 62 * <p>Note that this is not added to NetworkStack COnstantsShim because we need this constant in 63 * the framework library while ConstantsShim is only linked against the service library. 64 * 65 * @hide 66 */ 67 public static final String FEATURE_NAME = "android.hardware.thread_network"; 68 69 /** 70 * Permission allows changing Thread network state and access to Thread network credentials such 71 * as Network Key and PSKc. 72 * 73 * <p>This is the same value as android.Manifest.permission.THREAD_NETWORK_PRIVILEGED. That 74 * symbol is not available on U while this feature needs to support Android U TV devices, so 75 * here is making a copy of android.Manifest.permission.THREAD_NETWORK_PRIVILEGED. 76 * 77 * @hide 78 */ 79 public static final String PERMISSION_THREAD_NETWORK_PRIVILEGED = 80 "android.permission.THREAD_NETWORK_PRIVILEGED"; 81 82 /** 83 * This user restriction specifies if Thread network is disallowed on the device. If Thread 84 * network is disallowed it cannot be turned on via Settings. 85 * 86 * <p>this is a mirror of {@link UserManager#DISALLOW_THREAD_NETWORK} which is not available on 87 * Android U devices. 88 * 89 * @hide 90 */ 91 public static final String DISALLOW_THREAD_NETWORK = "no_thread_network"; 92 93 @NonNull private final Context mContext; 94 @NonNull private final List<ThreadNetworkController> mUnmodifiableControllerServices; 95 96 /** 97 * Creates a new ThreadNetworkManager instance. 98 * 99 * @hide 100 */ ThreadNetworkManager( @onNull Context context, @NonNull IThreadNetworkManager managerService)101 public ThreadNetworkManager( 102 @NonNull Context context, @NonNull IThreadNetworkManager managerService) { 103 this(context, makeControllers(managerService)); 104 } 105 makeControllers( @onNull IThreadNetworkManager managerService)106 private static List<ThreadNetworkController> makeControllers( 107 @NonNull IThreadNetworkManager managerService) { 108 requireNonNull(managerService, "managerService cannot be null"); 109 110 List<IThreadNetworkController> controllerServices; 111 112 try { 113 controllerServices = managerService.getAllThreadNetworkControllers(); 114 } catch (RemoteException e) { 115 e.rethrowFromSystemServer(); 116 return Collections.emptyList(); 117 } 118 119 return CollectionUtils.map(controllerServices, ThreadNetworkController::new); 120 } 121 ThreadNetworkManager( @onNull Context context, @NonNull List<ThreadNetworkController> controllerServices)122 private ThreadNetworkManager( 123 @NonNull Context context, @NonNull List<ThreadNetworkController> controllerServices) { 124 mContext = context; 125 mUnmodifiableControllerServices = Collections.unmodifiableList(controllerServices); 126 } 127 128 /** Returns the {@link ThreadNetworkController} object of all Thread networks. */ 129 @NonNull getAllThreadNetworkControllers()130 public List<ThreadNetworkController> getAllThreadNetworkControllers() { 131 return mUnmodifiableControllerServices; 132 } 133 } 134