ToastBarOperation.java revision c1922a93fb9540d4ff8c28a30d1ae58d3a3d73f9
1/*******************************************************************************
2 *      Copyright (C) 2011 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 *******************************************************************************/
17package com.android.mail.ui;
18
19import android.content.Context;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23import com.android.mail.R;
24import com.android.mail.providers.Folder;
25
26/**
27 * A simple holder class that stores the information to undo the application of a folder.
28 */
29public class ToastBarOperation implements Parcelable {
30    public static final int UNDO = 0;
31    public static final int ERROR = 1;
32    private final int mAction;
33    private final int mCount;
34    private final boolean mBatch;
35    private final int mType;
36    private final Folder mFolder;
37
38    /**
39     * Create a ToastBarOperation
40     *
41     * @param count Number of conversations this action would be applied to.
42     * @param menuId res id identifying the menu item tapped; used to determine what action was
43     *        performed
44     * @param operationFolder The {@link Folder} upon which the operation was run. This may be
45     *        <code>null</code>, but is required in {@link #getDescription(Context)} for certain
46     *        actions.
47     */
48    public ToastBarOperation(int count, int menuId, int type, boolean batch,
49            final Folder operationFolder) {
50        mCount = count;
51        mAction = menuId;
52        mBatch = batch;
53        mType = type;
54        mFolder = operationFolder;
55    }
56
57    public int getType() {
58        return mType;
59    }
60
61    public boolean isBatchUndo() {
62        return mBatch;
63    }
64
65    public ToastBarOperation(final Parcel in, final ClassLoader loader) {
66        mCount = in.readInt();
67        mAction = in.readInt();
68        mBatch = in.readInt() != 0;
69        mType = in.readInt();
70        mFolder = in.readParcelable(loader);
71    }
72
73    @Override
74    public String toString() {
75        final StringBuilder sb = new StringBuilder("{");
76        sb.append(super.toString());
77        sb.append(" mAction=");
78        sb.append(mAction);
79        sb.append(" mCount=");
80        sb.append(mCount);
81        sb.append(" mBatch=");
82        sb.append(mBatch);
83        sb.append(" mType=");
84        sb.append(mType);
85        sb.append(" mFolder=");
86        sb.append(mFolder);
87        sb.append("}");
88        return sb.toString();
89    }
90
91    @Override
92    public void writeToParcel(Parcel dest, int flags) {
93        dest.writeInt(mCount);
94        dest.writeInt(mAction);
95        dest.writeInt(mBatch ? 1 : 0);
96        dest.writeInt(mType);
97        dest.writeParcelable(mFolder, 0);
98    }
99
100    public static final ClassLoaderCreator<ToastBarOperation> CREATOR =
101            new ClassLoaderCreator<ToastBarOperation>() {
102        @Override
103        public ToastBarOperation createFromParcel(final Parcel source) {
104            return createFromParcel(source, null);
105        }
106
107        @Override
108        public ToastBarOperation[] newArray(final int size) {
109            return new ToastBarOperation[size];
110        }
111
112        @Override
113        public ToastBarOperation createFromParcel(final Parcel source, final ClassLoader loader) {
114            return new ToastBarOperation(source, loader);
115        }
116    };
117
118    /**
119     * Get a string description of the operation that will be performed
120     * when the user taps the undo bar.
121     */
122    public String getDescription(Context context) {
123        int resId = -1;
124        switch (mAction) {
125            case R.id.delete:
126                resId = R.plurals.conversation_deleted;
127                break;
128            case R.id.remove_folder:
129                return context.getString(R.string.folder_removed, mFolder.name);
130            case R.id.change_folder:
131                resId = R.plurals.conversation_folder_changed;
132                break;
133            case R.id.move_folder:
134                return context.getString(R.string.conversation_folder_moved, mFolder.name);
135            case R.id.archive:
136                resId = R.plurals.conversation_archived;
137                break;
138            case R.id.report_spam:
139                resId = R.plurals.conversation_spammed;
140                break;
141            case R.id.mark_not_spam:
142                resId = R.plurals.conversation_not_spam;
143                break;
144            case R.id.mark_not_important:
145                resId = R.plurals.conversation_not_important;
146                break;
147            case R.id.mute:
148                resId = R.plurals.conversation_muted;
149                break;
150            case R.id.remove_star:
151                resId = R.plurals.conversation_unstarred;
152                break;
153            case R.id.report_phishing:
154                resId = R.plurals.conversation_phished;
155                break;
156            default:
157                resId = -1;
158                break;
159        }
160        final String desc = (resId == -1) ? "" :
161                String.format(context.getResources().getQuantityString(resId, mCount), mCount);
162        return desc;
163    }
164
165    public String getSingularDescription(Context context, Folder folder) {
166        if (mAction == R.id.remove_folder) {
167            return context.getString(R.string.folder_removed, folder.name);
168        }
169        int resId = -1;
170        switch (mAction) {
171            case R.id.delete:
172                resId = R.string.deleted;
173                break;
174            case R.id.archive:
175                resId = R.string.archived;
176                break;
177        }
178        return (resId == -1) ? "" : context.getString(resId);
179    }
180
181    @Override
182    public int describeContents() {
183        return 0;
184    }
185}
186