diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-03-22 01:25:29 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2023-03-22 01:25:29 +0000 |
commit | 7ab729c5c04a1c4d0d539149e5588f6f670ca022 (patch) | |
tree | 85576bbfd06369ed4aeecc21a12581ee1c20aab2 | |
parent | d7a39e248e3fbf66650ea8100116d9a15879dc89 (diff) | |
parent | 2badd60a6afcb3fc5d8ef79aa621fbadd5ea2e8c (diff) | |
download | setupcompat-android14-s1-release.tar.gz |
Snap for 9786938 from 2badd60a6afcb3fc5d8ef79aa621fbadd5ea2e8c to udc-releaseandroid-vts-14.0_r4android-vts-14.0_r3android-vts-14.0_r2android-vts-14.0_r1android-security-14.0.0_r8android-security-14.0.0_r7android-security-14.0.0_r6android-security-14.0.0_r5android-security-14.0.0_r4android-security-14.0.0_r3android-security-14.0.0_r2android-security-14.0.0_r1android-platform-14.0.0_r7android-platform-14.0.0_r6android-platform-14.0.0_r5android-platform-14.0.0_r4android-platform-14.0.0_r3android-platform-14.0.0_r2android-platform-14.0.0_r1android-cts-14.0_r4android-cts-14.0_r3android-cts-14.0_r2android-cts-14.0_r1android-14.0.0_r28android-14.0.0_r2android-14.0.0_r15android-14.0.0_r14android-14.0.0_r13android-14.0.0_r1android14-tests-releaseandroid14-security-releaseandroid14-s2-releaseandroid14-s1-releaseandroid14-releaseandroid14-platform-release
Change-Id: Ibf7f4489d296394801fbe4e6f32ced09d3bfde9a
3 files changed, 54 insertions, 71 deletions
diff --git a/bts/java/com/google/android/setupcompat/bts/AbstractSetupBtsReceiver.java b/bts/java/com/google/android/setupcompat/bts/AbstractSetupBtsReceiver.java deleted file mode 100644 index 9e15e30..0000000 --- a/bts/java/com/google/android/setupcompat/bts/AbstractSetupBtsReceiver.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.android.setupcompat.bts; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.os.Binder; -import androidx.annotation.NonNull; -import com.google.android.setupcompat.internal.Preconditions; -import com.google.android.setupcompat.util.Logger; -import java.util.concurrent.Executor; - -/** Class to receive broadcast intent from SUW, and execute the client's task in the executor. */ -public abstract class AbstractSetupBtsReceiver extends BroadcastReceiver { - private static final Logger LOG = new Logger(AbstractSetupBtsReceiver.class); - - @Override - public void onReceive(Context context, Intent intent) { - if (intent != null && getIntentAction().equals(intent.getAction())) { - Executor executor = getExecutor(); - String simpleClassName = this.getClass().getSimpleName(); - if (executor != null) { - executor.execute( - () -> { - Preconditions.ensureNotOnMainThread(simpleClassName + "::onStartTask"); - onStartTask(); - }); - } - } else { - LOG.w( - "[" - + this.getClass().getSimpleName() - + "] Unauthorized binder uid=" - + Binder.getCallingUid() - + ", intentAction=" - + (intent == null ? "(null)" : intent.getAction())); - } - } - - /** - * Gets the intent action that expected to execute the task. Use to avoid the receiver launch - * unexpectedly. - */ - @NonNull - protected abstract String getIntentAction(); - - /** Returns the executor used to execute the task. */ - @NonNull - protected abstract Executor getExecutor(); - - /** Tasks can be done before activity launched, in order to remove the loading before activity. */ - protected abstract void onStartTask(); -} diff --git a/bts/java/com/google/android/setupcompat/bts/AbstractSetupBtsService.java b/bts/java/com/google/android/setupcompat/bts/AbstractSetupBtsService.java index eb10693..80066ab 100644 --- a/bts/java/com/google/android/setupcompat/bts/AbstractSetupBtsService.java +++ b/bts/java/com/google/android/setupcompat/bts/AbstractSetupBtsService.java @@ -256,7 +256,7 @@ public abstract class AbstractSetupBtsService extends Service { for (Signature signature : info.signingInfo.getApkContentsSigners()) { if (SETUP_WIZARD_RELEASE_CERTIFICATE_STRING.equals(signature.toCharsString()) - || (allowDebugKeys + || (isAllowDebugKeysOrBuild() && SETUP_WIZARD_DEBUG_CERTIFICATE_STRING.equals(signature.toCharsString()))) { return true; } @@ -273,6 +273,10 @@ public abstract class AbstractSetupBtsService extends Service { return false; } + private boolean isAllowDebugKeysOrBuild() { + return Build.TYPE.equals("userdebug") || Build.TYPE.equals("eng") || allowDebugKeys; + } + @VisibleForTesting boolean verifyCallingAppPermission() { int checkPermission = diff --git a/main/java/com/google/android/setupcompat/logging/SetupMetricsLogger.java b/main/java/com/google/android/setupcompat/logging/SetupMetricsLogger.java index 786494e..fab38a2 100644 --- a/main/java/com/google/android/setupcompat/logging/SetupMetricsLogger.java +++ b/main/java/com/google/android/setupcompat/logging/SetupMetricsLogger.java @@ -16,8 +16,10 @@ package com.google.android.setupcompat.logging; +import android.annotation.SuppressLint; import android.content.Context; import androidx.annotation.NonNull; +import androidx.annotation.VisibleForTesting; import com.google.android.setupcompat.internal.Preconditions; import com.google.android.setupcompat.internal.SetupCompatServiceInvoker; import com.google.android.setupcompat.logging.internal.MetricBundleConverter; @@ -25,7 +27,10 @@ import com.google.android.setupcompat.logging.internal.SetupMetricsLoggingConsta import com.google.android.setupcompat.util.Logger; import java.util.concurrent.TimeUnit; -/** SetupMetricsLogger provides an easy way to log custom metrics to SetupWizard. */ +/** + * SetupMetricsLogger provides an easy way to log custom metrics to SetupWizard. + * (go/suw-metrics-collection-api) + */ public class SetupMetricsLogger { private static final Logger LOG = new Logger("SetupMetricsLogger"); @@ -76,7 +81,7 @@ public class SetupMetricsLogger { } /** - * Logs setup collection metrics (go/suw-metrics-collection-api) + * Logs setup collection metrics */ public static void logMetrics( @NonNull Context context, @NonNull ScreenKey screenKey, @NonNull SetupMetric... metrics) { @@ -92,4 +97,46 @@ public class SetupMetricsLogger { MetricBundleConverter.createBundleForLoggingSetupMetric(screenKey, metric)); } } + + /** + * A non-static method to log setup collection metrics calling + * {@link #logMetrics(Context, ScreenKey, SetupMetric...)} as the actual implementation. This + * function is useful when performing unit tests in caller's implementation. + * <p> + * For unit testing, caller uses {@link #setInstanceForTesting(SetupMetricsLogger)} to inject the + * mocked SetupMetricsLogger instance and use {@link SetupMetricsLogger#get(Context)} to get the + * SetupMetricsLogger. And verify the this function is called with expected parameters. + * + * @see #logMetrics(Context, ScreenKey, SetupMetric...) + */ + public void logMetrics(@NonNull ScreenKey screenKey, @NonNull SetupMetric... metrics) { + SetupMetricsLogger.logMetrics(context, screenKey, metrics); + } + + private SetupMetricsLogger(Context context) { + this.context = context; + } + + private final Context context; + + /** Use this function to get a singleton of {@link SetupMetricsLogger} */ + public static synchronized SetupMetricsLogger get(Context context) { + if (instance == null) { + instance = new SetupMetricsLogger(context.getApplicationContext()); + } + + return instance; + } + + @VisibleForTesting + public static void setInstanceForTesting(SetupMetricsLogger testInstance) { + instance = testInstance; + } + + // The instance is coming from Application context which alive during the application activate and + // it's not depend on the activities life cycle, so we can avoid memory leak. However linter + // cannot distinguish Application context or activity context, so we add @SuppressLint to avoid + // lint error. + @SuppressLint("StaticFieldLeak") + private static SetupMetricsLogger instance; } |