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

Commit 90aba7ca authored by Jeff Brown's avatar Jeff Brown Committed by Android (Google) Code Review
Browse files

Merge "Add support for input devices that have vibrators."

parents 21e09bc4 a47425a1
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -22362,6 +22362,7 @@ package android.view {
    method public java.util.List<android.view.InputDevice.MotionRange> getMotionRanges();
    method public java.lang.String getName();
    method public int getSources();
    method public android.os.Vibrator getVibrator();
    method public boolean isVirtual();
    method public void writeToParcel(android.os.Parcel, int);
    field public static final android.os.Parcelable.Creator CREATOR;
+5 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.hardware.input;

import android.hardware.input.KeyboardLayout;
import android.hardware.input.IInputDevicesChangedListener;
import android.os.IBinder;
import android.view.InputDevice;
import android.view.InputEvent;

@@ -46,4 +47,8 @@ interface IInputManager {

    // Registers an input devices changed listener.
    void registerInputDevicesChangedListener(IInputDevicesChangedListener listener);

    // Input device vibrator control.
    void vibrate(int deviceId, in long[] pattern, int repeat, IBinder token);
    void cancelVibrate(int deviceId, IBinder token);
}
+52 −0
Original line number Diff line number Diff line
@@ -19,12 +19,14 @@ package android.hardware.input;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.content.Context;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.Vibrator;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;
@@ -586,6 +588,15 @@ public final class InputManager {
        return false;
    }

    /**
     * Gets a vibrator service associated with an input device, assuming it has one.
     * @return The vibrator, never null.
     * @hide
     */
    public Vibrator getInputDeviceVibrator(int deviceId) {
        return new InputDeviceVibrator(deviceId);
    }

    /**
     * Listens for changes in input devices.
     */
@@ -645,4 +656,45 @@ public final class InputManager {
            }
        }
    }

    private final class InputDeviceVibrator extends Vibrator {
        private final int mDeviceId;
        private final Binder mToken;

        public InputDeviceVibrator(int deviceId) {
            mDeviceId = deviceId;
            mToken = new Binder();
        }

        @Override
        public boolean hasVibrator() {
            return true;
        }

        @Override
        public void vibrate(long milliseconds) {
            vibrate(new long[] { 0, milliseconds}, -1);
        }

        @Override
        public void vibrate(long[] pattern, int repeat) {
            if (repeat >= pattern.length) {
                throw new ArrayIndexOutOfBoundsException();
            }
            try {
                mIm.vibrate(mDeviceId, pattern, repeat, mToken);
            } catch (RemoteException ex) {
                Log.w(TAG, "Failed to vibrate.", ex);
            }
        }

        @Override
        public void cancel() {
            try {
                mIm.cancelVibrate(mDeviceId, mToken);
            } catch (RemoteException ex) {
                Log.w(TAG, "Failed to cancel vibration.", ex);
            }
        }
    }
}
+55 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 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.util.Log;

/**
 * Vibrator implementation that does nothing.
 *
 * @hide
 */
public class NullVibrator extends Vibrator {
    private static final NullVibrator sInstance = new NullVibrator();

    private NullVibrator() {
    }

    public static NullVibrator getInstance() {
        return sInstance;
    }

    @Override
    public boolean hasVibrator() {
        return false;
    }

    @Override
    public void vibrate(long milliseconds) {
    }

    @Override
    public void vibrate(long[] pattern, int repeat) {
        if (repeat >= pattern.length) {
            throw new ArrayIndexOutOfBoundsException();
        }
    }

    @Override
    public void cancel() {
    }
}
+37 −1
Original line number Diff line number Diff line
@@ -16,9 +16,12 @@

package android.view;

import android.content.Context;
import android.hardware.input.InputManager;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.Vibrator;
import android.os.NullVibrator;

import java.util.ArrayList;
import java.util.List;
@@ -46,8 +49,11 @@ public final class InputDevice implements Parcelable {
    private final int mSources;
    private final int mKeyboardType;
    private final KeyCharacterMap mKeyCharacterMap;
    private final boolean mHasVibrator;
    private final ArrayList<MotionRange> mMotionRanges = new ArrayList<MotionRange>();

    private Vibrator mVibrator; // guarded by mMotionRanges during initialization

    /**
     * A mask for input source classes.
     * 
@@ -304,7 +310,7 @@ public final class InputDevice implements Parcelable {

    // Called by native code.
    private InputDevice(int id, int generation, String name, String descriptor, int sources,
            int keyboardType, KeyCharacterMap keyCharacterMap) {
            int keyboardType, KeyCharacterMap keyCharacterMap, boolean hasVibrator) {
        mId = id;
        mGeneration = generation;
        mName = name;
@@ -312,6 +318,7 @@ public final class InputDevice implements Parcelable {
        mSources = sources;
        mKeyboardType = keyboardType;
        mKeyCharacterMap = keyCharacterMap;
        mHasVibrator = hasVibrator;
    }

    private InputDevice(Parcel in) {
@@ -322,6 +329,7 @@ public final class InputDevice implements Parcelable {
        mSources = in.readInt();
        mKeyboardType = in.readInt();
        mKeyCharacterMap = KeyCharacterMap.CREATOR.createFromParcel(in);
        mHasVibrator = in.readInt() != 0;

        for (;;) {
            int axis = in.readInt();
@@ -521,6 +529,31 @@ public final class InputDevice implements Parcelable {
        mMotionRanges.add(new MotionRange(axis, source, min, max, flat, fuzz));
    }

    /**
     * Gets the vibrator service associated with the device, if there is one.
     * Even if the device does not have a vibrator, the result is never null.
     * Use {@link Vibrator#hasVibrator} to determine whether a vibrator is
     * present.
     *
     * Note that the vibrator associated with the device may be different from
     * the system vibrator.  To obtain an instance of the system vibrator instead, call
     * {@link Context#getSystemService} with {@link Context#VIBRATOR_SERVICE} as argument.
     *
     * @return The vibrator service associated with the device, never null.
     */
    public Vibrator getVibrator() {
        synchronized (mMotionRanges) {
            if (mVibrator == null) {
                if (mHasVibrator) {
                    mVibrator = InputManager.getInstance().getInputDeviceVibrator(mId);
                } else {
                    mVibrator = NullVibrator.getInstance();
                }
            }
            return mVibrator;
        }
    }

    /**
     * Provides information about the range of values for a particular {@link MotionEvent} axis.
     *
@@ -617,6 +650,7 @@ public final class InputDevice implements Parcelable {
        out.writeInt(mSources);
        out.writeInt(mKeyboardType);
        mKeyCharacterMap.writeToParcel(out, flags);
        out.writeInt(mHasVibrator ? 1 : 0);

        final int numRanges = mMotionRanges.size();
        for (int i = 0; i < numRanges; i++) {
@@ -657,6 +691,8 @@ public final class InputDevice implements Parcelable {
        }
        description.append("\n");

        description.append("  Has Vibrator: ").append(mHasVibrator).append("\n");

        description.append("  Sources: 0x").append(Integer.toHexString(mSources)).append(" (");
        appendSourceDescriptionIfApplicable(description, SOURCE_KEYBOARD, "keyboard");
        appendSourceDescriptionIfApplicable(description, SOURCE_DPAD, "dpad");
Loading