package android.telephony.mbms;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.*;
import android.content.pm.ServiceInfo;
import android.telephony.MbmsDownloadSession;
import android.telephony.MbmsStreamingSession;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class MbmsUtils {
private static final String LOG_TAG = "MbmsUtils";
public static boolean isContainedIn(File parent, File child) {
try {
String parentPath = parent.getCanonicalPath();
String childPath = child.getCanonicalPath();
return childPath.startsWith(parentPath);
} catch (IOException e) {
throw new RuntimeException("Failed to resolve canonical paths: " + e);
}
}
public static ComponentName toComponentName(ComponentInfo ci) {
return new ComponentName(ci.packageName, ci.name);
}
public static ComponentName getOverrideServiceName(Context context, String serviceAction) {
String metaDataKey = null;
switch (serviceAction) {
case MbmsDownloadSession.MBMS_DOWNLOAD_SERVICE_ACTION:
metaDataKey = MbmsDownloadSession.MBMS_DOWNLOAD_SERVICE_OVERRIDE_METADATA;
break;
case MbmsStreamingSession.MBMS_STREAMING_SERVICE_ACTION:
metaDataKey = MbmsStreamingSession.MBMS_STREAMING_SERVICE_OVERRIDE_METADATA;
break;
}
if (metaDataKey == null) {
return null;
}
ApplicationInfo appInfo;
try {
appInfo = context.getPackageManager()
.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
return null;
}
if (appInfo.metaData == null) {
return null;
}
String serviceComponent = appInfo.metaData.getString(metaDataKey);
if (serviceComponent == null) {
return null;
}
return ComponentName.unflattenFromString(serviceComponent);
}
public static ServiceInfo getMiddlewareServiceInfo(Context context, String serviceAction) {
PackageManager packageManager = context.getPackageManager();
Intent queryIntent = new Intent();
queryIntent.setAction(serviceAction);
ComponentName overrideService = getOverrideServiceName(context, serviceAction);
List<ResolveInfo> services;
if (overrideService == null) {
services = packageManager.queryIntentServices(queryIntent,
PackageManager.MATCH_SYSTEM_ONLY);
} else {
queryIntent.setComponent(overrideService);
services = packageManager.queryIntentServices(queryIntent,
PackageManager.MATCH_ALL);
}
if (services == null || services.size() == 0) {
Log.w(LOG_TAG, "No MBMS services found, cannot get service info");
return null;
}
if (services.size() > 1) {
Log.w(LOG_TAG, "More than one MBMS service found, cannot get unique service");
return null;
}
return services.get(0).serviceInfo;
}
public static int startBinding(Context context, String serviceAction,
ServiceConnection serviceConnection) {
Intent bindIntent = new Intent();
ServiceInfo mbmsServiceInfo =
MbmsUtils.getMiddlewareServiceInfo(context, serviceAction);
if (mbmsServiceInfo == null) {
return MbmsErrors.ERROR_NO_UNIQUE_MIDDLEWARE;
}
bindIntent.setComponent(MbmsUtils.toComponentName(mbmsServiceInfo));
context.bindService(bindIntent, serviceConnection, Context.BIND_AUTO_CREATE);
return MbmsErrors.SUCCESS;
}
public static File getEmbmsTempFileDirForService(Context context, String serviceId) {
String sanitizedServiceId = serviceId.replaceAll("[^a-zA-Z0-9_]", "_");
File embmsTempFileDir = MbmsTempFileProvider.getEmbmsTempFileDir(context);
return new File(embmsTempFileDir, sanitizedServiceId);
}
}