1/* Copyright (C) 2011 The Android Open Source Project
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16package com.android.emailcommon.service;
17
18import com.android.emailcommon.provider.Policy;
19
20/**
21 * Legacy class for policy storage as a bit field of flags
22 */
23public class LegacyPolicySet {
24
25    // Security (provisioning) flags
26        // bits 0..4: password length (0=no password required)
27    public static final int PASSWORD_LENGTH_MASK = 31;
28    public static final int PASSWORD_LENGTH_SHIFT = 0;
29    public static final int PASSWORD_LENGTH_MAX = 30;
30        // bits 5..8: password mode
31    public static final int PASSWORD_MODE_SHIFT = 5;
32    public static final int PASSWORD_MODE_MASK = 15 << PASSWORD_MODE_SHIFT;
33    public static final int PASSWORD_MODE_NONE = 0 << PASSWORD_MODE_SHIFT;
34    public static final int PASSWORD_MODE_SIMPLE = 1 << PASSWORD_MODE_SHIFT;
35    public static final int PASSWORD_MODE_STRONG = 2 << PASSWORD_MODE_SHIFT;
36        // bits 9..13: password failures -> wipe device (0=disabled)
37    public static final int PASSWORD_MAX_FAILS_SHIFT = 9;
38    public static final int PASSWORD_MAX_FAILS_MASK = 31 << PASSWORD_MAX_FAILS_SHIFT;
39    public static final int PASSWORD_MAX_FAILS_MAX = 31;
40        // bits 14..24: seconds to screen lock (0=not required)
41    public static final int SCREEN_LOCK_TIME_SHIFT = 14;
42    public static final int SCREEN_LOCK_TIME_MASK = 2047 << SCREEN_LOCK_TIME_SHIFT;
43    public static final int SCREEN_LOCK_TIME_MAX = 2047;
44        // bit 25: remote wipe capability required
45    public static final int REQUIRE_REMOTE_WIPE = 1 << 25;
46        // bit 26..35: password expiration (days; 0=not required)
47    public static final int PASSWORD_EXPIRATION_SHIFT = 26;
48    public static final long PASSWORD_EXPIRATION_MASK = 1023L << PASSWORD_EXPIRATION_SHIFT;
49    public static final int PASSWORD_EXPIRATION_MAX = 1023;
50        // bit 36..43: password history (length; 0=not required)
51    public static final int PASSWORD_HISTORY_SHIFT = 36;
52    public static final long PASSWORD_HISTORY_MASK = 255L << PASSWORD_HISTORY_SHIFT;
53    public static final int PASSWORD_HISTORY_MAX = 255;
54        // bit 44..48: min complex characters (0=not required)
55    public static final int PASSWORD_COMPLEX_CHARS_SHIFT = 44;
56    public static final long PASSWORD_COMPLEX_CHARS_MASK = 31L << PASSWORD_COMPLEX_CHARS_SHIFT;
57    public static final int PASSWORD_COMPLEX_CHARS_MAX = 31;
58        // bit 49: requires device encryption (internal)
59    public static final long REQUIRE_ENCRYPTION = 1L << 49;
60        // bit 50: requires external storage encryption
61    public static final long REQUIRE_ENCRYPTION_EXTERNAL = 1L << 50;
62
63    /**
64     * Convert legacy policy flags to a Policy
65     * @param flags legacy policy flags
66     * @return a Policy representing the legacy policy flag
67     */
68    public static Policy flagsToPolicy(long flags) {
69        Policy policy = new Policy();
70        policy.mPasswordMode = ((int) (flags & PASSWORD_MODE_MASK)) >> PASSWORD_MODE_SHIFT;
71        policy.mPasswordMinLength = (int) ((flags & PASSWORD_LENGTH_MASK) >> PASSWORD_LENGTH_SHIFT);
72        policy.mPasswordMaxFails =
73            (int) ((flags & PASSWORD_MAX_FAILS_MASK) >> PASSWORD_MAX_FAILS_SHIFT);
74        policy.mPasswordComplexChars =
75            (int) ((flags & PASSWORD_COMPLEX_CHARS_MASK) >> PASSWORD_COMPLEX_CHARS_SHIFT);
76        policy.mPasswordHistory = (int) ((flags & PASSWORD_HISTORY_MASK) >> PASSWORD_HISTORY_SHIFT);
77        policy.mPasswordExpirationDays =
78            (int) ((flags & PASSWORD_EXPIRATION_MASK) >> PASSWORD_EXPIRATION_SHIFT);
79        policy.mMaxScreenLockTime =
80            (int) ((flags & SCREEN_LOCK_TIME_MASK) >> SCREEN_LOCK_TIME_SHIFT);
81        policy.mRequireRemoteWipe = 0 != (flags & REQUIRE_REMOTE_WIPE);
82        policy.mRequireEncryption = 0 != (flags & REQUIRE_ENCRYPTION);
83        policy.mRequireEncryptionExternal = 0 != (flags & REQUIRE_ENCRYPTION_EXTERNAL);
84        return policy;
85    }
86}
87
88