1b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann/*
2b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann * Copyright (C) 2018 The Android Open Source Project
3b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann *
4b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann * Licensed under the Apache License, Version 2.0 (the "License");
5b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann * you may not use this file except in compliance with the License.
6b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann * You may obtain a copy of the License at
7b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann *
8b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann *      http://www.apache.org/licenses/LICENSE-2.0
9b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann *
10b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann * Unless required by applicable law or agreed to in writing, software
11b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann * distributed under the License is distributed on an "AS IS" BASIS,
12b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann * See the License for the specific language governing permissions and
14b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann * limitations under the License.
15b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann */
16b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann
17b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmannpackage com.android.server.print;
18b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann
19b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmannimport android.annotation.NonNull;
20b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmannimport android.annotation.Nullable;
21b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmannimport android.os.RemoteException;
22b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmannimport android.os.ShellCommand;
23b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmannimport android.os.UserHandle;
24b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmannimport android.print.IPrintManager;
25b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann
26b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmannimport java.io.PrintWriter;
27b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann
28b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann/**
29b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann * Shell command implementation for the print manager service
30b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann */
31b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmannfinal class PrintShellCommand extends ShellCommand {
32b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann    final @NonNull IPrintManager mService;
33b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann
34b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann    PrintShellCommand(@NonNull IPrintManager service) {
35b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        mService = service;
36b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann    }
37b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann
38b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann    @Override
39b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann    public int onCommand(@Nullable String cmd) {
40b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        if (cmd == null) {
41b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann            return handleDefaultCommands(cmd);
42b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        }
43b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        switch (cmd) {
44b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann            case "get-bind-instant-service-allowed": {
45b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann                return runGetBindInstantServiceAllowed();
46b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann            }
47b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann            case "set-bind-instant-service-allowed": {
48b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann                return runSetBindInstantServiceAllowed();
49b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann            }
50b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        }
51b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        return -1;
52b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann    }
53b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann
54b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann    private int runGetBindInstantServiceAllowed() {
55b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        final Integer userId = parseUserId();
56b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        if (userId == null) {
57b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann            return -1;
58b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        }
59b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        try {
60b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann            getOutPrintWriter().println(
61b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann                    Boolean.toString(mService.getBindInstantServiceAllowed(userId)));
62b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        } catch (RemoteException e) {
63b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann            e.rethrowFromSystemServer();
64b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        }
65b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        return 0;
66b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann    }
67b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann
68b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann    private int runSetBindInstantServiceAllowed() {
69b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        final Integer userId = parseUserId();
70b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        if (userId == null) {
71b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann            return -1;
72b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        }
73b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        final String allowed = getNextArgRequired();
74b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        if (allowed == null) {
75b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann            getErrPrintWriter().println("Error: no true/false specified");
76b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann            return -1;
77b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        }
78b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        try {
79b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann            mService.setBindInstantServiceAllowed(userId, Boolean.parseBoolean(allowed));
80b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        } catch (RemoteException e) {
81b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann            e.rethrowFromSystemServer();
82b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        }
83b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        return 0;
84b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann    }
85b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann
86b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann    private @Nullable Integer parseUserId() {
87b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        final String option = getNextOption();
88b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        if (option != null) {
89b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann            if (option.equals("--user")) {
90b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann                return UserHandle.parseUserArg(getNextArgRequired());
91b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann            } else {
92b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann                getErrPrintWriter().println("Unknown option: " + option);
93b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann                return null;
94b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann            }
95b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        }
96b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        return UserHandle.USER_SYSTEM;
97b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann    }
98b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann
99b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann    @Override
100b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann    public void onHelp() {
101b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        PrintWriter pw = getOutPrintWriter();
102b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        pw.println("Print service commands:");
103b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        pw.println("  help");
104b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        pw.println("    Print this help text.");
105b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        pw.println("  set-bind-instant-service-allowed [--user <USER_ID>] true|false ");
106b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        pw.println("    Set whether binding to print services provided by instant apps is "
107b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann                + "allowed.");
108b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        pw.println("  get-bind-instant-service-allowed [--user <USER_ID>]");
109b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann        pw.println("    Get whether binding to print services provided by instant apps is "
110b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann                + "allowed.");
111b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann    }
112b3baaab4af7e1ed8dfc5da1c976cc34d135fb82bPhilip P. Moltmann}
113