summaryrefslogtreecommitdiffstats
path: root/src/com/android/mail/ui/FolderOperations.java
blob: 82710d87438726a1cda0d3e38bb687a1487293fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*******************************************************************************
 *      Copyright (C) 2012 Google Inc.
 *      Licensed to The Android Open Source Project.
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *           http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 *******************************************************************************/

package com.android.mail.ui;

import com.android.mail.providers.Folder;
import com.android.mail.utils.LogTag;
import com.android.mail.utils.LogUtils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Object that contains a list of folder operations (application/removals of folders)
 *
 */
// This was earlier called FolderOperations
public class FolderOperations {
    /**
     * Map of the Folders that that either need to be applied or removed
     * The key is the canonical name of the Folder, and the value is a boolean,
     * when true, the Folder should be added, and when false, the Folder
     * should be removed
     */
    private final Map<String, Operation> mOperations;

    private static final String LOG_TAG = LogTag.getLogTag();

    public FolderOperations() {
        mOperations = Maps.newHashMap();
    }

    public FolderOperations(Folder folder, boolean add) {
        this();
        if (folder != null) {
            add(folder, add);
        } else {
            LogUtils.e(LOG_TAG, "FolderOperation created with null Folder object");
        }
    }

    /**
     * Adds an operation to the list of folder operations to be applied. The last
     * operation for a folder will be retained in the list of operations.
     * @param folder Folder to be applied
     * @param add True if the folder should be applied, or false if the folder should be removed
     */
    public void add(Folder folder, boolean add) {
        Operation operation = new Operation(folder, add);

        mOperations.put(folder.name, operation);
    }

    /**
     * Returns true if there is an operation for the specified folder
     * @param folder Folder
     * @return Returns true if there is a add or remove operation for
     * the specified folder
     */
    public boolean hasOperation(Folder folder) {
        return hasOperation(folder.name);
    }

    /**
     * Returns true if there is an operation for the specified folder
     * @param canonicalName Canonical name of the folder
     * @return Returns true if there is a add or remove operation for
     * the specified folder
     */
    public boolean hasOperation(String canonicalName) {
        return mOperations.containsKey(canonicalName);
    }

    /**
     * Returns true if the specified folder will be applied
     * @param folder Folder
     * @return Returns true if there is an operation that will apply the folder
     */
    public boolean hasApplyOperation(Folder folder) {
        return hasApplyOperation(folder.name);
    }

    /**
     * Returns true if the specified folder will be applied
     * @param canonicalName Canonical name of the folder
     * @return Returns true if there is an operation that will apply the folder
     */
    public boolean hasApplyOperation(String canonicalName) {
        if (hasOperation(canonicalName)) {
            Operation operation = mOperations.get(canonicalName);
            return operation.mAdd;
        }
        return false;
    }

    /**
     * Returns true if the specified folder will be removed
     * @param folder folder
     * @return Returns true if there is an operation that will remove the folder
     */
    public boolean hasRemoveOperation(Folder folder) {
        return hasRemoveOperation(folder.name);
    }

    /**
     * Returns true if the specified folder will be removed
     * @param canonicalName Canonical name of the folder
     * @return Returns true if there is an operation that will remove the folder
     */
    public boolean hasRemoveOperation(String canonicalName) {
        if (hasOperation(canonicalName)) {
            Operation operation = mOperations.get(canonicalName);
            return !operation.mAdd;
        }
        return false;
    }

    public void clear() {
        mOperations.clear();
    }

    /**
     * Return the number of folder operations
     */
    public int count() {
        return mOperations.size();
    }

    /**
     * Returns a FolderOperations object that will revert the operations described in
     * this FolderOperations instance
     * @return FolderOperations object that will revert
     */
    public FolderOperations undoOperation() {
        FolderOperations undoOperations = new FolderOperations();
        Set<Map.Entry<String, Operation>> operationSet = mOperations.entrySet();
        for (Map.Entry<String, Operation> operationItem : operationSet) {
            Operation operationToUndo = operationItem.getValue();
            undoOperations.add(operationToUndo.mFolder, !operationToUndo.mAdd);
        }
        return undoOperations;
    }

    /**
     * Returns an array of the folder operations
     * @return Array of the folder operations to perform
     */
    public List<Operation> getOperationList() {
        List<Operation> results = Lists.newArrayList();
        Set<Map.Entry<String, Operation>> operationSet = mOperations.entrySet();
        for (Map.Entry<String, Operation> operationItem : operationSet) {
            results.add(operationItem.getValue());
        }
        return results;
    }

    /**
     * Serialize the FolderOperations
     * Not implemented!!
     * TODO(viki): Copy over from Gmail Labels#serialize(FolderOperations)
     * @return Serialized representation of the folder operations
     */
    public static String serialize(FolderOperations operations) {
        return "";
    }

    /**
     * Deserialize a encoded string and instantiates a FolderOperations object
     * Not implemented!!
     * TODO(viki): Copy over from Gmail Labels#deSerialize(String)
     * @param encodedFolderOperations Encode FolderOperations string
     * @return FolderOperations object
     */
    public static FolderOperations deserialize(String encodedFolderOperations) {
        return null;
    }

    /**
     * An operation that can be performed on the folder.
     *
     */
    private class Operation {
        /**
         * True if the action is to include in the folder, false if the action is to remove it
         * from the folder.
         */
        public final boolean mAdd;

        /**
         * The  name of the folder for which the operation is performed.
         */
        public final Folder mFolder;

        /**
         * Create a new operation, which is to add the message to the given folder
         * @param folder Name of the folder.
         * @param add true if message has to be added to the folder. False if it has to be removed
         * from the existing folder.
         */
        private Operation(Folder folder, boolean add) {
            mFolder = folder;
            mAdd = add;
        }
    }

}