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

Commit a5afd373 authored by Sharon Su's avatar Sharon Su
Browse files

Server implementation of WallpaperEffectsService API.

Test: cts tests
Bug: 213238425
CTS-Coverage-Bug: 213238425
Change-Id: Id94764483b80a62a9fcaf8b2205b63e5cb66473c
parent 0b1dea81
Loading
Loading
Loading
Loading
+2 −0
Original line number Original line Diff line number Diff line
@@ -102,6 +102,7 @@ filegroup {
        ":services.usage-sources",
        ":services.usage-sources",
        ":services.usb-sources",
        ":services.usb-sources",
        ":services.voiceinteraction-sources",
        ":services.voiceinteraction-sources",
        ":services.wallpapereffectsgeneration-sources",
        ":services.wifi-sources",
        ":services.wifi-sources",
    ],
    ],
    visibility: ["//visibility:private"],
    visibility: ["//visibility:private"],
@@ -158,6 +159,7 @@ java_library {
        "services.usage",
        "services.usage",
        "services.usb",
        "services.usb",
        "services.voiceinteraction",
        "services.voiceinteraction",
        "services.wallpapereffectsgeneration",
        "services.wifi",
        "services.wifi",
        "service-blobstore",
        "service-blobstore",
        "service-jobscheduler",
        "service-jobscheduler",
+10 −1
Original line number Original line Diff line number Diff line
@@ -369,6 +369,8 @@ public final class SystemServer implements Dumpable {
            "com.android.server.adb.AdbService$Lifecycle";
            "com.android.server.adb.AdbService$Lifecycle";
    private static final String SPEECH_RECOGNITION_MANAGER_SERVICE_CLASS =
    private static final String SPEECH_RECOGNITION_MANAGER_SERVICE_CLASS =
            "com.android.server.speech.SpeechRecognitionManagerService";
            "com.android.server.speech.SpeechRecognitionManagerService";
    private static final String WALLPAPER_EFFECTS_GENERATION_MANAGER_SERVICE_CLASS =
            "com.android.server.wallpapereffectsgeneration.WallpaperEffectsGenerationManagerService";
    private static final String APP_PREDICTION_MANAGER_SERVICE_CLASS =
    private static final String APP_PREDICTION_MANAGER_SERVICE_CLASS =
            "com.android.server.appprediction.AppPredictionManagerService";
            "com.android.server.appprediction.AppPredictionManagerService";
    private static final String CONTENT_SUGGESTIONS_SERVICE_CLASS =
    private static final String CONTENT_SUGGESTIONS_SERVICE_CLASS =
@@ -1891,7 +1893,6 @@ public final class SystemServer implements Dumpable {
            }
            }
            t.traceEnd();
            t.traceEnd();



            t.traceBegin("StartIpSecService");
            t.traceBegin("StartIpSecService");
            try {
            try {
                ipSecService = IpSecService.create(context);
                ipSecService = IpSecService.create(context);
@@ -2132,6 +2133,14 @@ public final class SystemServer implements Dumpable {
                Slog.i(TAG, "Wallpaper service disabled by config");
                Slog.i(TAG, "Wallpaper service disabled by config");
            }
            }


            // WallpaperEffectsGeneration manager service
            // TODO (b/135218095): Use deviceHasConfigString(context,
            //  R.string.config_defaultWallpaperEffectsGenerationService)
            t.traceBegin("StartWallpaperEffectsGenerationService");
            mSystemServiceManager.startService(
                    WALLPAPER_EFFECTS_GENERATION_MANAGER_SERVICE_CLASS);
            t.traceEnd();

            t.traceBegin("StartAudioService");
            t.traceBegin("StartAudioService");
            if (!isArc) {
            if (!isArc) {
                mSystemServiceManager.startService(AudioService.Lifecycle.class);
                mSystemServiceManager.startService(AudioService.Lifecycle.class);
+22 −0
Original line number Original line Diff line number Diff line
package {
    // See: http://go/android-license-faq
    // A large-scale-change added 'default_applicable_licenses' to import
    // all of the 'license_kinds' from "frameworks_base_license"
    // to get the below license kinds:
    //   SPDX-license-identifier-Apache-2.0
    default_applicable_licenses: ["frameworks_base_license"],
}

filegroup {
    name: "services.wallpapereffectsgeneration-sources",
    srcs: ["java/**/*.java"],
    path: "java",
    visibility: ["//frameworks/base/services"],
}

java_library_static {
    name: "services.wallpapereffectsgeneration",
    defaults: ["platform_service_defaults"],
    srcs: [":services.wallpapereffectsgeneration-sources"],
    libs: ["services.core"],
}
+113 −0
Original line number Original line 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.wallpapereffectsgeneration;

import android.annotation.NonNull;
import android.content.ComponentName;
import android.content.Context;
import android.os.IBinder;
import android.service.wallpapereffectsgeneration.IWallpaperEffectsGenerationService;
import android.service.wallpapereffectsgeneration.WallpaperEffectsGenerationService;
import android.text.format.DateUtils;

import com.android.internal.infra.AbstractMultiplePendingRequestsRemoteService;


/**
 * Proxy to the
 * {@link android.service.wallpapereffectsgeneration.WallpaperEffectsGenerationService}
 * implementation in another process.
 */
public class RemoteWallpaperEffectsGenerationService extends
        AbstractMultiplePendingRequestsRemoteService<RemoteWallpaperEffectsGenerationService,
                IWallpaperEffectsGenerationService> {

    private static final String TAG =
            RemoteWallpaperEffectsGenerationService.class.getSimpleName();

    private static final long TIMEOUT_REMOTE_REQUEST_MILLIS = 2 * DateUtils.SECOND_IN_MILLIS;

    private final RemoteWallpaperEffectsGenerationServiceCallback mCallback;

    public RemoteWallpaperEffectsGenerationService(Context context,
            ComponentName componentName, int userId,
            RemoteWallpaperEffectsGenerationServiceCallback callback,
            boolean bindInstantServiceAllowed,
            boolean verbose) {
        super(context, WallpaperEffectsGenerationService.SERVICE_INTERFACE,
                componentName, userId, callback,
                context.getMainThreadHandler(),
                bindInstantServiceAllowed ? Context.BIND_ALLOW_INSTANT : 0,
                verbose, /* initialCapacity= */ 1);
        mCallback = callback;
    }

    @Override
    protected IWallpaperEffectsGenerationService getServiceInterface(IBinder service) {
        return IWallpaperEffectsGenerationService.Stub.asInterface(service);
    }

    @Override
    protected long getTimeoutIdleBindMillis() {
        return PERMANENT_BOUND_TIMEOUT_MS;
    }

    @Override
    protected long getRemoteRequestMillis() {
        return TIMEOUT_REMOTE_REQUEST_MILLIS;
    }

    /**
     * Schedules a request to bind to the remote service.
     */
    public void reconnect() {
        super.scheduleBind();
    }

    /**
     * Schedule async request on remote service.
     */
    public void scheduleOnResolvedService(
            @NonNull AsyncRequest<IWallpaperEffectsGenerationService> request) {
        scheduleAsyncRequest(request);
    }

    /**
     * Execute async request on remote service immediately instead of sending it to Handler queue.
     */
    public void executeOnResolvedService(
            @NonNull AsyncRequest<IWallpaperEffectsGenerationService> request) {
        executeAsyncRequest(request);
    }

    /**
     * Notifies server (WallpaperEffectsGenerationPerUserService) about unexpected events..
     */
    public interface RemoteWallpaperEffectsGenerationServiceCallback
            extends VultureCallback<RemoteWallpaperEffectsGenerationService> {
        /**
         * Notifies change in connected state of the remote service.
         */
        void onConnectedStateChanged(boolean connected);
    }

    @Override // from AbstractRemoteService
    protected void handleOnConnectedStateChanged(boolean connected) {
        if (mCallback != null) {
            mCallback.onConnectedStateChanged(connected);
        }
    }
}
+185 −0
Original line number Original line 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.wallpapereffectsgeneration;

import static android.Manifest.permission.MANAGE_WALLPAPER_EFFECTS_GENERATION;
import static android.app.ActivityManagerInternal.ALLOW_NON_FULL;
import static android.content.Context.WALLPAPER_EFFECTS_GENERATION_SERVICE;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.ActivityManagerInternal;
import android.app.wallpapereffectsgeneration.CinematicEffectRequest;
import android.app.wallpapereffectsgeneration.CinematicEffectResponse;
import android.app.wallpapereffectsgeneration.ICinematicEffectListener;
import android.app.wallpapereffectsgeneration.IWallpaperEffectsGenerationManager;
import android.content.Context;
import android.os.Binder;
import android.os.RemoteException;
import android.os.ResultReceiver;
import android.os.ShellCallback;
import android.util.Slog;

import com.android.server.LocalServices;
import com.android.server.infra.AbstractMasterSystemService;
import com.android.server.infra.FrameworkResourcesServiceNameResolver;
import com.android.server.wm.ActivityTaskManagerInternal;

import java.io.FileDescriptor;
import java.util.function.Consumer;

/**
 * A service used to return wallpaper effect given a request.
 */
public class WallpaperEffectsGenerationManagerService extends
        AbstractMasterSystemService<WallpaperEffectsGenerationManagerService,
                WallpaperEffectsGenerationPerUserService> {
    private static final String TAG =
            WallpaperEffectsGenerationManagerService.class.getSimpleName();
    private static final boolean DEBUG = false;
    private static final int MAX_TEMP_SERVICE_DURATION_MS = 1_000 * 60 * 2; // 2 minutes
    private final ActivityTaskManagerInternal mActivityTaskManagerInternal;

    public WallpaperEffectsGenerationManagerService(Context context) {
        super(context,
                new FrameworkResourcesServiceNameResolver(context,
                        com.android.internal.R.string.config_defaultWallpaperEffectsGenerationService),
                null,
                PACKAGE_UPDATE_POLICY_NO_REFRESH | PACKAGE_RESTART_POLICY_NO_REFRESH);
        mActivityTaskManagerInternal = LocalServices.getService(ActivityTaskManagerInternal.class);
    }

    @Override
    protected WallpaperEffectsGenerationPerUserService newServiceLocked(int resolvedUserId,
            boolean disabled) {
        return new WallpaperEffectsGenerationPerUserService(this, mLock, resolvedUserId);
    }

    @Override
    public void onStart() {
        publishBinderService(WALLPAPER_EFFECTS_GENERATION_SERVICE,
                new WallpaperEffectsGenerationManagerStub());
    }

    @Override
    protected void enforceCallingPermissionForManagement() {
        getContext().enforceCallingPermission(MANAGE_WALLPAPER_EFFECTS_GENERATION, TAG);
    }

    @Override // from AbstractMasterSystemService
    protected void onServicePackageUpdatedLocked(@UserIdInt int userId) {
        final WallpaperEffectsGenerationPerUserService service = peekServiceForUserLocked(userId);
        if (service != null) {
            service.onPackageUpdatedLocked();
        }
    }

    @Override // from AbstractMasterSystemService
    protected void onServicePackageRestartedLocked(@UserIdInt int userId) {
        final WallpaperEffectsGenerationPerUserService service = peekServiceForUserLocked(userId);
        if (service != null) {
            service.onPackageRestartedLocked();
        }
    }

    @Override
    protected int getMaximumTemporaryServiceDurationMs() {
        return MAX_TEMP_SERVICE_DURATION_MS;
    }

    private class WallpaperEffectsGenerationManagerStub
            extends IWallpaperEffectsGenerationManager.Stub {
        @Override
        public void generateCinematicEffect(@NonNull CinematicEffectRequest request,
                @NonNull ICinematicEffectListener listener) {
            if (!runForUserLocked("generateCinematicEffect", (service) ->
                    service.onGenerateCinematicEffectLocked(request, listener))) {
                try {
                    listener.onCinematicEffectGenerated(
                            new CinematicEffectResponse.Builder(
                                    CinematicEffectResponse.CINEMATIC_EFFECT_STATUS_ERROR,
                                    request.getTaskId()).build());
                } catch (RemoteException e) {
                    if (DEBUG) {
                        Slog.d(TAG, "fail to invoke cinematic effect listener for task["
                                + request.getTaskId() + "]");
                    }
                }
            }
        }

        @Override
        public void returnCinematicEffectResponse(@NonNull CinematicEffectResponse response) {
            runForUserLocked("returnCinematicResponse", (service) ->
                    service.onReturnCinematicEffectResponseLocked(response));
        }

        public void onShellCommand(@Nullable FileDescriptor in, @Nullable FileDescriptor out,
                @Nullable FileDescriptor err,
                @NonNull String[] args, @Nullable ShellCallback callback,
                @NonNull ResultReceiver resultReceiver) {
            new WallpaperEffectsGenerationManagerServiceShellCommand(
                    WallpaperEffectsGenerationManagerService.this)
                    .exec(this, in, out, err, args, callback, resultReceiver);
        }

        /**
         * Execute the operation for the user locked. Return true if
         * WallpaperEffectsGenerationPerUserService is found for the user.
         * Otherwise return false.
         */
        private boolean runForUserLocked(@NonNull final String func,
                @NonNull final Consumer<WallpaperEffectsGenerationPerUserService> c) {
            ActivityManagerInternal am = LocalServices.getService(ActivityManagerInternal.class);
            final int userId = am.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
                    Binder.getCallingUserHandle().getIdentifier(), false, ALLOW_NON_FULL,
                    null, null);
            if (DEBUG) {
                Slog.d(TAG, "runForUserLocked:" + func + " from pid=" + Binder.getCallingPid()
                        + ", uid=" + Binder.getCallingUid());
            }
            Context ctx = getContext();
            if (!(ctx.checkCallingPermission(MANAGE_WALLPAPER_EFFECTS_GENERATION)
                    == PERMISSION_GRANTED
                    || mServiceNameResolver.isTemporary(userId)
                    || mActivityTaskManagerInternal.isCallerRecents(Binder.getCallingUid()))) {
                String msg = "Permission Denial: Cannot call " + func + " from pid="
                        + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid();
                Slog.w(TAG, msg);
                throw new SecurityException(msg);
            }
            final long origId = Binder.clearCallingIdentity();
            boolean accepted = false;
            try {
                synchronized (mLock) {
                    final WallpaperEffectsGenerationPerUserService service =
                            getServiceForUserLocked(userId);
                    if (service != null) {
                        accepted = true;
                        c.accept(service);
                    }
                }
            } finally {
                Binder.restoreCallingIdentity(origId);
            }

            return accepted;
        }
    }
}
Loading