Commit 33a1f4f7 authored by Andrew Grieve's avatar Andrew Grieve Committed by Commit Bot

Android: Upstream code for variable service in BindService.java

Bug: 1003532
Change-Id: I8b3ec212067c0bfd145258e645c42db7ae140790
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1919734
Commit-Queue: Andrew Grieve <agrieve@chromium.org>
Reviewed-by: default avatarBo <boliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#716341}
parent 546bd201
......@@ -6,6 +6,9 @@ package org.chromium.base.compat;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.telephony.CellInfo;
import android.telephony.TelephonyManager;
......@@ -15,6 +18,7 @@ import org.chromium.base.annotations.VerifiesOnQ;
import org.chromium.base.task.AsyncTask;
import java.util.List;
import java.util.concurrent.Executor;
/**
* Utility class to use new APIs that were added in Q (API level 29). These need to exist in a
......@@ -38,4 +42,14 @@ public final class ApiHelperForQ {
}
});
}
public static boolean bindIsolatedService(Context context, Intent intent, int flags,
String instanceName, Executor executor, ServiceConnection connection) {
return context.bindIsolatedService(intent, flags, instanceName, executor, connection);
}
public static void updateServiceGroup(
Context context, ServiceConnection connection, int group, int importance) {
context.updateServiceGroup(connection, group, importance);
}
}
......@@ -16,7 +16,7 @@ import android.os.UserHandle;
import org.chromium.base.BuildConfig;
import org.chromium.base.BuildInfo;
import org.chromium.base.StrictModeContext;
import org.chromium.base.compat.ApiHelperForQ;
import java.lang.reflect.Method;
import java.util.concurrent.Executor;
......@@ -25,45 +25,19 @@ import java.util.concurrent.Executor;
* Class of static helper methods to call Context.bindService variants.
*/
final class BindService {
private static final Method sDoBindServiceQMethod;
private static final Method sUpdateServiceGroupQMethod;
static {
Method bindMethod = null;
Method updateServiceGroupMethod = null;
try (StrictModeContext ignored = StrictModeContext.allowDiskReads()) {
if (BuildInfo.isAtLeastQ() && !BuildConfig.IS_INCREMENTAL_INSTALL) {
Class<?> clazz =
Class.forName("org.chromium.base.process_launcher.BindServiceInternal");
bindMethod = clazz.getDeclaredMethod("doBindServiceQ", Context.class, Intent.class,
ServiceConnection.class, int.class, Executor.class, String.class);
updateServiceGroupMethod = clazz.getDeclaredMethod("updateServiceGroupQ",
Context.class, ServiceConnection.class, int.class, int.class);
}
} catch (Exception e) {
// Ignore exceptions.
} finally {
sDoBindServiceQMethod = bindMethod;
sUpdateServiceGroupQMethod = updateServiceGroupMethod;
}
}
private static Method sBindServiceAsUserMethod;
static boolean supportVariableConnections() {
return sDoBindServiceQMethod != null && sUpdateServiceGroupQMethod != null;
return BuildInfo.isAtLeastQ() && !BuildConfig.IS_INCREMENTAL_INSTALL;
}
// Note that handler is not guaranteed to be used, and client still need to correctly handle
// callbacks on the UI thread.
static boolean doBindService(Context context, Intent intent, ServiceConnection connection,
int flags, Handler handler, Executor executor, String instanceName) {
if (supportVariableConnections()) {
try {
return (boolean) sDoBindServiceQMethod.invoke(
null, context, intent, connection, flags, executor, instanceName);
} catch (Exception e) {
throw new RuntimeException(e);
}
if (supportVariableConnections() && instanceName != null) {
return ApiHelperForQ.bindIsolatedService(
context, intent, flags, instanceName, executor, connection);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
......@@ -82,15 +56,6 @@ final class BindService {
}
}
static void updateServiceGroup(
Context context, ServiceConnection connection, int group, int importance) {
try {
sUpdateServiceGroupQMethod.invoke(null, context, connection, group, importance);
} catch (ReflectiveOperationException e) {
// Ignore reflection errors.
}
}
private static boolean bindServiceByCall(
Context context, Intent intent, ServiceConnection connection, int flags) {
return context.bindService(intent, connection, flags);
......
......@@ -23,6 +23,7 @@ import org.chromium.base.MemoryPressureLevel;
import org.chromium.base.MemoryPressureListener;
import org.chromium.base.ThreadUtils;
import org.chromium.base.TraceEvent;
import org.chromium.base.compat.ApiHelperForQ;
import org.chromium.base.memory.MemoryPressureCallback;
import java.util.Arrays;
......@@ -160,7 +161,7 @@ public class ChildProcessConnection {
public void updateGroupImportance(int group, int importanceInGroup) {
assert isBound();
if (BindService.supportVariableConnections()) {
BindService.updateServiceGroup(mContext, this, group, importanceInGroup);
ApiHelperForQ.updateServiceGroup(mContext, this, group, importanceInGroup);
BindService.doBindService(mContext, mBindIntent, this, mBindFlags, mHandler,
mExecutor, mInstanceName);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment