1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package android.car.input;
17
18import android.annotation.CallSuper;
19import android.annotation.MainThread;
20import android.annotation.SystemApi;
21import android.app.Service;
22import android.car.CarLibLog;
23import android.content.Intent;
24import android.os.Bundle;
25import android.os.Handler;
26import android.os.IBinder;
27import android.os.Message;
28import android.os.Parcel;
29import android.os.Parcelable;
30import android.os.RemoteException;
31import android.util.Log;
32import android.view.KeyEvent;
33
34import java.io.FileDescriptor;
35import java.io.PrintWriter;
36import java.lang.ref.WeakReference;
37
38/**
39 * A service that is used for handling of input events.
40 *
41 * <p>To extend this class, you must declare the service in your manifest file with
42 * the {@code android.car.permission.BIND_CAR_INPUT_SERVICE} permission
43 * <pre>
44 * &lt;service android:name=".MyCarInputService"
45 *          android:permission="android.car.permission.BIND_CAR_INPUT_SERVICE">
46 * &lt;/service></pre>
47 * <p>Also, you will need to register this service in the following configuration file:
48 * {@code packages/services/Car/service/res/values/config.xml}
49 *
50 * @hide
51 */
52@SystemApi
53public abstract class CarInputHandlingService extends Service {
54    private static final String TAG = CarLibLog.TAG_INPUT;
55    private static final boolean DBG = false;
56
57    public static final String INPUT_CALLBACK_BINDER_KEY = "callback_binder";
58    public static final int INPUT_CALLBACK_BINDER_CODE = IBinder.FIRST_CALL_TRANSACTION;
59
60    private final InputFilter[] mHandledKeys;
61
62    private InputBinder mInputBinder;
63
64    protected CarInputHandlingService(InputFilter[] handledKeys) {
65        if (handledKeys == null) {
66            throw new IllegalArgumentException("handledKeys is null");
67        }
68
69        mHandledKeys = new InputFilter[handledKeys.length];
70        System.arraycopy(handledKeys, 0, mHandledKeys, 0, handledKeys.length);
71    }
72
73    @Override
74    @CallSuper
75    public IBinder onBind(Intent intent) {
76        if (DBG) {
77            Log.d(TAG, "onBind, intent: " + intent);
78        }
79
80        doCallbackIfPossible(intent.getExtras());
81
82        if (mInputBinder == null) {
83            mInputBinder = new InputBinder();
84        }
85
86        return mInputBinder;
87    }
88
89    private void doCallbackIfPossible(Bundle extras) {
90        if (extras == null) {
91            Log.i(TAG, "doCallbackIfPossible: extras are null");
92            return;
93        }
94        IBinder callbackBinder = extras.getBinder(INPUT_CALLBACK_BINDER_KEY);
95        if (callbackBinder == null) {
96            Log.i(TAG, "doCallbackIfPossible: callback IBinder is null");
97            return;
98        }
99        Parcel dataIn = Parcel.obtain();
100        dataIn.writeTypedArray(mHandledKeys, 0);
101        try {
102            callbackBinder.transact(INPUT_CALLBACK_BINDER_CODE, dataIn, null, IBinder.FLAG_ONEWAY);
103        } catch (RemoteException e) {
104            Log.e(TAG, "doCallbackIfPossible: callback failed", e);
105        }
106    }
107
108    /**
109     * Called when key event has been received.
110     */
111    @MainThread
112    protected abstract void onKeyEvent(KeyEvent keyEvent, int targetDisplay);
113
114    @Override
115    protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
116        writer.println("**" + getClass().getSimpleName() + "**");
117        writer.println("input binder: " + mInputBinder);
118    }
119
120    private class InputBinder extends ICarInputListener.Stub {
121        private final EventHandler mEventHandler;
122
123        InputBinder() {
124            mEventHandler = new EventHandler(CarInputHandlingService.this);
125        }
126
127        @Override
128        public void onKeyEvent(KeyEvent keyEvent, int targetDisplay) throws RemoteException {
129            mEventHandler.doKeyEvent(keyEvent, targetDisplay);
130        }
131    }
132
133    private static class EventHandler extends Handler {
134        private static final int KEY_EVENT = 0;
135        private final WeakReference<CarInputHandlingService> mRefService;
136
137        EventHandler(CarInputHandlingService service) {
138            mRefService = new WeakReference<>(service);
139        }
140
141        @Override
142        public void handleMessage(Message msg) {
143            CarInputHandlingService service = mRefService.get();
144            if (service == null) {
145                return;
146            }
147
148            if (msg.what == KEY_EVENT) {
149                service.onKeyEvent((KeyEvent) msg.obj, msg.arg1);
150            } else {
151                throw new IllegalArgumentException("Unexpected message: " + msg);
152            }
153        }
154
155        void doKeyEvent(KeyEvent event, int targetDisplay) {
156            sendMessage(obtainMessage(KEY_EVENT, targetDisplay, 0, event));
157        }
158    }
159
160    /**
161     * Filter for input events that are handled by custom service.
162     */
163    public static class InputFilter implements Parcelable {
164        public final int mKeyCode;
165        public final int mTargetDisplay;
166
167        public InputFilter(int keyCode, int targetDisplay) {
168            mKeyCode = keyCode;
169            mTargetDisplay = targetDisplay;
170        }
171
172        // Parcelling part
173        InputFilter(Parcel in) {
174            mKeyCode = in.readInt();
175            mTargetDisplay = in.readInt();
176        }
177
178        @Override
179        public int describeContents() {
180            return 0;
181        }
182
183        @Override
184        public void writeToParcel(Parcel dest, int flags) {
185            dest.writeInt(mKeyCode);
186            dest.writeInt(mTargetDisplay);
187        }
188
189        public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
190            public InputFilter createFromParcel(Parcel in) {
191                return new InputFilter(in);
192            }
193
194            public InputFilter[] newArray(int size) {
195                return new InputFilter[size];
196            }
197        };
198    }
199}
200