diff options
author | Eugene Susla <eugenesusla@google.com> | 2017-04-21 14:12:00 -0700 |
---|---|---|
committer | Eugene Susla <eugenesusla@google.com> | 2017-04-24 11:17:06 -0700 |
commit | aa43a4ba9f61504a22d10f2c6d233576791e7776 (patch) | |
tree | 85178ae14ceb5ef8c7072556bf565eed65423bae /services/companion | |
parent | 9870d0bef78949f0429de4f62532d98f2537c258 (diff) |
Check for uses-feature in Companion APIs
Fixes: 37629514
Test: Invoke API from app without uses-feature
Ensure an exception with an appropriate message is thrown
Invoke API from app with uses-feature
Ensure no exception
Change-Id: I53665732264ea2de2b4c8c251b1f00bf3a256dad
Diffstat (limited to 'services/companion')
-rw-r--r-- | services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java b/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java index 41b70a1312e8..9bb0f867f20a 100644 --- a/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java +++ b/services/companion/java/com/android/server/companion/CompanionDeviceManagerService.java @@ -36,6 +36,7 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; +import android.content.pm.FeatureInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.net.NetworkPolicyManager; @@ -203,13 +204,15 @@ public class CompanionDeviceManagerService extends SystemService implements Bind checkNotNull(request, "Request cannot be null"); checkNotNull(callback, "Callback cannot be null"); checkCallerIsSystemOr(callingPackage); + int userId = getCallingUserId(); + checkUsesFeature(callingPackage, userId); final long callingIdentity = Binder.clearCallingIdentity(); try { - //TODO bindServiceAsUser - getContext().bindService( + getContext().bindServiceAsUser( new Intent().setComponent(SERVICE_TO_BIND_TO), createServiceConnection(request, callback, callingPackage), - Context.BIND_AUTO_CREATE); + Context.BIND_AUTO_CREATE, + UserHandle.of(userId)); } finally { Binder.restoreCallingIdentity(callingIdentity); } @@ -219,6 +222,7 @@ public class CompanionDeviceManagerService extends SystemService implements Bind public List<String> getAssociations(String callingPackage, int userId) throws RemoteException { checkCallerIsSystemOr(callingPackage, userId); + checkUsesFeature(callingPackage, getCallingUserId()); return CollectionUtils.map( readAllAssociations(userId, callingPackage), a -> a.deviceAddress); @@ -230,6 +234,7 @@ public class CompanionDeviceManagerService extends SystemService implements Bind throws RemoteException { checkNotNull(deviceMacAddress); checkCallerIsSystemOr(callingPackage); + checkUsesFeature(callingPackage, getCallingUserId()); updateAssociations(associations -> CollectionUtils.remove(associations, new Association(getCallingUserId(), deviceMacAddress, callingPackage))); } @@ -282,11 +287,24 @@ public class CompanionDeviceManagerService extends SystemService implements Bind private void checkCanCallNotificationApi(String callingPackage) throws RemoteException { checkCallerIsSystemOr(callingPackage); - checkState(!ArrayUtils.isEmpty(readAllAssociations(getCallingUserId(), callingPackage)), + int userId = getCallingUserId(); + checkState(!ArrayUtils.isEmpty(readAllAssociations(userId, callingPackage)), "App must have an association before calling this API"); + checkUsesFeature(callingPackage, userId); } - } + private void checkUsesFeature(String pkg, int userId) { + FeatureInfo[] reqFeatures = getPackageInfo(pkg, userId).reqFeatures; + String requiredFeature = PackageManager.FEATURE_COMPANION_DEVICE_SETUP; + int numFeatures = ArrayUtils.size(reqFeatures); + for (int i = 0; i < numFeatures; i++) { + if (requiredFeature.equals(reqFeatures[i].name)) return; + } + throw new IllegalStateException("Must declare uses-feature " + + requiredFeature + + " in manifest to use this API"); + } + } private int getCallingUserId() { return UserHandle.getUserId(Binder.getCallingUid()); @@ -398,7 +416,9 @@ public class CompanionDeviceManagerService extends SystemService implements Bind return Binder.withCleanCallingIdentity(() -> { try { return getContext().getPackageManager().getPackageInfoAsUser( - packageName, PackageManager.GET_PERMISSIONS, userId); + packageName, + PackageManager.GET_PERMISSIONS | PackageManager.GET_CONFIGURATIONS, + userId); } catch (PackageManager.NameNotFoundException e) { Slog.e(LOG_TAG, "Failed to get PackageInfo for package " + packageName, e); return null; |