Dpm.java revision 41de9bb6fda113f7cfb8b7b8d64d07d3809f8f20
1/*
2 * Copyright (C) 2014 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
17package com.android.commands.dpm;
18
19import android.app.admin.IDevicePolicyManager;
20import android.content.ComponentName;
21import android.content.Context;
22import android.os.RemoteException;
23import android.os.ServiceManager;
24import android.os.UserHandle;
25
26import com.android.internal.os.BaseCommand;
27
28import java.io.PrintStream;
29
30public final class Dpm extends BaseCommand {
31
32    /**
33     * Command-line entry point.
34     *
35     * @param args The command-line arguments
36     */
37    public static void main(String[] args) {
38      (new Dpm()).run(args);
39    }
40
41    private static final String COMMAND_SET_DEVICE_OWNER = "set-device-owner";
42    private static final String COMMAND_SET_PROFILE_OWNER = "set-profile-owner";
43
44    private IDevicePolicyManager mDevicePolicyManager;
45
46    @Override
47    public void onShowUsage(PrintStream out) {
48        out.println(
49                "usage: dpm [subcommand] [options]\n" +
50                "usage: dpm set-device-owner <COMPONENT>\n" +
51                "usage: dpm set-profile-owner <COMPONENT> <USER_ID>\n" +
52                "\n" +
53                "dpm set-device-owner: Sets the given component as active admin, and its\n" +
54                "  package as device owner.\n" +
55                "\n" +
56                "dpm set-profile-owner: Sets the given component as active admin and profile" +
57                "  owner for an existing user.\n");
58    }
59
60    @Override
61    public void onRun() throws Exception {
62        mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
63                ServiceManager.getService(Context.DEVICE_POLICY_SERVICE));
64        if (mDevicePolicyManager == null) {
65            showError("Error: Could not access the Device Policy Manager. Is the system running?");
66            return;
67        }
68
69        String command = nextArgRequired();
70        switch (command) {
71            case COMMAND_SET_DEVICE_OWNER:
72                runSetDeviceOwner();
73                break;
74            case COMMAND_SET_PROFILE_OWNER:
75                runSetProfileOwner();
76                break;
77            default:
78                throw new IllegalArgumentException ("unknown command '" + command + "'");
79        }
80    }
81
82    private void runSetDeviceOwner() throws RemoteException {
83        ComponentName component = parseComponentName(nextArgRequired());
84        mDevicePolicyManager.setActiveAdmin(component, true /*refreshing*/, UserHandle.USER_OWNER);
85
86        String packageName = component.getPackageName();
87        try {
88            if (!mDevicePolicyManager.setDeviceOwner(packageName, null /*ownerName*/)) {
89                throw new RuntimeException(
90                        "Can't set package " + packageName + " as device owner.");
91            }
92        } catch (Exception e) {
93            // Need to remove the admin that we just added.
94            mDevicePolicyManager.removeActiveAdmin(component, UserHandle.USER_OWNER);
95            throw e;
96        }
97        System.out.println("Device owner set to package " + packageName);
98        System.out.println("Active admin set to component " + component.toShortString());
99    }
100
101    private void runSetProfileOwner() throws RemoteException {
102        ComponentName component = parseComponentName(nextArgRequired());
103        int userId = parseInt(nextArgRequired());
104        mDevicePolicyManager.setActiveAdmin(component, true /*refreshing*/, userId);
105
106        try {
107            if (!mDevicePolicyManager.setProfileOwner(component, "" /*ownerName*/, userId)) {
108                throw new RuntimeException("Can't set component " + component.toShortString() +
109                        " as profile owner for user " + userId);
110            }
111        } catch (Exception e) {
112            // Need to remove the admin that we just added.
113            mDevicePolicyManager.removeActiveAdmin(component, userId);
114            throw e;
115        }
116        System.out.println("Active admin and profile owner set to " + component.toShortString() +
117                " for user " + userId);
118    }
119
120    private ComponentName parseComponentName(String component) {
121        ComponentName cn = ComponentName.unflattenFromString(component);
122        if (cn == null) {
123            throw new IllegalArgumentException ("Invalid component " + component);
124        }
125        return cn;
126    }
127
128    private int parseInt(String argument) {
129        try {
130            return Integer.parseInt(argument);
131        } catch (NumberFormatException e) {
132            throw new IllegalArgumentException ("Invalid integer argument '" + argument + "'", e);
133        }
134    }
135}