1 /*
2  * Copyright (C) 2011 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.example.training.deviceadmin;
18 
19 import android.app.Activity;
20 import android.app.admin.DeviceAdminReceiver;
21 import android.app.admin.DevicePolicyManager;
22 import android.content.ComponentName;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.SharedPreferences;
26 import android.os.Build;
27 
28 public class Policy {
29     public static final int REQUEST_ADD_DEVICE_ADMIN = 1;
30 
31     private static final String APP_PREF = "APP_PREF";
32     private static final String KEY_PASSWORD_LENGTH = "PW_LENGTH";
33     private static final String KEY_PASSWORD_QUALITY = "PW_QUALITY";
34     private static final String KEY_PASSWORD_MIN_UPPERCASE = "PW_MIN_UPPERCASE";
35 
36     // Password quality values.  This list must match the list
37     // found in res/values/arrays.xml
38     final static int[] PASSWORD_QUALITY_VALUES = new int[] {
39         DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED,
40         DevicePolicyManager.PASSWORD_QUALITY_SOMETHING,
41         DevicePolicyManager.PASSWORD_QUALITY_NUMERIC,
42         DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC,
43         DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC,
44         DevicePolicyManager.PASSWORD_QUALITY_COMPLEX
45     };
46 
47     private int mPasswordQuality;
48     private int mPasswordLength;
49     private int mPasswordMinUpperCase;
50     private Context mContext;
51     private DevicePolicyManager mDPM;
52     private ComponentName mPolicyAdmin;
53 
Policy(Context context)54     public Policy(Context context) {
55         mContext = context;
56         mPasswordQuality = -1;
57         mPasswordLength = 0;
58         mPasswordMinUpperCase = 0;
59         mDPM = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
60         mPolicyAdmin = new ComponentName(context, PolicyAdmin.class);
61     }
62 
63     /**
64      * Saves the policy parameters.
65      *
66      * @param passwordQuality Password quality.
67      * @param passwordLength Password minimum length.
68      * @param passwordUppercase Password minimum number of upper case alphabets.
69      */
saveToLocal(int passwordQuality, int passwordLength, int passwordMinUppercase)70     public void saveToLocal(int passwordQuality, int passwordLength, int passwordMinUppercase) {
71         SharedPreferences.Editor editor =
72                 mContext.getSharedPreferences(APP_PREF, Context.MODE_PRIVATE).edit();
73         if (mPasswordQuality != passwordQuality) {
74             editor.putInt(KEY_PASSWORD_QUALITY, passwordQuality);
75             mPasswordQuality = passwordQuality;
76         }
77         if (mPasswordLength != passwordLength) {
78             editor.putInt(KEY_PASSWORD_LENGTH, passwordLength);
79             mPasswordLength = passwordLength;
80         }
81         if (mPasswordMinUpperCase != passwordMinUppercase) {
82             editor.putInt(KEY_PASSWORD_MIN_UPPERCASE, passwordMinUppercase);
83             mPasswordMinUpperCase = passwordMinUppercase;
84         }
85         editor.commit();
86     }
87 
readFromLocal()88     public void readFromLocal() {
89         SharedPreferences prefs = mContext.getSharedPreferences(APP_PREF, Context.MODE_PRIVATE);
90         mPasswordQuality = prefs.getInt(KEY_PASSWORD_QUALITY, -1);
91         mPasswordLength = prefs.getInt(KEY_PASSWORD_LENGTH, -1);
92         mPasswordMinUpperCase = prefs.getInt(KEY_PASSWORD_MIN_UPPERCASE, -1);
93     }
94 
95     /**
96      * Getter for password quality.
97      *
98      * @return
99      */
getPasswordQuality()100     public int getPasswordQuality() { return mPasswordQuality; }
101 
102     /**
103      * Getter for password length.
104      *
105      * @return
106      */
getPasswordLength()107     public int getPasswordLength() { return mPasswordLength; }
108 
109     /**
110      * Getter for password minimum upper case alphabets.
111      *
112      * @return
113      */
getPasswordMinUpperCase()114     public int getPasswordMinUpperCase() { return mPasswordMinUpperCase; }
115 
116     /**
117      * Getter for the policy administrator ComponentName object.
118      *
119      * @return
120      */
getPolicyAdmin()121     public ComponentName getPolicyAdmin() { return mPolicyAdmin; }
122 
123     /**
124      * Indicates whether the device administrator is currently active.
125      *
126      * @return
127      */
isAdminActive()128     public boolean isAdminActive() {
129         return mDPM.isAdminActive(mPolicyAdmin);
130     }
131 
isActivePasswordSufficient()132     public boolean isActivePasswordSufficient() {
133         return mDPM.isActivePasswordSufficient();
134     }
135 
isDeviceSecured()136     public boolean isDeviceSecured() {
137         return isAdminActive() && isActivePasswordSufficient();
138     }
139 
140     /**
141      * Configure policy defined in the object.
142      */
configurePolicy()143     public void configurePolicy() {
144         mDPM.setPasswordQuality(mPolicyAdmin, PASSWORD_QUALITY_VALUES[mPasswordQuality]);
145         mDPM.setPasswordMinimumLength(mPolicyAdmin, mPasswordLength);
146         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
147             mDPM.setPasswordMinimumUpperCase(mPolicyAdmin, mPasswordMinUpperCase);
148         }
149     }
150 
151     /**
152      * Through the PolicyAdmin receiver, the app can use this to trap various device
153      * administration events, such as password change, incorrect password entry, etc.
154      *
155      */
156     public static class PolicyAdmin extends DeviceAdminReceiver {
157 
158         @Override
onDisabled(Context context, Intent intent)159         public void onDisabled(Context context, Intent intent) {
160             // Called when the app is about to be deactivated as a device administrator.
161             // Deletes previously stored password policy.
162             super.onDisabled(context, intent);
163             SharedPreferences prefs = context.getSharedPreferences(APP_PREF, Activity.MODE_PRIVATE);
164             prefs.edit().clear().commit();
165         }
166     }
167 }
168 
169