ToastBarOperation.java revision 7747d15a9ca0be42e1c69b3b6c442c08ba37171f
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 com.android.mail.R;
20
21import android.content.Context;
22import android.os.Parcel;
23import android.os.Parcelable;
24import android.os.Parcelable.Creator;
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
37    /**
38     * Create a ToastBarOperation
39     *
40     * @param count Number of conversations this action would be applied to.
41     * @param menuId res id identifying the menu item tapped; used to determine
42     *            what action was performed
43     */
44    public ToastBarOperation(int count, int menuId, int type) {
45        mCount = count;
46        mAction = menuId;
47        mBatch = mCount > 1;
48        mType = type;
49    }
50
51    public int getType() {
52        return mType;
53    }
54
55    public boolean isBatchUndo() {
56        return mBatch;
57    }
58
59    public ToastBarOperation(Parcel in) {
60        mCount = in.readInt();
61        mAction = in.readInt();
62        mBatch = in.readInt() != 0;
63        mType = in.readInt();
64    }
65
66    @Override
67    public void writeToParcel(Parcel dest, int flags) {
68        dest.writeInt(mCount);
69        dest.writeInt(mAction);
70        dest.writeInt(mBatch ? 1 : 0);
71        dest.writeInt(mType);
72    }
73
74    @SuppressWarnings("hiding")
75    public static final Creator<ToastBarOperation> CREATOR = new Creator<ToastBarOperation>() {
76        @Override
77        public ToastBarOperation createFromParcel(Parcel source) {
78            return new ToastBarOperation(source);
79        }
80
81        @Override
82        public ToastBarOperation[] newArray(int size) {
83            return new ToastBarOperation[size];
84        }
85    };
86
87    /**
88     * Get a string description of the operation that will be performed
89     * when the user taps the undo bar.
90     */
91    public String getDescription(Context context) {
92        int resId = -1;
93        switch (mAction) {
94            case R.id.delete:
95                resId = R.plurals.conversation_deleted;
96                break;
97            case R.id.change_folder:
98                resId = R.plurals.conversation_folder_changed;
99                break;
100            case R.id.archive:
101                resId = R.plurals.conversation_archived;
102                break;
103            case R.id.report_spam:
104                resId = R.plurals.conversation_spammed;
105                break;
106            case R.id.mark_not_spam:
107                resId = R.plurals.conversation_not_spam;
108                break;
109            case R.id.mute:
110                resId = R.plurals.conversation_muted;
111                break;
112            case R.id.remove_star:
113                resId = R.plurals.conversation_unstarred;
114                break;
115            case R.id.report_phishing:
116                resId = R.plurals.conversation_phished;
117                break;
118            default:
119                resId = -1;
120                break;
121        }
122        final String desc = (resId == -1) ? "" :
123                String.format(context.getResources().getQuantityString(resId, mCount), mCount);
124        return desc;
125    }
126
127    public String getSingularDescription(Context context) {
128        int resId = -1;
129        switch (mAction) {
130            case R.id.delete:
131                resId = R.string.deleted;
132                break;
133            case R.id.archive:
134                resId = R.string.archived;
135                break;
136            case R.id.change_folder:
137                resId = R.string.folder_removed;
138                break;
139        }
140        return(resId == -1) ? "" : context.getString(resId);
141    }
142
143    @Override
144    public int describeContents() {
145        return 0;
146    }
147}
148