JBで追加されたFrameworkの機能InputManagerについて

JBからInputManagerが追加されています。

InputManagerについてはAndroidのFramework調査 第4回 InputManagerに記載していますが、ICSまではInputManagerはアプリケーション向けに公開されていませんでした。

Android4.1ではアプリケーション向けに機能が公開されました。
このクラスは、USBや、Bluetoothにて、新しいInputDevice(ジョイスティックやゲームパッドなど)が追加されたことをアプリケーションに通知するためのクラスです。

ゲームなど、外付けInputDeviceがあったほうがよいアプリケーションは、その接続通知を受けて、画面上にコントローラーのためのUIを表示していたのを消したりすることができるでしょう。

確認バージョン

もちろんAndroid 4.1

パッケージ:android.hardware.input
クラス:InputManager

現在接続中のInputDeviceの一覧を取得するには以下のように記載します。

        // InputManagerの取得
        InputManager inputManager = (InputManager) getSystemService(Context.INPUT_SERVICE);
        // InputDeviceのデバイスIDを取得する
        int[] ids = inputManager.getInputDeviceIds();
        int length = ids.length;
        for (int i = 0; i < length; i++) {
            Log.e(TAG, inputManager.getInputDevice(i).toString());
        }

パッケージ:android.view
クラス:InputDevice
上記クラスのtoString()メソッドはオーバーライドされており、文字列として、情報を吐き出してくれます。

    @Override
    public String toString() {
        StringBuilder description = new StringBuilder();
        description.append("Input Device ").append(mId).append(": ").append(mName).append("\n");
        description.append("  Descriptor: ").append(mDescriptor).append("\n");
        description.append("  Generation: ").append(mGeneration).append("\n");
        description.append("  Location: ").append(mIsExternal ? "external" : "built-in").append("\n");

        description.append("  Keyboard Type: ");
        switch (mKeyboardType) {
            case KEYBOARD_TYPE_NONE:
                description.append("none");
                break;
            case KEYBOARD_TYPE_NON_ALPHABETIC:
                description.append("non-alphabetic");
                break;
            case KEYBOARD_TYPE_ALPHABETIC:
                description.append("alphabetic");
                break;
        }
        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");
        appendSourceDescriptionIfApplicable(description, SOURCE_TOUCHSCREEN, "touchscreen");
        appendSourceDescriptionIfApplicable(description, SOURCE_MOUSE, "mouse");
        appendSourceDescriptionIfApplicable(description, SOURCE_STYLUS, "stylus");
        appendSourceDescriptionIfApplicable(description, SOURCE_TRACKBALL, "trackball");
        appendSourceDescriptionIfApplicable(description, SOURCE_TOUCHPAD, "touchpad");
        appendSourceDescriptionIfApplicable(description, SOURCE_JOYSTICK, "joystick");
        appendSourceDescriptionIfApplicable(description, SOURCE_GAMEPAD, "gamepad");
        description.append(" )\n");

        final int numAxes = mMotionRanges.size();
        for (int i = 0; i < numAxes; i++) {
            MotionRange range = mMotionRanges.get(i);
            description.append("    ").append(MotionEvent.axisToString(range.mAxis));
            description.append(": source=0x").append(Integer.toHexString(range.mSource));
            description.append(" min=").append(range.mMin);
            description.append(" max=").append(range.mMax);
            description.append(" flat=").append(range.mFlat);
            description.append(" fuzz=").append(range.mFuzz);
            description.append("\n");
        }
        return description.toString();
    }

端末に新しいInputDeviceが接続されたかどうかは、コールバックによって、通知を受けることができます。

コールバックの登録は下記

        // コールバックの登録処理
        inputManager.registerInputDeviceListener(mInputDeviceListener, new Handler());

コールバックはregisterInputDeviceListener()メソッドの第一引数に、
InputManager.InputManagerListenerインターフェースを実装したクラスを登録します。

    private InputDeviceListener mInputDeviceListener = new InputDeviceListener() {

        @Override
        public void onInputDeviceAdded(int deviceId) {
            // TODO デバイスが追加されたときの処理
            
        }

        @Override
        public void onInputDeviceRemoved(int deviceId) {
            // TODO デバイスが削除されたときの処理
            
        }

        @Override
        public void onInputDeviceChanged(int deviceId) {
            // TODO デバイスが変更されたときの処理
        }
    };

コールバックの解除は以下のメソッドで行います。

        // コールバックの登録解除処理
        inputManager.unregisterInputDeviceListener(mInputDeviceListener);

詳しいクラス図を次の機会にアップします。

以上になります。