OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.sync; | |
6 | |
7 import android.os.Parcel; | |
8 import android.os.Parcelable; | |
9 | |
10 import java.util.HashSet; | |
11 import java.util.Set; | |
12 | |
13 /** | |
14 * This enum describes the type of passphrase required, if any, to decrypt synce
d data. | |
15 * | |
16 * It implements the Android {@link Parcelable} interface so it is easy to pass
around in intents. | |
17 * | |
18 * It maps the native enum syncer::PassphraseType. | |
19 */ | |
20 public enum PassphraseType implements Parcelable { | |
21 IMPLICIT_PASSPHRASE(0), // GAIA-based passphrase (deprecated). | |
22 KEYSTORE_PASSPHRASE(1), // Keystore passphrase. | |
23 FROZEN_IMPLICIT_PASSPHRASE(2), // Frozen GAIA passphrase. | |
24 CUSTOM_PASSPHRASE(3); // User-provided passphrase. | |
25 | |
26 public static Parcelable.Creator CREATOR = | |
27 new Parcelable.Creator<PassphraseType>() { | |
28 @Override | |
29 public PassphraseType createFromParcel(Parcel parcel) { | |
30 return fromInternalValue(parcel.readInt()); | |
31 } | |
32 | |
33 @Override | |
34 public PassphraseType[] newArray(int size) { | |
35 return new PassphraseType[size]; | |
36 } | |
37 }; | |
38 | |
39 public static PassphraseType fromInternalValue(int value) { | |
40 for (PassphraseType type : values()) { | |
41 if (type.internalValue() == value) { | |
42 return type; | |
43 } | |
44 } | |
45 throw new IllegalArgumentException("No value for " + value + " found."); | |
46 } | |
47 | |
48 private final int mNativeValue; | |
49 | |
50 private PassphraseType(int nativeValue) { | |
51 mNativeValue = nativeValue; | |
52 } | |
53 | |
54 public Set<PassphraseType> getVisibleTypes() { | |
55 Set<PassphraseType> visibleTypes = new HashSet<>(); | |
56 switch (this) { | |
57 case IMPLICIT_PASSPHRASE: // Intentional fall through. | |
58 case KEYSTORE_PASSPHRASE: | |
59 visibleTypes.add(this); | |
60 visibleTypes.add(CUSTOM_PASSPHRASE); | |
61 break; | |
62 case FROZEN_IMPLICIT_PASSPHRASE: | |
63 visibleTypes.add(KEYSTORE_PASSPHRASE); | |
64 visibleTypes.add(FROZEN_IMPLICIT_PASSPHRASE); | |
65 break; | |
66 case CUSTOM_PASSPHRASE: | |
67 visibleTypes.add(KEYSTORE_PASSPHRASE); | |
68 visibleTypes.add(CUSTOM_PASSPHRASE); | |
69 break; | |
70 } | |
71 return visibleTypes; | |
72 } | |
73 | |
74 /** | |
75 * Get the types that are allowed to be enabled from the current type. | |
76 * | |
77 * @param isEncryptEverythingAllowed Whether encrypting all data is allowed. | |
78 */ | |
79 public Set<PassphraseType> getAllowedTypes(boolean isEncryptEverythingAllowe
d) { | |
80 Set<PassphraseType> allowedTypes = new HashSet<>(); | |
81 switch (this) { | |
82 case IMPLICIT_PASSPHRASE: // Intentional fall through. | |
83 case KEYSTORE_PASSPHRASE: | |
84 allowedTypes.add(this); | |
85 if (isEncryptEverythingAllowed) { | |
86 allowedTypes.add(CUSTOM_PASSPHRASE); | |
87 } | |
88 break; | |
89 case FROZEN_IMPLICIT_PASSPHRASE: // Intentional fall through. | |
90 case CUSTOM_PASSPHRASE: // Intentional fall through. | |
91 default: | |
92 break; | |
93 } | |
94 return allowedTypes; | |
95 } | |
96 | |
97 public int internalValue() { | |
98 // Since the values in this enums are constant and very small, this cast
is safe. | |
99 return mNativeValue; | |
100 } | |
101 | |
102 @Override | |
103 public int describeContents() { | |
104 return 0; | |
105 } | |
106 | |
107 @Override | |
108 public void writeToParcel(Parcel dest, int flags) { | |
109 dest.writeInt(mNativeValue); | |
110 } | |
111 } | |
OLD | NEW |