OSDN Git Service

merge in klp-release history after reset to klp-dev
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / ui / DialogDetailsView.java
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.ui;
18
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.content.DialogInterface.OnDismissListener;
24 import android.text.format.Formatter;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.BaseAdapter;
29 import android.widget.ListView;
30 import android.widget.TextView;
31
32 import com.android.gallery3d.R;
33 import com.android.gallery3d.app.AbstractGalleryActivity;
34 import com.android.gallery3d.common.Utils;
35 import com.android.gallery3d.data.MediaDetails;
36 import com.android.gallery3d.ui.DetailsAddressResolver.AddressResolvingListener;
37 import com.android.gallery3d.ui.DetailsHelper.CloseListener;
38 import com.android.gallery3d.ui.DetailsHelper.DetailsSource;
39 import com.android.gallery3d.ui.DetailsHelper.DetailsViewContainer;
40 import com.android.gallery3d.ui.DetailsHelper.ResolutionResolvingListener;
41
42 import java.text.DecimalFormat;
43 import java.util.ArrayList;
44 import java.util.Locale;
45 import java.util.Map.Entry;
46
47 public class DialogDetailsView implements DetailsViewContainer {
48     @SuppressWarnings("unused")
49     private static final String TAG = "DialogDetailsView";
50
51     private final AbstractGalleryActivity mActivity;
52     private DetailsAdapter mAdapter;
53     private MediaDetails mDetails;
54     private final DetailsSource mSource;
55     private int mIndex;
56     private Dialog mDialog;
57     private CloseListener mListener;
58
59     public DialogDetailsView(AbstractGalleryActivity activity, DetailsSource source) {
60         mActivity = activity;
61         mSource = source;
62     }
63
64     @Override
65     public void show() {
66         reloadDetails();
67         mDialog.show();
68     }
69
70     @Override
71     public void hide() {
72         mDialog.hide();
73     }
74
75     @Override
76     public void reloadDetails() {
77         int index = mSource.setIndex();
78         if (index == -1) return;
79         MediaDetails details = mSource.getDetails();
80         if (details != null) {
81             if (mIndex == index && mDetails == details) return;
82             mIndex = index;
83             mDetails = details;
84             setDetails(details);
85         }
86     }
87
88     private void setDetails(MediaDetails details) {
89         mAdapter = new DetailsAdapter(details);
90         String title = String.format(
91                 mActivity.getAndroidContext().getString(R.string.details_title),
92                 mIndex + 1, mSource.size());
93         ListView detailsList = (ListView) LayoutInflater.from(mActivity.getAndroidContext()).inflate(
94                 R.layout.details_list, null, false);
95         detailsList.setAdapter(mAdapter);
96         mDialog = new AlertDialog.Builder(mActivity)
97             .setView(detailsList)
98             .setTitle(title)
99             .setPositiveButton(R.string.close, new DialogInterface.OnClickListener() {
100                 @Override
101                 public void onClick(DialogInterface dialog, int whichButton) {
102                     mDialog.dismiss();
103                 }
104             })
105             .create();
106
107         mDialog.setOnDismissListener(new OnDismissListener() {
108             @Override
109             public void onDismiss(DialogInterface dialog) {
110                 if (mListener != null) {
111                     mListener.onClose();
112                 }
113             }
114         });
115     }
116
117
118     private class DetailsAdapter extends BaseAdapter
119         implements AddressResolvingListener, ResolutionResolvingListener {
120         private final ArrayList<String> mItems;
121         private int mLocationIndex;
122         private final Locale mDefaultLocale = Locale.getDefault();
123         private final DecimalFormat mDecimalFormat = new DecimalFormat(".####");
124         private int mWidthIndex = -1;
125         private int mHeightIndex = -1;
126
127         public DetailsAdapter(MediaDetails details) {
128             Context context = mActivity.getAndroidContext();
129             mItems = new ArrayList<String>(details.size());
130             mLocationIndex = -1;
131             setDetails(context, details);
132         }
133
134         private void setDetails(Context context, MediaDetails details) {
135             boolean resolutionIsValid = true;
136             String path = null;
137             for (Entry<Integer, Object> detail : details) {
138                 String value;
139                 switch (detail.getKey()) {
140                     case MediaDetails.INDEX_LOCATION: {
141                         double[] latlng = (double[]) detail.getValue();
142                         mLocationIndex = mItems.size();
143                         value = DetailsHelper.resolveAddress(mActivity, latlng, this);
144                         break;
145                     }
146                     case MediaDetails.INDEX_SIZE: {
147                         value = Formatter.formatFileSize(
148                                 context, (Long) detail.getValue());
149                         break;
150                     }
151                     case MediaDetails.INDEX_WHITE_BALANCE: {
152                         value = "1".equals(detail.getValue())
153                                 ? context.getString(R.string.manual)
154                                 : context.getString(R.string.auto);
155                         break;
156                     }
157                     case MediaDetails.INDEX_FLASH: {
158                         MediaDetails.FlashState flash =
159                                 (MediaDetails.FlashState) detail.getValue();
160                         // TODO: camera doesn't fill in the complete values, show more information
161                         // when it is fixed.
162                         if (flash.isFlashFired()) {
163                             value = context.getString(R.string.flash_on);
164                         } else {
165                             value = context.getString(R.string.flash_off);
166                         }
167                         break;
168                     }
169                     case MediaDetails.INDEX_EXPOSURE_TIME: {
170                         value = (String) detail.getValue();
171                         double time = Double.valueOf(value);
172                         if (time < 1.0f) {
173                             value = String.format(mDefaultLocale, "%d/%d", 1,
174                                     (int) (0.5f + 1 / time));
175                         } else {
176                             int integer = (int) time;
177                             time -= integer;
178                             value = String.valueOf(integer) + "''";
179                             if (time > 0.0001) {
180                                 value += String.format(mDefaultLocale, " %d/%d", 1,
181                                         (int) (0.5f + 1 / time));
182                             }
183                         }
184                         break;
185                     }
186                     case MediaDetails.INDEX_WIDTH:
187                         mWidthIndex = mItems.size();
188                         value = detail.getValue().toString();
189                         try {
190                             value = toLocalNumber(Integer.parseInt(value));
191                         } catch (NumberFormatException ex) {
192                             // Just keep the current "value" if we cannot parse
193                             // it as a fallback.
194                         }
195                         if (value.equalsIgnoreCase("0")) {
196                             value = context.getString(R.string.unknown);
197                             resolutionIsValid = false;
198                         }
199                         break;
200                     case MediaDetails.INDEX_HEIGHT: {
201                         mHeightIndex = mItems.size();
202                         value = detail.getValue().toString();
203                         try {
204                             value = toLocalNumber(Integer.parseInt(value));
205                         } catch (NumberFormatException ex) {
206                             // Just keep the current "value" if we cannot parse
207                             // it as a fallback.
208                         }
209                         if (value.equalsIgnoreCase("0")) {
210                             value = context.getString(R.string.unknown);
211                             resolutionIsValid = false;
212                         }
213                         break;
214                     }
215                     case MediaDetails.INDEX_PATH:
216                         // Prepend the new-line as a) paths are usually long, so
217                         // the formatting is better and b) an RTL UI will see it
218                         // as a separate section and interpret it for what it
219                         // is, rather than trying to make it RTL (which messes
220                         // up the path).
221                         value = "\n" + detail.getValue().toString();
222                         path = detail.getValue().toString();
223                         break;
224                     case MediaDetails.INDEX_ISO:
225                         value = toLocalNumber(Integer.parseInt((String) detail.getValue()));
226                         break;
227                     case MediaDetails.INDEX_FOCAL_LENGTH:
228                         double focalLength = Double.parseDouble(detail.getValue().toString());
229                         value = toLocalNumber(focalLength);
230                         break;
231                     default: {
232                         Object valueObj = detail.getValue();
233                         // This shouldn't happen, log its key to help us diagnose the problem.
234                         if (valueObj == null) {
235                             Utils.fail("%s's value is Null",
236                                     DetailsHelper.getDetailsName(context, detail.getKey()));
237                         }
238                         value = valueObj.toString();
239                     }
240                 }
241                 int key = detail.getKey();
242                 if (details.hasUnit(key)) {
243                     value = String.format("%s: %s %s", DetailsHelper.getDetailsName(
244                             context, key), value, context.getString(details.getUnit(key)));
245                 } else {
246                     value = String.format("%s: %s", DetailsHelper.getDetailsName(
247                             context, key), value);
248                 }
249                 mItems.add(value);
250             }
251             if (!resolutionIsValid) {
252                 DetailsHelper.resolveResolution(path, this);
253             }
254         }
255
256         @Override
257         public boolean areAllItemsEnabled() {
258             return false;
259         }
260
261         @Override
262         public boolean isEnabled(int position) {
263             return false;
264         }
265
266         @Override
267         public int getCount() {
268             return mItems.size();
269         }
270
271         @Override
272         public Object getItem(int position) {
273             return mDetails.getDetail(position);
274         }
275
276         @Override
277         public long getItemId(int position) {
278             return position;
279         }
280
281         @Override
282         public View getView(int position, View convertView, ViewGroup parent) {
283             TextView tv;
284             if (convertView == null) {
285                 tv = (TextView) LayoutInflater.from(mActivity.getAndroidContext()).inflate(
286                         R.layout.details, parent, false);
287             } else {
288                 tv = (TextView) convertView;
289             }
290             tv.setText(mItems.get(position));
291             return tv;
292         }
293
294         @Override
295         public void onAddressAvailable(String address) {
296             mItems.set(mLocationIndex, address);
297             notifyDataSetChanged();
298         }
299
300         @Override
301         public void onResolutionAvailable(int width, int height) {
302             if (width == 0 || height == 0) return;
303             // Update the resolution with the new width and height
304             Context context = mActivity.getAndroidContext();
305             String widthString = String.format(mDefaultLocale, "%s: %d",
306                     DetailsHelper.getDetailsName(
307                             context, MediaDetails.INDEX_WIDTH), width);
308             String heightString = String.format(mDefaultLocale, "%s: %d",
309                     DetailsHelper.getDetailsName(
310                             context, MediaDetails.INDEX_HEIGHT), height);
311             mItems.set(mWidthIndex, String.valueOf(widthString));
312             mItems.set(mHeightIndex, String.valueOf(heightString));
313             notifyDataSetChanged();
314         }
315
316         /** Converts the given integer to a localized String version. */
317         private String toLocalNumber(int n) {
318             return String.format(mDefaultLocale, "%d", n);
319         }
320
321         /** Converts the given double to a localized String version. */
322         private String toLocalNumber(double n) {
323             return mDecimalFormat.format(n);
324         }
325     }
326
327     @Override
328     public void setCloseListener(CloseListener listener) {
329         mListener = listener;
330     }
331 }