1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package android.service.carrier; 16 17 import android.annotation.CallSuper; 18 import android.annotation.Nullable; 19 import android.annotation.SuppressLint; 20 import android.app.Service; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.os.Bundle; 24 import android.os.IBinder; 25 import android.os.PersistableBundle; 26 import android.os.ResultReceiver; 27 import android.telephony.SubscriptionManager; 28 import android.telephony.TelephonyRegistryManager; 29 import android.util.Log; 30 31 import java.io.FileDescriptor; 32 import java.io.PrintWriter; 33 34 /** 35 * A service that exposes carrier-specific functionality to the system. 36 * <p> 37 * To extend this class, you must declare the service in your manifest file to require the 38 * {@link android.Manifest.permission#BIND_CARRIER_SERVICES} permission and include an intent 39 * filter with the {@link #CARRIER_SERVICE_INTERFACE}. If the service should have a long-lived 40 * binding, set <code>android.service.carrier.LONG_LIVED_BINDING</code> to <code>true</code> in the 41 * service's metadata. For example: 42 * </p> 43 * 44 * <pre>{@code 45 * <service android:name=".MyCarrierService" 46 * android:label="@string/service_name" 47 * android:permission="android.permission.BIND_CARRIER_SERVICES"> 48 * <intent-filter> 49 * <action android:name="android.service.carrier.CarrierService" /> 50 * </intent-filter> 51 * <meta-data android:name="android.service.carrier.LONG_LIVED_BINDING" 52 * android:value="true" /> 53 * </service> 54 * }</pre> 55 */ 56 public abstract class CarrierService extends Service { 57 58 private static final String LOG_TAG = "CarrierService"; 59 60 public static final String CARRIER_SERVICE_INTERFACE = "android.service.carrier.CarrierService"; 61 62 private final ICarrierService.Stub mStubWrapper; 63 CarrierService()64 public CarrierService() { 65 mStubWrapper = new ICarrierServiceWrapper(); 66 } 67 68 /** 69 * Override this method to set carrier configuration. 70 * <p> 71 * This method will be called by telephony services to get carrier-specific configuration 72 * values. The returned config will be saved by the system until, 73 * <ol> 74 * <li>The carrier app package is updated, or</li> 75 * <li>The carrier app requests a reload with 76 * {@link android.telephony.CarrierConfigManager#notifyConfigChangedForSubId 77 * notifyConfigChangedForSubId}.</li> 78 * </ol> 79 * This method can be called after a SIM card loads, which may be before or after boot. 80 * </p> 81 * <p> 82 * This method should not block for a long time. If expensive operations (e.g. network access) 83 * are required, this method can schedule the work and return null. Then, use 84 * {@link android.telephony.CarrierConfigManager#notifyConfigChangedForSubId 85 * notifyConfigChangedForSubId} to trigger a reload when the config is ready. 86 * </p> 87 * <p> 88 * Implementations should use the keys defined in {@link android.telephony.CarrierConfigManager 89 * CarrierConfigManager}. Any configuration values not set in the returned {@link 90 * PersistableBundle} may be overridden by the system's default configuration service. 91 * </p> 92 * 93 * @param id contains details about the current carrier that can be used to decide what 94 * configuration values to return. Instead of using details like MCCMNC to decide 95 * current carrier, it also contains subscription carrier id 96 * {@link android.telephony.TelephonyManager#getSimCarrierId()}, a platform-wide 97 * unique identifier for each carrier, CarrierConfigService can directly use carrier 98 * id as the key to look up the carrier info. 99 * @return a {@link PersistableBundle} object containing the configuration or null if default 100 * values should be used. 101 * @deprecated use {@link #onLoadConfig(int, CarrierIdentifier)} instead. 102 */ 103 @Deprecated onLoadConfig(CarrierIdentifier id)104 public abstract PersistableBundle onLoadConfig(CarrierIdentifier id); 105 106 /** 107 * Override this method to set carrier configuration on the given {@code subscriptionId}. 108 * <p> 109 * This method will be called by telephony services to get carrier-specific configuration 110 * values. The returned config will be saved by the system until, 111 * <ol> 112 * <li>The carrier app package is updated, or</li> 113 * <li>The carrier app requests a reload with 114 * {@link android.telephony.CarrierConfigManager#notifyConfigChangedForSubId 115 * notifyConfigChangedForSubId}.</li> 116 * </ol> 117 * This method can be called after a SIM card loads, which may be before or after boot. 118 * </p> 119 * <p> 120 * This method should not block for a long time. If expensive operations (e.g. network access) 121 * are required, this method can schedule the work and return null. Then, use 122 * {@link android.telephony.CarrierConfigManager#notifyConfigChangedForSubId 123 * notifyConfigChangedForSubId} to trigger a reload when the config is ready. 124 * </p> 125 * <p> 126 * Implementations should use the keys defined in {@link android.telephony.CarrierConfigManager 127 * CarrierConfigManager}. Any configuration values not set in the returned {@link 128 * PersistableBundle} may be overridden by the system's default configuration service. 129 * </p> 130 * <p> 131 * By default, this method just calls {@link #onLoadConfig(CarrierIdentifier)} with specified 132 * CarrierIdentifier {@code id}. Carrier app with target SDK 133 * {@link android.os.Build.VERSION_CODES#TIRAMISU} and above should override this method to 134 * load carrier configuration on the given {@code subscriptionId}. 135 * Note that {@link #onLoadConfig(CarrierIdentifier)} is still called prior to 136 * {@link android.os.Build.VERSION_CODES#TIRAMISU}. 137 * </p> 138 * 139 * @param subscriptionId the subscription on which the carrier app should load configuration 140 * @param id contains details about the current carrier that can be used to decide what 141 * configuration values to return. Instead of using details like MCCMNC to decide 142 * current carrier, it also contains subscription carrier id 143 * {@link android.telephony.TelephonyManager#getSimCarrierId()}, a platform-wide 144 * unique identifier for each carrier, CarrierConfigService can directly use carrier 145 * id as the key to look up the carrier info. 146 * @return a {@link PersistableBundle} object containing the configuration or null if default 147 * values should be used. 148 */ 149 @SuppressLint("NullableCollection") 150 @Nullable onLoadConfig(int subscriptionId, @Nullable CarrierIdentifier id)151 public PersistableBundle onLoadConfig(int subscriptionId, @Nullable CarrierIdentifier id) { 152 return onLoadConfig(id); 153 } 154 155 /** 156 * Informs the system of an intentional upcoming carrier network change by 157 * a carrier app. This call is optional and is only used to allow the 158 * system to provide alternative UI while telephony is performing an action 159 * that may result in intentional, temporary network lack of connectivity. 160 * <p> 161 * Based on the active parameter passed in, this method will either show or 162 * hide the alternative UI. There is no timeout associated with showing 163 * this UX, so a carrier app must be sure to call with active set to false 164 * sometime after calling with it set to true. 165 * <p> 166 * Requires Permission: calling app has carrier privileges. 167 * 168 * @param active Whether the carrier network change is or shortly will be 169 * active. Set this value to true to begin showing 170 * alternative UI and false to stop. 171 * @see android.telephony.TelephonyManager#hasCarrierPrivileges 172 * @deprecated use {@link #notifyCarrierNetworkChange(int, boolean)} instead. 173 * With no parameter to specify the subscription, this API will 174 * apply to all subscriptions that the carrier app has carrier 175 * privileges on. 176 */ 177 @Deprecated notifyCarrierNetworkChange(boolean active)178 public final void notifyCarrierNetworkChange(boolean active) { 179 TelephonyRegistryManager telephonyRegistryMgr = 180 (TelephonyRegistryManager) this.getSystemService( 181 Context.TELEPHONY_REGISTRY_SERVICE); 182 if (telephonyRegistryMgr != null) { 183 telephonyRegistryMgr.notifyCarrierNetworkChange(active); 184 } 185 } 186 187 /** 188 * Informs the system of an intentional upcoming carrier network change by a carrier app on the 189 * given {@code subscriptionId}. This call is optional and is only used to allow the system to 190 * provide alternative UI while telephony is performing an action that may result in 191 * intentional, temporary network lack of connectivity. 192 * 193 * <p>Based on the active parameter passed in, this method will either show or hide the 194 * alternative UI. There is no timeout associated with showing this UX, so a carrier app must 195 * be sure to call with active set to false sometime after calling with it set to true. 196 * 197 * <p>Requires Permission: calling app has carrier privileges. 198 * 199 * @param subscriptionId the subscription of the carrier network that trigger the change. 200 * @param active whether the carrier network change is or shortly will be active. Set this 201 * value to true to begin showing alternative UI and false to stop. 202 * @see android.telephony.TelephonyManager#hasCarrierPrivileges 203 */ notifyCarrierNetworkChange(int subscriptionId, boolean active)204 public final void notifyCarrierNetworkChange(int subscriptionId, boolean active) { 205 TelephonyRegistryManager telephonyRegistryMgr = this.getSystemService( 206 TelephonyRegistryManager.class); 207 if (telephonyRegistryMgr != null) { 208 telephonyRegistryMgr.notifyCarrierNetworkChange(subscriptionId, active); 209 } 210 } 211 212 /** 213 * If overriding this method, call through to the super method for any unknown actions. 214 * {@inheritDoc} 215 */ 216 @Override 217 @CallSuper onBind(Intent intent)218 public IBinder onBind(Intent intent) { 219 return mStubWrapper; 220 } 221 222 /** 223 * A wrapper around ICarrierService that forwards calls to implementations of 224 * {@link CarrierService}. 225 * @hide 226 */ 227 public class ICarrierServiceWrapper extends ICarrierService.Stub { 228 /** @hide */ 229 public static final int RESULT_OK = 0; 230 /** @hide */ 231 public static final int RESULT_ERROR = 1; 232 /** @hide */ 233 public static final String KEY_CONFIG_BUNDLE = "config_bundle"; 234 235 @Override getCarrierConfig(int phoneId, CarrierIdentifier id, ResultReceiver result)236 public void getCarrierConfig(int phoneId, CarrierIdentifier id, ResultReceiver result) { 237 try { 238 int subId = SubscriptionManager.getSubscriptionId(phoneId); 239 Bundle data = new Bundle(); 240 data.putParcelable(KEY_CONFIG_BUNDLE, CarrierService.this.onLoadConfig(subId, id)); 241 result.send(RESULT_OK, data); 242 } catch (Exception e) { 243 Log.e(LOG_TAG, "Error in onLoadConfig: " + e.getMessage(), e); 244 result.send(RESULT_ERROR, null); 245 } 246 } 247 248 @Override dump(FileDescriptor fd, PrintWriter pw, String[] args)249 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 250 CarrierService.this.dump(fd, pw, args); 251 } 252 } 253 } 254