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

Commit d049dc44 authored by Lais Andrade's avatar Lais Andrade
Browse files

Create IVibratorManagerService.aidl

Introduce skeleton for new VibratorManager service, with a single method
to recover the vibrator IDs.

Bug: 166586119
Test: atest FrameworksServicesTests:VibratorManagerServiceTest
Change-Id: Ie633c937c9030d837532548b200867f35968a9de
parent fe13065c
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
/**
 * Copyright (c) 2020, 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 android.os;

import android.os.VibrationAttributes;

/** {@hide} */
interface IVibratorManagerService {
    int[] getVibratorIds();
}
+1 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ per-file ExternalVibration.aidl = michaelwr@google.com
per-file ExternalVibration.java = michaelwr@google.com
per-file IExternalVibrationController.aidl = michaelwr@google.com
per-file IExternalVibratorService.aidl = michaelwr@google.com
per-file IVibratorManagerService.aidl = michaelwr@google.com
per-file IVibratorService.aidl = michaelwr@google.com
per-file NullVibrator.java = michaelwr@google.com
per-file SystemVibrator.java = michaelwr@google.com
+1 −2
Original line number Diff line number Diff line
@@ -2,8 +2,7 @@
per-file ConnectivityService.java,NetworkManagementService.java,NsdService.java = codewiz@google.com, ek@google.com, jchalard@google.com, junyulai@google.com, lorenzo@google.com, reminv@google.com, satk@google.com

# Vibrator / Threads
per-file VibratorService.java, DisplayThread.java = michaelwr@google.com
per-file VibratorService.java, DisplayThread.java = ogunwale@google.com
per-file VibratorManagerService.java, VibratorService.java, DisplayThread.java = michaelwr@google.com, ogunwale@google.com

# Zram writeback
per-file ZramWriteback.java = minchan@google.com, rajekumar@google.com, srnvs@google.com
+154 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 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;

import android.content.Context;
import android.os.IBinder;
import android.os.IVibratorManagerService;
import android.os.ResultReceiver;
import android.os.ShellCallback;
import android.os.ShellCommand;

import com.android.internal.annotations.VisibleForTesting;

import libcore.util.NativeAllocationRegistry;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.Arrays;

/** System implementation of {@link IVibratorManagerService}. */
public class VibratorManagerService extends IVibratorManagerService.Stub {
    private static final String TAG = "VibratorManagerService";
    private static final boolean DEBUG = false;

    private final Context mContext;
    private final NativeWrapper mNativeWrapper;
    private final int[] mVibratorIds;

    static native long nativeInit();

    static native long nativeGetFinalizer();

    static native int[] nativeGetVibratorIds(long nativeServicePtr);

    VibratorManagerService(Context context) {
        this(context, new Injector());
    }

    @VisibleForTesting
    VibratorManagerService(Context context, Injector injector) {
        mContext = context;
        mNativeWrapper = injector.getNativeWrapper();

        mNativeWrapper.init();

        int[] vibratorIds = mNativeWrapper.getVibratorIds();
        mVibratorIds = vibratorIds == null ? new int[0] : vibratorIds;
    }

    @Override // Binder call
    public int[] getVibratorIds() {
        return Arrays.copyOf(mVibratorIds, mVibratorIds.length);
    }

    @Override
    public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
            String[] args, ShellCallback cb, ResultReceiver resultReceiver) {
        new VibratorManagerShellCommand(this).exec(this, in, out, err, args, cb, resultReceiver);
    }

    /** Point of injection for test dependencies */
    @VisibleForTesting
    static class Injector {

        NativeWrapper getNativeWrapper() {
            return new NativeWrapper();
        }
    }

    /** Wrapper around the static-native methods of {@link VibratorManagerService} for tests. */
    @VisibleForTesting
    public static class NativeWrapper {

        private long mNativeServicePtr = 0;

        /** Returns native pointer to newly created controller and connects with HAL service. */
        public void init() {
            mNativeServicePtr = VibratorManagerService.nativeInit();
            long finalizerPtr = VibratorManagerService.nativeGetFinalizer();

            if (finalizerPtr != 0) {
                NativeAllocationRegistry registry =
                        NativeAllocationRegistry.createMalloced(
                                VibratorManagerService.class.getClassLoader(), finalizerPtr);
                registry.registerNativeAllocation(this, mNativeServicePtr);
            }
        }

        /** Returns vibrator ids. */
        public int[] getVibratorIds() {
            return VibratorManagerService.nativeGetVibratorIds(mNativeServicePtr);
        }
    }

    /** Provides limited functionality from {@link VibratorManagerService} as shell commands. */
    private final class VibratorManagerShellCommand extends ShellCommand {

        private final IBinder mToken;

        private VibratorManagerShellCommand(IBinder token) {
            mToken = token;
        }

        @Override
        public int onCommand(String cmd) {
            if ("list".equals(cmd)) {
                return runListVibrators();
            }
            return handleDefaultCommands(cmd);
        }

        private int runListVibrators() {
            try (PrintWriter pw = getOutPrintWriter();) {
                if (mVibratorIds.length == 0) {
                    pw.println("No vibrator found");
                } else {
                    for (int id : mVibratorIds) {
                        pw.println(id);
                    }
                }
                pw.println("");
                return 0;
            }
        }

        @Override
        public void onHelp() {
            try (PrintWriter pw = getOutPrintWriter();) {
                pw.println("Vibrator Manager commands:");
                pw.println("  help");
                pw.println("    Prints this help text.");
                pw.println("");
                pw.println("  list");
                pw.println("    Prints the id of device vibrators. This do not include any ");
                pw.println("    connected input device.");
                pw.println("");
            }
        }
    }
}
+10 −19
Original line number Diff line number Diff line
@@ -385,15 +385,7 @@ public class VibratorService extends IVibratorService.Stub
        mNativeWrapper = injector.getNativeWrapper();
        mH = injector.createHandler(Looper.myLooper());

        long nativeServicePtr = mNativeWrapper.vibratorInit(this::onVibrationComplete);
        long finalizerPtr = mNativeWrapper.vibratorGetFinalizer();

        if (finalizerPtr != 0) {
            NativeAllocationRegistry registry =
                    NativeAllocationRegistry.createMalloced(
                            VibratorService.class.getClassLoader(), finalizerPtr);
            registry.registerNativeAllocation(this, nativeServicePtr);
        }
        mNativeWrapper.vibratorInit(this::onVibrationComplete);

        // Reset the hardware to a default state, in case this is a runtime
        // restart instead of a fresh boot.
@@ -1746,18 +1738,17 @@ public class VibratorService extends IVibratorService.Stub
            return VibratorService.vibratorExists(mNativeServicePtr);
        }

        /**
         * Returns native pointer to newly created controller and initializes connection to vibrator
         * HAL service.
         */
        public long vibratorInit(OnCompleteListener listener) {
        /** Initializes connection to vibrator HAL service. */
        public void vibratorInit(OnCompleteListener listener) {
            mNativeServicePtr = VibratorService.vibratorInit(listener);
            return mNativeServicePtr;
        }
            long finalizerPtr = VibratorService.vibratorGetFinalizer();

        /** Returns pointer to native finalizer function to be called by GC. */
        public long vibratorGetFinalizer() {
            return VibratorService.vibratorGetFinalizer();
            if (finalizerPtr != 0) {
                NativeAllocationRegistry registry =
                        NativeAllocationRegistry.createMalloced(
                                VibratorService.class.getClassLoader(), finalizerPtr);
                registry.registerNativeAllocation(this, mNativeServicePtr);
            }
        }

        /** Turns vibrator on for given time. */
Loading