Merge "Changes to MbmsStreamingManager for test app"

am: df57734354

Change-Id: I661e2cb4609f556d31e464d1cadff459b9b80354
This commit is contained in:
Hall Liu
2017-05-10 01:05:26 +00:00
committed by android-build-merger
3 changed files with 149 additions and 32 deletions

View File

@@ -23,7 +23,7 @@ import android.telephony.mbms.DownloadCallback;
import android.telephony.mbms.DownloadRequest; import android.telephony.mbms.DownloadRequest;
import android.telephony.mbms.DownloadStatus; import android.telephony.mbms.DownloadStatus;
import android.telephony.mbms.IMbmsDownloadManagerCallback; import android.telephony.mbms.IMbmsDownloadManagerCallback;
import android.telephony.mbms.MbmsInitializationException; import android.telephony.mbms.MbmsException;
import android.telephony.mbms.vendor.IMbmsDownloadService; import android.telephony.mbms.vendor.IMbmsDownloadService;
import android.util.Log; import android.util.Log;
@@ -172,7 +172,7 @@ public class MbmsDownloadManager {
*/ */
public static MbmsDownloadManager createManager(Context context, public static MbmsDownloadManager createManager(Context context,
IMbmsDownloadManagerCallback listener, String downloadAppName) IMbmsDownloadManagerCallback listener, String downloadAppName)
throws MbmsInitializationException{ throws MbmsException {
MbmsDownloadManager mdm = new MbmsDownloadManager(context, listener, downloadAppName, MbmsDownloadManager mdm = new MbmsDownloadManager(context, listener, downloadAppName,
SubscriptionManager.getDefaultSubscriptionId()); SubscriptionManager.getDefaultSubscriptionId());
mdm.bindAndInitialize(); mdm.bindAndInitialize();
@@ -190,19 +190,19 @@ public class MbmsDownloadManager {
public static MbmsDownloadManager createManager(Context context, public static MbmsDownloadManager createManager(Context context,
IMbmsDownloadManagerCallback listener, String downloadAppName, int subId) IMbmsDownloadManagerCallback listener, String downloadAppName, int subId)
throws MbmsInitializationException { throws MbmsException {
MbmsDownloadManager mdm = new MbmsDownloadManager(context, listener, downloadAppName, MbmsDownloadManager mdm = new MbmsDownloadManager(context, listener, downloadAppName,
subId); subId);
mdm.bindAndInitialize(); mdm.bindAndInitialize();
return mdm; return mdm;
} }
private void bindAndInitialize() throws MbmsInitializationException { private void bindAndInitialize() throws MbmsException {
// TODO: bind // TODO: bind
try { try {
mService.initialize(mDownloadAppName, mSubId, mCallback); mService.initialize(mDownloadAppName, mSubId, mCallback);
} catch (RemoteException e) { } catch (RemoteException e) {
throw new MbmsInitializationException(0); // TODO: proper error code throw new MbmsException(0); // TODO: proper error code
} }
} }

View File

@@ -16,25 +16,68 @@
package android.telephony; package android.telephony;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.DeadObjectException;
import android.os.IBinder;
import android.os.RemoteException; import android.os.RemoteException;
import android.telephony.mbms.IMbmsStreamingManagerCallback; import android.telephony.mbms.IMbmsStreamingManagerCallback;
import android.telephony.mbms.IStreamingServiceCallback; import android.telephony.mbms.IStreamingServiceCallback;
import android.telephony.mbms.MbmsInitializationException; import android.telephony.mbms.MbmsException;
import android.telephony.mbms.StreamingService; import android.telephony.mbms.StreamingService;
import android.telephony.mbms.StreamingServiceInfo; import android.telephony.mbms.StreamingServiceInfo;
import android.telephony.mbms.vendor.IMbmsStreamingService; import android.telephony.mbms.vendor.IMbmsStreamingService;
import android.util.Log; import android.util.Log;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID; import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;
/** @hide */ /** @hide */
public class MbmsStreamingManager { public class MbmsStreamingManager {
private interface ServiceListener {
void onServiceConnected();
void onServiceDisconnected();
}
private static final String LOG_TAG = "MbmsStreamingManager"; private static final String LOG_TAG = "MbmsStreamingManager";
public static final String MBMS_STREAMING_SERVICE_ACTION =
"android.telephony.action.EmbmsStreaming";
private static final boolean DEBUG = true; private static final boolean DEBUG = true;
private static final int BIND_TIMEOUT_MS = 3000;
private IMbmsStreamingService mService; private IMbmsStreamingService mService;
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
if (service != null) {
Log.i(LOG_TAG, String.format("Connected to service %s", name));
synchronized (MbmsStreamingManager.this) {
mService = IMbmsStreamingService.Stub.asInterface(service);
mServiceListeners.forEach(ServiceListener::onServiceConnected);
}
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(LOG_TAG, String.format("Disconnected from service %s", name));
synchronized (MbmsStreamingManager.this) {
mService = null;
mServiceListeners.forEach(ServiceListener::onServiceDisconnected);
}
}
};
private List<ServiceListener> mServiceListeners = new LinkedList<>();
private IMbmsStreamingManagerCallback mCallbackToApp; private IMbmsStreamingManagerCallback mCallbackToApp;
private final String mAppName; private final String mAppName;
@@ -54,9 +97,8 @@ public class MbmsStreamingManager {
* Create a new MbmsStreamingManager using the given subscription ID. * Create a new MbmsStreamingManager using the given subscription ID.
* *
* Note that this call will bind a remote service and that may take a bit. This * Note that this call will bind a remote service and that may take a bit. This
* may throw an IllegalArgumentException or RemoteException. * may throw an {@link MbmsException}, indicating errors that may happen during
* TODO: document this and add exceptions that can be thrown for synchronous * the initialization or binding process.
* initialization/bind errors
* *
* @param context * @param context
* @param listener * @param listener
@@ -66,7 +108,7 @@ public class MbmsStreamingManager {
*/ */
public static MbmsStreamingManager create(Context context, public static MbmsStreamingManager create(Context context,
IMbmsStreamingManagerCallback listener, String streamingAppName, int subscriptionId) IMbmsStreamingManagerCallback listener, String streamingAppName, int subscriptionId)
throws MbmsInitializationException { throws MbmsException {
MbmsStreamingManager manager = new MbmsStreamingManager(context, listener, MbmsStreamingManager manager = new MbmsStreamingManager(context, listener,
streamingAppName, subscriptionId); streamingAppName, subscriptionId);
manager.bindAndInitialize(); manager.bindAndInitialize();
@@ -81,9 +123,8 @@ public class MbmsStreamingManager {
*/ */
public static MbmsStreamingManager create(Context context, public static MbmsStreamingManager create(Context context,
IMbmsStreamingManagerCallback listener, String streamingAppName) IMbmsStreamingManagerCallback listener, String streamingAppName)
throws MbmsInitializationException { throws MbmsException {
// TODO: get default sub id int subId = SubscriptionManager.getDefaultSubscriptionId();
int subId = INVALID_SUBSCRIPTION_ID;
MbmsStreamingManager manager = new MbmsStreamingManager(context, listener, MbmsStreamingManager manager = new MbmsStreamingManager(context, listener,
streamingAppName, subId); streamingAppName, subId);
manager.bindAndInitialize(); manager.bindAndInitialize();
@@ -94,8 +135,17 @@ public class MbmsStreamingManager {
* Terminates this instance, ending calls to the registered listener. Also terminates * Terminates this instance, ending calls to the registered listener. Also terminates
* any streaming services spawned from this instance. * any streaming services spawned from this instance.
*/ */
public void dispose() { public synchronized void dispose() {
// service.dispose(streamingAppName); if (mService == null) {
// Ignore and return, assume already disposed.
return;
}
try {
mService.dispose(mAppName, mSubscriptionId);
} catch (RemoteException e) {
// Ignore for now
}
mService = null;
} }
/** /**
@@ -155,23 +205,79 @@ public class MbmsStreamingManager {
return 0; return 0;
} }
private void logd(String str) { private void bindAndInitialize() throws MbmsException {
Log.d(LOG_TAG, str); // Query for the proper service
} PackageManager packageManager = mContext.getPackageManager();
Intent queryIntent = new Intent();
queryIntent.setAction(MBMS_STREAMING_SERVICE_ACTION);
List<ResolveInfo> streamingServices = packageManager.queryIntentServices(queryIntent,
PackageManager.MATCH_SYSTEM_ONLY);
private boolean isServiceConnected() { if (streamingServices == null || streamingServices.size() == 0) {
return mService != null; throw new MbmsException(
} MbmsException.ERROR_NO_SERVICE_INSTALLED);
}
if (streamingServices.size() > 1) {
throw new MbmsException(
MbmsException.ERROR_MULTIPLE_SERVICES_INSTALLED);
}
private void bindAndInitialize() throws MbmsInitializationException { // Kick off the binding, and synchronously wait until binding is complete
// TODO: bind to the service final CountDownLatch latch = new CountDownLatch(1);
try { ServiceListener bindListener = new ServiceListener() {
int returnCode = mService.initialize(mCallbackToApp, mAppName, mSubscriptionId); @Override
if (returnCode != 0) { public void onServiceConnected() {
throw new MbmsInitializationException(returnCode); latch.countDown();
}
@Override
public void onServiceDisconnected() {
}
};
synchronized (this) {
mServiceListeners.add(bindListener);
}
Intent bindIntent = new Intent();
bindIntent.setComponent(streamingServices.get(0).getComponentInfo().getComponentName());
mContext.bindService(bindIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
waitOnLatchWithTimeout(latch, BIND_TIMEOUT_MS);
// Remove the listener and call the initialization method through the interface.
synchronized (this) {
mServiceListeners.remove(bindListener);
if (mService == null) {
throw new MbmsException(MbmsException.ERROR_BIND_TIMEOUT_OR_FAILURE);
}
try {
int returnCode = mService.initialize(mCallbackToApp, mAppName, mSubscriptionId);
if (returnCode != MbmsException.SUCCESS) {
throw new MbmsException(returnCode);
}
} catch (RemoteException e) {
mService = null;
Log.e(LOG_TAG, "Service died before initialization");
throw new MbmsException(MbmsException.ERROR_INITIALIZATION_REMOTE_EXCEPTION);
}
}
}
private static void waitOnLatchWithTimeout(CountDownLatch l, long timeoutMs) {
long endTime = System.currentTimeMillis() + timeoutMs;
while (System.currentTimeMillis() < endTime) {
try {
l.await(timeoutMs, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
// keep waiting
}
if (l.getCount() <= 0) {
return;
} }
} catch (RemoteException e) {
throw new MbmsInitializationException(/* some error */ 0);
} }
} }
} }

View File

@@ -16,12 +16,23 @@
package android.telephony.mbms; package android.telephony.mbms;
import android.os.RemoteException;
/** @hide */ /** @hide */
public class MbmsInitializationException extends Exception { public class MbmsException extends RemoteException {
public static final int SUCCESS = 0;
public static final int ERROR_NO_SERVICE_INSTALLED = 1;
public static final int ERROR_MULTIPLE_SERVICES_INSTALLED = 2;
public static final int ERROR_BIND_TIMEOUT_OR_FAILURE = 3;
public static final int ERROR_INITIALIZATION_REMOTE_EXCEPTION = 4;
public static final int ERROR_ALREADY_INITIALIZED = 5;
private final int mErrorCode; private final int mErrorCode;
/** @hide */ /** @hide
public MbmsInitializationException(int errorCode) { * TODO: future systemapi
* */
public MbmsException(int errorCode) {
super(); super();
mErrorCode = errorCode; mErrorCode = errorCode;
} }