Aurimas Liutikas | 88c7ff1 | 2023-08-10 12:42:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.contentsuggestions; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.os.ShellCommand; |
| 21 | |
| 22 | import java.io.PrintWriter; |
| 23 | |
| 24 | /** |
| 25 | * The shell command implementation for the ContentSuggestionsManagerService. |
| 26 | */ |
| 27 | public class ContentSuggestionsManagerServiceShellCommand extends ShellCommand { |
| 28 | |
| 29 | private static final String TAG = |
| 30 | ContentSuggestionsManagerServiceShellCommand.class.getSimpleName(); |
| 31 | |
| 32 | private final ContentSuggestionsManagerService mService; |
| 33 | |
| 34 | public ContentSuggestionsManagerServiceShellCommand( |
| 35 | @NonNull ContentSuggestionsManagerService service) { |
| 36 | mService = service; |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public int onCommand(String cmd) { |
| 41 | if (cmd == null) { |
| 42 | return handleDefaultCommands(cmd); |
| 43 | } |
| 44 | final PrintWriter pw = getOutPrintWriter(); |
| 45 | switch (cmd) { |
| 46 | case "set": |
| 47 | return requestSet(pw); |
| 48 | case "get": |
| 49 | return requestGet(pw); |
| 50 | default: |
| 51 | return handleDefaultCommands(cmd); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void onHelp() { |
| 57 | try (PrintWriter pw = getOutPrintWriter()) { |
| 58 | pw.println("ContentSuggestionsManagerService commands:"); |
| 59 | pw.println(" help"); |
| 60 | pw.println(" Prints this help text."); |
| 61 | pw.println(""); |
| 62 | pw.println(" set temporary-service USER_ID [COMPONENT_NAME DURATION]"); |
| 63 | pw.println(" Temporarily (for DURATION ms) changes the service implementation."); |
| 64 | pw.println(" To reset, call with just the USER_ID argument."); |
| 65 | pw.println(""); |
| 66 | pw.println(" set default-service-enabled USER_ID [true|false]"); |
| 67 | pw.println(" Enable / disable the default service for the user."); |
| 68 | pw.println(""); |
| 69 | pw.println(" get default-service-enabled USER_ID"); |
| 70 | pw.println(" Checks whether the default service is enabled for the user."); |
| 71 | pw.println(""); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | private int requestSet(PrintWriter pw) { |
| 76 | final String what = getNextArgRequired(); |
| 77 | |
| 78 | switch(what) { |
| 79 | case "temporary-service": |
| 80 | return setTemporaryService(pw); |
| 81 | case "default-service-enabled": |
| 82 | return setDefaultServiceEnabled(); |
| 83 | default: |
| 84 | pw.println("Invalid set: " + what); |
| 85 | return -1; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private int requestGet(PrintWriter pw) { |
| 90 | final String what = getNextArgRequired(); |
| 91 | switch(what) { |
| 92 | case "default-service-enabled": |
| 93 | return getDefaultServiceEnabled(pw); |
| 94 | default: |
| 95 | pw.println("Invalid get: " + what); |
| 96 | return -1; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | private int setTemporaryService(PrintWriter pw) { |
| 101 | final int userId = Integer.parseInt(getNextArgRequired()); |
| 102 | String serviceName = getNextArg(); |
| 103 | if (serviceName == null) { |
| 104 | mService.resetTemporaryService(userId); |
| 105 | return 0; |
| 106 | } |
| 107 | final int duration = Integer.parseInt(getNextArgRequired()); |
| 108 | mService.setTemporaryService(userId, serviceName, duration); |
| 109 | pw.println("ContentSuggestionsService temporarily set to " + serviceName |
| 110 | + " for " + duration + "ms"); |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | private int setDefaultServiceEnabled() { |
| 115 | final int userId = getNextIntArgRequired(); |
| 116 | final boolean enabled = Boolean.parseBoolean(getNextArg()); |
| 117 | mService.setDefaultServiceEnabled(userId, enabled); |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | private int getDefaultServiceEnabled(PrintWriter pw) { |
| 122 | final int userId = getNextIntArgRequired(); |
| 123 | final boolean enabled = mService.isDefaultServiceEnabled(userId); |
| 124 | pw.println(enabled); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | private int getNextIntArgRequired() { |
| 129 | return Integer.parseInt(getNextArgRequired()); |
| 130 | } |
| 131 | } |