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.app.Service; 19 import android.content.Intent; 20 import android.os.Bundle; 21 import android.os.IBinder; 22 import android.os.PersistableBundle; 23 import android.os.RemoteException; 24 import android.os.ResultReceiver; 25 import android.os.ServiceManager; 26 import android.util.Log; 27 28 import com.android.internal.telephony.ITelephonyRegistry; 29 30 /** 31 * A service that exposes carrier-specific functionality to the system. 32 * <p> 33 * To extend this class, you must declare the service in your manifest file to require the 34 * {@link android.Manifest.permission#BIND_CARRIER_SERVICES} permission and include an intent 35 * filter with the {@link #CARRIER_SERVICE_INTERFACE}. If the service should have a long-lived 36 * binding, set <code>android.service.carrier.LONG_LIVED_BINDING</code> to <code>true</code> in the 37 * service's metadata. For example: 38 * </p> 39 * 40 * <pre>{@code 41 * <service android:name=".MyCarrierService" 42 * android:label="@string/service_name" 43 * android:permission="android.permission.BIND_CARRIER_SERVICES"> 44 * <intent-filter> 45 * <action android:name="android.service.carrier.CarrierService" /> 46 * </intent-filter> 47 * <meta-data android:name="android.service.carrier.LONG_LIVED_BINDING" 48 * android:value="true" /> 49 * </service> 50 * }</pre> 51 */ 52 public abstract class CarrierService extends Service { 53 54 private static final String LOG_TAG = "CarrierService"; 55 56 public static final String CARRIER_SERVICE_INTERFACE = "android.service.carrier.CarrierService"; 57 58 private static ITelephonyRegistry sRegistry; 59 60 private final ICarrierService.Stub mStubWrapper; 61 62 public CarrierService() { 63 mStubWrapper = new ICarrierServiceWrapper(); 64 if (sRegistry == null) { 65 sRegistry = ITelephonyRegistry.Stub.asInterface( 66 ServiceManager.getService("telephony.registry")); 67 } 68 } 69 70 /** 71 * Override this method to set carrier configuration. 72 * <p> 73 * This method will be called by telephony services to get carrier-specific configuration 74 * values. The returned config will be saved by the system until, 75 * <ol> 76 * <li>The carrier app package is updated, or</li> 77 * <li>The carrier app requests a reload with 78 * {@link android.telephony.CarrierConfigManager#notifyConfigChangedForSubId 79 * notifyConfigChangedForSubId}.</li> 80 * </ol> 81 * This method can be called after a SIM card loads, which may be before or after boot. 82 * </p> 83 * <p> 84 * This method should not block for a long time. If expensive operations (e.g. network access) 85 * are required, this method can schedule the work and return null. Then, use 86 * {@link android.telephony.CarrierConfigManager#notifyConfigChangedForSubId 87 * notifyConfigChangedForSubId} to trigger a reload when the config is ready. 88 * </p> 89 * <p> 90 * Implementations should use the keys defined in {@link android.telephony.CarrierConfigManager 91 * CarrierConfigManager}. Any configuration values not set in the returned {@link 92 * PersistableBundle} may be overridden by the system's default configuration service. 93 * </p> 94 * 95 * @param id contains details about the current carrier that can be used do decide what 96 * configuration values to return. 97 * @return a {@link PersistableBundle} object containing the configuration or null if default 98 * values should be used. 99 */ 100 public abstract PersistableBundle onLoadConfig(CarrierIdentifier id); 101 102 /** 103 * Informs the system of an intentional upcoming carrier network change by 104 * a carrier app. This call is optional and is only used to allow the 105 * system to provide alternative UI while telephony is performing an action 106 * that may result in intentional, temporary network lack of connectivity. 107 * <p> 108 * Based on the active parameter passed in, this method will either show or 109 * hide the alternative UI. There is no timeout associated with showing 110 * this UX, so a carrier app must be sure to call with active set to false 111 * sometime after calling with it set to true. 112 * <p> 113 * Requires Permission: 114 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE} 115 * or the calling app has carrier privileges. 116 * 117 * @param active Whether the carrier network change is or shortly will be 118 * active. Set this value to true to begin showing 119 * alternative UI and false to stop. 120 * @see android.telephony.TelephonyManager#hasCarrierPrivileges 121 */ 122 public final void notifyCarrierNetworkChange(boolean active) { 123 try { 124 if (sRegistry != null) sRegistry.notifyCarrierNetworkChange(active); 125 } catch (RemoteException | NullPointerException ex) {} 126 } 127 128 /** 129 * If overriding this method, call through to the super method for any unknown actions. 130 * {@inheritDoc} 131 */ 132 @Override 133 @CallSuper 134 public IBinder onBind(Intent intent) { 135 return mStubWrapper; 136 } 137 138 /** 139 * A wrapper around ICarrierService that forwards calls to implementations of 140 * {@link CarrierService}. 141 * @hide 142 */ 143 public class ICarrierServiceWrapper extends ICarrierService.Stub { 144 /** @hide */ 145 public static final int RESULT_OK = 0; 146 /** @hide */ 147 public static final int RESULT_ERROR = 1; 148 /** @hide */ 149 public static final String KEY_CONFIG_BUNDLE = "config_bundle"; 150 151 @Override 152 public void getCarrierConfig(CarrierIdentifier id, ResultReceiver result) { 153 try { 154 Bundle data = new Bundle(); 155 data.putParcelable(KEY_CONFIG_BUNDLE, CarrierService.this.onLoadConfig(id)); 156 result.send(RESULT_OK, data); 157 } catch (Exception e) { 158 Log.e(LOG_TAG, "Error in onLoadConfig: " + e.getMessage(), e); 159 result.send(RESULT_ERROR, null); 160 } 161 } 162 } 163 } 164