SetDevicePolicyTask.java revision 668d65f9a9319ed34f51733b8c6db225293f65bb
1/*
2 * Copyright 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.managedprovisioning.task;
18
19import android.app.admin.DevicePolicyManager;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.ActivityInfo;
24import android.content.pm.PackageInfo;
25import android.content.pm.PackageManager;
26import android.content.pm.PackageManager.NameNotFoundException;
27import android.text.TextUtils;
28
29import com.android.managedprovisioning.ProvisionLogger;
30
31public class SetDevicePolicyTask {
32    public static final int ERROR_PACKAGE_NOT_INSTALLED = 0;
33    public static final int ERROR_NO_RECEIVER = 1;
34    public static final int ERROR_OTHER = 2;
35
36    private final Callback mCallback;
37    private final Context mContext;
38    private String mAdminPackage;
39    private ComponentName mAdminComponent;
40    private final String mOwner;
41
42    private PackageManager mPackageManager;
43    private DevicePolicyManager mDevicePolicyManager;
44
45    public SetDevicePolicyTask(Context context, String owner, Callback callback) {
46        mCallback = callback;
47        mContext = context;
48        mOwner = owner;
49        mPackageManager = mContext.getPackageManager();
50        mDevicePolicyManager = (DevicePolicyManager) mContext.
51                getSystemService(Context.DEVICE_POLICY_SERVICE);
52    }
53
54    public void run(ComponentName adminComponent) {
55        mAdminComponent = adminComponent;
56        mAdminPackage = mAdminComponent.getPackageName();
57        enableDevicePolicyApp();
58        setActiveAdmin();
59        setDeviceOwner();
60        mCallback.onSuccess();
61    }
62
63    private void enableDevicePolicyApp() {
64        int enabledSetting = mPackageManager
65                .getApplicationEnabledSetting(mAdminPackage);
66        if (enabledSetting != PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
67            mPackageManager.setApplicationEnabledSetting(mAdminPackage,
68                    PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 0);
69        }
70    }
71
72    public void setActiveAdmin() {
73        ProvisionLogger.logd("Setting " + mAdminComponent + " as active admin.");
74        mDevicePolicyManager.setActiveAdmin(mAdminComponent, true);
75    }
76
77    public void setDeviceOwner() {
78        ProvisionLogger.logd("Setting " + mAdminPackage + " as device owner " + mOwner + ".");
79        if (!mDevicePolicyManager.isDeviceOwner(mAdminPackage)) {
80            mDevicePolicyManager.setDeviceOwner(mAdminPackage, mOwner);
81        }
82    }
83
84    public abstract static class Callback {
85        public abstract void onSuccess();
86        public abstract void onError(int errorCode);
87    }
88}
89