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 + ": "); 251076ab8307439f31e8197579f11b9bc3342fd5626Tony Mantler sb.append("FALSE -> "); 252aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if ((reasons & INACTIVE_NEED_ACTIVATION) != 0) { 253aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank sb.append("no_admin "); 254aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 255aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if ((reasons & INACTIVE_NEED_CONFIGURATION) != 0) { 256aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank sb.append("config "); 257aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 258aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if ((reasons & INACTIVE_NEED_PASSWORD) != 0) { 259aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank sb.append("password "); 260aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 261aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if ((reasons & INACTIVE_NEED_ENCRYPTION) != 0) { 262aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank sb.append("encryption "); 263aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 264f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if ((reasons & INACTIVE_PROTOCOL_POLICIES) != 0) { 265f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank sb.append("protocol "); 266f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 267560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(TAG, sb.toString()); 268aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 269469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler return reasons == 0; 270469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler } 271469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler 272469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler /** 273469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler * Return bits from isActive: Device Policy Manager has not been activated 274469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler */ 275469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler public final static int INACTIVE_NEED_ACTIVATION = 1; 276469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler 277469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler /** 278469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler * Return bits from isActive: Some required configuration is not correct (no user action). 279469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler */ 280469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler public final static int INACTIVE_NEED_CONFIGURATION = 2; 281469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler 282469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler /** 283469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler * Return bits from isActive: Password needs to be set or updated 284469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler */ 285469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler public final static int INACTIVE_NEED_PASSWORD = 4; 286469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler 287469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler /** 288469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler * Return bits from isActive: Encryption has not be enabled 289469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler */ 290469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler public final static int INACTIVE_NEED_ENCRYPTION = 8; 291469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler 292469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler /** 293f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * Return bits from isActive: Protocol-specific policies cannot be enforced 294f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank */ 295f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank public final static int INACTIVE_PROTOCOL_POLICIES = 16; 296f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank 297f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank /** 298d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler * API: Query used to determine if a given policy is "active" (the device is operating at 2993d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * the required security level). 300345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * 3013d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * This can be used when syncing a specific account, by passing a specific set of policies 3023d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * for that account. Or, it can be used at any time to compare the device 3033d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * state against the aggregate set of device policies stored in all accounts. 3043d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * 3053d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * This method is for queries only, and does not trigger any change in device state. 3063d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * 3071ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * NOTE: If there are multiple accounts with password expiration policies, the device 3081ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * password will be set to expire in the shortest required interval (most secure). This method 3091ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * will return 'false' as soon as the password expires - irrespective of which account caused 3101ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * the expiration. In other words, all accounts (that require expiration) will run/stop 3111ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * based on the requirements of the account with the shortest interval. 3121ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * 313d09cff08882e553afce919865a2cc60b657d4659Ben Komalo * @param policy the policies requested, or null to check aggregate stored policies 314469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler * @return zero if the requested policies are active, non-zero bits indicates that more work 315469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler * is needed (typically, by the user) before the required security polices are fully active. 316345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler */ 317aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank public int getInactiveReasons(Policy policy) { 31850d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler // select aggregate set if needed 319aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy == null) { 320aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank policy = getAggregatePolicy(); 32150d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler } 32250d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler // quick check for the "empty set" of no policies 323aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy == Policy.NO_POLICY) { 324469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler return 0; 32550d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler } 326469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler int reasons = 0; 327d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler DevicePolicyManager dpm = getDPM(); 328e7f4d3ebfcf497c015ba65be7ecebea8926b995cAndy Stadler if (isActiveAdmin()) { 329d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler // check each policy explicitly 330aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy.mPasswordMinLength > 0) { 331aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (dpm.getPasswordMinimumLength(mAdminName) < policy.mPasswordMinLength) { 332469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler reasons |= INACTIVE_NEED_PASSWORD; 333d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler } 334d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler } 335aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy.mPasswordMode > 0) { 336aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (dpm.getPasswordQuality(mAdminName) < policy.getDPManagerPasswordQuality()) { 337469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler reasons |= INACTIVE_NEED_PASSWORD; 338d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler } 339d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler if (!dpm.isActivePasswordSufficient()) { 340469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler reasons |= INACTIVE_NEED_PASSWORD; 341d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler } 342d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler } 343aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy.mMaxScreenLockTime > 0) { 344d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler // Note, we use seconds, dpm uses milliseconds 345aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (dpm.getMaximumTimeToLock(mAdminName) > policy.mMaxScreenLockTime * 1000) { 346469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler reasons |= INACTIVE_NEED_CONFIGURATION; 347d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler } 348d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler } 349aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy.mPasswordExpirationDays > 0) { 3501ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler // confirm that expirations are currently set 3511ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler long currentTimeout = dpm.getPasswordExpirationTimeout(mAdminName); 3521ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler if (currentTimeout == 0 353aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank || currentTimeout > policy.getDPManagerPasswordExpirationTimeout()) { 354469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler reasons |= INACTIVE_NEED_PASSWORD; 3551ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 3561ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler // confirm that the current password hasn't expired 3571ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler long expirationDate = dpm.getPasswordExpiration(mAdminName); 3581ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler long timeUntilExpiration = expirationDate - System.currentTimeMillis(); 3591ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler boolean expired = timeUntilExpiration < 0; 3601ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler if (expired) { 361469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler reasons |= INACTIVE_NEED_PASSWORD; 3621ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 3639b4988de43dbee6c06066caab63806e8c8303d7dMarc Blank } 364aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy.mPasswordHistory > 0) { 365aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (dpm.getPasswordHistoryLength(mAdminName) < policy.mPasswordHistory) { 366e86d8af163ada4d0b3f7c5cb0b32cfeb12da473cMarc Blank // There's no user action for changes here; this is just a configuration change 367e86d8af163ada4d0b3f7c5cb0b32cfeb12da473cMarc Blank reasons |= INACTIVE_NEED_CONFIGURATION; 3689b4988de43dbee6c06066caab63806e8c8303d7dMarc Blank } 3699b4988de43dbee6c06066caab63806e8c8303d7dMarc Blank } 370aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy.mPasswordComplexChars > 0) { 371aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (dpm.getPasswordMinimumNonLetter(mAdminName) < policy.mPasswordComplexChars) { 372469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler reasons |= INACTIVE_NEED_PASSWORD; 373469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler } 374469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler } 375aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policy.mRequireEncryption) { 376c2e638351c19ab22ad9ab4cce2853414c34724c3Andy Stadler int encryptionStatus = getDPM().getStorageEncryptionStatus(); 377469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler if (encryptionStatus != DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE) { 378469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler reasons |= INACTIVE_NEED_ENCRYPTION; 3799b4988de43dbee6c06066caab63806e8c8303d7dMarc Blank } 3809b4988de43dbee6c06066caab63806e8c8303d7dMarc Blank } 381ce582527bbdce6e7a5c255123e9f2b743467919eMarc Blank if (policy.mDontAllowCamera && !dpm.getCameraDisabled(mAdminName)) { 382ce582527bbdce6e7a5c255123e9f2b743467919eMarc Blank reasons |= INACTIVE_NEED_CONFIGURATION; 383ce582527bbdce6e7a5c255123e9f2b743467919eMarc Blank } 384d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler // password failures are counted locally - no test required here 385d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler // no check required for remote wipe (it's supported, if we're the admin) 3862a5eeea9213005060256054ec773e72406415ce4Andrew Stadler 387f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (policy.mProtocolPoliciesUnsupported != null) { 388f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank reasons |= INACTIVE_PROTOCOL_POLICIES; 389f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 390f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank 391469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler // If we made it all the way, reasons == 0 here. Otherwise it's a list of grievances. 392469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler return reasons; 393d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler } 394d71d0b223a5cd02e2a8f1ec5c3f8cebab170d65fAndrew Stadler // return false, not active 395469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler return INACTIVE_NEED_ACTIVATION; 396345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 397345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler 398345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler /** 39950d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler * Set the requested security level based on the aggregate set of requests. 40050d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler * If the set is empty, we release our device administration. If the set is non-empty, 40150d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler * we only proceed if we are already active as an admin. 4023d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler */ 4033d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler public void setActivePolicies() { 4043d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler DevicePolicyManager dpm = getDPM(); 40550d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler // compute aggregate set of policies 406aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank Policy aggregatePolicy = getAggregatePolicy(); 40750d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler // if empty set, detach from policy manager 408aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (aggregatePolicy == Policy.NO_POLICY) { 409f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (MailActivityEmail.DEBUG) { 410560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(TAG, "setActivePolicies: none, remove admin"); 411aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 41250d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler dpm.removeActiveAdmin(mAdminName); 413e7f4d3ebfcf497c015ba65be7ecebea8926b995cAndy Stadler } else if (isActiveAdmin()) { 414f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (MailActivityEmail.DEBUG) { 415560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(TAG, "setActivePolicies: " + aggregatePolicy); 416aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 4173d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler // set each policy in the policy manager 4183d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler // password mode & length 419aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank dpm.setPasswordQuality(mAdminName, aggregatePolicy.getDPManagerPasswordQuality()); 420aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank dpm.setPasswordMinimumLength(mAdminName, aggregatePolicy.mPasswordMinLength); 4213d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler // screen lock time 422aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank dpm.setMaximumTimeToLock(mAdminName, aggregatePolicy.mMaxScreenLockTime * 1000); 4233d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler // local wipe (failed passwords limit) 424aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank dpm.setMaximumFailedPasswordsForWipe(mAdminName, aggregatePolicy.mPasswordMaxFails); 4251ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler // password expiration (days until a password expires). API takes mSec. 4261ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler dpm.setPasswordExpirationTimeout(mAdminName, 427aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank aggregatePolicy.getDPManagerPasswordExpirationTimeout()); 4289b4988de43dbee6c06066caab63806e8c8303d7dMarc Blank // password history length (number of previous passwords that may not be reused) 429aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank dpm.setPasswordHistoryLength(mAdminName, aggregatePolicy.mPasswordHistory); 43022759bacd95385d95d3d9321f490763df1aba89dAndy Stadler // password minimum complex characters. 43122759bacd95385d95d3d9321f490763df1aba89dAndy Stadler // Note, in Exchange, "complex chars" simply means "non alpha", but in the DPM, 43222759bacd95385d95d3d9321f490763df1aba89dAndy Stadler // setting the quality to complex also defaults min symbols=1 and min numeric=1. 43322759bacd95385d95d3d9321f490763df1aba89dAndy Stadler // We always / safely clear minSymbols & minNumeric to zero (there is no policy 43422759bacd95385d95d3d9321f490763df1aba89dAndy Stadler // configuration in which we explicitly require a minimum number of digits or symbols.) 43522759bacd95385d95d3d9321f490763df1aba89dAndy Stadler dpm.setPasswordMinimumSymbols(mAdminName, 0); 43622759bacd95385d95d3d9321f490763df1aba89dAndy Stadler dpm.setPasswordMinimumNumeric(mAdminName, 0); 437aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank dpm.setPasswordMinimumNonLetter(mAdminName, aggregatePolicy.mPasswordComplexChars); 438d09cff08882e553afce919865a2cc60b657d4659Ben Komalo // Device capabilities 439d09cff08882e553afce919865a2cc60b657d4659Ben Komalo dpm.setCameraDisabled(mAdminName, aggregatePolicy.mDontAllowCamera); 440d09cff08882e553afce919865a2cc60b657d4659Ben Komalo 441469f2987dc11d153434e50eb04dd6b83b924d09dAndy Stadler // encryption required 442aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank dpm.setStorageEncryption(mAdminName, aggregatePolicy.mRequireEncryption); 4433d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler } 4443d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler } 4453d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler 4463d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler /** 4479ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank * Convenience method; see javadoc below 4489ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank */ 4499ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank public static void setAccountHoldFlag(Context context, long accountId, boolean newState) { 4509ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank Account account = Account.restoreAccountWithId(context, accountId); 4519ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank if (account != null) { 4529ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank setAccountHoldFlag(context, account, newState); 453bc53491b95e4de2e30b04e696f30d209539ec87aMarc Blank if (newState) { 454bc53491b95e4de2e30b04e696f30d209539ec87aMarc Blank // Make sure there's a notification up 455bc53491b95e4de2e30b04e696f30d209539ec87aMarc Blank NotificationController.getInstance(context).showSecurityNeededNotification(account); 456bc53491b95e4de2e30b04e696f30d209539ec87aMarc Blank } 4579ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank } 4589ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank } 4599ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank 4609ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank /** 4612a5eeea9213005060256054ec773e72406415ce4Andrew Stadler * API: Set/Clear the "hold" flag in any account. This flag serves a dual purpose: 4622a5eeea9213005060256054ec773e72406415ce4Andrew Stadler * Setting it gives us an indication that it was blocked, and clearing it gives EAS a 4632a5eeea9213005060256054ec773e72406415ce4Andrew Stadler * signal to try syncing again. 464076ab8307439f31e8197579f11b9bc3342fd5626Tony Mantler * @param context context 4659ba506c4dd498150555f6c59aa758f7467bf9236Marc Blank * @param account the account whose hold flag is to be set/cleared 4661ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * @param newState true = security hold, false = free to sync 4672a5eeea9213005060256054ec773e72406415ce4Andrew Stadler */ 4681ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler public static void setAccountHoldFlag(Context context, Account account, boolean newState) { 4692a5eeea9213005060256054ec773e72406415ce4Andrew Stadler if (newState) { 4702a5eeea9213005060256054ec773e72406415ce4Andrew Stadler account.mFlags |= Account.FLAGS_SECURITY_HOLD; 4712a5eeea9213005060256054ec773e72406415ce4Andrew Stadler } else { 4722a5eeea9213005060256054ec773e72406415ce4Andrew Stadler account.mFlags &= ~Account.FLAGS_SECURITY_HOLD; 4732a5eeea9213005060256054ec773e72406415ce4Andrew Stadler } 4742a5eeea9213005060256054ec773e72406415ce4Andrew Stadler ContentValues cv = new ContentValues(); 4752a5eeea9213005060256054ec773e72406415ce4Andrew Stadler cv.put(AccountColumns.FLAGS, account.mFlags); 4761ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler account.update(context, cv); 4772a5eeea9213005060256054ec773e72406415ce4Andrew Stadler } 4782a5eeea9213005060256054ec773e72406415ce4Andrew Stadler 4792a5eeea9213005060256054ec773e72406415ce4Andrew Stadler /** 4803d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * API: Sync service should call this any time a sync fails due to isActive() returning false. 481d62860821c2dbc14ab493b888cb129bd5addd53dAndrew Stadler * This will kick off the notify-acquire-admin-state process and/or increase the security level. 482345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * The caller needs to write the required policies into this account before making this call. 4833d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * Should not be called from UI thread - uses DB lookups to prepare new notifications 484345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * 485345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * @param accountId the account for which sync cannot proceed 486345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler */ 487345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler public void policiesRequired(long accountId) { 488f5418f1f93b02e7fab9f15eb201800b65510998eMarc Blank Account account = Account.restoreAccountWithId(mContext, accountId); 489844b14f851ce748b7b204125264bb3343c1a9039Marc Blank // In case the account has been deleted, just return 490844b14f851ce748b7b204125264bb3343c1a9039Marc Blank if (account == null) return; 491f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (account.mPolicyKey == 0) return; 492f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank Policy policy = Policy.restorePolicyWithId(mContext, account.mPolicyKey); 493f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (policy == null) return; 494f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (MailActivityEmail.DEBUG) { 495560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(TAG, "policiesRequired for " + account.mDisplayName + ": " + policy); 496aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank } 4971ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler 4982a5eeea9213005060256054ec773e72406415ce4Andrew Stadler // Mark the account as "on hold". 4991ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler setAccountHoldFlag(mContext, account, true); 5001ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler 501f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Put up an appropriate notification 502f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (policy.mProtocolPoliciesUnsupported == null) { 503f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank NotificationController.getInstance(mContext).showSecurityNeededNotification(account); 504f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } else { 505f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank NotificationController.getInstance(mContext).showSecurityUnsupportedNotification( 506f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank account); 507f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 508f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 509f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank 510f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank public static void clearAccountPolicy(Context context, Account account) { 511f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank setAccountPolicy(context, account, null, null); 512f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 513f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank 514f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank /** 515f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * Set the policy for an account atomically; this also removes any other policy associated with 516f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * the account and sets the policy key for the account. If policy is null, the policyKey is 517f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * set to 0 and the securitySyncKey to null. Also, update the account object to reflect the 518f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * current policyKey and securitySyncKey 519f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * @param context the caller's context 520f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * @param account the account whose policy is to be set 521f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * @param policy the policy to set, or null if we're clearing the policy 522f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank * @param securitySyncKey the security sync key for this account (ignored if policy is null) 523f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank */ 524f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank public static void setAccountPolicy(Context context, Account account, Policy policy, 525f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank String securitySyncKey) { 526f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 527f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank 528f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Make sure this is a valid policy set 529f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (policy != null) { 530f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank policy.normalize(); 531f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Add the new policy (no account will yet reference this) 532f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank ops.add(ContentProviderOperation.newInsert( 533f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank Policy.CONTENT_URI).withValues(policy.toContentValues()).build()); 534f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Make the policyKey of the account our newly created policy, and set the sync key 535f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank ops.add(ContentProviderOperation.newUpdate( 536f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank ContentUris.withAppendedId(Account.CONTENT_URI, account.mId)) 537f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank .withValueBackReference(AccountColumns.POLICY_KEY, 0) 538f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank .withValue(AccountColumns.SECURITY_SYNC_KEY, securitySyncKey) 539f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank .build()); 540f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } else { 541f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank ops.add(ContentProviderOperation.newUpdate( 542f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank ContentUris.withAppendedId(Account.CONTENT_URI, account.mId)) 543f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank .withValue(AccountColumns.SECURITY_SYNC_KEY, null) 544f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank .withValue(AccountColumns.POLICY_KEY, 0) 545f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank .build()); 546f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 547f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank 548f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Delete the previous policy associated with this account, if any 549f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (account.mPolicyKey > 0) { 550f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank ops.add(ContentProviderOperation.newDelete( 551f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank ContentUris.withAppendedId( 552f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank Policy.CONTENT_URI, account.mPolicyKey)).build()); 553f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 554f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank 555f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank try { 556f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank context.getContentResolver().applyBatch(EmailContent.AUTHORITY, ops); 557f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank account.refresh(context); 558bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu syncAccount(context, account); 559f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } catch (RemoteException e) { 560f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // This is fatal to a remote process 561f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank throw new IllegalStateException("Exception setting account policy."); 562f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } catch (OperationApplicationException e) { 563f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Can't happen; our provider doesn't throw this exception 564f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 565f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 566f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank 567bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu private static void syncAccount(final Context context, final Account account) { 568bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu final EmailServiceUtils.EmailServiceInfo info = 569bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu EmailServiceUtils.getServiceInfo(context, account.getProtocol(context)); 570bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu final android.accounts.Account amAccount = 571bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu new android.accounts.Account(account.mEmailAddress, info.accountType); 572bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu final Bundle extras = new Bundle(3); 573bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); 574bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu extras.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, true); 575bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true); 576bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu ContentResolver.requestSync(amAccount, EmailContent.AUTHORITY, extras); 577921c04d2ac5df091fb3c5cfa823e6dc2fc6cdf5aMartin Hibdon LogUtils.i(TAG, "requestSync SecurityPolicy syncAccount %s, %s", account.toString(), 578921c04d2ac5df091fb3c5cfa823e6dc2fc6cdf5aMartin Hibdon extras.toString()); 579bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu } 580bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu 581bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu public void syncAccount(final Account account) { 582bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu syncAccount(mContext, account); 583bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu } 584bd1e0b79e470afcfb9e6281711ad1038050c38fbYu Ping Hu 585f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank public void setAccountPolicy(long accountId, Policy policy, String securityKey) { 586f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank Account account = Account.restoreAccountWithId(mContext, accountId); 587f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank Policy oldPolicy = null; 588f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (account.mPolicyKey > 0) { 589f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank oldPolicy = Policy.restorePolicyWithId(mContext, account.mPolicyKey); 590f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 591940a335bab634a849b45f62d711601d57f59aeb1Yu Ping Hu 592940a335bab634a849b45f62d711601d57f59aeb1Yu Ping Hu // If attachment policies have changed, fix up any affected attachment records 593940a335bab634a849b45f62d711601d57f59aeb1Yu Ping Hu if (oldPolicy != null && securityKey != null) { 594940a335bab634a849b45f62d711601d57f59aeb1Yu Ping Hu if ((oldPolicy.mDontAllowAttachments != policy.mDontAllowAttachments) || 595940a335bab634a849b45f62d711601d57f59aeb1Yu Ping Hu (oldPolicy.mMaxAttachmentSize != policy.mMaxAttachmentSize)) { 596940a335bab634a849b45f62d711601d57f59aeb1Yu Ping Hu Policy.setAttachmentFlagsForNewPolicy(mContext, account, policy); 597940a335bab634a849b45f62d711601d57f59aeb1Yu Ping Hu } 598940a335bab634a849b45f62d711601d57f59aeb1Yu Ping Hu } 599940a335bab634a849b45f62d711601d57f59aeb1Yu Ping Hu 600f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank boolean policyChanged = (oldPolicy == null) || !oldPolicy.equals(policy); 601f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (!policyChanged && (TextUtilities.stringOrNullEquals(securityKey, 602f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank account.mSecuritySyncKey))) { 603560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(Logging.LOG_TAG, "setAccountPolicy; policy unchanged"); 604f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } else { 605f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank setAccountPolicy(mContext, account, policy, securityKey); 606f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank policiesUpdated(); 607f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 608f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank 609f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank boolean setHold = false; 610f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (policy.mProtocolPoliciesUnsupported != null) { 611f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // We can't support this, reasons in unsupportedRemotePolicies 612560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(Logging.LOG_TAG, 613f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank "Notify policies for " + account.mDisplayName + " not supported."); 614f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank setHold = true; 615f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank NotificationController.getInstance(mContext).showSecurityUnsupportedNotification( 616f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank account); 617f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Erase data 618f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank Uri uri = EmailProvider.uiUri("uiaccountdata", accountId); 619f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank mContext.getContentResolver().delete(uri, null, null); 620f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } else if (isActive(policy)) { 621f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank if (policyChanged) { 622560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(Logging.LOG_TAG, "Notify policies for " + account.mDisplayName 623560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy + " changed."); 624f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Notify that policies changed 625f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank NotificationController.getInstance(mContext).showSecurityChangedNotification( 626f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank account); 627bc53491b95e4de2e30b04e696f30d209539ec87aMarc Blank } else { 628560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(Logging.LOG_TAG, "Policy is active and unchanged; do not notify."); 629f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 630f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } else { 631f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank setHold = true; 632560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(Logging.LOG_TAG, "Notify policies for " + account.mDisplayName + 633f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank " are not being enforced."); 634f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Put up a notification 635f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank NotificationController.getInstance(mContext).showSecurityNeededNotification(account); 636f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank } 637f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank // Set/clear the account hold. 638f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank setAccountHoldFlag(mContext, account, setHold); 6393d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler } 6403d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler 6413d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler /** 6423d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * Called from the notification's intent receiver to register that the notification can be 6433d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * cleared now. 6443d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler */ 645c6df1d605fb3e235df6d4a21ae00632c9d6e3cc2Marc Blank public void clearNotification() { 646308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki NotificationController.getInstance(mContext).cancelSecurityNeededNotification(); 6473d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler } 6483d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler 6493d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler /** 65050d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler * API: Remote wipe (from server). This is final, there is no confirmation. It will only 651c82c1caf0138dbcef044ee41d24791f2ebeeb88aMarc Blank * return to the caller if there is an unexpected failure. The wipe includes external storage. 6523d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler */ 65350d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler public void remoteWipe() { 6543d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler DevicePolicyManager dpm = getDPM(); 6553d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler if (dpm.isAdminActive(mAdminName)) { 656c82c1caf0138dbcef044ee41d24791f2ebeeb88aMarc Blank dpm.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE); 65750d1610c43c70039e9a02b862ec43cd6ee3d7906Andrew Stadler } else { 658560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.d(Logging.LOG_TAG, "Could not remote wipe because not device admin."); 6593d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler } 660345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 661345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler /** 6623d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * If we are not the active device admin, try to become so. 6633d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * 664e7f4d3ebfcf497c015ba65be7ecebea8926b995cAndy Stadler * Also checks for any policies that we have added during the lifetime of this app. 665e7f4d3ebfcf497c015ba65be7ecebea8926b995cAndy Stadler * This catches the case where the user granted an earlier (smaller) set of policies 666e7f4d3ebfcf497c015ba65be7ecebea8926b995cAndy Stadler * but an app upgrade requires that new policies be granted. 667e7f4d3ebfcf497c015ba65be7ecebea8926b995cAndy Stadler * 6683d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * @return true if we are already active, false if we are not 669345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler */ 6703d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler public boolean isActiveAdmin() { 6713d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler DevicePolicyManager dpm = getDPM(); 672c2e638351c19ab22ad9ab4cce2853414c34724c3Andy Stadler return dpm.isAdminActive(mAdminName) 673c2e638351c19ab22ad9ab4cce2853414c34724c3Andy Stadler && dpm.hasGrantedPolicy(mAdminName, DeviceAdminInfo.USES_POLICY_EXPIRE_PASSWORD) 674aa0a3553974972561e089f2780fb7f6743b3303eBen Komalo && dpm.hasGrantedPolicy(mAdminName, DeviceAdminInfo.USES_ENCRYPTED_STORAGE) 675aa0a3553974972561e089f2780fb7f6743b3303eBen Komalo && dpm.hasGrantedPolicy(mAdminName, DeviceAdminInfo.USES_POLICY_DISABLE_CAMERA); 6763d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler } 677345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler 6783d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler /** 6793d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * Report admin component name - for making calls into device policy manager 6803d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler */ 6813d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler public ComponentName getAdminComponent() { 6823d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler return mAdminName; 6833d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler } 6843d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler 6853d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler /** 68602d59d21949a77c60859b615312f02e6d8003490Marc Blank * Delete all accounts whose security flags aren't zero (i.e. they have security enabled). 68702d59d21949a77c60859b615312f02e6d8003490Marc Blank * This method is synchronous, so it should normally be called within a worker thread (the 68802d59d21949a77c60859b615312f02e6d8003490Marc Blank * exception being for unit tests) 68902d59d21949a77c60859b615312f02e6d8003490Marc Blank * 69002d59d21949a77c60859b615312f02e6d8003490Marc Blank * @param context the caller's context 69102d59d21949a77c60859b615312f02e6d8003490Marc Blank */ 69202d59d21949a77c60859b615312f02e6d8003490Marc Blank /*package*/ void deleteSecuredAccounts(Context context) { 69302d59d21949a77c60859b615312f02e6d8003490Marc Blank ContentResolver cr = context.getContentResolver(); 69402d59d21949a77c60859b615312f02e6d8003490Marc Blank // Find all accounts with security and delete them 69502d59d21949a77c60859b615312f02e6d8003490Marc Blank Cursor c = cr.query(Account.CONTENT_URI, EmailContent.ID_PROJECTION, 696aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank Account.SECURITY_NONZERO_SELECTION, null, null); 69702d59d21949a77c60859b615312f02e6d8003490Marc Blank try { 698560bfadc3151f7a06f3b06e9a6c92cfa534c63ecScott Kennedy LogUtils.w(TAG, "Email administration disabled; deleting " + c.getCount() + 69902d59d21949a77c60859b615312f02e6d8003490Marc Blank " secured account(s)"); 70002d59d21949a77c60859b615312f02e6d8003490Marc Blank while (c.moveToNext()) { 701f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank long accountId = c.getLong(EmailContent.ID_PROJECTION_COLUMN); 702f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank Uri uri = EmailProvider.uiUri("uiaccountdata", accountId); 703f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank cr.delete(uri, null, null); 70402d59d21949a77c60859b615312f02e6d8003490Marc Blank } 70502d59d21949a77c60859b615312f02e6d8003490Marc Blank } finally { 70602d59d21949a77c60859b615312f02e6d8003490Marc Blank c.close(); 70702d59d21949a77c60859b615312f02e6d8003490Marc Blank } 708f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank policiesUpdated(); 70902d59d21949a77c60859b615312f02e6d8003490Marc Blank } 71002d59d21949a77c60859b615312f02e6d8003490Marc Blank 71102d59d21949a77c60859b615312f02e6d8003490Marc Blank /** 71202d59d21949a77c60859b615312f02e6d8003490Marc Blank * Internal handler for enabled->disabled transitions. Deletes all secured accounts. 713a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler * Must call from worker thread, not on UI thread. 7143d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler */ 71502d59d21949a77c60859b615312f02e6d8003490Marc Blank /*package*/ void onAdminEnabled(boolean isEnabled) { 716856e09d76ab62272e660fd4a08e25637f17319a0Andrew Stadler if (!isEnabled) { 717a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler deleteSecuredAccounts(mContext); 7183d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler } 7193d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler } 7203d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler 7213d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler /** 7221ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * Handle password expiration - if any accounts appear to have triggered this, put up 7231ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * warnings, or even shut them down. 7241ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * 7251ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * NOTE: If there are multiple accounts with password expiration policies, the device 7261ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * password will be set to expire in the shortest required interval (most secure). The logic 7271ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * in this method operates based on the aggregate setting - irrespective of which account caused 7281ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * the expiration. In other words, all accounts (that require expiration) will run/stop 7291ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * based on the requirements of the account with the shortest interval. 7301ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler */ 731a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler private void onPasswordExpiring(Context context) { 7321ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler // 1. Do we have any accounts that matter here? 7331ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler long nextExpiringAccountId = findShortestExpiration(context); 7341ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler 7351ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler // 2. If not, exit immediately 7361ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler if (nextExpiringAccountId == -1) { 7371ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler return; 7381ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 7391ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler 7401ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler // 3. If yes, are we warning or expired? 7411ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler long expirationDate = getDPM().getPasswordExpiration(mAdminName); 7421ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler long timeUntilExpiration = expirationDate - System.currentTimeMillis(); 7431ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler boolean expired = timeUntilExpiration < 0; 7441ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler if (!expired) { 7451ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler // 4. If warning, simply put up a generic notification and report that it came from 7461ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler // the shortest-expiring account. 747308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki NotificationController.getInstance(mContext).showPasswordExpiringNotification( 748308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki nextExpiringAccountId); 7491ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } else { 7501ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler // 5. Actually expired - find all accounts that expire passwords, and wipe them 751f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank boolean wiped = wipeExpiredAccounts(context); 7521ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler if (wiped) { 753308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki NotificationController.getInstance(mContext).showPasswordExpiredNotification( 754308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki nextExpiringAccountId); 7551ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 7561ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 7571ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 7581ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler 7591ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler /** 7601ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * Find the account with the shortest expiration time. This is always assumed to be 7611ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * the account that forces the password to be refreshed. 7621ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * @return -1 if no expirations, or accountId if one is found 7631ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler */ 764aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank @VisibleForTesting 765aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank /*package*/ static long findShortestExpiration(Context context) { 766aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank long policyId = Utility.getFirstRowLong(context, Policy.CONTENT_URI, Policy.ID_PROJECTION, 767aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank HAS_PASSWORD_EXPIRATION, null, PolicyColumns.PASSWORD_EXPIRATION_DAYS + " ASC", 768aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank EmailContent.ID_PROJECTION_COLUMN, -1L); 769aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (policyId < 0) return -1L; 770aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank return Policy.getAccountIdWithPolicyKey(context, policyId); 7711ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 7721ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler 7731ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler /** 7741ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * For all accounts that require password expiration, put them in security hold and wipe 7751ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * their data. 776076ab8307439f31e8197579f11b9bc3342fd5626Tony Mantler * @param context context 7771ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * @return true if one or more accounts were wiped 7781ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler */ 779aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank @VisibleForTesting 780f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank /*package*/ static boolean wipeExpiredAccounts(Context context) { 7811ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler boolean result = false; 782aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank Cursor c = context.getContentResolver().query(Policy.CONTENT_URI, 783aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank Policy.ID_PROJECTION, HAS_PASSWORD_EXPIRATION, null, null); 7841ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler try { 7851ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler while (c.moveToNext()) { 786aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank long policyId = c.getLong(Policy.ID_PROJECTION_COLUMN); 787aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank long accountId = Policy.getAccountIdWithPolicyKey(context, policyId); 788aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (accountId < 0) continue; 789aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank Account account = Account.restoreAccountWithId(context, accountId); 790aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank if (account != null) { 791aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank // Mark the account as "on hold". 792aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank setAccountHoldFlag(context, account, true); 793aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank // Erase data 794f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank Uri uri = EmailProvider.uiUri("uiaccountdata", accountId); 795f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank context.getContentResolver().delete(uri, null, null); 796aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank // Report one or more were found 797aeee10e57ef4d931e7708fde218d590453a82aeaMarc Blank result = true; 7981ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 7991ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 8001ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } finally { 8011ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler c.close(); 8021ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 8031ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler return result; 8041ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 8051ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler 8061ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler /** 807a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler * Callback from EmailBroadcastProcessorService. This provides the workers for the 808a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler * DeviceAdminReceiver calls. These should perform the work directly and not use async 809a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler * threads for completion. 810a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler */ 811a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler public static void onDeviceAdminReceiverMessage(Context context, int message) { 812a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler SecurityPolicy instance = SecurityPolicy.getInstance(context); 813a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler switch (message) { 814a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler case DEVICE_ADMIN_MESSAGE_ENABLED: 815a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler instance.onAdminEnabled(true); 816a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler break; 817a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler case DEVICE_ADMIN_MESSAGE_DISABLED: 818a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler instance.onAdminEnabled(false); 819a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler break; 820a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler case DEVICE_ADMIN_MESSAGE_PASSWORD_CHANGED: 821a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler // TODO make a small helper for this 822a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler // Clear security holds (if any) 823a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler Account.clearSecurityHoldOnAllAccounts(context); 824a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler // Cancel any active notifications (if any are posted) 825308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki NotificationController.getInstance(context).cancelPasswordExpirationNotifications(); 826a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler break; 827a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler case DEVICE_ADMIN_MESSAGE_PASSWORD_EXPIRING: 828a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler instance.onPasswordExpiring(instance.mContext); 829a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler break; 830a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler } 831a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler } 832a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler 833a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler /** 8343d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * Device Policy administrator. This is primarily a listener for device state changes. 8353d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler * Note: This is instantiated by incoming messages. 836a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler * Note: This is actually a BroadcastReceiver and must remain within the guidelines required 837a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler * for proper behavior, including avoidance of ANRs. 8385893e9e008d46cf4be0c7f709a6e77e2652c3dddAndrew Stadler * Note: We do not implement onPasswordFailed() because the default behavior of the 8395893e9e008d46cf4be0c7f709a6e77e2652c3dddAndrew Stadler * DevicePolicyManager - complete local wipe after 'n' failures - is sufficient. 8403d2b3b3b3554be2ac23d9a49fee00faa9693e857Andrew Stadler */ 8414ae83c58b3e136b4b1e859ee304ad1b332e9597fDianne Hackborn public static class PolicyAdmin extends DeviceAdminReceiver { 842345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler 843345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler /** 844345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * Called after the administrator is first enabled. 845345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler */ 846345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler @Override 847345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler public void onEnabled(Context context, Intent intent) { 848a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler EmailBroadcastProcessorService.processDevicePolicyMessage(context, 849a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler DEVICE_ADMIN_MESSAGE_ENABLED); 850345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 851856e09d76ab62272e660fd4a08e25637f17319a0Andrew Stadler 852345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler /** 853345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * Called prior to the administrator being disabled. 854345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler */ 855345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler @Override 856345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler public void onDisabled(Context context, Intent intent) { 857a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler EmailBroadcastProcessorService.processDevicePolicyMessage(context, 858a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler DEVICE_ADMIN_MESSAGE_DISABLED); 859345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 860856e09d76ab62272e660fd4a08e25637f17319a0Andrew Stadler 861345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler /** 86202d59d21949a77c60859b615312f02e6d8003490Marc Blank * Called when the user asks to disable administration; we return a warning string that 86302d59d21949a77c60859b615312f02e6d8003490Marc Blank * will be presented to the user 86402d59d21949a77c60859b615312f02e6d8003490Marc Blank */ 86502d59d21949a77c60859b615312f02e6d8003490Marc Blank @Override 86602d59d21949a77c60859b615312f02e6d8003490Marc Blank public CharSequence onDisableRequested(Context context, Intent intent) { 86702d59d21949a77c60859b615312f02e6d8003490Marc Blank return context.getString(R.string.disable_admin_warning); 86802d59d21949a77c60859b615312f02e6d8003490Marc Blank } 86902d59d21949a77c60859b615312f02e6d8003490Marc Blank 87002d59d21949a77c60859b615312f02e6d8003490Marc Blank /** 871345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler * Called after the user has changed their password. 872345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler */ 873345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler @Override 874345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler public void onPasswordChanged(Context context, Intent intent) { 875a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler EmailBroadcastProcessorService.processDevicePolicyMessage(context, 876a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler DEVICE_ADMIN_MESSAGE_PASSWORD_CHANGED); 8771ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler } 8781ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler 8791ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler /** 8801ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler * Called when device password is expiring 8811ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler */ 8821ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler @Override 8831ca111c19c83d54ad23bd8615d9c648e09ec3366Andy Stadler public void onPasswordExpiring(Context context, Intent intent) { 884a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler EmailBroadcastProcessorService.processDevicePolicyMessage(context, 885a2269e84c6134bfd3506e5489c7ccfd60c32d41fAndy Stadler DEVICE_ADMIN_MESSAGE_PASSWORD_EXPIRING); 886345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 887345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler } 888345fb8b737c1632fb2a7e69ac44b8612be6237edAndrew Stadler} 889