ToastBarOperation.java revision 6c570db71b8adc43d7dc2f31e6fa63cf50c1426e
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 final int resId; 124 if (mAction == R.id.delete) { 125 resId = R.plurals.conversation_deleted; 126 } else if (mAction == R.id.remove_folder) { 127 return context.getString(R.string.folder_removed, mFolder.name); 128 } else if (mAction == R.id.change_folders) { 129 resId = R.plurals.conversation_folder_changed; 130 } else if (mAction == R.id.move_folder) { 131 return context.getString(R.string.conversation_folder_moved, mFolder.name); 132 } else if (mAction == R.id.archive) { 133 resId = R.plurals.conversation_archived; 134 } else if (mAction == R.id.report_spam) { 135 resId = R.plurals.conversation_spammed; 136 } else if (mAction == R.id.mark_not_spam) { 137 resId = R.plurals.conversation_not_spam; 138 } else if (mAction == R.id.mark_not_important) { 139 resId = R.plurals.conversation_not_important; 140 } else if (mAction == R.id.mute) { 141 resId = R.plurals.conversation_muted; 142 } else if (mAction == R.id.remove_star) { 143 resId = R.plurals.conversation_unstarred; 144 } else if (mAction == R.id.report_phishing) { 145 resId = R.plurals.conversation_phished; 146 } else { 147 resId = -1; 148 } 149 final String desc = (resId == -1) ? "" : 150 String.format(context.getResources().getQuantityString(resId, mCount), mCount); 151 return desc; 152 } 153 154 public String getSingularDescription(Context context, Folder folder) { 155 if (mAction == R.id.remove_folder) { 156 return context.getString(R.string.folder_removed, folder.name); 157 } 158 final int resId; 159 if (mAction == R.id.delete) { 160 resId = R.string.deleted; 161 } else if (mAction == R.id.archive) { 162 resId = R.string.archived; 163 } else { 164 resId = -1; 165 } 166 return (resId == -1) ? "" : context.getString(resId); 167 } 168 169 @Override 170 public int describeContents() { 171 return 0; 172 } 173} 174