// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.sync.internal_api.pub;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * This enum describes the type of passphrase required, if any, to decrypt synced data.
 *
 * It implements the Android {@link Parcelable} interface so it is easy to pass around in intents.
 *
 * It maps the native enum syncer::PassphraseType, but has the additional values INVALID and NONE.
 */
public enum SyncDecryptionPassphraseType implements Parcelable {
    INVALID(-2),                   // Used as default value and is not a valid decryption type.
    NONE(-1),                      // No encryption (deprecated).
    IMPLICIT_PASSPHRASE(0),        // GAIA-based passphrase (deprecated).
    KEYSTORE_PASSPHRASE(1),        // Keystore passphrase.
    FROZEN_IMPLICIT_PASSPHRASE(2), // Frozen GAIA passphrase.
    CUSTOM_PASSPHRASE(3);          // User-provided passphrase.

    public static Parcelable.Creator CREATOR =
            new Parcelable.Creator<SyncDecryptionPassphraseType>() {
                @Override
                public SyncDecryptionPassphraseType createFromParcel(Parcel parcel) {
                    return fromInternalValue(parcel.readInt());
                }

                @Override
                public SyncDecryptionPassphraseType[] newArray(int size) {
                    return new SyncDecryptionPassphraseType[size];
                }
            };

    public static SyncDecryptionPassphraseType fromInternalValue(int value) {
        for (SyncDecryptionPassphraseType type : values()) {
            if (type.internalValue() == value) {
                return type;
            }
        }
        // Falling back to INVALID. Should not happen if |value| was retrieved from native.
        return INVALID;
    }

    private final int mNativeValue;

    private SyncDecryptionPassphraseType(int nativeValue) {
        mNativeValue = nativeValue;
    }

    public int internalValue() {
        // Since the values in this enums are constant and very small, this cast is safe.
        return mNativeValue;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mNativeValue);
    }
}

@@ -261,16 +261,65 @@
 lable;%0A%0A
+import java.util.HashSet;%0Aimport java.util.Set;%0A%0A
 /**%0A * T
@@ -2110,24 +2110,1726 @@
 lue;%0A    %7D%0A%0A
+%0A    public Set%3CSyncDecryptionPassphraseType%3E getVisibleTypes() %7B%0A        Set%3CSyncDecryptionPassphraseType%3E visibleTypes = new HashSet%3C%3E();%0A        switch (this) %7B%0A            case NONE:  // Intentional fall through.%0A            case IMPLICIT_PASSPHRASE:  // Intentional fall through.%0A            case KEYSTORE_PASSPHRASE:%0A                visibleTypes.add(this);%0A                visibleTypes.add(CUSTOM_PASSPHRASE);%0A                break;%0A            case FROZEN_IMPLICIT_PASSPHRASE:%0A                visibleTypes.add(KEYSTORE_PASSPHRASE);%0A                visibleTypes.add(FROZEN_IMPLICIT_PASSPHRASE);%0A                break;%0A            case CUSTOM_PASSPHRASE:%0A                visibleTypes.add(KEYSTORE_PASSPHRASE);%0A                visibleTypes.add(CUSTOM_PASSPHRASE);%0A                break;%0A            case INVALID:  // Intentional fall through.%0A            default:%0A                visibleTypes.add(this);%0A                break;%0A        %7D%0A        return visibleTypes;%0A    %7D%0A%0A    public Set%3CSyncDecryptionPassphraseType%3E getAllowedTypes() %7B%0A        Set%3CSyncDecryptionPassphraseType%3E allowedTypes = new HashSet%3C%3E();%0A        switch (this) %7B%0A            case NONE:  // Intentional fall through.%0A            case IMPLICIT_PASSPHRASE:  // Intentional fall through.%0A            case KEYSTORE_PASSPHRASE:%0A                allowedTypes.add(this);%0A                allowedTypes.add(CUSTOM_PASSPHRASE);%0A                break;%0A            case FROZEN_IMPLICIT_PASSPHRASE:  // Intentional fall through.%0A            case CUSTOM_PASSPHRASE:  // Intentional fall through.%0A            case INVALID:  // Intentional fall through.%0A            default:%0A                break;%0A        %7D%0A        return allowedTypes;%0A    %7D%0A%0A
     public i

Move passphrase dialog logic to the enum.