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.text.Spannable;
     23 import android.text.SpannableStringBuilder;
     24 import android.text.TextUtils;
     25 import android.text.style.ForegroundColorSpan;
     26 import android.view.View;
     27 import android.view.ViewGroup;
     28 import android.widget.CompoundButton;
     29 import android.widget.TextView;
     30 
     31 import com.android.mail.R;
     32 import com.android.mail.providers.Folder;
     33 import com.google.common.annotations.VisibleForTesting;
     34 
     35 import java.util.Set;
     36 
     37 public class HierarchicalFolderSelectorAdapter extends FolderSelectorAdapter {
     38 
     39     private Context mContext;
     40 
     41     public HierarchicalFolderSelectorAdapter(Context context, Cursor folders,
     42             Set<String> initiallySelected, int layout, String header) {
     43         super(context, folders, initiallySelected, layout, header);
     44         mContext = context;
     45     }
     46 
     47     public HierarchicalFolderSelectorAdapter(Context context, Cursor folders, int layout,
     48             String header, Folder excludedFolder) {
     49         super(context, folders, layout, header, excludedFolder);
     50         mContext = context;
     51     }
     52 
     53     @Override
     54     public View getView(int position, View convertView, ViewGroup parent) {
     55         final View view = super.getView(position, convertView, parent);
     56         if (isHeader(position)) {
     57             return view;
     58         }
     59         final FolderRow row = (FolderRow) getItem(position);
     60         final Folder folder = row.getFolder();
     61         final CompoundButton checkBox = (CompoundButton) view.findViewById(R.id.checkbox);
     62         final TextView display = (TextView) view.findViewById(R.id.folder_name);
     63         final CharSequence displayText = TextUtils.isEmpty(row.mPathName) ? folder.name
     64                 : truncateHierarchy(row.mPathName);
     65         if (checkBox != null) {
     66             checkBox.setText(TextUtils.isEmpty(row.mPathName) ? folder.name
     67                 : truncateHierarchy(row.mPathName), TextView.BufferType.SPANNABLE);
     68         } else {
     69             display.setText(displayText, TextView.BufferType.SPANNABLE);
     70         }
     71         return view;
     72     }
     73 
     74     /**
     75      * Truncation of a hierarchy works as follows:
     76      * 1) If there is just a folder name, return that.
     77      * 2) If there is a single parent and a folder name, return parent/folder.
     78      * 3) If there is > 1 but < 3 ancestors, return ancestor/ancestor2/folder
     79      * 4) If there are > 3 ancestors, return the top most ancestor, and direct parent
     80      * of the folder, and the folder: ancestor/.../directParent/folder
     81      */
     82     @VisibleForTesting
     83     protected SpannableStringBuilder truncateHierarchy(String hierarchy) {
     84         if (TextUtils.isEmpty(hierarchy)) {
     85             return null;
     86         }
     87         final String[] splitHierarchy = hierarchy.split("/");
     88         // We want to keep the last part of the hierachy, as that is the name of
     89         // the folder.
     90         final String folderName;
     91         final String topParentName;
     92         final String directParentName;
     93         final SpannableStringBuilder display = new SpannableStringBuilder();
     94         if (splitHierarchy != null && splitHierarchy.length > 0) {
     95             final int length = splitHierarchy.length;
     96             if (length > 2) {
     97                 topParentName = splitHierarchy[0];
     98                 directParentName = splitHierarchy[length - 2];
     99                 folderName = splitHierarchy[length - 1];
    100             } else if (length > 1) {
    101                 topParentName = splitHierarchy[0];
    102                 directParentName = null;
    103                 folderName = splitHierarchy[length - 1];
    104             } else {
    105                 topParentName = null;
    106                 directParentName = null;
    107                 folderName = splitHierarchy[0];
    108             }
    109             if (!TextUtils.isEmpty(directParentName)) {
    110                 final int formatString;
    111                 if (length > 3) {
    112                     formatString = R.string.hierarchical_folder_parent_top_ellip;
    113                 } else {
    114                     formatString = R.string.hierarchical_folder_parent_top;
    115                 }
    116                 display.append(mContext.getResources().getString(formatString, topParentName,
    117                         directParentName));
    118             } else if (!TextUtils.isEmpty(topParentName)) {
    119                 display.append(mContext.getResources().getString(R.string.hierarchical_folder_top,
    120                         topParentName, directParentName));
    121             }
    122             display.setSpan(new ForegroundColorSpan(R.color.hierarchical_folder_parent_color), 0,
    123                     display.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    124             display.append(folderName);
    125         }
    126         return display;
    127     }
    128 }
    129