1/*
2 * Copyright (C) 2010 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
17package com.android.gallery3d.data;
18
19import android.content.Context;
20
21import com.android.gallery3d.R;
22
23import java.util.ArrayList;
24import java.util.Map;
25import java.util.TreeMap;
26
27public class TagClustering extends Clustering {
28    @SuppressWarnings("unused")
29    private static final String TAG = "TagClustering";
30
31    private ArrayList<ArrayList<Path>> mClusters;
32    private String[] mNames;
33    private String mUntaggedString;
34
35    public TagClustering(Context context) {
36        mUntaggedString = context.getResources().getString(R.string.untagged);
37    }
38
39    @Override
40    public void run(MediaSet baseSet) {
41        final TreeMap<String, ArrayList<Path>> map =
42                new TreeMap<String, ArrayList<Path>>();
43        final ArrayList<Path> untagged = new ArrayList<Path>();
44
45        baseSet.enumerateTotalMediaItems(new MediaSet.ItemConsumer() {
46            @Override
47            public void consume(int index, MediaItem item) {
48                Path path = item.getPath();
49
50                String[] tags = item.getTags();
51                if (tags == null || tags.length == 0) {
52                    untagged.add(path);
53                    return;
54                }
55                for (int j = 0; j < tags.length; j++) {
56                    String key = tags[j];
57                    ArrayList<Path> list = map.get(key);
58                    if (list == null) {
59                        list = new ArrayList<Path>();
60                        map.put(key, list);
61                    }
62                    list.add(path);
63                }
64            }
65        });
66
67        int m = map.size();
68        mClusters = new ArrayList<ArrayList<Path>>();
69        mNames = new String[m + ((untagged.size() > 0) ? 1 : 0)];
70        int i = 0;
71        for (Map.Entry<String, ArrayList<Path>> entry : map.entrySet()) {
72            mNames[i++] = entry.getKey();
73            mClusters.add(entry.getValue());
74        }
75        if (untagged.size() > 0) {
76            mNames[i++] = mUntaggedString;
77            mClusters.add(untagged);
78        }
79    }
80
81    @Override
82    public int getNumberOfClusters() {
83        return mClusters.size();
84    }
85
86    @Override
87    public ArrayList<Path> getCluster(int index) {
88        return mClusters.get(index);
89    }
90
91    @Override
92    public String getClusterName(int index) {
93        return mNames[index];
94    }
95}
96