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