From 17a4a4e2ff4496ce5a272736d8d7d6d3ec55fdcd Mon Sep 17 00:00:00 2001 From: Kathy Chen Date: Thu, 10 Feb 2022 03:19:41 +0000 Subject: Fix a bug where after the client calls startConsentActivity(), the framework is not loading the test activity as expected. Add permission to shell for cts test. Test: end-to-end test with dev aiai and wellbeing apks Bug: 218708840 Change-Id: Ia1c6a3575d85df274b4e6fcbed4881e576fcbfac Ignore-AOSP-First: to prevent new feature leak. --- core/res/res/values/symbols.xml | 3 ++ packages/Shell/AndroidManifest.xml | 3 ++ .../AmbientContextManagerPerUserService.java | 53 ++++++++++++---------- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index d731180bd56a..be14e695556e 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -3699,6 +3699,9 @@ + + + diff --git a/packages/Shell/AndroidManifest.xml b/packages/Shell/AndroidManifest.xml index e9f940a65e83..741fe4f22e74 100644 --- a/packages/Shell/AndroidManifest.xml +++ b/packages/Shell/AndroidManifest.xml @@ -603,6 +603,9 @@ + + + diff --git a/services/core/java/com/android/server/ambientcontext/AmbientContextManagerPerUserService.java b/services/core/java/com/android/server/ambientcontext/AmbientContextManagerPerUserService.java index ab4d856c1197..b73cf5b20ec0 100644 --- a/services/core/java/com/android/server/ambientcontext/AmbientContextManagerPerUserService.java +++ b/services/core/java/com/android/server/ambientcontext/AmbientContextManagerPerUserService.java @@ -29,6 +29,7 @@ import android.app.PendingIntent; import android.app.ambientcontext.AmbientContextEvent; import android.app.ambientcontext.AmbientContextEventRequest; import android.app.ambientcontext.AmbientContextManager; +import android.content.ActivityNotFoundException; import android.content.ComponentName; import android.content.Context; import android.content.Intent; @@ -39,7 +40,6 @@ import android.os.Binder; import android.os.Bundle; import android.os.RemoteCallback; import android.os.RemoteException; -import android.provider.Settings; import android.service.ambientcontext.AmbientContextDetectionResult; import android.service.ambientcontext.AmbientContextDetectionServiceStatus; import android.text.TextUtils; @@ -289,7 +289,7 @@ final class AmbientContextManagerPerUserService extends return; } - if ((recentTasks != null) || recentTasks.getList().isEmpty()) { + if ((recentTasks == null) || recentTasks.getList().isEmpty()) { Slog.e(TAG, "Recent task list is empty!"); return; } @@ -297,40 +297,48 @@ final class AmbientContextManagerPerUserService extends task = recentTasks.getList().get(0); if (!callingPackage.equals(task.topActivityInfo.packageName)) { Slog.e(TAG, "Recent task package name: " + task.topActivityInfo.packageName - + " doesn't match with camera client package name: " + callingPackage); + + " doesn't match with client package name: " + callingPackage); return; } // Start activity as the same task from the callingPackage - Intent intent = new Intent(); ComponentName consentComponent = getConsentComponent(); - if (consentComponent != null) { - Slog.e(TAG, "Starting consent activity for " + callingPackage); + if (consentComponent == null) { + Slog.e(TAG, "Consent component not found!"); + return; + } + + Slog.d(TAG, "Starting consent activity for " + callingPackage); + Intent intent = new Intent(); + final long identity = Binder.clearCallingIdentity(); + try { Context context = getContext(); - String packageNameExtraKey = Settings.Secure.getStringForUser( - context.getContentResolver(), - Settings.Secure.AMBIENT_CONTEXT_PACKAGE_NAME_EXTRA_KEY, - userId); - String eventArrayExtraKey = Settings.Secure.getStringForUser( - context.getContentResolver(), - Settings.Secure.AMBIENT_CONTEXT_EVENT_ARRAY_EXTRA_KEY, - userId); - - // Create consent activity intent with the calling package name and requested events. + String packageNameExtraKey = context.getResources().getString( + com.android.internal.R.string.config_ambientContextPackageNameExtraKey); + String eventArrayExtraKey = context.getResources().getString( + com.android.internal.R.string.config_ambientContextEventArrayExtraKey); + + // Create consent activity intent with the calling package name and requested events intent.setComponent(consentComponent); if (packageNameExtraKey != null) { intent.putExtra(packageNameExtraKey, callingPackage); + } else { + Slog.d(TAG, "Missing packageNameExtraKey for consent activity"); } if (eventArrayExtraKey != null) { intent.putExtra(eventArrayExtraKey, eventTypes); + } else { + Slog.d(TAG, "Missing eventArrayExtraKey for consent activity"); } // Set parent to the calling app's task ActivityOptions options = ActivityOptions.makeBasic(); options.setLaunchTaskId(task.taskId); - context.startActivity(intent, options.toBundle()); - } else { - Slog.e(TAG, "Consent component not found!"); + context.startActivityAsUser(intent, options.toBundle(), context.getUser()); + } catch (ActivityNotFoundException e) { + Slog.e(TAG, "unable to start consent activity"); + } finally { + Binder.restoreCallingIdentity(identity); } } @@ -339,13 +347,12 @@ final class AmbientContextManagerPerUserService extends */ private ComponentName getConsentComponent() { Context context = getContext(); - String consentComponent = Settings.Secure.getStringForUser( - context.getContentResolver(), - Settings.Secure.AMBIENT_CONTEXT_CONSENT_COMPONENT, - getUserId()); + String consentComponent = context.getResources().getString( + com.android.internal.R.string.config_defaultAmbientContextConsentComponent); if (TextUtils.isEmpty(consentComponent)) { return null; } + Slog.i(TAG, "Consent component name: " + consentComponent); return ComponentName.unflattenFromString(consentComponent); } -- cgit v1.2.3-59-g8ed1b