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