blob: c351d891bc6189c34d912eb1da9821f300274685 [file] [log] [blame]
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -08001/*
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
15package android.service.carrier;
16
Andrew Flynnceaed682015-06-09 12:36:58 +000017import android.annotation.CallSuper;
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -080018import android.app.Service;
19import android.content.Intent;
Jonathan Basseri49b54d62017-07-25 13:34:36 -070020import android.os.Bundle;
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -080021import android.os.IBinder;
Jonathan Basseri4f9ad1672015-05-12 10:06:32 -070022import android.os.PersistableBundle;
Andrew Flynnceaed682015-06-09 12:36:58 +000023import android.os.RemoteException;
Jonathan Basseri49b54d62017-07-25 13:34:36 -070024import android.os.ResultReceiver;
Andrew Flynnceaed682015-06-09 12:36:58 +000025import android.os.ServiceManager;
Jonathan Basseri49b54d62017-07-25 13:34:36 -070026import android.util.Log;
Andrew Flynnceaed682015-06-09 12:36:58 +000027
28import com.android.internal.telephony.ITelephonyRegistry;
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -080029
30/**
Zach Johnson42ecc9e2015-05-22 15:56:09 -070031 * A service that exposes carrier-specific functionality to the system.
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -080032 * <p>
33 * To extend this class, you must declare the service in your manifest file to require the
Zach Johnsoncdaaa912015-05-13 16:25:12 -070034 * {@link android.Manifest.permission#BIND_CARRIER_SERVICES} permission and include an intent
Zach Johnson08a244c2015-06-19 18:12:27 -070035 * filter with the {@link #CARRIER_SERVICE_INTERFACE}. If the service should have a long-lived
Ricardo Loo Forondaf2a8aea2017-10-26 17:02:31 -070036 * binding, set <code>android.service.carrier.LONG_LIVED_BINDING</code> to <code>true</code> in the
37 * service's metadata. For example:
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -080038 * </p>
39 *
40 * <pre>{@code
Zach Johnson42ecc9e2015-05-22 15:56:09 -070041 * <service android:name=".MyCarrierService"
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -080042 * android:label="@string/service_name"
Zach Johnsoncdaaa912015-05-13 16:25:12 -070043 * android:permission="android.permission.BIND_CARRIER_SERVICES">
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -080044 * <intent-filter>
Zach Johnson08a244c2015-06-19 18:12:27 -070045 * <action android:name="android.service.carrier.CarrierService" />
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -080046 * </intent-filter>
Zach Johnson08a244c2015-06-19 18:12:27 -070047 * <meta-data android:name="android.service.carrier.LONG_LIVED_BINDING"
48 * android:value="true" />
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -080049 * </service>
50 * }</pre>
51 */
Zach Johnson42ecc9e2015-05-22 15:56:09 -070052public abstract class CarrierService extends Service {
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -080053
Jonathan Basseri49b54d62017-07-25 13:34:36 -070054 private static final String LOG_TAG = "CarrierService";
55
Zach Johnson08a244c2015-06-19 18:12:27 -070056 public static final String CARRIER_SERVICE_INTERFACE = "android.service.carrier.CarrierService";
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -080057
Andrew Flynnceaed682015-06-09 12:36:58 +000058 private static ITelephonyRegistry sRegistry;
59
Zach Johnson42ecc9e2015-05-22 15:56:09 -070060 private final ICarrierService.Stub mStubWrapper;
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -080061
Zach Johnson42ecc9e2015-05-22 15:56:09 -070062 public CarrierService() {
63 mStubWrapper = new ICarrierServiceWrapper();
Andrew Flynnceaed682015-06-09 12:36:58 +000064 if (sRegistry == null) {
65 sRegistry = ITelephonyRegistry.Stub.asInterface(
66 ServiceManager.getService("telephony.registry"));
67 }
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -080068 }
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
Jonathan Basseri5ea0c8f2015-06-08 12:25:55 -070078 * {@link android.telephony.CarrierConfigManager#notifyConfigChangedForSubId
79 * notifyConfigChangedForSubId}.</li>
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -080080 * </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
Jonathan Basseri5ea0c8f2015-06-08 12:25:55 -070086 * {@link android.telephony.CarrierConfigManager#notifyConfigChangedForSubId
87 * notifyConfigChangedForSubId} to trigger a reload when the config is ready.
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -080088 * </p>
89 * <p>
90 * Implementations should use the keys defined in {@link android.telephony.CarrierConfigManager
Jonathan Basseri4f9ad1672015-05-12 10:06:32 -070091 * CarrierConfigManager}. Any configuration values not set in the returned {@link
92 * PersistableBundle} may be overridden by the system's default configuration service.
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -080093 * </p>
94 *
95 * @param id contains details about the current carrier that can be used do decide what
chen xud95965a2018-11-11 19:01:50 -080096 * configuration values to return. Instead of using details like MCCMNC to decide
97 * current carrier, it also contains subscription carrier id
98 * {@link android.telephony.TelephonyManager#getSimCarrierId()}, a platform-wide
99 * unique identifier for each carrier, CarrierConfigService can directly use carrier
100 * id as the key to look up the carrier info.
Jonathan Basseri4f9ad1672015-05-12 10:06:32 -0700101 * @return a {@link PersistableBundle} object containing the configuration or null if default
102 * values should be used.
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800103 */
Jonathan Basseri4f9ad1672015-05-12 10:06:32 -0700104 public abstract PersistableBundle onLoadConfig(CarrierIdentifier id);
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800105
Andrew Flynnceaed682015-06-09 12:36:58 +0000106 /**
107 * Informs the system of an intentional upcoming carrier network change by
108 * a carrier app. This call is optional and is only used to allow the
109 * system to provide alternative UI while telephony is performing an action
110 * that may result in intentional, temporary network lack of connectivity.
111 * <p>
112 * Based on the active parameter passed in, this method will either show or
113 * hide the alternative UI. There is no timeout associated with showing
114 * this UX, so a carrier app must be sure to call with active set to false
115 * sometime after calling with it set to true.
116 * <p>
117 * Requires Permission:
118 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
Jeff Davidson8e1a4932016-11-18 12:45:11 -0800119 * or the calling app has carrier privileges.
Andrew Flynnceaed682015-06-09 12:36:58 +0000120 *
121 * @param active Whether the carrier network change is or shortly will be
122 * active. Set this value to true to begin showing
123 * alternative UI and false to stop.
Jeff Davidson8e1a4932016-11-18 12:45:11 -0800124 * @see android.telephony.TelephonyManager#hasCarrierPrivileges
Andrew Flynnceaed682015-06-09 12:36:58 +0000125 */
126 public final void notifyCarrierNetworkChange(boolean active) {
127 try {
128 if (sRegistry != null) sRegistry.notifyCarrierNetworkChange(active);
129 } catch (RemoteException | NullPointerException ex) {}
130 }
131
132 /**
133 * If overriding this method, call through to the super method for any unknown actions.
134 * {@inheritDoc}
135 */
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800136 @Override
Andrew Flynnceaed682015-06-09 12:36:58 +0000137 @CallSuper
138 public IBinder onBind(Intent intent) {
Zach Johnson08a244c2015-06-19 18:12:27 -0700139 return mStubWrapper;
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800140 }
141
142 /**
Zach Johnson42ecc9e2015-05-22 15:56:09 -0700143 * A wrapper around ICarrierService that forwards calls to implementations of
144 * {@link CarrierService}.
Jonathan Basseri49b54d62017-07-25 13:34:36 -0700145 * @hide
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800146 */
Jonathan Basseri49b54d62017-07-25 13:34:36 -0700147 public class ICarrierServiceWrapper extends ICarrierService.Stub {
148 /** @hide */
149 public static final int RESULT_OK = 0;
150 /** @hide */
151 public static final int RESULT_ERROR = 1;
152 /** @hide */
153 public static final String KEY_CONFIG_BUNDLE = "config_bundle";
154
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800155 @Override
Jonathan Basseri49b54d62017-07-25 13:34:36 -0700156 public void getCarrierConfig(CarrierIdentifier id, ResultReceiver result) {
157 try {
158 Bundle data = new Bundle();
159 data.putParcelable(KEY_CONFIG_BUNDLE, CarrierService.this.onLoadConfig(id));
160 result.send(RESULT_OK, data);
161 } catch (Exception e) {
162 Log.e(LOG_TAG, "Error in onLoadConfig: " + e.getMessage(), e);
163 result.send(RESULT_ERROR, null);
164 }
Jonathan Basseri9a1c9b62015-02-25 13:01:52 -0800165 }
166 }
167}