1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.mail.drawer;
18 
19 import android.support.annotation.IntDef;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 
24 import com.android.bitmap.BitmapCache;
25 import com.android.mail.bitmap.ContactResolver;
26 import com.android.mail.providers.Account;
27 import com.android.mail.providers.Folder;
28 import com.android.mail.ui.ControllableActivity;
29 import com.android.mail.ui.FolderListFragment;
30 import com.android.mail.utils.FolderUri;
31 
32 import java.lang.annotation.Retention;
33 import java.lang.annotation.RetentionPolicy;
34 
35 
36 /**
37  * An element that is shown in the {@link com.android.mail.ui.FolderListFragment}. This class is
38  * only used for elements that are shown in the {@link com.android.mail.ui.DrawerFragment}.
39  * This class is an enumeration of a few element types: Account, a folder, a recent folder,
40  * or a header (a resource string). A {@link DrawerItem} can only be one type and can never
41  * switch types. Items are created using methods like
42  * {@link DrawerItem#ofAccount(ControllableActivity, Account, int, boolean, BitmapCache,
43  * ContactResolver)},
44  * {@link DrawerItem#ofWaitView(ControllableActivity)}, etc.
45  *
46  * Once created, the item can create a view using
47  * {@link #getView(android.view.View, android.view.ViewGroup)}.
48  */
49 public abstract class DrawerItem {
50     public final Folder mFolder;
51     public final Account mAccount;
52 
53     /** These are view types for view recycling purposes */
54     @Retention(RetentionPolicy.CLASS)
55     @IntDef({VIEW_FOLDER, VIEW_HEADER, VIEW_BLANK_HEADER, VIEW_BOTTOM_SPACE, VIEW_ACCOUNT,
56             VIEW_WAITING_FOR_SYNC, VIEW_FOOTER_HELP, VIEW_FOOTER_SETTINGS})
57     public @interface DrawerItemType {}
58     /** A normal folder, also a child, if a parent is specified. */
59     public static final int VIEW_FOLDER = 0;
60     /** A text-label which serves as a header in sectioned lists. */
61     public static final int VIEW_HEADER = 1;
62     /** A blank divider which serves as a header in sectioned lists. */
63     public static final int VIEW_BLANK_HEADER = 2;
64     /** A spacer which serves as a footer below the last item. */
65     public static final int VIEW_BOTTOM_SPACE = 3;
66     /** An account object, which allows switching accounts rather than folders. */
67     public static final int VIEW_ACCOUNT = 4;
68     /** An expandable object for expanding/collapsing more of the list */
69     public static final int VIEW_WAITING_FOR_SYNC = 5;
70     /** A footer item for Help */
71     public static final int VIEW_FOOTER_HELP = 6;
72     /** A footer item for Settings */
73     public static final int VIEW_FOOTER_SETTINGS = 7;
74     /** The value (1-indexed) of the last View type.  Useful when returning the number of types. */
75     private static final int LAST_FIELD = VIEW_FOOTER_SETTINGS + 1;
76 
77     /** The parent activity */
78     protected final ControllableActivity mActivity;
79     protected final LayoutInflater mInflater;
80 
81     /**
82      * These values determine the behavior of the drawer items.
83      *
84      * Either {@link #FOLDER_INBOX}, {@link #FOLDER_RECENT} or {@link #FOLDER_OTHER} when
85      * {@link #getType()} is {@link #VIEW_FOLDER}, or {@link #NONFOLDER_ITEM} otherwise.
86      */
87     @Retention(RetentionPolicy.CLASS)
88     @IntDef({UNSET, NONFOLDER_ITEM, FOLDER_INBOX, FOLDER_RECENT, FOLDER_OTHER})
89     public @interface DrawerItemCategory {}
90     public final @DrawerItemCategory int mItemCategory;
91     /** Non existent item or folder type not yet set */
92     public static final int UNSET = 0;
93     /** An unclickable text-header visually separating the different types. */
94     public static final int NONFOLDER_ITEM = 0;
95     /** An inbox folder: Inbox, ...*/
96     public static final int FOLDER_INBOX = 1;
97     /** A folder from whom a conversation was recently viewed */
98     public static final int FOLDER_RECENT = 2;
99     /** A non-inbox folder that is shown in the "everything else" group. */
100     public static final int FOLDER_OTHER = 3;
101 
102     /**
103      * Creates a drawer item with every instance variable specified.
104      *
105      * @param activity the underlying activity
106      * @param folder a non-null folder, if this is a folder type
107      * @param itemCategory the type of the folder. For folders this is:
108      *            {@link #FOLDER_INBOX}, {@link #FOLDER_RECENT}, {@link #FOLDER_OTHER},
109      *            or for non-folders this is {@link #NONFOLDER_ITEM}
110      * @param account the account object, for an account drawer element
111      */
DrawerItem(ControllableActivity activity, Folder folder, @DrawerItemCategory int itemCategory, Account account)112     protected DrawerItem(ControllableActivity activity, Folder folder,
113             @DrawerItemCategory int itemCategory, Account account) {
114         mActivity = activity;
115         mFolder = folder;
116         mItemCategory = itemCategory;
117         mAccount = account;
118         mInflater = LayoutInflater.from(activity.getActivityContext());
119     }
120 
121     /**
122      * Create a folder item with the given type.
123      *
124      * @param activity the underlying activity
125      * @param folder a folder that this item represents
126      * @param itemCategory one of {@link #FOLDER_INBOX}, {@link #FOLDER_RECENT} or
127      * {@link #FOLDER_OTHER}
128      * @return a drawer item for the folder.
129      */
ofFolder(ControllableActivity activity, Folder folder, @DrawerItemCategory int itemCategory)130     public static DrawerItem ofFolder(ControllableActivity activity, Folder folder,
131             @DrawerItemCategory int itemCategory) {
132         return new FolderDrawerItem(activity, folder, itemCategory);
133     }
134 
135     /**
136      * Creates an item from an account.
137      * @param activity the underlying activity
138      * @param account the account to create a drawer item for
139      * @param unreadCount the unread count of the account, pass zero if
140      * @param isCurrentAccount true if the account is the current account, false otherwise
141      * @return a drawer item for the account.
142      */
ofAccount(ControllableActivity activity, Account account, int unreadCount, boolean isCurrentAccount, BitmapCache cache, ContactResolver contactResolver)143     public static DrawerItem ofAccount(ControllableActivity activity, Account account,
144             int unreadCount, boolean isCurrentAccount, BitmapCache cache,
145             ContactResolver contactResolver) {
146         return new AccountDrawerItem(activity, account, unreadCount, isCurrentAccount, cache,
147                 contactResolver);
148     }
149 
150     /**
151      * Create a header item with a string resource.
152      *
153      * @param activity the underlying activity
154      * @param resource the string resource: R.string.all_folders_heading
155      * @return a drawer item for the header.
156      */
ofHeader(ControllableActivity activity, int resource)157     public static DrawerItem ofHeader(ControllableActivity activity, int resource) {
158         return new HeaderDrawerItem(activity, resource);
159     }
160 
ofBlankHeader(ControllableActivity activity)161     public static DrawerItem ofBlankHeader(ControllableActivity activity) {
162         return new BlankHeaderDrawerItem(activity);
163     }
164 
ofBottomSpace(ControllableActivity activity)165     public static DrawerItem ofBottomSpace(ControllableActivity activity) {
166         return new BottomSpaceDrawerItem(activity);
167     }
168 
169     /**
170      * Create a "waiting for initialization" item.
171      *
172      * @param activity the underlying activity
173      * @return a drawer item with an indeterminate progress indicator.
174      */
ofWaitView(ControllableActivity activity)175     public static DrawerItem ofWaitView(ControllableActivity activity) {
176         return new WaitViewDrawerItem(activity);
177     }
178 
ofHelpItem(ControllableActivity activity, Account account, FolderListFragment.DrawerStateListener drawerListener)179     public static DrawerItem ofHelpItem(ControllableActivity activity, Account account,
180             FolderListFragment.DrawerStateListener drawerListener) {
181         return new HelpItem(activity, account, drawerListener);
182     }
183 
ofSettingsItem(ControllableActivity activity, Account account, FolderListFragment.DrawerStateListener drawerListener)184     public static DrawerItem ofSettingsItem(ControllableActivity activity, Account account,
185             FolderListFragment.DrawerStateListener drawerListener) {
186         return new SettingsItem(activity, account, drawerListener);
187     }
188 
189     /**
190      * Returns a view for the given item. The method signature is identical to that required by a
191      * {@link android.widget.ListAdapter#getView(int, android.view.View, android.view.ViewGroup)}.
192      */
getView(View convertView, ViewGroup parent)193     public abstract View getView(View convertView, ViewGroup parent);
194 
195     /**
196      * Book-keeping for how many different view types there are.
197      * @return number of different types of view items
198      */
getViewTypeCount()199     public static int getViewTypeCount() {
200         return LAST_FIELD;
201     }
202 
203     /**
204      * Returns whether this view is enabled or not. An enabled view is one that accepts user taps
205      * and acts upon them.
206      * @return true if this view is enabled, false otherwise.
207      */
isItemEnabled()208     public abstract boolean isItemEnabled();
209 
210     /**
211      * Returns whether this view is highlighted or not.
212      *
213      *
214      * @param currentFolder The current folder, according to the
215      *                      {@link com.android.mail.ui.FolderListFragment}
216      * @param currentType The type of the current folder. We want to only highlight a folder once.
217      *                    A folder might be in two places at once: in "All Folders", and in
218      *                    "Recent Folder". Valid types of selected folders are :
219      *                    {@link DrawerItem#FOLDER_INBOX}, {@link DrawerItem#FOLDER_RECENT} or
220      *                    {@link DrawerItem#FOLDER_OTHER}, or {@link DrawerItem#UNSET}.
221 
222      * @return true if this DrawerItem results in a view that is highlighted (this DrawerItem is
223      *              the current folder.
224      */
isHighlighted(FolderUri currentFolder, int currentType)225     public abstract boolean isHighlighted(FolderUri currentFolder, int currentType);
226 
getType()227     public abstract @DrawerItemType int getType();
228 
onClick(View v)229     public void onClick(View v) {}
230 }
231 
232