SecurityPolicy.java revision 940a335bab634a849b45f62d711601d57f59aeb1
1345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler/* 2345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * Copyright (C) 2010 The Android Open Source Project 3345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * 4345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * Licensed under the Apache License, Version 2.0 (the "License"); 5345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * you may not use this file except in compliance with the License. 6345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * You may obtain a copy of the License at 7345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * 8345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * http://www.apache.org/licenses/LICENSE-2.0 9345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * 10345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * Unless required by applicable law or agreed to in writing, software 11345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * distributed under the License is distributed on an "AS IS" BASIS, 12345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * See the License for the specific language governing permissions and 14345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * limitations under the License. 15345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler */ 16345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler 17345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadlerpackage com.android.email; 18345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler 19e7f4d3ebfcf497c015ba65be7ecebea8926b995cAndy Stadlerimport android.app.admin.DeviceAdminInfo; 206d0016229adc13fefe68820fe4d6e46f530952baDianne Hackbornimport android.app.admin.DeviceAdminReceiver; 216d0016229adc13fefe68820fe4d6e46f530952baDianne Hackbornimport android.app.admin.DevicePolicyManager; 22d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadlerimport android.content.ComponentName; 23f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blankimport android.content.ContentProviderOperation; 2402d59d21949a77c60859b615312f02e6d8003490Marc Blankimport android.content.ContentResolver; 25f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blankimport android.content.ContentUris; 263d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadlerimport android.content.ContentValues; 27345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadlerimport android.content.Context; 28345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadlerimport android.content.Intent; 29f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blankimport android.content.OperationApplicationException; 30345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadlerimport android.database.Cursor; 31f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blankimport android.net.Uri; 32bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Huimport android.os.Bundle; 33f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blankimport android.os.RemoteException; 34345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler 35f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blankimport com.android.email.provider.EmailProvider; 36c6df1d605fb3e235df6d4a21ae00632c9d6e3cc2Marc Blankimport com.android.email.service.EmailBroadcastProcessorService; 37bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Huimport com.android.email.service.EmailServiceUtils; 38f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blankimport com.android.email2.ui.MailActivityEmail; 39c6df1d605fb3e235df6d4a21ae00632c9d6e3cc2Marc Blankimport com.android.emailcommon.Logging; 40c6df1d605fb3e235df6d4a21ae00632c9d6e3cc2Marc Blankimport com.android.emailcommon.provider.Account; 41c6df1d605fb3e235df6d4a21ae00632c9d6e3cc2Marc Blankimport com.android.emailcommon.provider.EmailContent; 42c6df1d605fb3e235df6d4a21ae00632c9d6e3cc2Marc Blankimport com.android.emailcommon.provider.EmailContent.AccountColumns; 43c6df1d605fb3e235df6d4a21ae00632c9d6e3cc2Marc Blankimport com.android.emailcommon.provider.EmailContent.PolicyColumns; 44c6df1d605fb3e235df6d4a21ae00632c9d6e3cc2Marc Blankimport com.android.emailcommon.provider.Policy; 45f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blankimport com.android.emailcommon.utility.TextUtilities; 46c6df1d605fb3e235df6d4a21ae00632c9d6e3cc2Marc Blankimport com.android.emailcommon.utility.Utility; 47560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedyimport com.android.mail.utils.LogUtils; 48c6df1d605fb3e235df6d4a21ae00632c9d6e3cc2Marc Blankimport com.google.common.annotations.VisibleForTesting; 49c6df1d605fb3e235df6d4a21ae00632c9d6e3cc2Marc Blank 50f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blankimport java.util.ArrayList; 51f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank 52345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler/** 53d71d0b223a5cd02e2a8f1ec5c3f8cebab170d65fAndrew Stadler * Utility functions to support reading and writing security policies, and handshaking the device 54d71d0b223a5cd02e2a8f1ec5c3f8cebab170d65fAndrew Stadler * into and out of various security states. 55345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler */ 56345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadlerpublic class SecurityPolicy { 57aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank private static final String TAG = "Email/SecurityPolicy"; 58345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler private static SecurityPolicy sInstance = null; 59345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler private Context mContext; 60d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler private DevicePolicyManager mDPM; 61d09cff08882e553afce919865a2cc60b657d4659Ben Komalo private final ComponentName mAdminName; 62aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank private Policy mAggregatePolicy; 632a5eeea9213005060256054ec773e72406415ce4Andrew Stadler 64a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler // Messages used for DevicePolicyManager callbacks 65a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler private static final int DEVICE_ADMIN_MESSAGE_ENABLED = 1; 66a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler private static final int DEVICE_ADMIN_MESSAGE_DISABLED = 2; 67a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler private static final int DEVICE_ADMIN_MESSAGE_PASSWORD_CHANGED = 3; 68a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler private static final int DEVICE_ADMIN_MESSAGE_PASSWORD_EXPIRING = 4; 69a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler 70aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank private static final String HAS_PASSWORD_EXPIRATION = 71aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank PolicyColumns.PASSWORD_EXPIRATION_DAYS + ">0"; 72aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank 73345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler /** 74345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * Get the security policy instance 75345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler */ 76345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler public synchronized static SecurityPolicy getInstance(Context context) { 77345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler if (sInstance == null) { 78a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler sInstance = new SecurityPolicy(context.getApplicationContext()); 79345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 80345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler return sInstance; 81345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 82345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler 83345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler /** 84345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * Private constructor (one time only) 85345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler */ 86345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler private SecurityPolicy(Context context) { 87968be441b4c253668c4ee1c7a3f8e4b0eb12cf24Makoto Onuki mContext = context.getApplicationContext(); 88d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler mDPM = null; 89d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler mAdminName = new ComponentName(context, PolicyAdmin.class); 90d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler mAggregatePolicy = null; 91345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 92345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler 93345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler /** 94345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * For testing only: Inject context into already-created instance 95345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler */ 96345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler /* package */ void setContext(Context context) { 97345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler mContext = context; 98345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 99345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler 100345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler /** 101345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * Compute the aggregate policy for all accounts that require it, and record it. 102345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * 103345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * The business logic is as follows: 104345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * min password length take the max 105345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * password mode take the max (strongest mode) 106345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * max password fails take the min 107345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * max screen lock time take the min 108345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * require remote wipe take the max (logical or) 1099b4988de43dbee6c06066caab63806e8c8303d7dMarc Blank * password history take the max (strongest mode) 1101ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * password expiration take the min (strongest mode) 1119b4988de43dbee6c06066caab63806e8c8303d7dMarc Blank * password complex chars take the max (strongest mode) 112469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler * encryption take the max (logical or) 113968be441b4c253668c4ee1c7a3f8e4b0eb12cf24Makoto Onuki * 114d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler * @return a policy representing the strongest aggregate. If no policy sets are defined, 115d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler * a lightweight "nothing required" policy will be returned. Never null. 116345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler */ 117d09cff08882e553afce919865a2cc60b657d4659Ben Komalo @VisibleForTesting 118d09cff08882e553afce919865a2cc60b657d4659Ben Komalo Policy computeAggregatePolicy() { 119345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler boolean policiesFound = false; 120aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank Policy aggregate = new Policy(); 121aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mPasswordMinLength = Integer.MIN_VALUE; 122aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mPasswordMode = Integer.MIN_VALUE; 123aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mPasswordMaxFails = Integer.MAX_VALUE; 124aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mPasswordHistory = Integer.MIN_VALUE; 125aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mPasswordExpirationDays = Integer.MAX_VALUE; 126aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mPasswordComplexChars = Integer.MIN_VALUE; 127aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mMaxScreenLockTime = Integer.MAX_VALUE; 128aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mRequireRemoteWipe = false; 129aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mRequireEncryption = false; 130e76962b1b9c66ecc3fd49cd4c956f234365bfe5cBen Komalo 131e76962b1b9c66ecc3fd49cd4c956f234365bfe5cBen Komalo // This can never be supported at this time. It exists only for historic reasons where 132e76962b1b9c66ecc3fd49cd4c956f234365bfe5cBen Komalo // this was able to be supported prior to the introduction of proper removable storage 133e76962b1b9c66ecc3fd49cd4c956f234365bfe5cBen Komalo // support for external storage. 134aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mRequireEncryptionExternal = false; 135aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank 136aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank Cursor c = mContext.getContentResolver().query(Policy.CONTENT_URI, 137aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank Policy.CONTENT_PROJECTION, null, null, null); 138aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank Policy policy = new Policy(); 139345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler try { 140345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler while (c.moveToNext()) { 141aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank policy.restore(c); 142f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (MailActivityEmail.DEBUG) { 143560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(TAG, "Aggregate from: " + policy); 144aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 145aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mPasswordMinLength = 146aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank Math.max(policy.mPasswordMinLength, aggregate.mPasswordMinLength); 147aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mPasswordMode = Math.max(policy.mPasswordMode, aggregate.mPasswordMode); 148aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy.mPasswordMaxFails > 0) { 149aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mPasswordMaxFails = 150aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank Math.min(policy.mPasswordMaxFails, aggregate.mPasswordMaxFails); 151aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 152aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy.mMaxScreenLockTime > 0) { 153aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mMaxScreenLockTime = Math.min(policy.mMaxScreenLockTime, 154aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mMaxScreenLockTime); 155aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 156aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy.mPasswordHistory > 0) { 157aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mPasswordHistory = 158aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank Math.max(policy.mPasswordHistory, aggregate.mPasswordHistory); 159345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 160aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy.mPasswordExpirationDays > 0) { 161aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mPasswordExpirationDays = 162aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank Math.min(policy.mPasswordExpirationDays, aggregate.mPasswordExpirationDays); 163aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 164aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy.mPasswordComplexChars > 0) { 165aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mPasswordComplexChars = Math.max(policy.mPasswordComplexChars, 166aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mPasswordComplexChars); 167aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 168aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mRequireRemoteWipe |= policy.mRequireRemoteWipe; 169aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mRequireEncryption |= policy.mRequireEncryption; 170d09cff08882e553afce919865a2cc60b657d4659Ben Komalo aggregate.mDontAllowCamera |= policy.mDontAllowCamera; 171aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank policiesFound = true; 172345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 173345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } finally { 174345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler c.close(); 175345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 176345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler if (policiesFound) { 1773d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler // final cleanup pass converts any untouched min/max values to zero (not specified) 178aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (aggregate.mPasswordMinLength == Integer.MIN_VALUE) aggregate.mPasswordMinLength = 0; 179aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (aggregate.mPasswordMode == Integer.MIN_VALUE) aggregate.mPasswordMode = 0; 180aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (aggregate.mPasswordMaxFails == Integer.MAX_VALUE) aggregate.mPasswordMaxFails = 0; 181aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (aggregate.mMaxScreenLockTime == Integer.MAX_VALUE) aggregate.mMaxScreenLockTime = 0; 182aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (aggregate.mPasswordHistory == Integer.MIN_VALUE) aggregate.mPasswordHistory = 0; 183aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (aggregate.mPasswordExpirationDays == Integer.MAX_VALUE) 184aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mPasswordExpirationDays = 0; 185aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (aggregate.mPasswordComplexChars == Integer.MIN_VALUE) 186aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregate.mPasswordComplexChars = 0; 187f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (MailActivityEmail.DEBUG) { 188560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(TAG, "Calculated Aggregate: " + aggregate); 189aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 190aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank return aggregate; 191345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 192f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (MailActivityEmail.DEBUG) { 193560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(TAG, "Calculated Aggregate: no policy"); 194aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 195aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank return Policy.NO_POLICY; 196345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 197345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler 198345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler /** 19950d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler * Return updated aggregate policy, from cached value if possible 20050d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler */ 201aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank public synchronized Policy getAggregatePolicy() { 20250d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler if (mAggregatePolicy == null) { 20350d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler mAggregatePolicy = computeAggregatePolicy(); 20450d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler } 20550d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler return mAggregatePolicy; 20650d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler } 20750d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler 20850d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler /** 209d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler * Get the dpm. This mainly allows us to make some utility calls without it, for testing. 210d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler */ 211a0d080558ff06f88f000cf424803c8241dd8d2ebAndy Stadler /* package */ synchronized DevicePolicyManager getDPM() { 212d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler if (mDPM == null) { 213d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler mDPM = (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); 214d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler } 215d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler return mDPM; 216d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler } 217d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler 218d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler /** 219f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * API: Report that policies may have been updated due to rewriting values in an Account; we 220f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * clear the aggregate policy (so it can be recomputed) and set the policies in the DPM 221d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler */ 222f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank public synchronized void policiesUpdated() { 223d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler mAggregatePolicy = null; 224f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank setActivePolicies(); 225d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler } 226d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler 227d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler /** 22850d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler * API: Report that policies may have been updated *and* the caller vouches that the 22950d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler * change is a reduction in policies. This forces an immediate change to device state. 23050d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler * Typically used when deleting accounts, although we may use it for server-side policy 23150d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler * rollbacks. 23250d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler */ 23350d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler public void reducePolicies() { 234f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (MailActivityEmail.DEBUG) { 235560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(TAG, "reducePolicies"); 236aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 237f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank policiesUpdated(); 238a0d080558ff06f88f000cf424803c8241dd8d2ebAndy Stadler } 239a0d080558ff06f88f000cf424803c8241dd8d2ebAndy Stadler 240a0d080558ff06f88f000cf424803c8241dd8d2ebAndy Stadler /** 241469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler * API: Query used to determine if a given policy is "active" (the device is operating at 242469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler * the required security level). 243469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler * 244d09cff08882e553afce919865a2cc60b657d4659Ben Komalo * @param policy the policies requested, or null to check aggregate stored policies 245469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler * @return true if the requested policies are active, false if not. 246469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler */ 247aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank public boolean isActive(Policy policy) { 248aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank int reasons = getInactiveReasons(policy); 249f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (MailActivityEmail.DEBUG && (reasons != 0)) { 250aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank StringBuilder sb = new StringBuilder("isActive for " + policy + ": "); 251aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (reasons == 0) { 252aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank sb.append("true"); 253aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } else { 254aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank sb.append("FALSE -> "); 255aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 256aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if ((reasons & INACTIVE_NEED_ACTIVATION) != 0) { 257aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank sb.append("no_admin "); 258aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 259aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if ((reasons & INACTIVE_NEED_CONFIGURATION) != 0) { 260aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank sb.append("config "); 261aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 262aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if ((reasons & INACTIVE_NEED_PASSWORD) != 0) { 263aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank sb.append("password "); 264aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 265aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if ((reasons & INACTIVE_NEED_ENCRYPTION) != 0) { 266aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank sb.append("encryption "); 267aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 268f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if ((reasons & INACTIVE_PROTOCOL_POLICIES) != 0) { 269f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank sb.append("protocol "); 270f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 271560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(TAG, sb.toString()); 272aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 273469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler return reasons == 0; 274469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler } 275469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler 276469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler /** 277469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler * Return bits from isActive: Device Policy Manager has not been activated 278469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler */ 279469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler public final static int INACTIVE_NEED_ACTIVATION = 1; 280469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler 281469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler /** 282469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler * Return bits from isActive: Some required configuration is not correct (no user action). 283469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler */ 284469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler public final static int INACTIVE_NEED_CONFIGURATION = 2; 285469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler 286469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler /** 287469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler * Return bits from isActive: Password needs to be set or updated 288469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler */ 289469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler public final static int INACTIVE_NEED_PASSWORD = 4; 290469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler 291469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler /** 292469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler * Return bits from isActive: Encryption has not be enabled 293469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler */ 294469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler public final static int INACTIVE_NEED_ENCRYPTION = 8; 295469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler 296469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler /** 297f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * Return bits from isActive: Protocol-specific policies cannot be enforced 298f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank */ 299f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank public final static int INACTIVE_PROTOCOL_POLICIES = 16; 300f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank 301f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank /** 302d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler * API: Query used to determine if a given policy is "active" (the device is operating at 3033d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * the required security level). 304345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * 3053d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * This can be used when syncing a specific account, by passing a specific set of policies 3063d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * for that account. Or, it can be used at any time to compare the device 3073d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * state against the aggregate set of device policies stored in all accounts. 3083d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * 3093d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * This method is for queries only, and does not trigger any change in device state. 3103d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * 3111ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * NOTE: If there are multiple accounts with password expiration policies, the device 3121ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * password will be set to expire in the shortest required interval (most secure). This method 3131ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * will return 'false' as soon as the password expires - irrespective of which account caused 3141ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * the expiration. In other words, all accounts (that require expiration) will run/stop 3151ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * based on the requirements of the account with the shortest interval. 3161ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * 317d09cff08882e553afce919865a2cc60b657d4659Ben Komalo * @param policy the policies requested, or null to check aggregate stored policies 318469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler * @return zero if the requested policies are active, non-zero bits indicates that more work 319469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler * is needed (typically, by the user) before the required security polices are fully active. 320345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler */ 321aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank public int getInactiveReasons(Policy policy) { 32250d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler // select aggregate set if needed 323aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy == null) { 324aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank policy = getAggregatePolicy(); 32550d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler } 32650d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler // quick check for the "empty set" of no policies 327aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy == Policy.NO_POLICY) { 328469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler return 0; 32950d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler } 330469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler int reasons = 0; 331d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler DevicePolicyManager dpm = getDPM(); 332e7f4d3ebfcf497c015ba65be7ecebea8926b995cAndy Stadler if (isActiveAdmin()) { 333d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler // check each policy explicitly 334aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy.mPasswordMinLength > 0) { 335aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (dpm.getPasswordMinimumLength(mAdminName) < policy.mPasswordMinLength) { 336469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler reasons |= INACTIVE_NEED_PASSWORD; 337d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler } 338d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler } 339aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy.mPasswordMode > 0) { 340aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (dpm.getPasswordQuality(mAdminName) < policy.getDPManagerPasswordQuality()) { 341469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler reasons |= INACTIVE_NEED_PASSWORD; 342d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler } 343d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler if (!dpm.isActivePasswordSufficient()) { 344469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler reasons |= INACTIVE_NEED_PASSWORD; 345d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler } 346d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler } 347aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy.mMaxScreenLockTime > 0) { 348d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler // Note, we use seconds, dpm uses milliseconds 349aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (dpm.getMaximumTimeToLock(mAdminName) > policy.mMaxScreenLockTime * 1000) { 350469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler reasons |= INACTIVE_NEED_CONFIGURATION; 351d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler } 352d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler } 353aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy.mPasswordExpirationDays > 0) { 3541ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler // confirm that expirations are currently set 3551ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler long currentTimeout = dpm.getPasswordExpirationTimeout(mAdminName); 3561ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler if (currentTimeout == 0 357aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank || currentTimeout > policy.getDPManagerPasswordExpirationTimeout()) { 358469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler reasons |= INACTIVE_NEED_PASSWORD; 3591ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 3601ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler // confirm that the current password hasn't expired 3611ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler long expirationDate = dpm.getPasswordExpiration(mAdminName); 3621ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler long timeUntilExpiration = expirationDate - System.currentTimeMillis(); 3631ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler boolean expired = timeUntilExpiration < 0; 3641ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler if (expired) { 365469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler reasons |= INACTIVE_NEED_PASSWORD; 3661ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 3679b4988de43dbee6c06066caab63806e8c8303d7dMarc Blank } 368aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy.mPasswordHistory > 0) { 369aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (dpm.getPasswordHistoryLength(mAdminName) < policy.mPasswordHistory) { 370e86d8af163ada4d0b3f7c5cb0b32cfeb12da473cMarc Blank // There's no user action for changes here; this is just a configuration change 371e86d8af163ada4d0b3f7c5cb0b32cfeb12da473cMarc Blank reasons |= INACTIVE_NEED_CONFIGURATION; 3729b4988de43dbee6c06066caab63806e8c8303d7dMarc Blank } 3739b4988de43dbee6c06066caab63806e8c8303d7dMarc Blank } 374aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy.mPasswordComplexChars > 0) { 375aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (dpm.getPasswordMinimumNonLetter(mAdminName) < policy.mPasswordComplexChars) { 376469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler reasons |= INACTIVE_NEED_PASSWORD; 377469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler } 378469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler } 379aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy.mRequireEncryption) { 380c2e638351c19ab22ad9ab4cce2853414c34724c3Andy Stadler int encryptionStatus = getDPM().getStorageEncryptionStatus(); 381469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler if (encryptionStatus != DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE) { 382469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler reasons |= INACTIVE_NEED_ENCRYPTION; 3839b4988de43dbee6c06066caab63806e8c8303d7dMarc Blank } 3849b4988de43dbee6c06066caab63806e8c8303d7dMarc Blank } 385ce582527bbdce6e7a5c255123e9f2b743467919eMarc Blank if (policy.mDontAllowCamera && !dpm.getCameraDisabled(mAdminName)) { 386ce582527bbdce6e7a5c255123e9f2b743467919eMarc Blank reasons |= INACTIVE_NEED_CONFIGURATION; 387ce582527bbdce6e7a5c255123e9f2b743467919eMarc Blank } 388d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler // password failures are counted locally - no test required here 389d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler // no check required for remote wipe (it's supported, if we're the admin) 3902a5eeea9213005060256054ec773e72406415ce4Andrew Stadler 391f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (policy.mProtocolPoliciesUnsupported != null) { 392f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank reasons |= INACTIVE_PROTOCOL_POLICIES; 393f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 394f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank 395469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler // If we made it all the way, reasons == 0 here. Otherwise it's a list of grievances. 396469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler return reasons; 397d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler } 398d71d0b223a5cd02e2a8f1ec5c3f8cebab170d65fAndrew Stadler // return false, not active 399469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler return INACTIVE_NEED_ACTIVATION; 400345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 401345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler 402345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler /** 40350d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler * Set the requested security level based on the aggregate set of requests. 40450d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler * If the set is empty, we release our device administration. If the set is non-empty, 40550d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler * we only proceed if we are already active as an admin. 4063d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler */ 4073d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler public void setActivePolicies() { 4083d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler DevicePolicyManager dpm = getDPM(); 40950d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler // compute aggregate set of policies 410aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank Policy aggregatePolicy = getAggregatePolicy(); 41150d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler // if empty set, detach from policy manager 412aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (aggregatePolicy == Policy.NO_POLICY) { 413f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (MailActivityEmail.DEBUG) { 414560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(TAG, "setActivePolicies: none, remove admin"); 415aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 41650d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler dpm.removeActiveAdmin(mAdminName); 417e7f4d3ebfcf497c015ba65be7ecebea8926b995cAndy Stadler } else if (isActiveAdmin()) { 418f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (MailActivityEmail.DEBUG) { 419560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(TAG, "setActivePolicies: " + aggregatePolicy); 420aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 4213d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler // set each policy in the policy manager 4223d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler // password mode & length 423aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank dpm.setPasswordQuality(mAdminName, aggregatePolicy.getDPManagerPasswordQuality()); 424aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank dpm.setPasswordMinimumLength(mAdminName, aggregatePolicy.mPasswordMinLength); 4253d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler // screen lock time 426aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank dpm.setMaximumTimeToLock(mAdminName, aggregatePolicy.mMaxScreenLockTime * 1000); 4273d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler // local wipe (failed passwords limit) 428aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank dpm.setMaximumFailedPasswordsForWipe(mAdminName, aggregatePolicy.mPasswordMaxFails); 4291ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler // password expiration (days until a password expires). API takes mSec. 4301ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler dpm.setPasswordExpirationTimeout(mAdminName, 431aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregatePolicy.getDPManagerPasswordExpirationTimeout()); 4329b4988de43dbee6c06066caab63806e8c8303d7dMarc Blank // password history length (number of previous passwords that may not be reused) 433aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank dpm.setPasswordHistoryLength(mAdminName, aggregatePolicy.mPasswordHistory); 43422759bacd95385d95d3d9321f490763df1aba89dAndy Stadler // password minimum complex characters. 43522759bacd95385d95d3d9321f490763df1aba89dAndy Stadler // Note, in Exchange, "complex chars" simply means "non alpha", but in the DPM, 43622759bacd95385d95d3d9321f490763df1aba89dAndy Stadler // setting the quality to complex also defaults min symbols=1 and min numeric=1. 43722759bacd95385d95d3d9321f490763df1aba89dAndy Stadler // We always / safely clear minSymbols & minNumeric to zero (there is no policy 43822759bacd95385d95d3d9321f490763df1aba89dAndy Stadler // configuration in which we explicitly require a minimum number of digits or symbols.) 43922759bacd95385d95d3d9321f490763df1aba89dAndy Stadler dpm.setPasswordMinimumSymbols(mAdminName, 0); 44022759bacd95385d95d3d9321f490763df1aba89dAndy Stadler dpm.setPasswordMinimumNumeric(mAdminName, 0); 441aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank dpm.setPasswordMinimumNonLetter(mAdminName, aggregatePolicy.mPasswordComplexChars); 442d09cff08882e553afce919865a2cc60b657d4659Ben Komalo // Device capabilities 443d09cff08882e553afce919865a2cc60b657d4659Ben Komalo dpm.setCameraDisabled(mAdminName, aggregatePolicy.mDontAllowCamera); 444d09cff08882e553afce919865a2cc60b657d4659Ben Komalo 445469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler // encryption required 446aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank dpm.setStorageEncryption(mAdminName, aggregatePolicy.mRequireEncryption); 4473d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler } 4483d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler } 4493d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler 4503d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler /** 4519ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank * Convenience method; see javadoc below 4529ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank */ 4539ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank public static void setAccountHoldFlag(Context context, long accountId, boolean newState) { 4549ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank Account account = Account.restoreAccountWithId(context, accountId); 4559ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank if (account != null) { 4569ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank setAccountHoldFlag(context, account, newState); 457bc53491b95e4de2e30b04e696f30d209539ec87aMarc Blank if (newState) { 458bc53491b95e4de2e30b04e696f30d209539ec87aMarc Blank // Make sure there's a notification up 459bc53491b95e4de2e30b04e696f30d209539ec87aMarc Blank NotificationController.getInstance(context).showSecurityNeededNotification(account); 460bc53491b95e4de2e30b04e696f30d209539ec87aMarc Blank } 4619ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank } 4629ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank } 4639ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank 4649ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank /** 4652a5eeea9213005060256054ec773e72406415ce4Andrew Stadler * API: Set/Clear the "hold" flag in any account. This flag serves a dual purpose: 4662a5eeea9213005060256054ec773e72406415ce4Andrew Stadler * Setting it gives us an indication that it was blocked, and clearing it gives EAS a 4672a5eeea9213005060256054ec773e72406415ce4Andrew Stadler * signal to try syncing again. 4681ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * @param context 4699ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank * @param account the account whose hold flag is to be set/cleared 4701ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * @param newState true = security hold, false = free to sync 4712a5eeea9213005060256054ec773e72406415ce4Andrew Stadler */ 4721ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler public static void setAccountHoldFlag(Context context, Account account, boolean newState) { 4732a5eeea9213005060256054ec773e72406415ce4Andrew Stadler if (newState) { 4742a5eeea9213005060256054ec773e72406415ce4Andrew Stadler account.mFlags |= Account.FLAGS_SECURITY_HOLD; 4752a5eeea9213005060256054ec773e72406415ce4Andrew Stadler } else { 4762a5eeea9213005060256054ec773e72406415ce4Andrew Stadler account.mFlags &= ~Account.FLAGS_SECURITY_HOLD; 4772a5eeea9213005060256054ec773e72406415ce4Andrew Stadler } 4782a5eeea9213005060256054ec773e72406415ce4Andrew Stadler ContentValues cv = new ContentValues(); 4792a5eeea9213005060256054ec773e72406415ce4Andrew Stadler cv.put(AccountColumns.FLAGS, account.mFlags); 4801ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler account.update(context, cv); 4812a5eeea9213005060256054ec773e72406415ce4Andrew Stadler } 4822a5eeea9213005060256054ec773e72406415ce4Andrew Stadler 4832a5eeea9213005060256054ec773e72406415ce4Andrew Stadler /** 4843d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * API: Sync service should call this any time a sync fails due to isActive() returning false. 485d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler * This will kick off the notify-acquire-admin-state process and/or increase the security level. 486345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * The caller needs to write the required policies into this account before making this call. 4873d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * Should not be called from UI thread - uses DB lookups to prepare new notifications 488345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * 489345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * @param accountId the account for which sync cannot proceed 490345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler */ 491345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler public void policiesRequired(long accountId) { 492f5418f1f93b02e7fab9f15eb201800b65510998eMarc Blank Account account = Account.restoreAccountWithId(mContext, accountId); 493844b14f851ce748b7b204125264bb3343c1a9039Marc Blank // In case the account has been deleted, just return 494844b14f851ce748b7b204125264bb3343c1a9039Marc Blank if (account == null) return; 495f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (account.mPolicyKey == 0) return; 496f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank Policy policy = Policy.restorePolicyWithId(mContext, account.mPolicyKey); 497f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (policy == null) return; 498f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (MailActivityEmail.DEBUG) { 499560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(TAG, "policiesRequired for " + account.mDisplayName + ": " + policy); 500aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 5011ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler 5022a5eeea9213005060256054ec773e72406415ce4Andrew Stadler // Mark the account as "on hold". 5031ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler setAccountHoldFlag(mContext, account, true); 5041ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler 505f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Put up an appropriate notification 506f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (policy.mProtocolPoliciesUnsupported == null) { 507f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank NotificationController.getInstance(mContext).showSecurityNeededNotification(account); 508f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } else { 509f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank NotificationController.getInstance(mContext).showSecurityUnsupportedNotification( 510f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank account); 511f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 512f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 513f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank 514f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank public static void clearAccountPolicy(Context context, Account account) { 515f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank setAccountPolicy(context, account, null, null); 516f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 517f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank 518f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank /** 519f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * Set the policy for an account atomically; this also removes any other policy associated with 520f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * the account and sets the policy key for the account. If policy is null, the policyKey is 521f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * set to 0 and the securitySyncKey to null. Also, update the account object to reflect the 522f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * current policyKey and securitySyncKey 523f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * @param context the caller's context 524f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * @param account the account whose policy is to be set 525f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * @param policy the policy to set, or null if we're clearing the policy 526f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * @param securitySyncKey the security sync key for this account (ignored if policy is null) 527f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank */ 528f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank public static void setAccountPolicy(Context context, Account account, Policy policy, 529f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank String securitySyncKey) { 530f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 531f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank 532f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Make sure this is a valid policy set 533f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (policy != null) { 534f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank policy.normalize(); 535f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Add the new policy (no account will yet reference this) 536f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank ops.add(ContentProviderOperation.newInsert( 537f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank Policy.CONTENT_URI).withValues(policy.toContentValues()).build()); 538f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Make the policyKey of the account our newly created policy, and set the sync key 539f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank ops.add(ContentProviderOperation.newUpdate( 540f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank ContentUris.withAppendedId(Account.CONTENT_URI, account.mId)) 541f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank .withValueBackReference(AccountColumns.POLICY_KEY, 0) 542f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank .withValue(AccountColumns.SECURITY_SYNC_KEY, securitySyncKey) 543f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank .build()); 544f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } else { 545f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank ops.add(ContentProviderOperation.newUpdate( 546f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank ContentUris.withAppendedId(Account.CONTENT_URI, account.mId)) 547f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank .withValue(AccountColumns.SECURITY_SYNC_KEY, null) 548f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank .withValue(AccountColumns.POLICY_KEY, 0) 549f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank .build()); 550f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 551f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank 552f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Delete the previous policy associated with this account, if any 553f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (account.mPolicyKey > 0) { 554f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank ops.add(ContentProviderOperation.newDelete( 555f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank ContentUris.withAppendedId( 556f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank Policy.CONTENT_URI, account.mPolicyKey)).build()); 557f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 558f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank 559f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank try { 560f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank context.getContentResolver().applyBatch(EmailContent.AUTHORITY, ops); 561f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank account.refresh(context); 562bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu syncAccount(context, account); 563f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } catch (RemoteException e) { 564f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // This is fatal to a remote process 565f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank throw new IllegalStateException("Exception setting account policy."); 566f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } catch (OperationApplicationException e) { 567f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Can't happen; our provider doesn't throw this exception 568f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 569f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 570f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank 571bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu private static void syncAccount(final Context context, final Account account) { 572bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu final EmailServiceUtils.EmailServiceInfo info = 573bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu EmailServiceUtils.getServiceInfo(context, account.getProtocol(context)); 574bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu final android.accounts.Account amAccount = 575bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu new android.accounts.Account(account.mEmailAddress, info.accountType); 576bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu final Bundle extras = new Bundle(3); 577bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); 578bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu extras.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, true); 579bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true); 580bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu ContentResolver.requestSync(amAccount, EmailContent.AUTHORITY, extras); 581bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu } 582bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu 583bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu public void syncAccount(final Account account) { 584bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu syncAccount(mContext, account); 585bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu } 586bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu 587f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank public void setAccountPolicy(long accountId, Policy policy, String securityKey) { 588f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank Account account = Account.restoreAccountWithId(mContext, accountId); 589f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank Policy oldPolicy = null; 590f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (account.mPolicyKey > 0) { 591f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank oldPolicy = Policy.restorePolicyWithId(mContext, account.mPolicyKey); 592f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 593940a335bab634a849b45f62d711601d57f59aeb1Yu Ping Hu 594940a335bab634a849b45f62d711601d57f59aeb1Yu Ping Hu // If attachment policies have changed, fix up any affected attachment records 595940a335bab634a849b45f62d711601d57f59aeb1Yu Ping Hu if (oldPolicy != null && securityKey != null) { 596940a335bab634a849b45f62d711601d57f59aeb1Yu Ping Hu if ((oldPolicy.mDontAllowAttachments != policy.mDontAllowAttachments) || 597940a335bab634a849b45f62d711601d57f59aeb1Yu Ping Hu (oldPolicy.mMaxAttachmentSize != policy.mMaxAttachmentSize)) { 598940a335bab634a849b45f62d711601d57f59aeb1Yu Ping Hu Policy.setAttachmentFlagsForNewPolicy(mContext, account, policy); 599940a335bab634a849b45f62d711601d57f59aeb1Yu Ping Hu } 600940a335bab634a849b45f62d711601d57f59aeb1Yu Ping Hu } 601940a335bab634a849b45f62d711601d57f59aeb1Yu Ping Hu 602f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank boolean policyChanged = (oldPolicy == null) || !oldPolicy.equals(policy); 603f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (!policyChanged && (TextUtilities.stringOrNullEquals(securityKey, 604f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank account.mSecuritySyncKey))) { 605560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(Logging.LOG_TAG, "setAccountPolicy; policy unchanged"); 606f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } else { 607f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank setAccountPolicy(mContext, account, policy, securityKey); 608f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank policiesUpdated(); 609f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 610f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank 611f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank boolean setHold = false; 612f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (policy.mProtocolPoliciesUnsupported != null) { 613f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // We can't support this, reasons in unsupportedRemotePolicies 614560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(Logging.LOG_TAG, 615f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank "Notify policies for " + account.mDisplayName + " not supported."); 616f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank setHold = true; 617f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank NotificationController.getInstance(mContext).showSecurityUnsupportedNotification( 618f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank account); 619f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Erase data 620f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank Uri uri = EmailProvider.uiUri("uiaccountdata", accountId); 621f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank mContext.getContentResolver().delete(uri, null, null); 622f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } else if (isActive(policy)) { 623f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (policyChanged) { 624560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(Logging.LOG_TAG, "Notify policies for " + account.mDisplayName 625560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy + " changed."); 626f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Notify that policies changed 627f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank NotificationController.getInstance(mContext).showSecurityChangedNotification( 628f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank account); 629bc53491b95e4de2e30b04e696f30d209539ec87aMarc Blank } else { 630560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(Logging.LOG_TAG, "Policy is active and unchanged; do not notify."); 631f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 632f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } else { 633f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank setHold = true; 634560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(Logging.LOG_TAG, "Notify policies for " + account.mDisplayName + 635f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank " are not being enforced."); 636f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Put up a notification 637f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank NotificationController.getInstance(mContext).showSecurityNeededNotification(account); 638f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 639f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Set/clear the account hold. 640f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank setAccountHoldFlag(mContext, account, setHold); 6413d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler } 6423d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler 6433d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler /** 6443d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * Called from the notification's intent receiver to register that the notification can be 6453d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * cleared now. 6463d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler */ 647c6df1d605fb3e235df6d4a21ae00632c9d6e3cc2Marc Blank public void clearNotification() { 648308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki NotificationController.getInstance(mContext).cancelSecurityNeededNotification(); 6493d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler } 6503d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler 6513d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler /** 65250d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler * API: Remote wipe (from server). This is final, there is no confirmation. It will only 653c82c1caf0138dbcef044ee41d24791f2ebeeb88aMarc Blank * return to the caller if there is an unexpected failure. The wipe includes external storage. 6543d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler */ 65550d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler public void remoteWipe() { 6563d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler DevicePolicyManager dpm = getDPM(); 6573d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler if (dpm.isAdminActive(mAdminName)) { 658c82c1caf0138dbcef044ee41d24791f2ebeeb88aMarc Blank dpm.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE); 65950d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler } else { 660560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(Logging.LOG_TAG, "Could not remote wipe because not device admin."); 6613d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler } 662345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 663345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler /** 6643d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * If we are not the active device admin, try to become so. 6653d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * 666e7f4d3ebfcf497c015ba65be7ecebea8926b995cAndy Stadler * Also checks for any policies that we have added during the lifetime of this app. 667e7f4d3ebfcf497c015ba65be7ecebea8926b995cAndy Stadler * This catches the case where the user granted an earlier (smaller) set of policies 668e7f4d3ebfcf497c015ba65be7ecebea8926b995cAndy Stadler * but an app upgrade requires that new policies be granted. 669e7f4d3ebfcf497c015ba65be7ecebea8926b995cAndy Stadler * 6703d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * @return true if we are already active, false if we are not 671345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler */ 6723d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler public boolean isActiveAdmin() { 6733d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler DevicePolicyManager dpm = getDPM(); 674c2e638351c19ab22ad9ab4cce2853414c34724c3Andy Stadler return dpm.isAdminActive(mAdminName) 675c2e638351c19ab22ad9ab4cce2853414c34724c3Andy Stadler && dpm.hasGrantedPolicy(mAdminName, DeviceAdminInfo.USES_POLICY_EXPIRE_PASSWORD) 676aa0a3553974972561e089f2780fb7f6743b3303eBen Komalo && dpm.hasGrantedPolicy(mAdminName, DeviceAdminInfo.USES_ENCRYPTED_STORAGE) 677aa0a3553974972561e089f2780fb7f6743b3303eBen Komalo && dpm.hasGrantedPolicy(mAdminName, DeviceAdminInfo.USES_POLICY_DISABLE_CAMERA); 6783d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler } 679345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler 6803d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler /** 6813d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * Report admin component name - for making calls into device policy manager 6823d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler */ 6833d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler public ComponentName getAdminComponent() { 6843d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler return mAdminName; 6853d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler } 6863d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler 6873d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler /** 68802d59d21949a77c60859b615312f02e6d8003490Marc Blank * Delete all accounts whose security flags aren't zero (i.e. they have security enabled). 68902d59d21949a77c60859b615312f02e6d8003490Marc Blank * This method is synchronous, so it should normally be called within a worker thread (the 69002d59d21949a77c60859b615312f02e6d8003490Marc Blank * exception being for unit tests) 69102d59d21949a77c60859b615312f02e6d8003490Marc Blank * 69202d59d21949a77c60859b615312f02e6d8003490Marc Blank * @param context the caller's context 69302d59d21949a77c60859b615312f02e6d8003490Marc Blank */ 69402d59d21949a77c60859b615312f02e6d8003490Marc Blank /*package*/ void deleteSecuredAccounts(Context context) { 69502d59d21949a77c60859b615312f02e6d8003490Marc Blank ContentResolver cr = context.getContentResolver(); 69602d59d21949a77c60859b615312f02e6d8003490Marc Blank // Find all accounts with security and delete them 69702d59d21949a77c60859b615312f02e6d8003490Marc Blank Cursor c = cr.query(Account.CONTENT_URI, EmailContent.ID_PROJECTION, 698aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank Account.SECURITY_NONZERO_SELECTION, null, null); 69902d59d21949a77c60859b615312f02e6d8003490Marc Blank try { 700560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.w(TAG, "Email administration disabled; deleting " + c.getCount() + 70102d59d21949a77c60859b615312f02e6d8003490Marc Blank " secured account(s)"); 70202d59d21949a77c60859b615312f02e6d8003490Marc Blank while (c.moveToNext()) { 703f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank long accountId = c.getLong(EmailContent.ID_PROJECTION_COLUMN); 704f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank Uri uri = EmailProvider.uiUri("uiaccountdata", accountId); 705f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank cr.delete(uri, null, null); 70602d59d21949a77c60859b615312f02e6d8003490Marc Blank } 70702d59d21949a77c60859b615312f02e6d8003490Marc Blank } finally { 70802d59d21949a77c60859b615312f02e6d8003490Marc Blank c.close(); 70902d59d21949a77c60859b615312f02e6d8003490Marc Blank } 710f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank policiesUpdated(); 71102d59d21949a77c60859b615312f02e6d8003490Marc Blank } 71202d59d21949a77c60859b615312f02e6d8003490Marc Blank 71302d59d21949a77c60859b615312f02e6d8003490Marc Blank /** 71402d59d21949a77c60859b615312f02e6d8003490Marc Blank * Internal handler for enabled->disabled transitions. Deletes all secured accounts. 715a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler * Must call from worker thread, not on UI thread. 7163d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler */ 71702d59d21949a77c60859b615312f02e6d8003490Marc Blank /*package*/ void onAdminEnabled(boolean isEnabled) { 718856e09d76ab62272e660fd4a08e25637f17319a0Andrew Stadler if (!isEnabled) { 719a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler deleteSecuredAccounts(mContext); 7203d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler } 7213d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler } 7223d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler 7233d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler /** 7241ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * Handle password expiration - if any accounts appear to have triggered this, put up 7251ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * warnings, or even shut them down. 7261ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * 7271ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * NOTE: If there are multiple accounts with password expiration policies, the device 7281ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * password will be set to expire in the shortest required interval (most secure). The logic 7291ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * in this method operates based on the aggregate setting - irrespective of which account caused 7301ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * the expiration. In other words, all accounts (that require expiration) will run/stop 7311ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * based on the requirements of the account with the shortest interval. 7321ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler */ 733a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler private void onPasswordExpiring(Context context) { 7341ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler // 1. Do we have any accounts that matter here? 7351ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler long nextExpiringAccountId = findShortestExpiration(context); 7361ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler 7371ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler // 2. If not, exit immediately 7381ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler if (nextExpiringAccountId == -1) { 7391ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler return; 7401ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 7411ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler 7421ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler // 3. If yes, are we warning or expired? 7431ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler long expirationDate = getDPM().getPasswordExpiration(mAdminName); 7441ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler long timeUntilExpiration = expirationDate - System.currentTimeMillis(); 7451ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler boolean expired = timeUntilExpiration < 0; 7461ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler if (!expired) { 7471ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler // 4. If warning, simply put up a generic notification and report that it came from 7481ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler // the shortest-expiring account. 749308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki NotificationController.getInstance(mContext).showPasswordExpiringNotification( 750308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki nextExpiringAccountId); 7511ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } else { 7521ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler // 5. Actually expired - find all accounts that expire passwords, and wipe them 753f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank boolean wiped = wipeExpiredAccounts(context); 7541ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler if (wiped) { 755308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki NotificationController.getInstance(mContext).showPasswordExpiredNotification( 756308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki nextExpiringAccountId); 7571ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 7581ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 7591ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 7601ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler 7611ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler /** 7621ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * Find the account with the shortest expiration time. This is always assumed to be 7631ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * the account that forces the password to be refreshed. 7641ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * @return -1 if no expirations, or accountId if one is found 7651ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler */ 766aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank @VisibleForTesting 767aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank /*package*/ static long findShortestExpiration(Context context) { 768aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank long policyId = Utility.getFirstRowLong(context, Policy.CONTENT_URI, Policy.ID_PROJECTION, 769aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank HAS_PASSWORD_EXPIRATION, null, PolicyColumns.PASSWORD_EXPIRATION_DAYS + " ASC", 770aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank EmailContent.ID_PROJECTION_COLUMN, -1L); 771aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policyId < 0) return -1L; 772aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank return Policy.getAccountIdWithPolicyKey(context, policyId); 7731ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 7741ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler 7751ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler /** 7761ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * For all accounts that require password expiration, put them in security hold and wipe 7771ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * their data. 7781ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * @param context 7791ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * @param controller 7801ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * @return true if one or more accounts were wiped 7811ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler */ 782aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank @VisibleForTesting 783f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank /*package*/ static boolean wipeExpiredAccounts(Context context) { 7841ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler boolean result = false; 785aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank Cursor c = context.getContentResolver().query(Policy.CONTENT_URI, 786aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank Policy.ID_PROJECTION, HAS_PASSWORD_EXPIRATION, null, null); 7871ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler try { 7881ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler while (c.moveToNext()) { 789aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank long policyId = c.getLong(Policy.ID_PROJECTION_COLUMN); 790aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank long accountId = Policy.getAccountIdWithPolicyKey(context, policyId); 791aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (accountId < 0) continue; 792aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank Account account = Account.restoreAccountWithId(context, accountId); 793aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (account != null) { 794aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank // Mark the account as "on hold". 795aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank setAccountHoldFlag(context, account, true); 796aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank // Erase data 797f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank Uri uri = EmailProvider.uiUri("uiaccountdata", accountId); 798f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank context.getContentResolver().delete(uri, null, null); 799aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank // Report one or more were found 800aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank result = true; 8011ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 8021ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 8031ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } finally { 8041ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler c.close(); 8051ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 8061ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler return result; 8071ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 8081ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler 8091ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler /** 810a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler * Callback from EmailBroadcastProcessorService. This provides the workers for the 811a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler * DeviceAdminReceiver calls. These should perform the work directly and not use async 812a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler * threads for completion. 813a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler */ 814a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler public static void onDeviceAdminReceiverMessage(Context context, int message) { 815a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler SecurityPolicy instance = SecurityPolicy.getInstance(context); 816a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler switch (message) { 817a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler case DEVICE_ADMIN_MESSAGE_ENABLED: 818a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler instance.onAdminEnabled(true); 819a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler break; 820a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler case DEVICE_ADMIN_MESSAGE_DISABLED: 821a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler instance.onAdminEnabled(false); 822a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler break; 823a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler case DEVICE_ADMIN_MESSAGE_PASSWORD_CHANGED: 824a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler // TODO make a small helper for this 825a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler // Clear security holds (if any) 826a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler Account.clearSecurityHoldOnAllAccounts(context); 827a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler // Cancel any active notifications (if any are posted) 828308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki NotificationController.getInstance(context).cancelPasswordExpirationNotifications(); 829a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler break; 830a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler case DEVICE_ADMIN_MESSAGE_PASSWORD_EXPIRING: 831a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler instance.onPasswordExpiring(instance.mContext); 832a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler break; 833a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler } 834a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler } 835a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler 836a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler /** 8373d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * Device Policy administrator. This is primarily a listener for device state changes. 8383d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * Note: This is instantiated by incoming messages. 839a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler * Note: This is actually a BroadcastReceiver and must remain within the guidelines required 840a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler * for proper behavior, including avoidance of ANRs. 8415893e9e008d46cf4be0c7f709a6e77e2652c3dddAndrew Stadler * Note: We do not implement onPasswordFailed() because the default behavior of the 8425893e9e008d46cf4be0c7f709a6e77e2652c3dddAndrew Stadler * DevicePolicyManager - complete local wipe after 'n' failures - is sufficient. 8433d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler */ 8444ae83c58b3e136b4b1e859ee304ad1b332e9597fDianne Hackborn public static class PolicyAdmin extends DeviceAdminReceiver { 845345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler 846345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler /** 847345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * Called after the administrator is first enabled. 848345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler */ 849345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler @Override 850345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler public void onEnabled(Context context, Intent intent) { 851a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler EmailBroadcastProcessorService.processDevicePolicyMessage(context, 852a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler DEVICE_ADMIN_MESSAGE_ENABLED); 853345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 854856e09d76ab62272e660fd4a08e25637f17319a0Andrew Stadler 855345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler /** 856345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * Called prior to the administrator being disabled. 857345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler */ 858345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler @Override 859345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler public void onDisabled(Context context, Intent intent) { 860a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler EmailBroadcastProcessorService.processDevicePolicyMessage(context, 861a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler DEVICE_ADMIN_MESSAGE_DISABLED); 862345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 863856e09d76ab62272e660fd4a08e25637f17319a0Andrew Stadler 864345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler /** 86502d59d21949a77c60859b615312f02e6d8003490Marc Blank * Called when the user asks to disable administration; we return a warning string that 86602d59d21949a77c60859b615312f02e6d8003490Marc Blank * will be presented to the user 86702d59d21949a77c60859b615312f02e6d8003490Marc Blank */ 86802d59d21949a77c60859b615312f02e6d8003490Marc Blank @Override 86902d59d21949a77c60859b615312f02e6d8003490Marc Blank public CharSequence onDisableRequested(Context context, Intent intent) { 87002d59d21949a77c60859b615312f02e6d8003490Marc Blank return context.getString(R.string.disable_admin_warning); 87102d59d21949a77c60859b615312f02e6d8003490Marc Blank } 87202d59d21949a77c60859b615312f02e6d8003490Marc Blank 87302d59d21949a77c60859b615312f02e6d8003490Marc Blank /** 874345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * Called after the user has changed their password. 875345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler */ 876345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler @Override 877345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler public void onPasswordChanged(Context context, Intent intent) { 878a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler EmailBroadcastProcessorService.processDevicePolicyMessage(context, 879a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler DEVICE_ADMIN_MESSAGE_PASSWORD_CHANGED); 8801ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 8811ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler 8821ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler /** 8831ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * Called when device password is expiring 8841ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler */ 8851ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler @Override 8861ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler public void onPasswordExpiring(Context context, Intent intent) { 887a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler EmailBroadcastProcessorService.processDevicePolicyMessage(context, 888a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler DEVICE_ADMIN_MESSAGE_PASSWORD_EXPIRING); 889345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 890345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 891345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler} 892