aboutsummaryrefslogtreecommitdiff
path: root/experimental/service/src/com/android/experimentalcar/TouchDriverAwarenessSupplier.java
blob: fa8bc34c706c6a3e3b13426d6fbd180841f0eb06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Copyright (C) 2019 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.experimentalcar;

import android.car.experimental.DriverAwarenessEvent;
import android.car.experimental.DriverAwarenessSupplierConfig;
import android.car.experimental.DriverAwarenessSupplierService;
import android.car.experimental.IDriverAwarenessSupplier;
import android.car.experimental.IDriverAwarenessSupplierCallback;
import android.content.Context;
import android.hardware.input.InputManager;
import android.os.Looper;
import android.os.RemoteException;
import android.os.SystemClock;
import android.util.Log;
import android.view.Display;
import android.view.InputChannel;
import android.view.InputEvent;
import android.view.InputEventReceiver;
import android.view.InputMonitor;
import android.view.MotionEvent;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * A driver awareness supplier that estimates the driver's current awareness level based on touches
 * on the headunit.
 */
public class TouchDriverAwarenessSupplier extends IDriverAwarenessSupplier.Stub {

    private static final String TAG = "Car.TouchAwarenessSupplier";
    private static final String TOUCH_INPUT_CHANNEL_NAME = "TouchDriverAwarenessInputChannel";

    private static final long MAX_STALENESS = DriverAwarenessSupplierService.NO_STALENESS;

    @VisibleForTesting
    static final float INITIAL_DRIVER_AWARENESS_VALUE = 1.0f;

    private final AtomicInteger mCurrentPermits = new AtomicInteger();
    private final ScheduledExecutorService mRefreshScheduler;
    private final Looper mLooper;
    private final Context mContext;
    private final ITimeSource mTimeSource;
    private final Runnable mRefreshPermitRunnable;
    private final IDriverAwarenessSupplierCallback mDriverAwarenessSupplierCallback;

    private final Object mLock = new Object();

    @GuardedBy("mLock")
    private long mLastEventMillis;

    @GuardedBy("mLock")
    private ScheduledFuture<?> mRefreshScheduleHandle;

    @GuardedBy("mLock")
    private Config mConfig;

    // Main thread only. Hold onto reference to avoid garbage collection
    private InputMonitor mInputMonitor;

    // Main thread only. Hold onto reference to avoid garbage collection
    private InputEventReceiver mInputEventReceiver;

    TouchDriverAwarenessSupplier(Context context,
            IDriverAwarenessSupplierCallback driverAwarenessSupplierCallback, Looper looper) {
        this(context, driverAwarenessSupplierCallback, Executors.newScheduledThreadPool(1),
                looper, new SystemTimeSource());
    }

    @VisibleForTesting
    TouchDriverAwarenessSupplier(
            Context context,
            IDriverAwarenessSupplierCallback driverAwarenessSupplierCallback,
            ScheduledExecutorService refreshScheduler,
            Looper looper,
            ITimeSource timeSource) {
        mContext = context;
        mDriverAwarenessSupplierCallback = driverAwarenessSupplierCallback;
        mRefreshScheduler = refreshScheduler;
        mLooper = looper;
        mTimeSource = timeSource;
        mRefreshPermitRunnable =
                () -> {
                    synchronized (mLock) {
                        handlePermitRefreshLocked(mTimeSource.elapsedRealtime());
                    }
                };
    }


    @Override
    public void onReady() {
        try {
            mDriverAwarenessSupplierCallback.onConfigLoaded(
                    new DriverAwarenessSupplierConfig(MAX_STALENESS));
        } catch (RemoteException e) {
            Log.e(TAG, "Unable to send config - abandoning ready process", e);
            return;
        }
        // send an initial event, as required by the IDriverAwarenessSupplierCallback spec
        try {
            mDriverAwarenessSupplierCallback.onDriverAwarenessUpdated(
                    new DriverAwarenessEvent(mTimeSource.elapsedRealtime(),
                            INITIAL_DRIVER_AWARENESS_VALUE));
        } catch (RemoteException e) {
            Log.e(TAG, "Unable to emit initial awareness event", e);
        }
        synchronized (mLock) {
            mConfig = loadConfig();
            logd("Config loaded: " + mConfig);
            mCurrentPermits.set(mConfig.getMaxPermits());
        }
        startTouchMonitoring();
    }

    @Override
    public void setCallback(IDriverAwarenessSupplierCallback callback) {
        // no-op - the callback is initialized in the constructor
    }

    private Config loadConfig() {
        int maxPermits = mContext.getResources().getInteger(
                R.integer.driverAwarenessTouchModelMaxPermits);
        if (maxPermits <= 0) {
            throw new IllegalArgumentException("driverAwarenessTouchModelMaxPermits must be >0");
        }
        int refreshIntervalMillis = mContext.getResources().getInteger(
                R.integer.driverAwarenessTouchModelPermitRefreshIntervalMs);
        if (refreshIntervalMillis <= 0) {
            throw new IllegalArgumentException(
                    "driverAwarenessTouchModelPermitRefreshIntervalMs must be >0");
        }
        int throttleDurationMillis = mContext.getResources().getInteger(
                R.integer.driverAwarenessTouchModelThrottleMs);
        if (throttleDurationMillis <= 0) {
            throw new IllegalArgumentException("driverAwarenessTouchModelThrottleMs must be >0");
        }
        return new Config(maxPermits, refreshIntervalMillis, throttleDurationMillis);
    }

