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 17 package com.android.gallery3d.data; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 22 import com.android.gallery3d.R; 23 24 import java.util.ArrayList; 25 26 public class SizeClustering extends Clustering { 27 @SuppressWarnings("unused") 28 private static final String TAG = "SizeClustering"; 29 30 private Context mContext; 31 private ArrayList<Path>[] mClusters; 32 private String[] mNames; 33 private long mMinSizes[]; 34 35 private static final long MEGA_BYTES = 1024L*1024; 36 private static final long GIGA_BYTES = 1024L*1024*1024; 37 38 private static final long[] SIZE_LEVELS = { 39 0, 40 1 * MEGA_BYTES, 41 10 * MEGA_BYTES, 42 100 * MEGA_BYTES, 43 1 * GIGA_BYTES, 44 2 * GIGA_BYTES, 45 4 * GIGA_BYTES, 46 }; 47 SizeClustering(Context context)48 public SizeClustering(Context context) { 49 mContext = context; 50 } 51 52 @SuppressWarnings("unchecked") 53 @Override run(MediaSet baseSet)54 public void run(MediaSet baseSet) { 55 @SuppressWarnings("unchecked") 56 final ArrayList<Path>[] group = new ArrayList[SIZE_LEVELS.length]; 57 baseSet.enumerateTotalMediaItems(new MediaSet.ItemConsumer() { 58 @Override 59 public void consume(int index, MediaItem item) { 60 // Find the cluster this item belongs to. 61 long size = item.getSize(); 62 int i; 63 for (i = 0; i < SIZE_LEVELS.length - 1; i++) { 64 if (size < SIZE_LEVELS[i + 1]) { 65 break; 66 } 67 } 68 69 ArrayList<Path> list = group[i]; 70 if (list == null) { 71 list = new ArrayList<Path>(); 72 group[i] = list; 73 } 74 list.add(item.getPath()); 75 } 76 }); 77 78 int count = 0; 79 for (int i = 0; i < group.length; i++) { 80 if (group[i] != null) { 81 count++; 82 } 83 } 84 85 mClusters = new ArrayList[count]; 86 mNames = new String[count]; 87 mMinSizes = new long[count]; 88 89 Resources res = mContext.getResources(); 90 int k = 0; 91 // Go through group in the reverse order, so the group with the largest 92 // size will show first. 93 for (int i = group.length - 1; i >= 0; i--) { 94 if (group[i] == null) continue; 95 96 mClusters[k] = group[i]; 97 if (i == 0) { 98 mNames[k] = String.format( 99 res.getString(R.string.size_below), getSizeString(i + 1)); 100 } else if (i == group.length - 1) { 101 mNames[k] = String.format( 102 res.getString(R.string.size_above), getSizeString(i)); 103 } else { 104 String minSize = getSizeString(i); 105 String maxSize = getSizeString(i + 1); 106 mNames[k] = String.format( 107 res.getString(R.string.size_between), minSize, maxSize); 108 } 109 mMinSizes[k] = SIZE_LEVELS[i]; 110 k++; 111 } 112 } 113 getSizeString(int index)114 private String getSizeString(int index) { 115 long bytes = SIZE_LEVELS[index]; 116 if (bytes >= GIGA_BYTES) { 117 return (bytes / GIGA_BYTES) + "GB"; 118 } else { 119 return (bytes / MEGA_BYTES) + "MB"; 120 } 121 } 122 123 @Override getNumberOfClusters()124 public int getNumberOfClusters() { 125 return mClusters.length; 126 } 127 128 @Override getCluster(int index)129 public ArrayList<Path> getCluster(int index) { 130 return mClusters[index]; 131 } 132 133 @Override getClusterName(int index)134 public String getClusterName(int index) { 135 return mNames[index]; 136 } 137 getMinSize(int index)138 public long getMinSize(int index) { 139 return mMinSizes[index]; 140 } 141 } 142