1 /*
2  * Copyright (C) 2023 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 
17 package com.google.android.car.kitchensink.util;
18 
19 import android.car.CarOccupantZoneManager;
20 import android.util.Log;
21 
22 import com.android.internal.util.Preconditions;
23 
24 public final class InjectKeyEventUtils {
25     private static final String TAG = InjectKeyEventUtils.class.getSimpleName();
26     private static final String PREFIX_INJECTING_KEY_CMD = "cmd car_service inject-key ";
27     private static final String OPTION_SEAT = " -s ";
28 
injectKeyByShell(CarOccupantZoneManager.OccupantZoneInfo zone, int keyCode)29     public static void injectKeyByShell(CarOccupantZoneManager.OccupantZoneInfo zone,
30             int keyCode) {
31         Preconditions.checkArgument(zone != null, " zone cannot be null");
32         // generate a command message
33         StringBuilder sb = new StringBuilder()
34                 .append(PREFIX_INJECTING_KEY_CMD)
35                 .append(OPTION_SEAT)
36                 .append(zone.seat)
37                 .append(' ')
38                 .append(keyCode)
39                 .append("\n");
40         try {
41             Runtime.getRuntime().exec(sb.toString());
42         } catch (Exception e) {
43             Log.e(TAG, "Cannot flush", e);
44         }
45     }
46 }
47