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.Context;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.os.IBinder;
23 import android.os.PersistableBundle;
24 import android.os.ResultReceiver;
25 import android.telephony.TelephonyRegistryManager;
26 import android.util.Log;
27 
28 import java.io.FileDescriptor;
29 import java.io.PrintWriter;
30 
31 /**
32  * A service that exposes carrier-specific functionality to the system.
33  * <p>
34  * To extend this class, you must declare the service in your manifest file to require the
35  * {@link android.Manifest.permission#BIND_CARRIER_SERVICES} permission and include an intent
36  * filter with the {@link #CARRIER_SERVICE_INTERFACE}. If the service should have a long-lived
37  * binding, set <code>android.service.carrier.LONG_LIVED_BINDING</code> to <code>true</code> in the
38  * service's metadata. For example:
39  * </p>
40  *
41  * <pre>{@code
42  * <service android:name=".MyCarrierService"
43  *       android:label="@string/service_name"
44  *       android:permission="android.permission.BIND_CARRIER_SERVICES">
45  *  <intent-filter>
46  *      <action android:name="android.service.carrier.CarrierService" />
47  *  </intent-filter>
48  *  <meta-data android:name="android.service.carrier.LONG_LIVED_BINDING"
49  *             android:value="true" />
50  * </service>
51  * }</pre>
52  */
53 public abstract class CarrierService extends Service {
54 
55     private static final String LOG_TAG = "CarrierService";
56 
57     public static final String CARRIER_SERVICE_INTERFACE = "android.service.carrier.CarrierService";
58 
59     private final ICarrierService.Stub mStubWrapper;
60 
CarrierService()61     public CarrierService() {
62         mStubWrapper = new ICarrierServiceWrapper();
63     }
64 
65     /**
66      * Override this method to set carrier configuration.
67      * <p>
68      * This method will be called by telephony services to get carrier-specific configuration
69      * values. The returned config will be saved by the system until,
70      * <ol>
71      * <li>The carrier app package is updated, or</li>
72      * <li>The carrier app requests a reload with
73      * {@link android.telephony.CarrierConfigManager#notifyConfigChangedForSubId
74      * notifyConfigChangedForSubId}.</li>
75      * </ol>
76      * This method can be called after a SIM card loads, which may be before or after boot.
77      * </p>
78      * <p>
79      * This method should not block for a long time. If expensive operations (e.g. network access)
80      * are required, this method can schedule the work and return null. Then, use
81      * {@link android.telephony.CarrierConfigManager#notifyConfigChangedForSubId
82      * notifyConfigChangedForSubId} to trigger a reload when the config is ready.
83      * </p>
84      * <p>
85      * Implementations should use the keys defined in {@link android.telephony.CarrierConfigManager
86      * CarrierConfigManager}. Any configuration values not set in the returned {@link
87      * PersistableBundle} may be overridden by the system's default configuration service.
88      * </p>
89      *
90      * @param id contains details about the current carrier that can be used do decide what
91      *           configuration values to return. Instead of using details like MCCMNC to decide
92      *           current carrier, it also contains subscription carrier id
93      *           {@link android.telephony.TelephonyManager#getSimCarrierId()}, a platform-wide
94      *           unique identifier for each carrier, CarrierConfigService can directly use carrier
95      *           id as the key to look up the carrier info.
96      * @return a {@link PersistableBundle} object containing the configuration or null if default
97      *         values should be used.
98      */
onLoadConfig(CarrierIdentifier id)99     public abstract PersistableBundle onLoadConfig(CarrierIdentifier id);
100 
101     /**
102      * Informs the system of an intentional upcoming carrier network change by
103      * a carrier app. This call is optional and is only used to allow the
104      * system to provide alternative UI while telephony is performing an action
105      * that may result in intentional, temporary network lack of connectivity.
106      * <p>
107      * Based on the active parameter passed in, this method will either show or
108      * hide the alternative UI. There is no timeout associated with showing
109      * this UX, so a carrier app must be sure to call with active set to false
110      * sometime after calling with it set to true.
111      * <p>
112      * Requires Permission: calling app has carrier privileges.
113      *
114      * @param active Whether the carrier network change is or shortly will be
115      *               active. Set this value to true to begin showing
116      *               alternative UI and false to stop.
117      * @see android.telephony.TelephonyManager#hasCarrierPrivileges
118      */
notifyCarrierNetworkChange(boolean active)119     public final void notifyCarrierNetworkChange(boolean active) {
120         TelephonyRegistryManager telephonyRegistryMgr =
121             (TelephonyRegistryManager) this.getSystemService(
122                 Context.TELEPHONY_REGISTRY_SERVICE);
123         if (telephonyRegistryMgr != null) {
124             telephonyRegistryMgr.notifyCarrierNetworkChange(active);
125         }
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
onBind(Intent intent)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
getCarrierConfig(CarrierIdentifier id, ResultReceiver result)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         @Override
dump(FileDescriptor fd, PrintWriter pw, String[] args)164         protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
165             CarrierService.this.dump(fd, pw, args);
166         }
167     }
168 }
169