    /**
     * Starts monitoring touches.
     */
    @VisibleForTesting
    // TODO(b/146802952) handle touch monitoring on multiple displays
    void startTouchMonitoring() {
        InputManager inputManager = (InputManager) mContext.getSystemService(Context.INPUT_SERVICE);
        mInputMonitor = inputManager.monitorGestureInput(
                TOUCH_INPUT_CHANNEL_NAME,
                Display.DEFAULT_DISPLAY);
        mInputEventReceiver = new TouchReceiver(
                mInputMonitor.getInputChannel(),
                mLooper);
    }

    /**
     * Refreshes permits on the interval specified by {@code R.integer
     * .driverAwarenessTouchModelPermitRefreshIntervalMs}.
     */
    @GuardedBy("mLock")
    private void schedulePermitRefreshLocked() {
        logd("Scheduling permit refresh interval (ms): "
                + mConfig.getPermitRefreshIntervalMillis());
        mRefreshScheduleHandle = mRefreshScheduler.scheduleAtFixedRate(
                mRefreshPermitRunnable,
                mConfig.getPermitRefreshIntervalMillis(),
                mConfig.getPermitRefreshIntervalMillis(),
                TimeUnit.MILLISECONDS);
    }

    /**
     * Stops the scheduler for refreshing the number of permits.
     */
    @GuardedBy("mLock")
    private void stopPermitRefreshLocked() {
        logd("Stopping permit refresh");
        if (mRefreshScheduleHandle != null) {
            mRefreshScheduleHandle.cancel(true);
            mRefreshScheduleHandle = null;
        }
    }

    /**
     * Consume a single permit if the event should not be throttled.
     */
    @VisibleForTesting
    @GuardedBy("mLock")
    void consumePermitLocked(long timestamp) {
        long timeSinceLastEvent = timestamp - mLastEventMillis;
        boolean isEventAccepted = timeSinceLastEvent >= mConfig.getThrottleDurationMillis();
        if (!isEventAccepted) {
            logd("Ignoring consumePermit request: event throttled");
            return;
        }
        mLastEventMillis = timestamp;
        int curPermits = mCurrentPermits.updateAndGet(cur -> Math.max(0, cur - 1));
        logd("Permit consumed to: " + curPermits);

        if (mRefreshScheduleHandle == null) {
            schedulePermitRefreshLocked();
        }

        try {
            mDriverAwarenessSupplierCallback.onDriverAwarenessUpdated(
                    new DriverAwarenessEvent(timestamp,
                            (float) curPermits / mConfig.getMaxPermits()));
        } catch (RemoteException e) {
            Log.e(TAG, "Unable to emit awareness event", e);
        }
    }

    @VisibleForTesting
    @GuardedBy("mLock")
    void handlePermitRefreshLocked(long timestamp) {
        int curPermits = mCurrentPermits.updateAndGet(
                cur -> Math.min(cur + 1, mConfig.getMaxPermits()));
        logd("Permit refreshed to: " + curPermits);
        if (curPermits == mConfig.getMaxPermits()) {
            stopPermitRefreshLocked();
        }
        try {
            mDriverAwarenessSupplierCallback.onDriverAwarenessUpdated(
                    new DriverAwarenessEvent(timestamp,
                            (float) curPermits / mConfig.getMaxPermits()));
        } catch (RemoteException e) {
            Log.e(TAG, "Unable to emit awareness event", e);
        }
    }

    private static void logd(String message) {
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, message);
        }
    }

    /**
     * Receiver of all touch events. This receiver filters out all events except {@link
     * MotionEvent#ACTION_UP} events.
     */
    private class TouchReceiver extends InputEventReceiver {

        /**
         * Creates an input event receiver bound to the specified input channel.
         *
         * @param inputChannel The input channel.
         * @param looper       The looper to use when invoking callbacks.
         */
        TouchReceiver(InputChannel inputChannel, Looper looper) {
            super(inputChannel, looper);
        }

        @Override
        public void onInputEvent(InputEvent event) {
            if (!(event instanceof MotionEvent)) {
                return;
            }

            MotionEvent motionEvent = (MotionEvent) event;
            if (motionEvent.getActionMasked() == MotionEvent.ACTION_UP) {
                logd("ACTION_UP touch received");
                synchronized (mLock) {
                    consumePermitLocked(SystemClock.elapsedRealtime());
                }
            }
        }
    }

    /**
     * Configuration for a {@link TouchDriverAwarenessSupplier}.
     */
    private static class Config {

        private final int mMaxPermits;
        private final int mPermitRefreshIntervalMillis;
        private final int mThrottleDurationMillis;

        /**
         * Creates an instance of {@link Config}.
         *
         * @param maxPermits                  the maximum number of permits in the user's
         *                                    attention buffer. A user's number of permits will
         *                                    never refresh to a value higher than this.
         * @param permitRefreshIntervalMillis the refresh interval in milliseconds for refreshing
         *                                    permits
         * @param throttleDurationMillis      the duration in milliseconds representing the window
         *                                    that permit consumption is ignored after an event.
         */
        private Config(
                int maxPermits,
                int permitRefreshIntervalMillis,
                int throttleDurationMillis) {
            mMaxPermits = maxPermits;
            mPermitRefreshIntervalMillis = permitRefreshIntervalMillis;
            mThrottleDurationMillis = throttleDurationMillis;
        }

        int getMaxPermits() {
            return mMaxPermits;
        }

        int getPermitRefreshIntervalMillis() {
            return mPermitRefreshIntervalMillis;
        }

        int getThrottleDurationMillis() {
            return mThrottleDurationMillis;
        }

        @Override
        public String toString() {
            return String.format(
                    "Config{mMaxPermits=%s, mPermitRefreshIntervalMillis=%s, "
                            + "mThrottleDurationMillis=%s}",
                    mMaxPermits,
                    mPermitRefreshIntervalMillis,
                    mThrottleDurationMillis);
        }
    }
}