2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.ui;
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;
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;
42 import java.text.DecimalFormat;
43 import java.util.ArrayList;
44 import java.util.Locale;
45 import java.util.Map.Entry;
47 public class DialogDetailsView implements DetailsViewContainer {
48 @SuppressWarnings("unused")
49 private static final String TAG = "DialogDetailsView";
51 private final AbstractGalleryActivity mActivity;
52 private DetailsAdapter mAdapter;
53 private MediaDetails mDetails;
54 private final DetailsSource mSource;
56 private Dialog mDialog;
57 private CloseListener mListener;
59 public DialogDetailsView(AbstractGalleryActivity activity, DetailsSource source) {
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;
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)
99 .setPositiveButton(R.string.close, new DialogInterface.OnClickListener() {
101 public void onClick(DialogInterface dialog, int whichButton) {
107 mDialog.setOnDismissListener(new OnDismissListener() {
109 public void onDismiss(DialogInterface dialog) {
110 if (mListener != null) {
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;
127 public DetailsAdapter(MediaDetails details) {
128 Context context = mActivity.getAndroidContext();
129 mItems = new ArrayList<String>(details.size());
131 setDetails(context, details);
134 private void setDetails(Context context, MediaDetails details) {
135 boolean resolutionIsValid = true;
137 for (Entry<Integer, Object> detail : details) {
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);
146 case MediaDetails.INDEX_SIZE: {
147 value = Formatter.formatFileSize(
148 context, (Long) detail.getValue());
151 case MediaDetails.INDEX_WHITE_BALANCE: {
152 value = "1".equals(detail.getValue())
153 ? context.getString(R.string.manual)
154 : context.getString(R.string.auto);
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
162 if (flash.isFlashFired()) {
163 value = context.getString(R.string.flash_on);
165 value = context.getString(R.string.flash_off);
169 case MediaDetails.INDEX_EXPOSURE_TIME: {
170 value = (String) detail.getValue();
171 double time = Double.valueOf(value);
173 value = String.format(mDefaultLocale, "%d/%d", 1,
174 (int) (0.5f + 1 / time));
176 int integer = (int) time;
178 value = String.valueOf(integer) + "''";
180 value += String.format(mDefaultLocale, " %d/%d", 1,
181 (int) (0.5f + 1 / time));
186 case MediaDetails.INDEX_WIDTH:
187 mWidthIndex = mItems.size();
188 value = detail.getValue().toString();
190 value = toLocalNumber(Integer.parseInt(value));
191 } catch (NumberFormatException ex) {
192 // Just keep the current "value" if we cannot parse
195 if (value.equalsIgnoreCase("0")) {
196 value = context.getString(R.string.unknown);
197 resolutionIsValid = false;
200 case MediaDetails.INDEX_HEIGHT: {
201 mHeightIndex = mItems.size();
202 value = detail.getValue().toString();
204 value = toLocalNumber(Integer.parseInt(value));
205 } catch (NumberFormatException ex) {
206 // Just keep the current "value" if we cannot parse
209 if (value.equalsIgnoreCase("0")) {
210 value = context.getString(R.string.unknown);
211 resolutionIsValid = false;
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
221 value = "\n" + detail.getValue().toString();
222 path = detail.getValue().toString();
224 case MediaDetails.INDEX_ISO:
225 value = toLocalNumber(Integer.parseInt((String) detail.getValue()));
227 case MediaDetails.INDEX_FOCAL_LENGTH:
228 double focalLength = Double.parseDouble(detail.getValue().toString());
229 value = toLocalNumber(focalLength);
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()));
238 value = valueObj.toString();
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)));
246 value = String.format("%s: %s", DetailsHelper.getDetailsName(
247 context, key), value);
251 if (!resolutionIsValid) {
252 DetailsHelper.resolveResolution(path, this);
257 public boolean areAllItemsEnabled() {
262 public boolean isEnabled(int position) {
267 public int getCount() {
268 return mItems.size();
272 public Object getItem(int position) {
273 return mDetails.getDetail(position);
277 public long getItemId(int position) {
282 public View getView(int position, View convertView, ViewGroup parent) {
284 if (convertView == null) {
285 tv = (TextView) LayoutInflater.from(mActivity.getAndroidContext()).inflate(
286 R.layout.details, parent, false);
288 tv = (TextView) convertView;
290 tv.setText(mItems.get(position));
295 public void onAddressAvailable(String address) {
296 mItems.set(mLocationIndex, address);
297 notifyDataSetChanged();
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();
316 /** Converts the given integer to a localized String version. */
317 private String toLocalNumber(int n) {
318 return String.format(mDefaultLocale, "%d", n);
321 /** Converts the given double to a localized String version. */
322 private String toLocalNumber(double n) {
323 return mDecimalFormat.format(n);
328 public void setCloseListener(CloseListener listener) {
329 mListener = listener;