Merge "Add support for new ContextHub and HAL APIs" into sc-dev

This commit is contained in:
TreeHugger Robot
2021-02-17 05:42:48 +00:00
committed by Android (Google) Code Review
6 changed files with 202 additions and 37 deletions

View File

@@ -19,11 +19,11 @@ package com.android.server.location.contexthub;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import android.Manifest;
import android.annotation.Nullable;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.hardware.contexthub.V1_0.ContextHubMsg;
import android.hardware.contexthub.V1_0.IContexthub;
import android.hardware.contexthub.V1_0.Result;
import android.hardware.location.ContextHubInfo;
import android.hardware.location.ContextHubManager;
@@ -39,6 +39,7 @@ import android.util.proto.ProtoOutputStream;
import com.android.server.location.ClientBrokerProto;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
@@ -67,7 +68,7 @@ public class ContextHubClientBroker extends IContextHubClient.Stub
/*
* The proxy to talk to the Context Hub HAL.
*/
private final IContexthub mContextHubProxy;
private final IContextHubWrapper mContextHubProxy;
/*
* The manager that registered this client.
@@ -95,6 +96,12 @@ public class ContextHubClientBroker extends IContextHubClient.Stub
*/
private boolean mRegistered = true;
/**
* String containing an attribution tag that was denoted in the {@link Context} of the
* creator of this broker. This is used when attributing the permissions usage of the broker.
*/
private @Nullable String mAttributionTag;
/*
* Internal interface used to invoke client callbacks.
*/
@@ -176,9 +183,9 @@ public class ContextHubClientBroker extends IContextHubClient.Stub
}
/* package */ ContextHubClientBroker(
Context context, IContexthub contextHubProxy, ContextHubClientManager clientManager,
ContextHubInfo contextHubInfo, short hostEndPointId,
IContextHubClientCallback callback) {
Context context, IContextHubWrapper contextHubProxy,
ContextHubClientManager clientManager, ContextHubInfo contextHubInfo,
short hostEndPointId, IContextHubClientCallback callback, String attributionTag) {
mContext = context;
mContextHubProxy = contextHubProxy;
mClientManager = clientManager;
@@ -187,15 +194,17 @@ public class ContextHubClientBroker extends IContextHubClient.Stub
mCallbackInterface = callback;
mPendingIntentRequest = new PendingIntentRequest();
mPackage = mContext.getPackageManager().getNameForUid(Binder.getCallingUid());
mAttributionTag = attributionTag;
mHasAccessContextHubPermission = context.checkCallingPermission(
Manifest.permission.ACCESS_CONTEXT_HUB) == PERMISSION_GRANTED;
}
/* package */ ContextHubClientBroker(
Context context, IContexthub contextHubProxy, ContextHubClientManager clientManager,
ContextHubInfo contextHubInfo, short hostEndPointId, PendingIntent pendingIntent,
long nanoAppId) {
Context context, IContextHubWrapper contextHubProxy,
ContextHubClientManager clientManager, ContextHubInfo contextHubInfo,
short hostEndPointId, PendingIntent pendingIntent, long nanoAppId,
String attributionTag) {
mContext = context;
mContextHubProxy = contextHubProxy;
mClientManager = clientManager;
@@ -203,6 +212,7 @@ public class ContextHubClientBroker extends IContextHubClient.Stub
mHostEndPointId = hostEndPointId;
mPendingIntentRequest = new PendingIntentRequest(pendingIntent, nanoAppId);
mPackage = pendingIntent.getCreatorPackage();
mAttributionTag = attributionTag;
mHasAccessContextHubPermission = context.checkCallingPermission(
Manifest.permission.ACCESS_CONTEXT_HUB) == PERMISSION_GRANTED;
@@ -227,7 +237,10 @@ public class ContextHubClientBroker extends IContextHubClient.Stub
int contextHubId = mAttachedContextHubInfo.getId();
try {
result = mContextHubProxy.sendMessageToHub(contextHubId, messageToNanoApp);
// TODO(166846988): Fill in host permissions before sending a message.
result = mContextHubProxy.sendMessageToHub(
contextHubId, messageToNanoApp,
new ArrayList<String>() /* hostPermissions */);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in sendMessageToNanoApp (target hub ID = "
+ contextHubId + ")", e);
@@ -262,6 +275,21 @@ public class ContextHubClientBroker extends IContextHubClient.Stub
onClientExit();
}
/**
* Used to override the attribution tag with a newer value if a PendingIntent broker is
* retrieved.
*/
/* package */ void setAttributionTag(String attributionTag) {
mAttributionTag = attributionTag;
}
/**
* @return the attribution tag associated with this broker.
*/
/* package */ String getAttributionTag() {
return mAttributionTag;
}
/**
* @return the ID of the context hub this client is attached to
*/
@@ -508,6 +536,9 @@ public class ContextHubClientBroker extends IContextHubClient.Stub
String out = "[ContextHubClient ";
out += "endpointID: " + getHostEndPointId() + ", ";
out += "contextHub: " + getAttachedContextHubId() + ", ";
if (mAttributionTag != null) {
out += "attributionTag: " + getAttributionTag() + ", ";
}
if (mPendingIntentRequest.isValid()) {
out += "intentCreatorPackage: " + mPackage + ", ";
out += "nanoAppId: 0x" + Long.toHexString(mPendingIntentRequest.getNanoAppId());

View File

@@ -20,7 +20,6 @@ import android.annotation.IntDef;
import android.app.PendingIntent;
import android.content.Context;
import android.hardware.contexthub.V1_0.ContextHubMsg;
import android.hardware.contexthub.V1_0.IContexthub;
import android.hardware.location.ContextHubInfo;
import android.hardware.location.IContextHubClient;
import android.hardware.location.IContextHubClientCallback;
@@ -71,7 +70,7 @@ import java.util.function.Consumer;
/*
* The proxy to talk to the Context Hub.
*/
private final IContexthub mContextHubProxy;
private final IContextHubWrapper mContextHubProxy;
/*
* A mapping of host endpoint IDs to the ContextHubClientBroker object of registered clients.
@@ -138,7 +137,7 @@ import java.util.function.Consumer;
}
/* package */ ContextHubClientManager(
Context context, IContexthub contextHubProxy) {
Context context, IContextHubWrapper contextHubProxy) {
mContext = context;
mContextHubProxy = contextHubProxy;
}
@@ -148,19 +147,21 @@ import java.util.function.Consumer;
*
* @param contextHubInfo the object describing the hub this client is attached to
* @param clientCallback the callback interface of the client to register
* @param attributionTag an optional attribution tag within the given package
*
* @return the client interface
*
* @throws IllegalStateException if max number of clients have already registered
*/
/* package */ IContextHubClient registerClient(
ContextHubInfo contextHubInfo, IContextHubClientCallback clientCallback) {
ContextHubInfo contextHubInfo, IContextHubClientCallback clientCallback,
String attributionTag) {
ContextHubClientBroker broker;
synchronized (this) {
short hostEndPointId = getHostEndPointId();
broker = new ContextHubClientBroker(
mContext, mContextHubProxy, this /* clientManager */, contextHubInfo,
hostEndPointId, clientCallback);
hostEndPointId, clientCallback, attributionTag);
mHostEndPointIdToClientMap.put(hostEndPointId, broker);
mRegistrationRecordDeque.add(
new RegistrationRecord(broker.toString(), ACTION_REGISTERED));
@@ -185,13 +186,15 @@ import java.util.function.Consumer;
* @param pendingIntent the callback interface of the client to register
* @param contextHubInfo the object describing the hub this client is attached to
* @param nanoAppId the ID of the nanoapp to receive Intent events for
* @param attributionTag an optional attribution tag within the given package
*
* @return the client interface
*
* @throws IllegalStateException if there were too many registered clients at the service
*/
/* package */ IContextHubClient registerClient(
ContextHubInfo contextHubInfo, PendingIntent pendingIntent, long nanoAppId) {
ContextHubInfo contextHubInfo, PendingIntent pendingIntent, long nanoAppId,
String attributionTag) {
ContextHubClientBroker broker;
String registerString = "Regenerated";
synchronized (this) {
@@ -201,11 +204,15 @@ import java.util.function.Consumer;
short hostEndPointId = getHostEndPointId();
broker = new ContextHubClientBroker(
mContext, mContextHubProxy, this /* clientManager */, contextHubInfo,
hostEndPointId, pendingIntent, nanoAppId);
hostEndPointId, pendingIntent, nanoAppId, attributionTag);
mHostEndPointIdToClientMap.put(hostEndPointId, broker);
registerString = "Registered";
mRegistrationRecordDeque.add(
new RegistrationRecord(broker.toString(), ACTION_REGISTERED));
} else {
// Update the attribution tag to the latest value provided by the client app in
// case the app was updated and decided to change its tag.
broker.setAttributionTag(attributionTag);
}
}

View File

@@ -27,10 +27,10 @@ import android.hardware.SensorPrivacyManager;
import android.hardware.contexthub.V1_0.AsyncEventType;
import android.hardware.contexthub.V1_0.ContextHub;
import android.hardware.contexthub.V1_0.ContextHubMsg;
import android.hardware.contexthub.V1_0.HubAppInfo;
import android.hardware.contexthub.V1_0.IContexthubCallback;
import android.hardware.contexthub.V1_0.Result;
import android.hardware.contexthub.V1_0.TransactionResult;
import android.hardware.contexthub.V1_2.HubAppInfo;
import android.hardware.contexthub.V1_2.IContexthubCallback;
import android.hardware.location.ContextHubInfo;
import android.hardware.location.ContextHubMessage;
import android.hardware.location.ContextHubTransaction;
@@ -53,6 +53,7 @@ import android.os.RemoteException;
import android.os.UserHandle;
import android.provider.Settings;
import android.util.Log;
import android.util.Pair;
import android.util.proto.ProtoOutputStream;
import com.android.internal.util.DumpUtils;
@@ -137,7 +138,9 @@ public class ContextHubService extends IContextHubService.Stub {
@Override
public void handleClientMsg(ContextHubMsg message) {
handleClientMessageCallback(mContextHubId, message);
handleClientMessageCallback(mContextHubId, message,
Collections.emptyList() /* nanoappPermissions */,
Collections.emptyList() /* messageContentPermissions */);
}
@Override
@@ -156,7 +159,21 @@ public class ContextHubService extends IContextHubService.Stub {
}
@Override
public void handleAppsInfo(ArrayList<HubAppInfo> nanoAppInfoList) {
public void handleAppsInfo(
ArrayList<android.hardware.contexthub.V1_0.HubAppInfo> nanoAppInfoList) {
handleQueryAppsCallback(mContextHubId,
ContextHubServiceUtil.toHubAppInfo_1_2(nanoAppInfoList));
}
@Override
public void handleClientMsg_1_2(android.hardware.contexthub.V1_2.ContextHubMsg message,
ArrayList<String> messageContentPermissions) {
handleClientMessageCallback(mContextHubId, message.msg_1_0, message.permissions,
messageContentPermissions);
}
@Override
public void handleAppsInfo_1_2(ArrayList<HubAppInfo> nanoAppInfoList) {
handleQueryAppsCallback(mContextHubId, nanoAppInfoList);
}
}
@@ -174,30 +191,31 @@ public class ContextHubService extends IContextHubService.Stub {
return;
}
mClientManager = new ContextHubClientManager(mContext, mContextHubWrapper.getHub());
mClientManager = new ContextHubClientManager(mContext, mContextHubWrapper);
mTransactionManager = new ContextHubTransactionManager(
mContextHubWrapper.getHub(), mClientManager, mNanoAppStateManager);
List<ContextHub> hubList;
Pair<List<ContextHub>, List<String>> hubInfo;
try {
hubList = mContextHubWrapper.getHub().getHubs();
hubInfo = mContextHubWrapper.getHubs();
} catch (RemoteException e) {
Log.e(TAG, "RemoteException while getting Context Hub info", e);
hubList = Collections.emptyList();
hubInfo = new Pair(Collections.emptyList(), Collections.emptyList());
}
mContextHubIdToInfoMap = Collections.unmodifiableMap(
ContextHubServiceUtil.createContextHubInfoMap(hubList));
ContextHubServiceUtil.createContextHubInfoMap(hubInfo.first));
mContextHubInfoList = new ArrayList<>(mContextHubIdToInfoMap.values());
HashMap<Integer, IContextHubClient> defaultClientMap = new HashMap<>();
for (int contextHubId : mContextHubIdToInfoMap.keySet()) {
ContextHubInfo contextHubInfo = mContextHubIdToInfoMap.get(contextHubId);
IContextHubClient client = mClientManager.registerClient(
contextHubInfo, createDefaultClientCallback(contextHubId));
contextHubInfo, createDefaultClientCallback(contextHubId),
null /* attributionTag */);
defaultClientMap.put(contextHubId, client);
try {
mContextHubWrapper.getHub().registerCallback(
mContextHubWrapper.registerCallback(
contextHubId, new ContextHubServiceCallback(contextHubId));
} catch (RemoteException e) {
Log.e(TAG, "RemoteException while registering service callback for hub (ID = "
@@ -596,7 +614,9 @@ public class ContextHubService extends IContextHubService.Stub {
* @param contextHubId the ID of the hub the message came from
* @param message the message contents
*/
private void handleClientMessageCallback(int contextHubId, ContextHubMsg message) {
private void handleClientMessageCallback(
int contextHubId, ContextHubMsg message, List<String> nanoappPermissions,
List<String> messageContentPermissions) {
mClientManager.onMessageFromNanoApp(contextHubId, message);
}
@@ -721,7 +741,7 @@ public class ContextHubService extends IContextHubService.Stub {
}
ContextHubInfo contextHubInfo = mContextHubIdToInfoMap.get(contextHubId);
return mClientManager.registerClient(contextHubInfo, clientCallback);
return mClientManager.registerClient(contextHubInfo, clientCallback, attributionTag);
}
/**
@@ -745,7 +765,8 @@ public class ContextHubService extends IContextHubService.Stub {
}
ContextHubInfo contextHubInfo = mContextHubIdToInfoMap.get(contextHubId);
return mClientManager.registerClient(contextHubInfo, pendingIntent, nanoAppId);
return mClientManager.registerClient(
contextHubInfo, pendingIntent, nanoAppId, attributionTag);
}
/**

View File

@@ -23,8 +23,8 @@ import android.content.Context;
import android.hardware.contexthub.V1_0.ContextHub;
import android.hardware.contexthub.V1_0.ContextHubMsg;
import android.hardware.contexthub.V1_0.HostEndPoint;
import android.hardware.contexthub.V1_0.HubAppInfo;
import android.hardware.contexthub.V1_0.Result;
import android.hardware.contexthub.V1_2.HubAppInfo;
import android.hardware.location.ContextHubInfo;
import android.hardware.location.ContextHubTransaction;
import android.hardware.location.NanoAppBinary;
@@ -161,7 +161,8 @@ import java.util.Set;
ArrayList<NanoAppState> nanoAppStateList = new ArrayList<>();
for (HubAppInfo appInfo : nanoAppInfoList) {
nanoAppStateList.add(
new NanoAppState(appInfo.appId, appInfo.version, appInfo.enabled));
new NanoAppState(appInfo.info_1_0.appId, appInfo.info_1_0.version,
appInfo.info_1_0.enabled, appInfo.permissions));
}
return nanoAppStateList;
@@ -255,4 +256,26 @@ import java.util.Set;
return ContextHubTransaction.RESULT_FAILED_UNKNOWN;
}
}
/**
* Converts old list of HubAppInfo received from the HAL to V1.2 HubAppInfo objects.
*
* @param oldInfoList list of V1.0 HubAppInfo objects
* @return list of V1.2 HubAppInfo objects
*/
/* package */
static ArrayList<HubAppInfo> toHubAppInfo_1_2(
ArrayList<android.hardware.contexthub.V1_0.HubAppInfo> oldInfoList) {
ArrayList newAppInfo = new ArrayList<HubAppInfo>();
for (android.hardware.contexthub.V1_0.HubAppInfo oldInfo : oldInfoList) {
HubAppInfo newInfo = new HubAppInfo();
newInfo.info_1_0.appId = oldInfo.appId;
newInfo.info_1_0.version = oldInfo.version;
newInfo.info_1_0.memUsage = oldInfo.memUsage;
newInfo.info_1_0.enabled = oldInfo.enabled;
newInfo.permissions = new ArrayList<String>();
newAppInfo.add(newInfo);
}
return newAppInfo;
}
}

View File

@@ -16,11 +16,17 @@
package com.android.server.location.contexthub;
import android.annotation.Nullable;
import android.hardware.contexthub.V1_0.ContextHub;
import android.hardware.contexthub.V1_1.Setting;
import android.hardware.contexthub.V1_1.SettingValue;
import android.hardware.contexthub.V1_2.IContexthubCallback;
import android.os.RemoteException;
import android.util.Log;
import android.util.Pair;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;
/**
@@ -86,6 +92,24 @@ public abstract class IContextHubWrapper {
return (proxy == null) ? null : new ContextHubWrapperV1_2(proxy);
}
/**
* Calls the appropriate getHubs function depending on the HAL version.
*/
public abstract Pair<List<ContextHub>, List<String>> getHubs() throws RemoteException;
/**
* Calls the appropriate registerCallback function depending on the HAL version.
*/
public abstract void registerCallback(
int hubId, IContexthubCallback callback) throws RemoteException;
/**
* Calls the appropriate sendMessageToHub function depending on the HAL version.
*/
public abstract int sendMessageToHub(
int hubId, android.hardware.contexthub.V1_0.ContextHubMsg message,
ArrayList<String> hostPermissions) throws RemoteException;
/**
* @return A valid instance of Contexthub HAL 1.0.
*/
@@ -148,6 +172,21 @@ public abstract class IContextHubWrapper {
mHub = hub;
}
public Pair<List<ContextHub>, List<String>> getHubs() throws RemoteException {
return new Pair(mHub.getHubs(), new ArrayList<String>());
}
public void registerCallback(
int hubId, IContexthubCallback callback) throws RemoteException {
mHub.registerCallback(hubId, callback);
}
public int sendMessageToHub(
int hubId, android.hardware.contexthub.V1_0.ContextHubMsg message,
ArrayList<String> hostPermissions) throws RemoteException {
return mHub.sendMessageToHub(hubId, message);
}
public android.hardware.contexthub.V1_0.IContexthub getHub() {
return mHub;
}
@@ -188,6 +227,21 @@ public abstract class IContextHubWrapper {
mHub = hub;
}
public Pair<List<ContextHub>, List<String>> getHubs() throws RemoteException {
return new Pair(mHub.getHubs(), new ArrayList<String>());
}
public void registerCallback(
int hubId, IContexthubCallback callback) throws RemoteException {
mHub.registerCallback(hubId, callback);
}
public int sendMessageToHub(
int hubId, android.hardware.contexthub.V1_0.ContextHubMsg message,
ArrayList<String> hostPermissions) throws RemoteException {
return mHub.sendMessageToHub(hubId, message);
}
public android.hardware.contexthub.V1_0.IContexthub getHub() {
return mHub;
}
@@ -227,13 +281,42 @@ public abstract class IContextHubWrapper {
}
}
private static class ContextHubWrapperV1_2 extends IContextHubWrapper {
private android.hardware.contexthub.V1_2.IContexthub mHub;
private static class ContextHubWrapperV1_2 extends IContextHubWrapper
implements android.hardware.contexthub.V1_2.IContexthub.getHubs_1_2Callback {
private final android.hardware.contexthub.V1_2.IContexthub mHub;
private Pair<List<ContextHub>, List<String>> mHubInfo =
new Pair<>(Collections.emptyList(), Collections.emptyList());
ContextHubWrapperV1_2(android.hardware.contexthub.V1_2.IContexthub hub) {
mHub = hub;
}
@Override
public void onValues(ArrayList<ContextHub> hubs, ArrayList<String> supportedPermissions) {
mHubInfo = new Pair(hubs, supportedPermissions);
}
public Pair<List<ContextHub>, List<String>> getHubs() throws RemoteException {
mHub.getHubs_1_2(this);
return mHubInfo;
}
public void registerCallback(
int hubId, IContexthubCallback callback) throws RemoteException {
mHub.registerCallback_1_2(hubId, callback);
}
public int sendMessageToHub(
int hubId, android.hardware.contexthub.V1_0.ContextHubMsg message,
ArrayList<String> hostPermissions) throws RemoteException {
android.hardware.contexthub.V1_2.ContextHubMsg newMessage =
new android.hardware.contexthub.V1_2.ContextHubMsg();
newMessage.msg_1_0 = message;
newMessage.permissions = hostPermissions;
return mHub.sendMessageToHub_1_2(hubId, newMessage);
}
public android.hardware.contexthub.V1_0.IContexthub getHub() {
return mHub;
}

View File

@@ -17,7 +17,7 @@
package com.android.server.location.contexthub;
import android.annotation.Nullable;
import android.hardware.contexthub.V1_0.HubAppInfo;
import android.hardware.contexthub.V1_2.HubAppInfo;
import android.hardware.location.NanoAppInstanceInfo;
import android.util.Log;
@@ -154,8 +154,8 @@ import java.util.function.Consumer;
synchronized void updateCache(int contextHubId, List<HubAppInfo> nanoAppInfoList) {
HashSet<Long> nanoAppIdSet = new HashSet<>();
for (HubAppInfo appInfo : nanoAppInfoList) {
handleQueryAppEntry(contextHubId, appInfo.appId, appInfo.version);
nanoAppIdSet.add(appInfo.appId);
handleQueryAppEntry(contextHubId, appInfo.info_1_0.appId, appInfo.info_1_0.version);
nanoAppIdSet.add(appInfo.info_1_0.appId);
}
Iterator<NanoAppInstanceInfo> iterator = mNanoAppHash.values().iterator();