Home | History | Annotate | Download | only in accessibility
      1 /*
      2  * Copyright (C) 2017 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.accessibility;
     18 
     19 import android.annotation.NonNull;
     20 import android.os.ShellCommand;
     21 import android.os.UserHandle;
     22 
     23 import java.io.PrintWriter;
     24 
     25 /**
     26  * Shell command implementation for the accessibility manager service
     27  */
     28 final class AccessibilityShellCommand extends ShellCommand {
     29     final @NonNull AccessibilityManagerService mService;
     30 
     31     AccessibilityShellCommand(@NonNull AccessibilityManagerService service) {
     32         mService = service;
     33     }
     34 
     35     @Override
     36     public int onCommand(String cmd) {
     37         if (cmd == null) {
     38             return handleDefaultCommands(cmd);
     39         }
     40         switch (cmd) {
     41             case "get-bind-instant-service-allowed": {
     42                 return runGetBindInstantServiceAllowed();
     43             }
     44             case "set-bind-instant-service-allowed": {
     45                 return runSetBindInstantServiceAllowed();
     46             }
     47         }
     48         return -1;
     49     }
     50 
     51     private int runGetBindInstantServiceAllowed() {
     52         final Integer userId = parseUserId();
     53         if (userId == null) {
     54             return -1;
     55         }
     56         getOutPrintWriter().println(Boolean.toString(
     57                 mService.getBindInstantServiceAllowed(userId)));
     58         return 0;
     59     }
     60 
     61     private int runSetBindInstantServiceAllowed() {
     62         final Integer userId = parseUserId();
     63         if (userId == null) {
     64             return -1;
     65         }
     66         final String allowed = getNextArgRequired();
     67         if (allowed == null) {
     68             getErrPrintWriter().println("Error: no true/false specified");
     69             return -1;
     70         }
     71         mService.setBindInstantServiceAllowed(userId,
     72                 Boolean.parseBoolean(allowed));
     73         return 0;
     74     }
     75 
     76     private Integer parseUserId() {
     77         final String option = getNextOption();
     78         if (option != null) {
     79             if (option.equals("--user")) {
     80                 return UserHandle.parseUserArg(getNextArgRequired());
     81             } else {
     82                 getErrPrintWriter().println("Unknown option: " + option);
     83                 return null;
     84             }
     85         }
     86         return UserHandle.USER_SYSTEM;
     87     }
     88 
     89     @Override
     90     public void onHelp() {
     91         PrintWriter pw = getOutPrintWriter();
     92         pw.println("Accessibility service (accessibility) commands:");
     93         pw.println("  help");
     94         pw.println("    Print this help text.");
     95         pw.println("  set-bind-instant-service-allowed [--user <USER_ID>] true|false ");
     96         pw.println("    Set whether binding to services provided by instant apps is allowed.");
     97         pw.println("  get-bind-instant-service-allowed [--user <USER_ID>]");
     98         pw.println("    Get whether binding to services provided by instant apps is allowed.");
     99     }
    100 }