Skip to content

Commit edcb2c3

Browse files
TreeHugger RobotAndroid (Google) Code Review
authored andcommitted
Merge "Initial implementation of 'cmd voiceinteraction'." into rvc-dev
2 parents 8acb04e + b6ebc24 commit edcb2c3

2 files changed

Lines changed: 144 additions & 0 deletions

File tree

services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
import android.os.RemoteCallback;
5757
import android.os.RemoteCallbackList;
5858
import android.os.RemoteException;
59+
import android.os.ResultReceiver;
60+
import android.os.ShellCallback;
5961
import android.os.Trace;
6062
import android.os.UserHandle;
6163
import android.os.UserManagerInternal;
@@ -1390,6 +1392,13 @@ public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
13901392
mSoundTriggerInternal.dump(fd, pw, args);
13911393
}
13921394

1395+
@Override
1396+
public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
1397+
String[] args, ShellCallback callback, ResultReceiver resultReceiver) {
1398+
new VoiceInteractionManagerServiceShellCommand(mServiceStub)
1399+
.exec(this, in, out, err, args, callback, resultReceiver);
1400+
}
1401+
13931402
@Override
13941403
public void setUiHints(Bundle hints) {
13951404
synchronized (this) {
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright (C) 2020 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.server.voiceinteraction;
17+
18+
import android.os.Bundle;
19+
import android.os.RemoteException;
20+
import android.os.ShellCommand;
21+
import android.util.Slog;
22+
23+
import com.android.internal.app.IVoiceInteractionSessionShowCallback;
24+
import com.android.server.voiceinteraction.VoiceInteractionManagerService.VoiceInteractionManagerServiceStub;
25+
26+
import java.io.PrintWriter;
27+
import java.util.concurrent.CountDownLatch;
28+
import java.util.concurrent.TimeUnit;
29+
import java.util.concurrent.atomic.AtomicInteger;
30+
31+
/**
32+
* Shell command for {@link VoiceInteractionManagerService}.
33+
*/
34+
final class VoiceInteractionManagerServiceShellCommand extends ShellCommand {
35+
private static final String TAG = "VoiceInteractionManager";
36+
private static final long TIMEOUT_MS = 5_000;
37+
38+
private final VoiceInteractionManagerServiceStub mService;
39+
40+
VoiceInteractionManagerServiceShellCommand(VoiceInteractionManagerServiceStub service) {
41+
mService = service;
42+
}
43+
44+
@Override
45+
public int onCommand(String cmd) {
46+
if (cmd == null) {
47+
return handleDefaultCommands(cmd);
48+
}
49+
PrintWriter pw = getOutPrintWriter();
50+
switch (cmd) {
51+
case "show":
52+
return requestShow(pw);
53+
case "hide":
54+
return requestHide(pw);
55+
default:
56+
return handleDefaultCommands(cmd);
57+
}
58+
}
59+
60+
@Override
61+
public void onHelp() {
62+
try (PrintWriter pw = getOutPrintWriter();) {
63+
pw.println("VoiceInteraction Service (voiceinteraction) commands:");
64+
pw.println(" help");
65+
pw.println(" Prints this help text.");
66+
pw.println("");
67+
pw.println(" show");
68+
pw.println(" Shows a session for the active service");
69+
pw.println("");
70+
pw.println(" hide");
71+
pw.println(" Hides the current session");
72+
pw.println("");
73+
}
74+
}
75+
76+
private int requestShow(PrintWriter pw) {
77+
Slog.i(TAG, "requestShow()");
78+
CountDownLatch latch = new CountDownLatch(1);
79+
AtomicInteger result = new AtomicInteger();
80+
81+
IVoiceInteractionSessionShowCallback callback =
82+
new IVoiceInteractionSessionShowCallback.Stub() {
83+
@Override
84+
public void onFailed() throws RemoteException {
85+
Slog.w(TAG, "onFailed()");
86+
pw.println("callback failed");
87+
result.set(1);
88+
latch.countDown();
89+
}
90+
91+
@Override
92+
public void onShown() throws RemoteException {
93+
Slog.d(TAG, "onShown()");
94+
result.set(0);
95+
latch.countDown();
96+
}
97+
};
98+
99+
try {
100+
Bundle args = new Bundle();
101+
boolean ok = mService.showSessionForActiveService(args, /* sourceFlags= */ 0, callback,
102+
/* activityToken= */ null);
103+
104+
if (!ok) {
105+
pw.println("showSessionForActiveService() returned false");
106+
return 1;
107+
}
108+
109+
if (!latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
110+
pw.printf("Callback not called in %d ms\n", TIMEOUT_MS);
111+
return 1;
112+
}
113+
} catch (Exception e) {
114+
return handleError(pw, "showSessionForActiveService()", e);
115+
}
116+
117+
return 0;
118+
}
119+
120+
private int requestHide(PrintWriter pw) {
121+
Slog.i(TAG, "requestHide()");
122+
try {
123+
mService.hideCurrentSession();
124+
} catch (Exception e) {
125+
return handleError(pw, "requestHide()", e);
126+
}
127+
return 0;
128+
}
129+
130+
private static int handleError(PrintWriter pw, String message, Exception e) {
131+
Slog.e(TAG, "error calling " + message, e);
132+
pw.printf("Error calling %s: %s\n", message, e);
133+
return 1;
134+
}
135+
}

0 commit comments

Comments
 (0)