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