Revision | 33c43ca8019d3de907239f940664cd0866c55220 (tree) |
---|---|
Time | 2014-06-02 05:06:27 |
Author | Masahiko, SAWAI <say@user...> |
Commiter | Masahiko, SAWAI |
Changed date and datetime format
@@ -18,7 +18,7 @@ | ||
18 | 18 | <string name="preferences_view_category_title">表示</string> |
19 | 19 | <string name="date_format">日付フォーマット</string> |
20 | 20 | <string name="date_format_title">日付フォーマット</string> |
21 | - <string name="date_format_row">書式なし</string> | |
21 | + <string name="date_format_raw">書式なし</string> | |
22 | 22 | <string name="date_format_system">システム書式</string> |
23 | 23 | |
24 | 24 | <string name="preferences_csv_category_title">CSV</string> |
@@ -1,11 +1,11 @@ | ||
1 | 1 | <?xml version="1.0" encoding="utf-8"?> |
2 | 2 | <resources> |
3 | 3 | <string-array name="date_format_entries" > |
4 | - <item>@string/date_format_row</item> | |
4 | + <item>@string/date_format_raw</item> | |
5 | 5 | <item>@string/date_format_system</item> |
6 | 6 | </string-array> |
7 | 7 | <string-array name="date_format_values" > |
8 | - <item>DATE_FORMAT_ROW</item> | |
9 | - <item>DATE_FORMAT_SYSTEM</item> | |
8 | + <item>@string/date_format_value_raw</item> | |
9 | + <item>@string/date_format_value_system</item> | |
10 | 10 | </string-array> |
11 | 11 | </resources> |
@@ -11,7 +11,9 @@ | ||
11 | 11 | <string name="data_keeping_period_default_value">30</string> |
12 | 12 | |
13 | 13 | <string name="date_format_key">date_format</string> |
14 | - <string name="date_format_default_value">DATE_FORMAT_ROW</string> | |
14 | + <string name="date_format_default_value">@string/date_format_value_raw</string> | |
15 | + <string name="date_format_value_raw">DATE_FORMAT_RAW</string> | |
16 | + <string name="date_format_value_system">DATE_FORMAT_SYSTEM</string> | |
15 | 17 | |
16 | 18 | <string name="csv_auto_export_enabled_key">csv_auto_export_enabled</string> |
17 | 19 | <bool name="csv_auto_export_enabled_default_value">true</bool> |
@@ -18,7 +18,7 @@ | ||
18 | 18 | <string name="preferences_view_category_title">View</string> |
19 | 19 | <string name="date_format">Date Format</string> |
20 | 20 | <string name="date_format_title">Date Format</string> |
21 | - <string name="date_format_row">Row Format</string> | |
21 | + <string name="date_format_raw">Raw Format</string> | |
22 | 22 | <string name="date_format_system">System Format</string> |
23 | 23 | |
24 | 24 | <string name="preferences_csv_category_title">CSV</string> |
@@ -24,8 +24,15 @@ | ||
24 | 24 | package org.routine_work.simple_battery_logger; |
25 | 25 | |
26 | 26 | import android.content.Context; |
27 | +import android.text.format.DateFormat; | |
28 | +import android.util.Log; | |
29 | +import java.text.ParseException; | |
30 | +import java.text.SimpleDateFormat; | |
31 | +import java.util.Date; | |
27 | 32 | import java.util.HashMap; |
28 | 33 | import java.util.Map; |
34 | +import org.routine_work.simple_battery_logger.db.DBConstants; | |
35 | +import org.routine_work.simple_battery_logger.util.PreferenceUtils; | |
29 | 36 | |
30 | 37 | /** |
31 | 38 | * |
@@ -34,6 +41,7 @@ import java.util.Map; | ||
34 | 41 | public class BatteryUtils |
35 | 42 | { |
36 | 43 | |
44 | + private static final String LOG_TAG = "simple-battery-logger"; | |
37 | 45 | private static final Map<Integer, Integer> STATUS_CODE_AND_RESOURCE_MAP = new HashMap<Integer, Integer>(); |
38 | 46 | private static final Map<Integer, Integer> HEALTH_CODE_AND_RESOURCE_MAP = new HashMap<Integer, Integer>(); |
39 | 47 | private static final Map<Integer, Integer> PLUGGED_CODE_AND_RESOURCE_MAP = new HashMap<Integer, Integer>(); |
@@ -110,4 +118,63 @@ public class BatteryUtils | ||
110 | 118 | } |
111 | 119 | return pluggedText; |
112 | 120 | } |
121 | + | |
122 | + public static String getFormattedDate(Context context, String rawDateString) | |
123 | + { | |
124 | + String formattedDate = null; | |
125 | + | |
126 | + String dateFormatValueSystem = context.getString(R.string.date_format_value_system); | |
127 | + if (dateFormatValueSystem.equals(PreferenceUtils.getDateFormat(context))) | |
128 | + { | |
129 | + SimpleDateFormat rawDateFormat = new SimpleDateFormat(DBConstants.DATE_FORMAT); | |
130 | + try | |
131 | + { | |
132 | + Date datetime = rawDateFormat.parse(rawDateString); | |
133 | + | |
134 | + java.text.DateFormat dateFormat = DateFormat.getLongDateFormat(context); | |
135 | + formattedDate = dateFormat.format(datetime); | |
136 | + } | |
137 | + catch (ParseException ex) | |
138 | + { | |
139 | + Log.e(LOG_TAG, "Parse date failed.", ex); | |
140 | + } | |
141 | + } | |
142 | + | |
143 | + if (formattedDate == null) | |
144 | + { | |
145 | + formattedDate = rawDateString; | |
146 | + } | |
147 | + | |
148 | + return formattedDate; | |
149 | + } | |
150 | + | |
151 | + public static String getFormattedDatetime(Context context, String rawDatetimeString) | |
152 | + { | |
153 | + String formattedDatetime = null; | |
154 | + | |
155 | + String dateFormatValueSystem = context.getString(R.string.date_format_value_system); | |
156 | + if (dateFormatValueSystem.equals(PreferenceUtils.getDateFormat(context))) | |
157 | + { | |
158 | + SimpleDateFormat rawDatetimeFormat = new SimpleDateFormat(DBConstants.DATETIME_FORMAT); | |
159 | + try | |
160 | + { | |
161 | + Date datetime = rawDatetimeFormat.parse(rawDatetimeString); | |
162 | + | |
163 | + java.text.DateFormat dateFormat = DateFormat.getDateFormat(context); | |
164 | + java.text.DateFormat timeFormat = DateFormat.getTimeFormat(context); | |
165 | + formattedDatetime = dateFormat.format(datetime) + " " + timeFormat.format(datetime); | |
166 | + } | |
167 | + catch (ParseException ex) | |
168 | + { | |
169 | + Log.e(LOG_TAG, "Parse datetime failed.", ex); | |
170 | + } | |
171 | + } | |
172 | + | |
173 | + if (formattedDatetime == null) | |
174 | + { | |
175 | + formattedDatetime = rawDatetimeString; | |
176 | + } | |
177 | + | |
178 | + return formattedDatetime; | |
179 | + } | |
113 | 180 | } |
@@ -32,6 +32,7 @@ import android.content.SharedPreferences; | ||
32 | 32 | import android.database.Cursor; |
33 | 33 | import android.database.sqlite.SQLiteDatabase; |
34 | 34 | import android.os.Bundle; |
35 | +import android.text.TextUtils; | |
35 | 36 | import android.view.ContextMenu; |
36 | 37 | import android.view.ContextMenu.ContextMenuInfo; |
37 | 38 | import android.view.Menu; |
@@ -54,18 +55,20 @@ import org.routine_work.util.SystemResourceUtils; | ||
54 | 55 | |
55 | 56 | public class DatabaseDateListActivity extends ListActivity |
56 | 57 | implements SharedPreferences.OnSharedPreferenceChangeListener, |
58 | + SimpleCursorAdapter.ViewBinder, | |
57 | 59 | OnItemClickListener, DBConstants |
58 | 60 | { |
59 | 61 | |
60 | - private static final int REQUEST_CODE_BATTERY_HISTORY_LIST_ACTIVITY = 100; | |
61 | 62 | private static final String LOG_TAG = "simple-battery-logger"; |
62 | 63 | private static final String[] MAPPING_FROM = new String[] |
63 | 64 | { |
64 | 65 | BatteryHistoryDate.Columns.DATE, |
66 | + BatteryHistoryDate.Columns.DATE, | |
65 | 67 | }; |
66 | 68 | private static final int[] MAPPING_TO = new int[] |
67 | 69 | { |
68 | - android.R.id.text1 | |
70 | + android.R.id.text1, // for formatted date text | |
71 | + android.R.id.text2 // for raw date text | |
69 | 72 | }; |
70 | 73 | private BatteryHistoryDBHelper dbHelper; |
71 | 74 | private SQLiteDatabase db; |
@@ -155,15 +158,17 @@ public class DatabaseDateListActivity extends ListActivity | ||
155 | 158 | public void onItemClick(AdapterView<?> parentView, View itemView, int position, long id) |
156 | 159 | { |
157 | 160 | Log.v(LOG_TAG, "Hello"); |
158 | - if (itemView instanceof TextView) | |
161 | + View text2View = itemView.findViewById(android.R.id.text2); | |
162 | + if (text2View instanceof TextView) | |
159 | 163 | { |
160 | - TextView dateTextView = (TextView) itemView; | |
161 | - String dateTextString = dateTextView.getText().toString(); | |
162 | - Log.d(LOG_TAG, "clicked date text => " + dateTextString); | |
163 | - if (dateTextString != null && dateTextString.length() > 0) | |
164 | + TextView dateTextView = (TextView) text2View; | |
165 | + String rawDateText = dateTextView.getText().toString(); | |
166 | + Log.d(LOG_TAG, "clicked date text => " + rawDateText); | |
167 | + | |
168 | + if (TextUtils.isEmpty(rawDateText) == false) | |
164 | 169 | { |
165 | 170 | Intent intent = new Intent(this, DatabaseDateViewerActivity.class); |
166 | - intent.putExtra(DatabaseDateViewerActivity.EXTRA_DATE, dateTextString); | |
171 | + intent.putExtra(DatabaseDateViewerActivity.EXTRA_DATE, rawDateText); | |
167 | 172 | startActivity(intent); |
168 | 173 | } |
169 | 174 | } |
@@ -244,11 +249,12 @@ public class DatabaseDateListActivity extends ListActivity | ||
244 | 249 | switch (itemId) |
245 | 250 | { |
246 | 251 | case R.id.export_csv_menuitem: |
247 | - if (menuInfo.targetView instanceof TextView) | |
252 | + View text2View = menuInfo.targetView.findViewById(android.R.id.text2); | |
253 | + if (text2View instanceof TextView) | |
248 | 254 | { |
249 | - TextView textView = (TextView) menuInfo.targetView; | |
250 | - String dateText = textView.getText().toString(); | |
251 | - exportCsvFile(dateText); | |
255 | + TextView textView = (TextView) text2View; | |
256 | + String rawDateText = textView.getText().toString(); | |
257 | + exportCsvFile(rawDateText); | |
252 | 258 | } |
253 | 259 | result = true; |
254 | 260 | break; |
@@ -260,6 +266,36 @@ public class DatabaseDateListActivity extends ListActivity | ||
260 | 266 | return result; |
261 | 267 | } |
262 | 268 | |
269 | + @Override | |
270 | + public boolean setViewValue(View view, Cursor cursor, int columnIndex) | |
271 | + { | |
272 | + | |
273 | + String textValue = null; | |
274 | + if (columnIndex == cursor.getColumnIndex(BatteryHistoryDate.Columns.DATE)) | |
275 | + { | |
276 | + if (android.R.id.text1 == view.getId()) | |
277 | + { | |
278 | + String rawDateString = cursor.getString(columnIndex); | |
279 | + textValue = BatteryUtils.getFormattedDate(this, rawDateString); | |
280 | + } | |
281 | + } | |
282 | + | |
283 | + if (textValue == null) | |
284 | + { | |
285 | + textValue = cursor.getString(columnIndex); | |
286 | + } | |
287 | + | |
288 | + TextView textView = (TextView) view; | |
289 | + textView.setText(textValue); | |
290 | + | |
291 | +// if (android.R.id.text2 == view.getId()) | |
292 | +// { | |
293 | +// view.setVisibility(View.GONE); | |
294 | +// } | |
295 | + | |
296 | + return true; | |
297 | + } | |
298 | + | |
263 | 299 | private void initializeListData() |
264 | 300 | { |
265 | 301 | Log.v(LOG_TAG, "Hello"); |
@@ -273,9 +309,11 @@ public class DatabaseDateListActivity extends ListActivity | ||
273 | 309 | cursor = dbHelper.getBatteryHistoryDateListCursor(db); |
274 | 310 | |
275 | 311 | adapter = new SimpleCursorAdapter(this, |
276 | - android.R.layout.simple_list_item_1, | |
312 | + android.R.layout.simple_list_item_2, | |
277 | 313 | cursor, MAPPING_FROM, MAPPING_TO); |
314 | + adapter.setViewBinder(this); | |
278 | 315 | setListAdapter(adapter); |
316 | + | |
279 | 317 | Log.v(LOG_TAG, "Bye"); |
280 | 318 | } |
281 | 319 |
@@ -308,15 +346,15 @@ public class DatabaseDateListActivity extends ListActivity | ||
308 | 346 | Log.v(LOG_TAG, " Bye"); |
309 | 347 | } |
310 | 348 | |
311 | - private void exportCsvFile(String dateText) | |
349 | + private void exportCsvFile(String rawDateText) | |
312 | 350 | { |
313 | 351 | Log.v(LOG_TAG, "Hello"); |
352 | + Log.d(LOG_TAG, "rawDateText => " + rawDateText); | |
314 | 353 | |
315 | - Log.d(LOG_TAG, "dateText => " + dateText); | |
316 | - if (dateText != null) | |
354 | + if (TextUtils.isEmpty(rawDateText) == false) | |
317 | 355 | { |
318 | 356 | Intent intent = new Intent(this, ExportOneDayDataCsvFileService.class); |
319 | - intent.putExtra(ExportOneDayDataCsvFileService.EXTRA_DATE, dateText); | |
357 | + intent.putExtra(ExportOneDayDataCsvFileService.EXTRA_DATE, rawDateText); | |
320 | 358 | startService(intent); |
321 | 359 | } |
322 | 360 |
@@ -199,7 +199,7 @@ public class DatabaseDateViewerActivity extends ListActivity | ||
199 | 199 | public boolean setViewValue(View view, Cursor cursor, int columnIndex) |
200 | 200 | { |
201 | 201 | |
202 | - String textValue = "?"; | |
202 | + String textValue; | |
203 | 203 | if (columnIndex == cursor.getColumnIndex(BatteryHistory.Columns.STATUS)) |
204 | 204 | { |
205 | 205 | int statusCode = cursor.getInt(columnIndex); |
@@ -210,6 +210,11 @@ public class DatabaseDateViewerActivity extends ListActivity | ||
210 | 210 | int pluggedCode = cursor.getInt(columnIndex); |
211 | 211 | textValue = BatteryUtils.getPluggedText(this, pluggedCode); |
212 | 212 | } |
213 | + else if (columnIndex == cursor.getColumnIndex(BatteryHistory.Columns.DATETIME)) | |
214 | + { | |
215 | + String datetimeString = cursor.getString(columnIndex); | |
216 | + textValue = BatteryUtils.getFormattedDatetime(this, datetimeString); | |
217 | + } | |
213 | 218 | else if (columnIndex == cursor.getColumnIndex(BatteryHistory.Columns.TEMPERATURE)) |
214 | 219 | { |
215 | 220 | int temperature = cursor.getInt(columnIndex); |
@@ -243,6 +248,7 @@ public class DatabaseDateViewerActivity extends ListActivity | ||
243 | 248 | R.layout.battery_history_list_item, cursor, MAPPING_FROM, MAPPING_TO); |
244 | 249 | adapter.setViewBinder(this); |
245 | 250 | setListAdapter(adapter); |
251 | + | |
246 | 252 | Log.v(LOG_TAG, "Bye"); |
247 | 253 | } |
248 | 254 |