summaryrefslogtreecommitdiffstats
path: root/src/com/android/mail/ui/HierarchicalFolderSelectorAdapter.java
blob: 0f52e6565a8af0ceafe15b078bb459867d5caa2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*******************************************************************************
 *      Copyright (C) 2012 Google Inc.
 *      Licensed to The Android Open Source Project.
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *           http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 *******************************************************************************/

package com.android.mail.ui;

import android.content.Context;
import android.database.Cursor;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.android.mail.R;
import com.android.mail.providers.Folder;
import com.google.common.annotations.VisibleForTesting;

import java.util.Set;

public class HierarchicalFolderSelectorAdapter extends FolderSelectorAdapter {

    private Context mContext;

    @VisibleForTesting
    public HierarchicalFolderSelectorAdapter(Context context, Cursor folders,
            Set<String> initiallySelected, int layout) {
        super(context, folders, initiallySelected, layout);
        mContext = context;
    }

    public HierarchicalFolderSelectorAdapter(Context context, Cursor folders,
            int layout, Folder excludedFolder) {
        super(context, folders, layout, excludedFolder);
        mContext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final View view = super.getView(position, convertView, parent);
        final FolderRow row = (FolderRow) getItem(position);
        final Folder folder = row.getFolder();
        final CompoundButton checkBox = (CompoundButton) view.findViewById(R.id.checkbox);
        final TextView display = (TextView) view.findViewById(R.id.folder_name);
        final CharSequence displayText = TextUtils.isEmpty(row.mPathName) ? folder.name
                : truncateHierarchy(row.mPathName);
        if (checkBox != null) {
            checkBox.setText(TextUtils.isEmpty(row.mPathName) ? folder.name
                : truncateHierarchy(row.mPathName), TextView.BufferType.SPANNABLE);
        } else {
            display.setText(displayText, TextView.BufferType.SPANNABLE);
        }
        return view;
    }

    /**
     * Truncation of a hierarchy works as follows:
     * 1) If there is just a folder name, return that.
     * 2) If there is a single parent and a folder name, return parent/folder.
     * 3) If there is > 1 but < 3 ancestors, return ancestor/ancestor2/folder
     * 4) If there are > 3 ancestors, return the top most ancestor, and direct parent
     * of the folder, and the folder: ancestor/.../directParent/folder
     */
    @VisibleForTesting
    protected SpannableStringBuilder truncateHierarchy(String hierarchy) {
        if (TextUtils.isEmpty(hierarchy)) {
            return null;
        }
        final String[] splitHierarchy = hierarchy.split("/");
        // We want to keep the last part of the hierachy, as that is the name of
        // the folder.
        final String folderName;
        final String topParentName;
        final String directParentName;
        final SpannableStringBuilder display = new SpannableStringBuilder();
        if (splitHierarchy != null && splitHierarchy.length > 0) {
            final int length = splitHierarchy.length;
            if (length > 2) {
                topParentName = splitHierarchy[0];
                directParentName = splitHierarchy[length - 2];
                folderName = splitHierarchy[length - 1];
            } else if (length > 1) {
                topParentName = splitHierarchy[0];
                directParentName = null;
                folderName = splitHierarchy[length - 1];
            } else {
                topParentName = null;
                directParentName = null;
                folderName = splitHierarchy[0];
            }
            if (!TextUtils.isEmpty(directParentName)) {
                final int formatString;
                if (length > 3) {
                    formatString = R.string.hierarchical_folder_parent_top_ellip;
                } else {
                    formatString = R.string.hierarchical_folder_parent_top;
                }
                display.append(mContext.getResources().getString(formatString, topParentName,
                        directParentName));
            } else if (!TextUtils.isEmpty(topParentName)) {
                display.append(mContext.getResources().getString(R.string.hierarchical_folder_top,
                        topParentName));
            }
            display.setSpan(new ForegroundColorSpan(R.color.hierarchical_folder_parent_color), 0,
                    display.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            display.append(folderName);
        }
        return display;
    }
}