ToastBarOperation.java revision 6a3d5ce0b18f58fcfa1af0315dc8ddc7331c2c5f
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 void writeToParcel(Parcel dest, int flags) {
75        dest.writeInt(mCount);
76        dest.writeInt(mAction);
77        dest.writeInt(mBatch ? 1 : 0);
78        dest.writeInt(mType);
79        dest.writeParcelable(mFolder, 0);
80    }
81
82    public static final ClassLoaderCreator<ToastBarOperation> CREATOR =
83            new ClassLoaderCreator<ToastBarOperation>() {
84        @Override
85        public ToastBarOperation createFromParcel(final Parcel source) {
86            return createFromParcel(source, null);
87        }
88
89        @Override
90        public ToastBarOperation[] newArray(final int size) {
91            return new ToastBarOperation[size];
92        }
93
94        @Override
95        public ToastBarOperation createFromParcel(final Parcel source, final ClassLoader loader) {
96            return new ToastBarOperation(source, loader);
97        }
98    };
99
100    /**
101     * Get a string description of the operation that will be performed
102     * when the user taps the undo bar.
103     */
104    public String getDescription(Context context) {
105        int resId = -1;
106        switch (mAction) {
107            case R.id.delete:
108                resId = R.plurals.conversation_deleted;
109                break;
110            case R.id.remove_folder:
111                return context.getString(R.string.folder_removed, mFolder.name);
112            case R.id.change_folder:
113                resId = R.plurals.conversation_folder_changed;
114                break;
115            case R.id.move_folder:
116                return context.getString(R.string.conversation_folder_moved, mFolder.name);
117            case R.id.archive:
118                resId = R.plurals.conversation_archived;
119                break;
120            case R.id.report_spam:
121                resId = R.plurals.conversation_spammed;
122                break;
123            case R.id.mark_not_spam:
124                resId = R.plurals.conversation_not_spam;
125                break;
126            case R.id.mark_not_important:
127                resId = R.plurals.conversation_not_important;
128                break;
129            case R.id.mute:
130                resId = R.plurals.conversation_muted;
131                break;
132            case R.id.remove_star:
133                resId = R.plurals.conversation_unstarred;
134                break;
135            case R.id.report_phishing:
136                resId = R.plurals.conversation_phished;
137                break;
138            default:
139                resId = -1;
140                break;
141        }
142        final String desc = (resId == -1) ? "" :
143                String.format(context.getResources().getQuantityString(resId, mCount), mCount);
144        return desc;
145    }
146
147    public String getSingularDescription(Context context, Folder folder) {
148        if (mAction == R.id.remove_folder) {
149            return context.getString(R.string.folder_removed, folder.name);
150        }
151        int resId = -1;
152        switch (mAction) {
153            case R.id.delete:
154                resId = R.string.deleted;
155                break;
156            case R.id.archive:
157                resId = R.string.archived;
158                break;
159        }
160        return (resId == -1) ? "" : context.getString(resId);
161    }
162
163    @Override
164    public int describeContents() {
165        return 0;
166    }
167}
168