• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 Google Inc.
3  * Licensed to The Android Open Source Project.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.android.mail.ui;
19 
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import com.android.mail.providers.Folder;
24 import com.google.common.base.Objects;
25 import com.google.common.collect.ImmutableList;
26 
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.HashSet;
31 
32 public class FolderOperation implements Parcelable {
33     /** An immutable, empty conversation list */
34     public static final Collection<FolderOperation> EMPTY = Collections.emptyList();
35     public final Folder mFolder;
36     public final boolean mAdd;
37 
FolderOperation(Folder folder, Boolean operation)38     public FolderOperation(Folder folder, Boolean operation) {
39         mAdd = operation;
40         mFolder = folder;
41     }
42 
43     /**
44      * Get all the unique folders associated with a set of folder operations.
45      * @param ops Operations to inspect
46      * @return ArrayList of Folder objects
47      */
getFolders(Collection<FolderOperation> ops)48     public static ArrayList<Folder> getFolders(Collection<FolderOperation> ops) {
49         HashSet<Folder> folders = new HashSet<Folder>();
50         for (FolderOperation op : ops) {
51             folders.add(op.mFolder);
52         }
53         return new ArrayList<Folder>(folders);
54     }
55 
56     /**
57      * Returns a collection of a single FolderOperation. This method always
58      * returns a valid collection even if the input folder is null.
59      *
60      * @param in a FolderOperation, possibly null.
61      * @return a collection of the folder.
62      */
listOf(FolderOperation in)63     public static Collection<FolderOperation> listOf(FolderOperation in) {
64         final Collection<FolderOperation> target = (in == null) ? EMPTY : ImmutableList.of(in);
65         return target;
66     }
67 
68     /**
69      * Return if a set of folder operations removes the specified folder or adds
70      * inbox to a trashed conversation, making it a destructive operation.
71      */
isDestructive(Collection<FolderOperation> folderOps, Folder folder)72     public static boolean isDestructive(Collection<FolderOperation> folderOps, Folder folder) {
73         for (FolderOperation op : folderOps) {
74             if (Objects.equal(op.mFolder, folder) && !op.mAdd) {
75                 return true;
76             }
77             if (folder.isTrash() && op.mFolder.isInbox()) {
78                 return true;
79             }
80         }
81         return false;
82     }
83 
84     // implements Parcelable
85 
FolderOperation(Parcel in)86     FolderOperation(Parcel in) {
87         mAdd = in.readByte() != 0;
88         mFolder = in.readParcelable(getClass().getClassLoader());
89     }
90 
91     @Override
describeContents()92     public int describeContents() {
93         return 0;
94     }
95 
96     @Override
writeToParcel(Parcel dest, int flags)97     public void writeToParcel(Parcel dest, int flags) {
98         dest.writeByte((byte)(mAdd ? 1 : 0));
99         dest.writeParcelable(mFolder, 0);
100     }
101 
102     public static final Creator<FolderOperation> CREATOR = new Creator<FolderOperation>() {
103         @Override
104         public FolderOperation createFromParcel(Parcel source) {
105             return new FolderOperation(source);
106         }
107 
108         @Override
109         public FolderOperation[] newArray(int size) {
110             return new FolderOperation[size];
111         }
112     };
113 }
114