aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/filemanager/adapters/QuickSearchAdapter.java
blob: 15835522af7a1d935a5d0a256229202175bf8c92 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright (C) 2015 The CyanogenMod 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.cyanogenmod.filemanager.adapters;

import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.cyanogenmod.filemanager.R;
import com.cyanogenmod.filemanager.activities.SearchActivity;
import com.cyanogenmod.filemanager.ui.IconHolder;
import com.cyanogenmod.filemanager.util.FileHelper;
import com.cyanogenmod.filemanager.util.MimeTypeHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.cyanogenmod.filemanager.util.MimeTypeHelper.MimeTypeCategory.APP;
import static com.cyanogenmod.filemanager.util.MimeTypeHelper.MimeTypeCategory.COMPRESS;
import static com.cyanogenmod.filemanager.util.MimeTypeHelper.MimeTypeCategory.IMAGE;
import static com.cyanogenmod.filemanager.util.MimeTypeHelper.MimeTypeCategory.AUDIO;
import static com.cyanogenmod.filemanager.util.MimeTypeHelper.MimeTypeCategory.VIDEO;
import static com.cyanogenmod.filemanager.util.MimeTypeHelper.MimeTypeCategory.DOCUMENT;

public class QuickSearchAdapter extends ArrayAdapter<MimeTypeHelper.MimeTypeCategory>
        implements View.OnClickListener {

    static String MIME_TYPE_LOCALIZED_NAMES[];

    static Map<MimeTypeHelper.MimeTypeCategory, Integer> QUICK_SEARCH_ICONS
            = new HashMap<MimeTypeHelper.MimeTypeCategory, Integer>();
    static {
        QUICK_SEARCH_ICONS.put(MimeTypeHelper.MimeTypeCategory.IMAGE,
                R.drawable.ic_category_images);
        QUICK_SEARCH_ICONS.put(MimeTypeHelper.MimeTypeCategory.AUDIO, R.drawable.ic_category_audio);
        QUICK_SEARCH_ICONS.put(MimeTypeHelper.MimeTypeCategory.VIDEO, R.drawable.ic_category_video);
        QUICK_SEARCH_ICONS.put(MimeTypeHelper.MimeTypeCategory.DOCUMENT,
                R.drawable.ic_category_docs);
        QUICK_SEARCH_ICONS.put(MimeTypeHelper.MimeTypeCategory.APP, R.drawable.ic_category_apps);
        QUICK_SEARCH_ICONS.put(MimeTypeHelper.MimeTypeCategory.COMPRESS,
                R.drawable.ic_category_archives);
    }

    public static final List<MimeTypeHelper.MimeTypeCategory> QUICK_SEARCH_LIST
            = new ArrayList<MimeTypeHelper.MimeTypeCategory>() {
        {
            add(IMAGE);
            add(AUDIO);
            add(VIDEO);
            add(DOCUMENT);
            add(APP);
            add(COMPRESS);
        }
    };

    IconHolder mIconHolder;

    public QuickSearchAdapter(Context context, int resource) {
        super(context, resource);

        MIME_TYPE_LOCALIZED_NAMES =
                MimeTypeHelper.MimeTypeCategory.getDefinedLocalizedNames(context);
        mIconHolder = new IconHolder(context, false);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = (convertView == null) ? LayoutInflater.from(getContext()).inflate(
                R.layout.quick_search_item, parent, false) : convertView;

        MimeTypeHelper.MimeTypeCategory item = getItem(position);
        String typeTitle = MIME_TYPE_LOCALIZED_NAMES[item.ordinal()];
        TextView typeTitleTV = (TextView) convertView
                .findViewById(R.id.navigation_view_item_name);
        ImageView typeIconIV = (ImageView) convertView
                .findViewById(R.id.navigation_view_item_icon);

        typeTitleTV.setText(typeTitle);
        convertView.setOnClickListener(this);
        convertView.setTag(position);

        int colorId = MimeTypeHelper.getIconColorFromIconId(getContext(), QUICK_SEARCH_ICONS.get(item));
        setIcon(typeIconIV, getContext().getResources().getDrawable(QUICK_SEARCH_ICONS.get(item)),
                R.color.navigation_view_icon_unselected, R.drawable.ic_icon_background,
                getContext().getResources().getColor(colorId));

        return convertView;
    }

    // Set drawable as icon
    private void setIcon(ImageView view, Drawable iconDrawable, int iconColorId, int backgroundId,
                         int backgroundColor) {

        StateListDrawable stateListDrawable = new StateListDrawable();
        addUnselected(getContext().getResources(), stateListDrawable, iconDrawable, iconColorId);

        ColorStateList colorList = new ColorStateList(
                new int[][]{new int[]{android.R.attr.state_selected},
                        new int[]{}},
                new int[]{getContext().getResources()
                        .getColor(R.color.navigation_view_icon_selected), backgroundColor});

        view.setBackgroundResource(backgroundId);
        view.setBackgroundTintList(colorList);
        view.setImageDrawable(stateListDrawable);
    }

    // default
    private void addUnselected(Resources res, StateListDrawable drawable, Drawable iconDrawable,
                               int colorId) {
        iconDrawable.setTint(res.getColor(colorId));
        drawable.addState(new int[0], iconDrawable);
    }

    @Override
    public void onClick(View view) {
        Integer position = (Integer) view.getTag();

        Intent intent = new Intent(getContext(), SearchActivity.class);
        intent.setAction(Intent.ACTION_SEARCH);
        intent.putExtra(SearchActivity.EXTRA_SEARCH_DIRECTORY, FileHelper.ROOT_DIRECTORY);
        intent.putExtra(SearchManager.QUERY, "*"); // Use wild-card '*'

        ArrayList<MimeTypeHelper.MimeTypeCategory> searchCategories =
                new ArrayList<MimeTypeHelper.MimeTypeCategory>();
        MimeTypeHelper.MimeTypeCategory selectedCategory = QUICK_SEARCH_LIST.get(position);
        searchCategories.add(selectedCategory);
        // a one off case where we implicitly want to also search for TEXT mimetypes when the
        // DOCUMENTS category is selected
        if (selectedCategory == MimeTypeHelper.MimeTypeCategory.DOCUMENT) {
            searchCategories.add(
                    MimeTypeHelper.MimeTypeCategory.TEXT);
        }
        intent.putExtra(SearchActivity.EXTRA_SEARCH_MIMETYPE, searchCategories);

        getContext().startActivity(intent);
    }
}