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        ActionableToastBar.ActionClickedListener {
31    public static final int UNDO = 0;
32    public static final int ERROR = 1;
33    private final int mAction;
34    private final int mCount;
35    private final boolean mBatch;
36    private final int mType;
37    private final Folder mFolder;
38
39    /**
40     * Create a ToastBarOperation
41     *
42     * @param count Number of conversations this action would be applied to.
43     * @param menuId res id identifying the menu item tapped; used to determine what action was
44     *        performed
45     * @param operationFolder The {@link Folder} upon which the operation was run. This may be
46     *        <code>null</code>, but is required in {@link #getDescription(Context)} for certain
47     *        actions.
48     */
49    public ToastBarOperation(int count, int menuId, int type, boolean batch,
50            final Folder operationFolder) {
51        mCount = count;
52        mAction = menuId;
53        mBatch = batch;
54        mType = type;
55        mFolder = operationFolder;
56    }
57
58    public int getType() {
59        return mType;
60    }
61
62    public boolean isBatchUndo() {
63        return mBatch;
64    }
65
66    public ToastBarOperation(final Parcel in, final ClassLoader loader) {
67        mCount = in.readInt();
68        mAction = in.readInt();
69        mBatch = in.readInt() != 0;
70        mType = in.readInt();
71        mFolder = in.readParcelable(loader);
72    }
73
74    @Override
75    public String toString() {
76        final StringBuilder sb = new StringBuilder("{");
77        sb.append(super.toString());
78        sb.append(" mAction=");
79        sb.append(mAction);
80        sb.append(" mCount=");
81        sb.append(mCount);
82        sb.append(" mBatch=");
83        sb.append(mBatch);
84        sb.append(" mType=");
85        sb.append(mType);
86        sb.append(" mFolder=");
87        sb.append(mFolder);
88        sb.append("}");
89        return sb.toString();
90    }
91
92    @Override
93    public void writeToParcel(Parcel dest, int flags) {
94        dest.writeInt(mCount);
95        dest.writeInt(mAction);
96        dest.writeInt(mBatch ? 1 : 0);
97        dest.writeInt(mType);
98        dest.writeParcelable(mFolder, 0);
99    }
100
101    public static final ClassLoaderCreator<ToastBarOperation> CREATOR =
102            new ClassLoaderCreator<ToastBarOperation>() {
103        @Override
104        public ToastBarOperation createFromParcel(final Parcel source) {
105            return createFromParcel(source, null);
106        }
107
108        @Override
109        public ToastBarOperation[] newArray(final int size) {
110            return new ToastBarOperation[size];
111        }
112
113        @Override
114        public ToastBarOperation createFromParcel(final Parcel source, final ClassLoader loader) {
115            return new ToastBarOperation(source, loader);
116        }
117    };
118
119    /**
120     * Get a string description of the operation that will be performed
121     * when the user taps the undo bar.
122     */
123    public String getDescription(Context context) {
124        final int resId;
125        if (mAction == R.id.delete) {
126            resId = R.plurals.conversation_deleted;
127        } else if (mAction == R.id.remove_folder) {
128            return context.getString(R.string.folder_removed, mFolder.name);
129        } else if (mAction == R.id.change_folders) {
130            resId = R.plurals.conversation_folder_changed;
131        } else if (mAction == R.id.move_folder) {
132            return context.getString(R.string.conversation_folder_moved, mFolder.name);
133        } else if (mAction == R.id.archive) {
134            resId = R.plurals.conversation_archived;
135        } else if (mAction == R.id.report_spam) {
136            resId = R.plurals.conversation_spammed;
137        } else if (mAction == R.id.mark_not_spam) {
138            resId = R.plurals.conversation_not_spam;
139        } else if (mAction == R.id.mark_not_important) {
140            resId = R.plurals.conversation_not_important;
141        } else if (mAction == R.id.mute) {
142            resId = R.plurals.conversation_muted;
143        } else if (mAction == R.id.remove_star) {
144            resId = R.plurals.conversation_unstarred;
145        } else if (mAction == R.id.report_phishing) {
146            resId = R.plurals.conversation_phished;
147        } else {
148            resId = -1;
149        }
150        final String desc = (resId == -1) ? "" :
151                String.format(context.getResources().getQuantityString(resId, mCount), mCount);
152        return desc;
153    }
154
155    public String getSingularDescription(Context context, Folder folder) {
156        if (mAction == R.id.remove_folder) {
157            return context.getString(R.string.folder_removed, folder.name);
158        }
159        final int resId;
160        if (mAction ==  R.id.delete) {
161            resId = R.string.deleted;
162        } else if (mAction == R.id.archive) {
163            resId = R.string.archived;
164        } else {
165            resId = -1;
166        }
167        return (resId == -1) ? "" : context.getString(resId);
168    }
169
170    @Override
171    public int describeContents() {
172        return 0;
173    }
174
175    /**
176     * Returns true if this object should take precedence
177     * when the {@link ActionableToastBar}'s action button is clicked.
178     * If <code>true</code>, the listener passed in {@link ActionableToastBar#show}
179     * will not be used. The default implementation returns false. Derived
180     * classes should override if this behavior is desired.
181     */
182    public boolean shouldTakeOnActionClickedPrecedence() {
183        return false;
184    }
185
186    @Override
187    public void onActionClicked(Context context) {
188        // DO NOTHING
189    }
190
191    public void onToastBarTimeout(Context context) {
192        // DO NOTHING
193    }
194}
195