|
| 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