Dpm.java revision a52562ca9a4144cf30e6d5c6ffe856cc8e284464
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_ACTIVE_ADMIN = "set-active-admin";
42    private static final String COMMAND_SET_DEVICE_OWNER = "set-device-owner";
43    private static final String COMMAND_SET_PROFILE_OWNER = "set-profile-owner";
44
45    private IDevicePolicyManager mDevicePolicyManager;
46    private int mUserId = UserHandle.USER_SYSTEM;
47    private String mName = "";
48    private ComponentName mComponent = null;
49
50    @Override
51    public void onShowUsage(PrintStream out) {
52        out.println(
53                "usage: dpm [subcommand] [options]\n" +
54                "usage: dpm set-active-admin [ --user <USER_ID> ] <COMPONENT>\n" +
55                // STOPSHIP Finalize it
56                "usage: dpm set-device-owner [ --user <USER_ID> *EXPERIMENTAL* ] [ --name <NAME> ] <COMPONENT>\n" +
57                "usage: dpm set-profile-owner [ --user <USER_ID> ] [ --name <NAME> ] <COMPONENT>\n" +
58                "\n" +
59                "dpm set-active-admin: Sets the given component as active admin" +
60                " for an existing user.\n" +
61                "\n" +
62                "dpm set-device-owner: Sets the given component as active admin, and its\n" +
63                "  package as device owner.\n" +
64                "\n" +
65                "dpm set-profile-owner: Sets the given component as active admin and profile" +
66                "  owner for an existing user.\n");
67    }
68
69    @Override
70    public void onRun() throws Exception {
71        mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface(
72                ServiceManager.getService(Context.DEVICE_POLICY_SERVICE));
73        if (mDevicePolicyManager == null) {
74            showError("Error: Could not access the Device Policy Manager. Is the system running?");
75            return;
76        }
77
78        String command = nextArgRequired();
79        switch (command) {
80            case COMMAND_SET_ACTIVE_ADMIN:
81                runSetActiveAdmin();
82                break;
83            case COMMAND_SET_DEVICE_OWNER:
84                runSetDeviceOwner();
85                break;
86            case COMMAND_SET_PROFILE_OWNER:
87                runSetProfileOwner();
88                break;
89            default:
90                throw new IllegalArgumentException ("unknown command '" + command + "'");
91        }
92    }
93
94    private void parseArgs(boolean canHaveUser, boolean canHaveName) {
95        String opt;
96        while ((opt = nextOption()) != null) {
97            if (canHaveUser && "--user".equals(opt)) {
98                mUserId = parseInt(nextArgRequired());
99            } else if (canHaveName && "--name".equals(opt)) {
100                mName = nextArgRequired();
101            } else {
102                throw new IllegalArgumentException("Unknown option: " + opt);
103            }
104        }
105        mComponent = parseComponentName(nextArgRequired());
106    }
107
108    private void runSetActiveAdmin() throws RemoteException {
109        parseArgs(/*canHaveUser=*/ true, /*canHaveName=*/ false);
110        mDevicePolicyManager.setActiveAdmin(mComponent, true /*refreshing*/, mUserId);
111
112        System.out.println("Success: Active admin set to component " + mComponent.toShortString());
113    }
114
115    private void runSetDeviceOwner() throws RemoteException {
116        parseArgs(/*canHaveUser=*/ true, /*canHaveName=*/ true);
117        mDevicePolicyManager.setActiveAdmin(mComponent, true /*refreshing*/, mUserId);
118
119        try {
120            if (!mDevicePolicyManager.setDeviceOwner(mComponent, mName, mUserId)) {
121                throw new RuntimeException(
122                        "Can't set package " + mComponent + " as device owner.");
123            }
124        } catch (Exception e) {
125            // Need to remove the admin that we just added.
126            mDevicePolicyManager.removeActiveAdmin(mComponent, UserHandle.USER_SYSTEM);
127            throw e;
128        }
129        System.out.println("Success: Device owner set to package " + mComponent);
130        System.out.println("Active admin set to component " + mComponent.toShortString());
131    }
132
133    private void runSetProfileOwner() throws RemoteException {
134        parseArgs(/*canHaveUser=*/ true, /*canHaveName=*/ true);
135        mDevicePolicyManager.setActiveAdmin(mComponent, true /*refreshing*/, mUserId);
136
137        try {
138            if (!mDevicePolicyManager.setProfileOwner(mComponent, mName, mUserId)) {
139                throw new RuntimeException("Can't set component " + mComponent.toShortString() +
140                        " as profile owner for user " + mUserId);
141            }
142        } catch (Exception e) {
143            // Need to remove the admin that we just added.
144            mDevicePolicyManager.removeActiveAdmin(mComponent, mUserId);
145            throw e;
146        }
147        System.out.println("Success: Active admin and profile owner set to "
148                + mComponent.toShortString() + " for user " + mUserId);
149    }
150
151    private ComponentName parseComponentName(String component) {
152        ComponentName cn = ComponentName.unflattenFromString(component);
153        if (cn == null) {
154            throw new IllegalArgumentException ("Invalid component " + component);
155        }
156        return cn;
157    }
158
159    private int parseInt(String argument) {
160        try {
161            return Integer.parseInt(argument);
162        } catch (NumberFormatException e) {
163            throw new IllegalArgumentException ("Invalid integer argument '" + argument + "'", e);
164        }
165    }
166}