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  */
16 package com.android.car.hal;
17 
18 import android.util.Log;
19 import android.view.KeyEvent;
20 
21 import com.android.car.CarLog;
22 import com.android.car.vehiclenetwork.VehicleNetworkConsts;
23 import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleDisplay;
24 import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleHwKeyInputAction;
25 import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfig;
26 import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue;
27 
28 import java.io.PrintWriter;
29 import java.util.LinkedList;
30 import java.util.List;
31 
32 public class InputHalService extends HalServiceBase {
33 
34     public static final int DISPLAY_MAIN = VehicleDisplay.VEHICLE_DISPLAY_MAIN;
35     public static final int DISPLAY_INSTRUMENT_CLUSTER =
36             VehicleDisplay.VEHICLE_DISPLAY_INSTRUMENT_CLUSTER;
37 
38     public interface InputListener {
onKeyEvent(KeyEvent event, int targetDisplay)39         void onKeyEvent(KeyEvent event, int targetDisplay);
40     }
41 
42     private static final boolean DBG = false;
43 
44     private boolean mKeyInputSupported = false;
45     private InputListener mListener;
46 
setInputListener(InputListener listener)47     public void setInputListener(InputListener listener) {
48         synchronized (this) {
49             if (!mKeyInputSupported) {
50                 Log.w(CarLog.TAG_INPUT, "input listener set while key input not supported");
51                 return;
52             }
53             mListener = listener;
54         }
55         VehicleHal.getInstance().subscribeProperty(this,
56                 VehicleNetworkConsts.VEHICLE_PROPERTY_HW_KEY_INPUT, 0);
57     }
58 
isKeyInputSupported()59     public synchronized boolean isKeyInputSupported() {
60         return mKeyInputSupported;
61     }
62 
63     @Override
init()64     public void init() {
65     }
66 
67     @Override
release()68     public void release() {
69         synchronized (this) {
70             mListener = null;
71             mKeyInputSupported = false;
72         }
73     }
74 
75     @Override
takeSupportedProperties(List<VehiclePropConfig> allProperties)76     public List<VehiclePropConfig> takeSupportedProperties(List<VehiclePropConfig> allProperties) {
77         List<VehiclePropConfig> supported = new LinkedList<VehiclePropConfig>();
78         for (VehiclePropConfig p: allProperties) {
79             if (p.getProp() == VehicleNetworkConsts.VEHICLE_PROPERTY_HW_KEY_INPUT) {
80                 supported.add(p);
81                 synchronized (this) {
82                     mKeyInputSupported = true;
83                 }
84             }
85         }
86         return supported;
87     }
88 
89     @Override
handleHalEvents(List<VehiclePropValue> values)90     public void handleHalEvents(List<VehiclePropValue> values) {
91         InputListener listener = null;
92         synchronized (this) {
93             listener = mListener;
94         }
95         if (listener == null) {
96             Log.w(CarLog.TAG_INPUT, "Input event while listener is null");
97             return;
98         }
99         for (VehiclePropValue v : values) {
100             if (v.getProp() != VehicleNetworkConsts.VEHICLE_PROPERTY_HW_KEY_INPUT) {
101                 Log.e(CarLog.TAG_INPUT, "Wrong event dispatched, prop:0x" +
102                         Integer.toHexString(v.getProp()));
103                 continue;
104             }
105             int action = (v.getInt32Values(0) ==
106                     VehicleHwKeyInputAction.VEHICLE_HW_KEY_INPUT_ACTION_DOWN) ?
107                             KeyEvent.ACTION_DOWN : KeyEvent.ACTION_UP;
108             int code = v.getInt32Values(1);
109             int display = v.getInt32Values(2);
110             if (DBG) {
111                 Log.i(CarLog.TAG_INPUT, "hal event code:" + code + ",action:" + action +
112                         ",display:" + display);
113             }
114             KeyEvent event = new KeyEvent(action, code);
115             listener.onKeyEvent(event, display);
116         }
117     }
118 
119     @Override
dump(PrintWriter writer)120     public void dump(PrintWriter writer) {
121         writer.println("*Input HAL*");
122         writer.println("mKeyInputSupported:" + mKeyInputSupported);
123     }
124 
125 }
126