Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b2f2546b authored by Lili Zou's avatar Lili Zou Committed by Android (Google) Code Review
Browse files

Merge "Fix shell command bugs for supporting CTS test. Add service unit test....

Merge "Fix shell command bugs for supporting CTS test. Add service unit test. Fix javadoc." into tm-qpr-dev
parents 0c296801 7a168cc8
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -153,7 +153,7 @@ public final class AmbientContextManager {
     *   eventTypes.add(AmbientContextEvent.EVENT_SNORE);
     *
     *   // Create Consumer
     *   Consumer<Integer> statusConsumer = response -> {
     *   Consumer<Integer> statusConsumer = status -> {
     *     int status = status.getStatusCode();
     *     if (status == AmbientContextManager.STATUS_SUCCESS) {
     *       // Show user it's enabled
+14 −28
Original line number Diff line number Diff line
@@ -21,12 +21,12 @@ import static java.lang.System.out;
import android.annotation.NonNull;
import android.app.ambientcontext.AmbientContextEvent;
import android.app.ambientcontext.AmbientContextEventRequest;
import android.app.ambientcontext.AmbientContextManager;
import android.content.ComponentName;
import android.os.Binder;
import android.os.RemoteCallback;
import android.os.ShellCommand;
import android.service.ambientcontext.AmbientContextDetectionResult;
import android.service.ambientcontext.AmbientContextDetectionServiceStatus;

import java.io.PrintWriter;

@@ -51,13 +51,13 @@ final class AmbientContextShellCommand extends ShellCommand {
    /** Callbacks for AmbientContextEventService results used internally for testing. */
    static class TestableCallbackInternal {
        private AmbientContextDetectionResult mLastResult;
        private AmbientContextDetectionServiceStatus mLastStatus;
        private int mLastStatus;

        public AmbientContextDetectionResult getLastResult() {
            return mLastResult;
        }

        public AmbientContextDetectionServiceStatus getLastStatus() {
        public int getLastStatus() {
            return mLastStatus;
        }

@@ -80,13 +80,10 @@ final class AmbientContextShellCommand extends ShellCommand {
        @NonNull
        private RemoteCallback createRemoteStatusCallback() {
            return new RemoteCallback(result -> {
                AmbientContextDetectionServiceStatus status =
                        (AmbientContextDetectionServiceStatus) result.get(
                                AmbientContextDetectionServiceStatus.STATUS_RESPONSE_BUNDLE_KEY);
                int status = result.getInt(AmbientContextManager.STATUS_RESPONSE_BUNDLE_KEY);
                final long token = Binder.clearCallingIdentity();
                try {
                    mLastStatus = status;
                    out.println("Status available: " + status);
                } finally {
                    Binder.restoreCallingIdentity(token);
                }
@@ -110,8 +107,6 @@ final class AmbientContextShellCommand extends ShellCommand {
                return runStopDetection();
            case "get-last-status-code":
                return getLastStatusCode();
            case "get-last-package-name":
                return getLastPackageName();
            case "query-service-status":
                return runQueryServiceStatus();
            case "get-bound-package":
@@ -126,7 +121,8 @@ final class AmbientContextShellCommand extends ShellCommand {
    private int runStartDetection() {
        final int userId = Integer.parseInt(getNextArgRequired());
        final String packageName = getNextArgRequired();
        mService.startDetection(userId, REQUEST, packageName,
        mService.startDetection(
                userId, REQUEST, packageName,
                sTestableCallbackInternal.createRemoteDetectionResultCallback(),
                sTestableCallbackInternal.createRemoteStatusCallback());
        return 0;
@@ -151,18 +147,9 @@ final class AmbientContextShellCommand extends ShellCommand {
    }

    private int getLastStatusCode() {
        AmbientContextDetectionServiceStatus lastResponse =
                sTestableCallbackInternal.getLastStatus();
        if (lastResponse == null) {
            return -1;
        }
        return lastResponse.getStatusCode();
    }

    private int getLastPackageName() {
        AmbientContextDetectionServiceStatus lastResponse =
                sTestableCallbackInternal.getLastStatus();
        out.println(lastResponse == null ? "" : lastResponse.getPackageName());
        final PrintWriter resultPrinter = getOutPrintWriter();
        int lastStatus = sTestableCallbackInternal.getLastStatus();
        resultPrinter.println(lastStatus);
        return 0;
    }

@@ -174,22 +161,21 @@ final class AmbientContextShellCommand extends ShellCommand {
        pw.println("    Print this help text.");
        pw.println();
        pw.println("  start-detection USER_ID PACKAGE_NAME: Starts AmbientContextEvent detection.");
        pw.println("  stop-detection USER_ID: Stops AmbientContextEvent detection.");
        pw.println("  stop-detection USER_ID PACKAGE_NAME: Stops AmbientContextEvent detection.");
        pw.println("  get-last-status-code: Prints the latest request status code.");
        pw.println("  get-last-package-name: Prints the latest request package name.");
        pw.println("  query-event-status USER_ID PACKAGE_NAME: Prints the event status code.");
        pw.println("  query-service-status USER_ID PACKAGE_NAME: Prints the service status code.");
        pw.println("  get-bound-package USER_ID:"
                + "     Print the bound package that implements the service.");
        pw.println("  set-temporary-service USER_ID [COMPONENT_NAME DURATION]");
        pw.println("  set-temporary-service USER_ID [PACKAGE_NAME] [COMPONENT_NAME DURATION]");
        pw.println("    Temporarily (for DURATION ms) changes the service implementation.");
        pw.println("    To reset, call with just the USER_ID argument.");
    }

    private int getBoundPackageName() {
        final PrintWriter out = getOutPrintWriter();
        final PrintWriter resultPrinter = getOutPrintWriter();
        final int userId = Integer.parseInt(getNextArgRequired());
        final ComponentName componentName = mService.getComponentName(userId);
        out.println(componentName == null ? "" : componentName.getPackageName());
        resultPrinter.println(componentName == null ? "" : componentName.getPackageName());
        return 0;
    }

+64 −0
Original line number Diff line number Diff line
/*
 * 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.android.server.ambientcontext;

import static com.google.common.truth.Truth.assertThat;

import android.app.PendingIntent;
import android.app.ambientcontext.AmbientContextEvent;
import android.app.ambientcontext.AmbientContextEventRequest;
import android.content.Intent;
import android.os.RemoteCallback;
import android.os.UserHandle;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest;

import org.junit.Test;

/**
 * Unit test for {@link AmbientContextManagerService}.
 * atest FrameworksServicesTests:AmbientContextManagerServiceTest
 */
public class AmbientContextManagerServiceTest {
    public static final String SYSTEM_PACKAGE_NAME = "com.android.frameworks.servicestests";
    private static final int USER_ID = UserHandle.USER_SYSTEM;

    @SmallTest
    @Test
    public void testClientRequest() {
        AmbientContextEventRequest request = new AmbientContextEventRequest.Builder()
                .addEventType(AmbientContextEvent.EVENT_COUGH)
                .build();
        Intent intent = new Intent();
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                InstrumentationRegistry.getTargetContext(), 0,
                intent, PendingIntent.FLAG_IMMUTABLE);
        AmbientContextManagerService.ClientRequest clientRequest =
                new AmbientContextManagerService.ClientRequest(USER_ID, request,
                        pendingIntent, new RemoteCallback(result -> {}));

        assertThat(clientRequest.getRequest()).isEqualTo(request);
        assertThat(clientRequest.getPackageName()).isEqualTo(SYSTEM_PACKAGE_NAME);
        assertThat(clientRequest.hasUserId(USER_ID)).isTrue();
        assertThat(clientRequest.hasUserId(-1)).isFalse();
        assertThat(clientRequest.hasUserIdAndPackageName(USER_ID, SYSTEM_PACKAGE_NAME)).isTrue();
        assertThat(clientRequest.hasUserIdAndPackageName(-1, SYSTEM_PACKAGE_NAME)).isFalse();
        assertThat(clientRequest.hasUserIdAndPackageName(USER_ID, "random.package.name"))
                .isFalse();
    }
}
+1 −0
Original line number Diff line number Diff line
include /services/core/java/com/android/server/ambientcontext/OWNERS