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