blob: 6b3584c1309ff70faf4ef21c85501537bf6ba215 [file] [log] [blame]
/*
* Copyright 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.telephony;
import android.annotation.CallSuper;
import android.annotation.SystemApi;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.util.SparseArray;
import java.util.ArrayList;
import java.util.List;
/**
* Base class of network service. Services that extend NetworkService must register the service in
* their AndroidManifest to be detected by the framework. They must be protected by the permission
* "android.permission.BIND_NETWORK_SERVICE". The network service definition in the manifest must
* follow the following format:
* ...
* <service android:name=".xxxNetworkService"
* android:permission="android.permission.BIND_NETWORK_SERVICE" >
* <intent-filter>
* <action android:name="android.telephony.NetworkService" />
* </intent-filter>
* </service>
* @hide
*/
@SystemApi
public abstract class NetworkService extends Service {
private final String TAG = NetworkService.class.getSimpleName();
public static final String NETWORK_SERVICE_INTERFACE = "android.telephony.NetworkService";
public static final String NETWORK_SERVICE_EXTRA_SLOT_ID = "android.telephony.extra.SLOT_ID";
private static final int NETWORK_SERVICE_INTERNAL_REQUEST_INITIALIZE_SERVICE = 1;
private static final int NETWORK_SERVICE_GET_REGISTRATION_STATE = 2;
private static final int NETWORK_SERVICE_REGISTER_FOR_STATE_CHANGE = 3;
private static final int NETWORK_SERVICE_UNREGISTER_FOR_STATE_CHANGE = 4;
private static final int NETWORK_SERVICE_INDICATION_NETWORK_STATE_CHANGED = 5;
private final HandlerThread mHandlerThread;
private final NetworkServiceHandler mHandler;
private final SparseArray<NetworkServiceProvider> mServiceMap = new SparseArray<>();
private final SparseArray<INetworkServiceWrapper> mBinderMap = new SparseArray<>();
/**
* The abstract class of the actual network service implementation. The network service provider
* must extend this class to support network connection. Note that each instance of network
* service is associated with one physical SIM slot.
*/
public class NetworkServiceProvider {
private final int mSlotId;
private final List<INetworkServiceCallback>
mNetworkRegistrationStateChangedCallbacks = new ArrayList<>();
public NetworkServiceProvider(int slotId) {
mSlotId = slotId;
}
/**
* @return SIM slot id the network service associated with.
*/
public final int getSlotId() {
return mSlotId;
}
/**
* API to get network registration state. The result will be passed to the callback.
* @param domain
* @param callback
* @return SIM slot id the network service associated with.
*/
public void getNetworkRegistrationState(int domain, NetworkServiceCallback callback) {
callback.onGetNetworkRegistrationStateComplete(
NetworkServiceCallback.RESULT_ERROR_UNSUPPORTED, null);
}
public final void notifyNetworkRegistrationStateChanged() {
mHandler.obtainMessage(NETWORK_SERVICE_INDICATION_NETWORK_STATE_CHANGED,
mSlotId, 0, null).sendToTarget();
}
private void registerForStateChanged(INetworkServiceCallback callback) {
synchronized (mNetworkRegistrationStateChangedCallbacks) {
mNetworkRegistrationStateChangedCallbacks.add(callback);
}
}
private void unregisterForStateChanged(INetworkServiceCallback callback) {
synchronized (mNetworkRegistrationStateChangedCallbacks) {
mNetworkRegistrationStateChangedCallbacks.remove(callback);
}
}
private void notifyStateChangedToCallbacks() {
for (INetworkServiceCallback callback : mNetworkRegistrationStateChangedCallbacks) {
try {
callback.onNetworkStateChanged();
} catch (RemoteException exception) {
// Doing nothing.
}
}
}
/**
* Called when the instance of network service is destroyed (e.g. got unbind or binder died).
*/
@CallSuper
protected void onDestroy() {
mNetworkRegistrationStateChangedCallbacks.clear();
}
}
private class NetworkServiceHandler extends Handler {
NetworkServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message message) {
final int slotId = message.arg1;
final INetworkServiceCallback callback = (INetworkServiceCallback) message.obj;
NetworkServiceProvider service;
synchronized (mServiceMap) {
service = mServiceMap.get(slotId);
}
switch (message.what) {
case NETWORK_SERVICE_INTERNAL_REQUEST_INITIALIZE_SERVICE:
service = createNetworkServiceProvider(message.arg1);
if (service != null) {
mServiceMap.put(slotId, service);
}
break;
case NETWORK_SERVICE_GET_REGISTRATION_STATE:
if (service == null) break;
int domainId = message.arg2;
service.getNetworkRegistrationState(domainId,
new NetworkServiceCallback(callback));
break;
case NETWORK_SERVICE_REGISTER_FOR_STATE_CHANGE:
if (service == null) break;
service.registerForStateChanged(callback);
break;
case NETWORK_SERVICE_UNREGISTER_FOR_STATE_CHANGE:
if (service == null) break;
service.unregisterForStateChanged(callback);
break;
case NETWORK_SERVICE_INDICATION_NETWORK_STATE_CHANGED:
if (service == null) break;
service.notifyStateChangedToCallbacks();
break;
default:
break;
}
}
}
/** @hide */
protected NetworkService() {
mHandlerThread = new HandlerThread(TAG);
mHandlerThread.start();
mHandler = new NetworkServiceHandler(mHandlerThread.getLooper());
log("network service created");
}
/**
* Create the instance of {@link NetworkServiceProvider}. Network service provider must override
* this method to facilitate the creation of {@link NetworkServiceProvider} instances. The system
* will call this method after binding the network service for each active SIM slot id.
*
* @param slotId SIM slot id the network service associated with.
* @return Network service object
*/
protected abstract NetworkServiceProvider createNetworkServiceProvider(int slotId);
/** @hide */
@Override
public IBinder onBind(Intent intent) {
if (intent == null || !NETWORK_SERVICE_INTERFACE.equals(intent.getAction())) {
loge("Unexpected intent " + intent);
return null;
}
int slotId = intent.getIntExtra(
NETWORK_SERVICE_EXTRA_SLOT_ID, SubscriptionManager.INVALID_SIM_SLOT_INDEX);
if (!SubscriptionManager.isValidSlotIndex(slotId)) {
loge("Invalid slot id " + slotId);
return null;
}
log("onBind: slot id=" + slotId);
INetworkServiceWrapper binder = mBinderMap.get(slotId);
if (binder == null) {
Message msg = mHandler.obtainMessage(
NETWORK_SERVICE_INTERNAL_REQUEST_INITIALIZE_SERVICE);
msg.arg1 = slotId;
msg.sendToTarget();
binder = new INetworkServiceWrapper(slotId);
mBinderMap.put(slotId, binder);
}
return binder;
}
/** @hide */
@Override
public boolean onUnbind(Intent intent) {
int slotId = intent.getIntExtra(NETWORK_SERVICE_EXTRA_SLOT_ID,
SubscriptionManager.INVALID_SIM_SLOT_INDEX);
if (mBinderMap.get(slotId) != null) {
NetworkServiceProvider serviceImpl;
synchronized (mServiceMap) {
serviceImpl = mServiceMap.get(slotId);
}
// We assume only one component might bind to the service. So if onUnbind is ever
// called, we destroy the serviceImpl.
if (serviceImpl != null) {
serviceImpl.onDestroy();
}
mBinderMap.remove(slotId);
}
return false;
}
/** @hide */
@Override
public void onDestroy() {
synchronized (mServiceMap) {
for (int i = 0; i < mServiceMap.size(); i++) {
NetworkServiceProvider serviceImpl = mServiceMap.get(i);
if (serviceImpl != null) {
serviceImpl.onDestroy();
}
}
mServiceMap.clear();
}
mHandlerThread.quit();
}
/**
* A wrapper around INetworkService that forwards calls to implementations of
* {@link NetworkService}.
*/
private class INetworkServiceWrapper extends INetworkService.Stub {
private final int mSlotId;
INetworkServiceWrapper(int slotId) {
mSlotId = slotId;
}
@Override
public void getNetworkRegistrationState(int domain, INetworkServiceCallback callback) {
mHandler.obtainMessage(NETWORK_SERVICE_GET_REGISTRATION_STATE, mSlotId,
domain, callback).sendToTarget();
}
@Override
public void registerForNetworkRegistrationStateChanged(INetworkServiceCallback callback) {
mHandler.obtainMessage(NETWORK_SERVICE_REGISTER_FOR_STATE_CHANGE, mSlotId,
0, callback).sendToTarget();
}
@Override
public void unregisterForNetworkRegistrationStateChanged(INetworkServiceCallback callback) {
mHandler.obtainMessage(NETWORK_SERVICE_UNREGISTER_FOR_STATE_CHANGE, mSlotId,
0, callback).sendToTarget();
}
}
private final void log(String s) {
Rlog.d(TAG, s);
}
private final void loge(String s) {
Rlog.e(TAG, s);
}
}