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 
17 package com.android.server.musicrecognition;
18 
19 import android.os.ShellCommand;
20 
21 import java.io.PrintWriter;
22 
23 /** Handles adb shell commands send to MusicRecognitionManagerService. */
24 class MusicRecognitionManagerServiceShellCommand extends ShellCommand {
25 
26     private final MusicRecognitionManagerService mService;
27 
MusicRecognitionManagerServiceShellCommand(MusicRecognitionManagerService service)28     MusicRecognitionManagerServiceShellCommand(MusicRecognitionManagerService service) {
29         mService = service;
30     }
31 
32     @Override
onCommand(String cmd)33     public int onCommand(String cmd) {
34         if (cmd == null) {
35             return handleDefaultCommands(cmd);
36         }
37         final PrintWriter pw = getOutPrintWriter();
38         if ("set".equals(cmd)) {
39             return requestSet(pw);
40         }
41         return handleDefaultCommands(cmd);
42     }
43 
requestSet(PrintWriter pw)44     private int requestSet(PrintWriter pw) {
45         final String what = getNextArgRequired();
46         if ("temporary-service".equals(what)) {
47             return setTemporaryService(pw);
48         }
49         pw.println("Invalid set: " + what);
50         return -1;
51     }
52 
setTemporaryService(PrintWriter pw)53     private int setTemporaryService(PrintWriter pw) {
54         final int userId = Integer.parseInt(getNextArgRequired());
55         final String serviceName = getNextArg();
56         if (serviceName == null) {
57             mService.resetTemporaryService(userId);
58             return 0;
59         }
60         final int duration = Integer.parseInt(getNextArgRequired());
61         mService.setTemporaryService(userId, serviceName, duration);
62         pw.println("MusicRecognitionService temporarily set to " + serviceName + " for "
63                 + duration + "ms");
64         return 0;
65     }
66 
67     @Override
onHelp()68     public void onHelp() {
69         try (PrintWriter pw = getOutPrintWriter();) {
70             pw.println("MusicRecognition Service (music_recognition) commands:");
71             pw.println("  help");
72             pw.println("    Prints this help text.");
73             pw.println("");
74             pw.println("  set temporary-service USER_ID [COMPONENT_NAME DURATION]");
75             pw.println("    Temporarily (for DURATION ms) changes the service implementation.");
76             pw.println("    To reset, call with just the USER_ID argument.");
77             pw.println("");
78         }
79     }
80 }
81