OSDN Git Service

3rd party folders not appearing on resume Crash on computing sorted intersections...
[android-x86/packages-apps-Gallery2.git] / src / com / cooliris / media / DetailMode.java
1 package com.cooliris.media;
2
3 import java.text.DateFormat;
4 import java.util.ArrayList;
5 import java.util.Date;
6
7 import android.content.Context;
8 import android.content.res.Resources;;
9
10 public final class DetailMode {
11     public static CharSequence[] populateDetailModeStrings(Context context, ArrayList<MediaBucket> buckets) {
12         int numBuckets = buckets.size();
13         if (MediaBucketList.isSetSelection(buckets) && numBuckets == 1) {
14             // If just 1 set was selected, save the trouble of processing the items in the set again.
15             // We have already processed details for that set.
16             return populateSetViewDetailModeStrings(context, MediaBucketList.getFirstSetSelection(buckets), 1);
17         } else if (MediaBucketList.isSetSelection(buckets) || MediaBucketList.isMultipleItemSelection(buckets)) {
18             // Cycle through the items and add them to the selection items set.
19             MediaSet selectedItemsSet = new MediaSet();
20             for (int i = 0; i < numBuckets; i++) {
21                 MediaBucket bucket = buckets.get(i);
22                 ArrayList<MediaItem> currItems = null;
23                 int numCurrItems = 0;
24                 if (MediaBucketList.isSetSelection(bucket)) {
25                     MediaSet currSet = bucket.mediaSet;
26                     if (currSet != null) {
27                         currItems = currSet.getItems();
28                         numCurrItems = currSet.getNumItems();
29                     }
30                 } else {
31                     currItems = bucket.mediaItems;
32                     numCurrItems = currItems.size();
33                 }
34                 if (currItems != null) {
35                     for (int j = 0; j < numCurrItems; j++) {
36                         selectedItemsSet.addItem(currItems.get(j));
37                     }
38                 }
39             }
40             return populateSetViewDetailModeStrings(context, selectedItemsSet, numBuckets);
41         } else {
42             return populateItemViewDetailModeStrings(context, MediaBucketList.getFirstItemSelection(buckets)); 
43         }
44     }
45
46     private static CharSequence[] populateSetViewDetailModeStrings(Context context, MediaSet selectedItemsSet, int numOriginalSets) {
47         if (selectedItemsSet == null) {
48             return null;
49         }
50         Resources resources = context.getResources();
51         ArrayList<CharSequence> strings = new ArrayList<CharSequence>();
52         
53         // Number of albums selected.
54         if (numOriginalSets == 1) {
55             strings.add("1 " + resources.getString(R.string.album_selected));
56         } else {
57             strings.add(Integer.toString(numOriginalSets) + " " + resources.getString(R.string.albums_selected));
58         }
59
60         // Number of items selected.
61         int numItems = selectedItemsSet.mNumItemsLoaded;
62         if (numItems == 1) {
63             strings.add("1 " + resources.getString(R.string.item_selected));
64         } else {
65             strings.add(Integer.toString(numItems) + " " + resources.getString(R.string.items_selected));
66         }
67         
68         DateFormat dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.SHORT);
69         
70         // Start and end times of the selected items.
71         if (selectedItemsSet.areTimestampsAvailable()) {
72             long minTimestamp = selectedItemsSet.mMinTimestamp;
73             long maxTimestamp = selectedItemsSet.mMaxTimestamp;
74             if (selectedItemsSet.isPicassaSet()) {
75                 minTimestamp -= Gallery.CURRENT_TIME_ZONE.getOffset(minTimestamp);
76                 maxTimestamp -= Gallery.CURRENT_TIME_ZONE.getOffset(maxTimestamp);
77             }
78             strings.add(resources.getString(R.string.start) + ": " + dateTimeFormat.format(new Date(minTimestamp)));
79             strings.add(resources.getString(R.string.end) + ": " + dateTimeFormat.format(new Date(maxTimestamp)));
80         } else if (selectedItemsSet.areAddedTimestampsAvailable()) {
81             long minTimestamp = selectedItemsSet.mMinAddedTimestamp;
82             long maxTimestamp = selectedItemsSet.mMaxAddedTimestamp;
83             if (selectedItemsSet.isPicassaSet()) {
84                 minTimestamp -= Gallery.CURRENT_TIME_ZONE.getOffset(minTimestamp);
85                 maxTimestamp -= Gallery.CURRENT_TIME_ZONE.getOffset(maxTimestamp);
86             }
87             strings.add(resources.getString(R.string.start) + ": " + dateTimeFormat.format(new Date(minTimestamp)));
88             strings.add(resources.getString(R.string.end) + ": " + dateTimeFormat.format(new Date(maxTimestamp)));            
89         } else {
90             strings.add(resources.getString(R.string.start) + ": " + resources.getString(R.string.date_unknown));
91             strings.add(resources.getString(R.string.end) + ": " + resources.getString(R.string.date_unknown));
92         }
93
94         // The location of the selected items.
95         String locationString = null;
96         if (selectedItemsSet.mLatLongDetermined) {
97             locationString = selectedItemsSet.mReverseGeocodedLocation;
98             if (locationString == null) {
99                 // Try computing the location if it does not exist.
100                 ReverseGeocoder reverseGeocoder = ((Gallery) context).getReverseGeocoder();
101                 locationString = reverseGeocoder.computeMostGranularCommonLocation(selectedItemsSet);
102             }
103         }
104         if (locationString != null && locationString.length() > 0) {
105                 strings.add(resources.getString(R.string.location) + ": " + locationString);
106         }
107         int numStrings = strings.size();
108         CharSequence[] stringsArr = new CharSequence[numStrings];
109         for (int i = 0; i < numStrings; ++i) {
110                 stringsArr[i] = strings.get(i);
111         }
112         return stringsArr;
113     }
114
115     private static CharSequence[] populateItemViewDetailModeStrings(Context context, MediaItem item) {
116         if (item == null) {
117             return null;
118         }
119         Resources resources = context.getResources(); 
120         CharSequence[] strings = new CharSequence[5];
121         strings[0] = resources.getString(R.string.title) + ": " + item.mCaption;
122         strings[1] = resources.getString(R.string.type) + ": " + item.getDisplayMimeType();
123         
124         DateFormat dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.SHORT);
125         
126         if (item.isDateTakenValid()) {
127             long dateTaken = item.mDateTakenInMs;
128             if (item.isPicassaItem()) {
129                 dateTaken -= Gallery.CURRENT_TIME_ZONE.getOffset(dateTaken);
130             }
131             strings[2] = resources.getString(R.string.taken_on) + ": " + dateTimeFormat.format(new Date(dateTaken));
132         } else if (item.isDateAddedValid()) {
133             long dateAdded = item.mDateAddedInSec * 1000;
134             if (item.isPicassaItem()) {
135                 dateAdded -= Gallery.CURRENT_TIME_ZONE.getOffset(dateAdded);
136             }
137             // TODO: Make this added_on as soon as translations are ready.
138             //strings[2] = resources.getString(R.string.added_on) + ": " + DateFormat.format("h:mmaa MMM dd yyyy", dateAdded);
139             strings[2] = resources.getString(R.string.taken_on) + ": " + dateTimeFormat.format(new Date(dateAdded));
140         } else {
141             strings[2] = resources.getString(R.string.taken_on) + ": " + resources.getString(R.string.date_unknown);
142         }
143         MediaSet parentMediaSet = item.mParentMediaSet;
144         if (parentMediaSet == null) {
145             strings[3] = resources.getString(R.string.album) + ":";
146         } else {
147             strings[3] = resources.getString(R.string.album) + ": " + parentMediaSet.mName;
148         }
149         ReverseGeocoder reverseGeocoder = ((Gallery) context).getReverseGeocoder();
150         String locationString = item.getReverseGeocodedLocation(reverseGeocoder);
151         if (locationString == null || locationString.length() == 0) {
152             locationString = context.getResources().getString(R.string.location_unknown);
153         }
154         strings[4] = resources.getString(R.string.location) + ": " + locationString;
155         return strings;
156     }
157 }