Home | History | Annotate | Download | only in ui
      1 /*******************************************************************************
      2  *      Copyright (C) 2012 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  *******************************************************************************/
     17 
     18 package com.android.mail.ui;
     19 
     20 import android.content.Context;
     21 import android.database.Cursor;
     22 import android.database.MatrixCursor;
     23 import android.net.Uri;
     24 
     25 import com.android.mail.providers.Folder;
     26 import com.android.mail.providers.UIProvider;
     27 import com.android.mail.utils.FolderUri;
     28 import com.android.mail.utils.MatrixCursorWithCachedColumns;
     29 
     30 import java.util.Set;
     31 
     32 public class AddableFolderSelectorAdapter extends FolderSelectorAdapter {
     33 
     34     public AddableFolderSelectorAdapter(Context context, Cursor folders,
     35             Set<String> selected, int layout) {
     36         super(context, folders, selected, layout);
     37     }
     38 
     39     /**
     40      * Essentially uses filterFolders with no filter to convert the cursors
     41      */
     42     public static Cursor filterFolders(final Cursor folderCursor) {
     43         return filterFolders(folderCursor, null /* excludedTypes */, null /* initiallySelected */,
     44                 true /* includeInitiallySelected, useless in this case */);
     45     }
     46 
     47     /**
     48      * @param folderCursor
     49      * @param excludedTypes folder types that we want to filter out.
     50      * @param initiallySelected set of folder uris that are previously selected.
     51      * @param includeOnlyInitiallySelected if we want to ONLY include or exclude initiallySelected,
     52      *   doesn't do anything if initiallySelected is null.
     53      * @return
     54      */
     55     public static Cursor filterFolders(final Cursor folderCursor,
     56             final Set<Integer> excludedTypes, final Set<String> initiallySelected,
     57             final boolean includeOnlyInitiallySelected) {
     58         final int projectionSize = UIProvider.FOLDERS_PROJECTION.length;
     59         final MatrixCursor cursor =
     60                 new MatrixCursorWithCachedColumns(UIProvider.FOLDERS_PROJECTION);
     61         final Object[] folder = new Object[projectionSize];
     62         if (folderCursor.moveToFirst()) {
     63             do {
     64                 final int type = folderCursor.getInt(UIProvider.FOLDER_TYPE_COLUMN);
     65 
     66                 // Check for excluded types
     67                 boolean exclude = false;
     68                 if (excludedTypes != null) {
     69                     for (final int excludedType : excludedTypes) {
     70                         if (Folder.isType(type, excludedType)) {
     71                             exclude = true;
     72                             break;
     73                         }
     74                     }
     75                 }
     76 
     77                 if (initiallySelected != null) {
     78                     // TODO: there has to be a better way to get this value...
     79                     String uri = new FolderUri(Uri.parse(folderCursor.getString(
     80                             UIProvider.FOLDER_URI_COLUMN))).getComparisonUri().toString();
     81                     // Check if the folder is already selected and if we are trying to include only
     82                     // the ones that were initially selected or only the ones that aren't.
     83                     // Xor is what we want, since we essentially want the following:
     84                     // (includeOnlyInitial && !contains) || (!includeOnlyInitial && contains)
     85                     exclude |= includeOnlyInitiallySelected ^ initiallySelected.contains(uri);
     86                 }
     87 
     88                 if (exclude) {
     89                     continue;
     90                 }
     91 
     92                 if (Folder.isType(type, UIProvider.FolderType.INBOX)
     93                         || Folder.isType(type, UIProvider.FolderType.DEFAULT)) {
     94                     folder[UIProvider.FOLDER_ID_COLUMN] = folderCursor
     95                             .getLong(UIProvider.FOLDER_ID_COLUMN);
     96                     folder[UIProvider.FOLDER_PERSISTENT_ID_COLUMN] = folderCursor
     97                             .getString(UIProvider.FOLDER_PERSISTENT_ID_COLUMN);
     98                     folder[UIProvider.FOLDER_URI_COLUMN] = folderCursor
     99                             .getString(UIProvider.FOLDER_URI_COLUMN);
    100                     folder[UIProvider.FOLDER_NAME_COLUMN] = folderCursor
    101                             .getString(UIProvider.FOLDER_NAME_COLUMN);
    102                     folder[UIProvider.FOLDER_HAS_CHILDREN_COLUMN] = folderCursor
    103                             .getInt(UIProvider.FOLDER_HAS_CHILDREN_COLUMN);
    104                     folder[UIProvider.FOLDER_CAPABILITIES_COLUMN] = folderCursor
    105                             .getInt(UIProvider.FOLDER_CAPABILITIES_COLUMN);
    106                     folder[UIProvider.FOLDER_SYNC_WINDOW_COLUMN] = folderCursor
    107                             .getInt(UIProvider.FOLDER_SYNC_WINDOW_COLUMN);
    108                     folder[UIProvider.FOLDER_CONVERSATION_LIST_URI_COLUMN] = folderCursor
    109                             .getString(UIProvider.FOLDER_CONVERSATION_LIST_URI_COLUMN);
    110                     folder[UIProvider.FOLDER_CHILD_FOLDERS_LIST_COLUMN] = folderCursor
    111                             .getString(UIProvider.FOLDER_CHILD_FOLDERS_LIST_COLUMN);
    112                     folder[UIProvider.FOLDER_UNSEEN_COUNT_COLUMN] = folderCursor
    113                             .getInt(UIProvider.FOLDER_UNSEEN_COUNT_COLUMN);
    114                     folder[UIProvider.FOLDER_UNREAD_COUNT_COLUMN] = folderCursor
    115                             .getInt(UIProvider.FOLDER_UNREAD_COUNT_COLUMN);
    116                     folder[UIProvider.FOLDER_TOTAL_COUNT_COLUMN] = folderCursor
    117                             .getInt(UIProvider.FOLDER_TOTAL_COUNT_COLUMN);
    118                     folder[UIProvider.FOLDER_REFRESH_URI_COLUMN] = folderCursor
    119                             .getString(UIProvider.FOLDER_REFRESH_URI_COLUMN);
    120                     folder[UIProvider.FOLDER_SYNC_STATUS_COLUMN] = folderCursor
    121                             .getInt(UIProvider.FOLDER_SYNC_STATUS_COLUMN);
    122                     folder[UIProvider.FOLDER_LAST_SYNC_RESULT_COLUMN] = folderCursor
    123                             .getInt(UIProvider.FOLDER_LAST_SYNC_RESULT_COLUMN);
    124                     folder[UIProvider.FOLDER_TYPE_COLUMN] = type;
    125                     folder[UIProvider.FOLDER_ICON_RES_ID_COLUMN] = folderCursor
    126                             .getInt(UIProvider.FOLDER_ICON_RES_ID_COLUMN);
    127                     folder[UIProvider.FOLDER_BG_COLOR_COLUMN] = folderCursor
    128                             .getString(UIProvider.FOLDER_BG_COLOR_COLUMN);
    129                     folder[UIProvider.FOLDER_FG_COLOR_COLUMN] = folderCursor
    130                             .getString(UIProvider.FOLDER_FG_COLOR_COLUMN);
    131                     folder[UIProvider.FOLDER_LOAD_MORE_URI_COLUMN] = folderCursor
    132                             .getString(UIProvider.FOLDER_LOAD_MORE_URI_COLUMN);
    133                     folder[UIProvider.FOLDER_HIERARCHICAL_DESC_COLUMN] = folderCursor
    134                             .getString(UIProvider.FOLDER_HIERARCHICAL_DESC_COLUMN);
    135                     folder[UIProvider.FOLDER_LAST_MESSAGE_TIMESTAMP_COLUMN] = folderCursor
    136                             .getLong(UIProvider.FOLDER_LAST_MESSAGE_TIMESTAMP_COLUMN);
    137                     folder[UIProvider.FOLDER_PARENT_URI_COLUMN] = folderCursor
    138                             .getString(UIProvider.FOLDER_PARENT_URI_COLUMN);
    139                     cursor.addRow(folder);
    140                 }
    141             } while (folderCursor.moveToNext());
    142         }
    143         return cursor;
    144     }
    145 }
    146