OSDN Git Service

記録データのタイトル編集を少し変更。機能的には一揃い。
[gokigen/JoggingTimer.git] / wear / src / main / java / net / osdn / gokigen / joggingtimer / recorddetail / RecordDetailSetup.java
1 package net.osdn.gokigen.joggingtimer.recorddetail;
2
3 import android.content.Intent;
4 import android.database.Cursor;
5 import android.support.annotation.NonNull;
6 import android.support.wearable.activity.WearableActivity;
7 import android.util.Log;
8
9 import net.osdn.gokigen.joggingtimer.R;
10 import net.osdn.gokigen.joggingtimer.storage.ITimeEntryDatabase;
11 import net.osdn.gokigen.joggingtimer.storage.ITimeEntryDatabaseCallback;
12 import net.osdn.gokigen.joggingtimer.storage.TimeEntryDatabaseFactory;
13 import net.osdn.gokigen.joggingtimer.storage.contract.TimeEntryData;
14 import net.osdn.gokigen.joggingtimer.storage.contract.TimeEntryIndex;
15 import net.osdn.gokigen.joggingtimer.utilities.CreateModelData;
16 import net.osdn.gokigen.joggingtimer.utilities.CreateModelDataDialog;
17 import net.osdn.gokigen.joggingtimer.utilities.TimeStringConvert;
18
19 import static android.provider.BaseColumns._ID;
20 import static net.osdn.gokigen.joggingtimer.storage.contract.TimeEntryIndex.EntryIndex.COLUMN_NAME_MEMO;
21 import static net.osdn.gokigen.joggingtimer.storage.contract.TimeEntryIndex.EntryIndex.COLUMN_NAME_START_TIME;
22 import static net.osdn.gokigen.joggingtimer.storage.contract.TimeEntryIndex.EntryIndex.COLUMN_NAME_TIME_DURATION;
23 import static net.osdn.gokigen.joggingtimer.storage.contract.TimeEntryIndex.EntryIndex.COLUMN_NAME_TITLE;
24
25 /**
26  *
27  *
28  */
29 public class RecordDetailSetup  implements ITimeEntryDatabaseCallback, IDetailEditor
30 {
31     private final String TAG = toString();
32     private final WearableActivity activity;
33     private final long indexId;
34     private final IDatabaseReadyNotify callback;
35     private final IRecordOperation operation;
36     private final CreateModelData.IEditedModelDataCallback editCallback;
37     private ITimeEntryDatabase database = null;
38
39     /**
40      *
41      *
42      */
43     RecordDetailSetup(WearableActivity activity, long indexId, IDatabaseReadyNotify callback, IRecordOperation operation, CreateModelData.IEditedModelDataCallback  editCallback)
44     {
45         this.activity = activity;
46         this.indexId = indexId;
47         this.callback = callback;
48         this.operation = operation;
49         this.editCallback = editCallback;
50     }
51
52     /**
53      *
54      *
55      */
56     void setup()
57     {
58         Log.v(TAG, "setup()");
59         database = new TimeEntryDatabaseFactory(activity, this).getEntryDatabase();
60         Thread thread = new Thread(new Runnable() {
61             @Override
62             public void run()
63             {
64                 try
65                 {
66                     database.prepare();
67                 }
68                 catch (Exception e)
69                 {
70                     e.printStackTrace();
71                 }
72             }
73         });
74         thread.start();
75     }
76
77     /**
78      *
79      *
80      */
81     @Override
82     public void prepareFinished(boolean isReady)
83     {
84         if (!isReady)
85         {
86             callback.databaseSetupFinished(false);
87             return;
88         }
89         final IDetailEditor editor = this;
90         Thread thread = new Thread(new Runnable() {
91             @Override
92             public void run()
93             {
94                 boolean ret = false;
95                 try
96                 {
97                     operation.clearRecord();
98                     Cursor cursor = database.getAllDetailData(indexId);
99                     int index = 0;
100                     long startTime = 0;
101                     long previousLapTime = 0;
102                     long morePreviousTime = 0;
103                     while (cursor.moveToNext())
104                     {
105                         long dataId = cursor.getLong(cursor.getColumnIndex(_ID));
106                         long indexId = cursor.getLong(cursor.getColumnIndex(TimeEntryData.EntryData.COLUMN_NAME_INDEX_ID));
107                         long entryTime = cursor.getLong(cursor.getColumnIndex(TimeEntryData.EntryData.COLUMN_NAME_TIME_ENTRY));
108                         int recordType = cursor.getInt(cursor.getColumnIndex(TimeEntryData.EntryData.COLUMN_NAME_RECORD_TYPE));
109
110                         if (index == 0)
111                         {
112                             // first record
113                             startTime = entryTime;
114                             previousLapTime = entryTime;
115                             morePreviousTime = entryTime;
116                         }
117                         else
118                         {
119                             long lapTime = entryTime - previousLapTime;
120                             long overallTime = entryTime - startTime;
121                             long differenceTime = (lapTime) - (previousLapTime - morePreviousTime);
122                             operation.addRecord(new DetailRecord(indexId, dataId, recordType, index, lapTime, overallTime, differenceTime,  editor));
123                             morePreviousTime = previousLapTime;
124                             previousLapTime = entryTime;
125                         }
126                         index++;
127                     }
128                     activity.runOnUiThread(new Runnable()
129                     {
130                         @Override
131                         public void run()
132                         {
133                             operation.dataSetChangeFinished();
134                         }
135                     });
136                     ret = true;
137                 }
138                 catch (Exception e)
139                 {
140                     e.printStackTrace();
141                 }
142                 callback.databaseSetupFinished(ret);
143             }
144         });
145         thread.start();
146     }
147
148     /**
149      *
150      *
151      */
152     void setEditIndexData(@NonNull final String title, final int icon)
153     {
154         final EditIndexData data = new EditIndexData(title, icon);
155         Thread thread = new Thread(new Runnable() {
156             @Override
157             public void run() {
158                 database.updateIndexData(indexId, data.getTitle(), data.getIcon());
159                 callback.updatedIndexData(false);
160             }
161         });
162         thread.start();
163     }
164
165     /**
166      *
167      *
168      */
169     EditIndexData getEditIndexData()
170     {
171         String title = "";
172         int iconId = R.drawable.ic_android_black_24dp;
173         try
174         {
175             Cursor cursor = database.getIndexdata(indexId);
176             while (cursor.moveToNext())
177             {
178                 title = cursor.getString(cursor.getColumnIndex(TimeEntryIndex.EntryIndex.COLUMN_NAME_TITLE));
179                 iconId = cursor.getInt(cursor.getColumnIndex(TimeEntryIndex.EntryIndex.COLUMN_NAME_ICON_ID));
180             }
181             return (new EditIndexData(title, iconId));
182         }
183         catch (Exception e)
184         {
185             e.printStackTrace();
186         }
187         return (null);
188     }
189
190     /**
191      *
192      *
193      */
194     void setReferenceData()
195     {
196         Thread thread = new Thread(new Runnable() {
197             @Override
198             public void run() {
199                 database.setReferenceIndexData(indexId);
200                 callback.updatedIndexData(true);
201             }
202         });
203         thread.start();
204     }
205
206     /**
207      *
208      *
209      */
210     @Override
211     public void dataEntryFinished(OperationType operationType, boolean result, long id, String title)
212     {
213          //
214     }
215
216     /**
217      *
218      *
219      */
220     @Override
221     public void timeEntryFinished(OperationType operationType, boolean result, long indexId, long dataId)
222     {
223         //
224     }
225
226     @Override
227     public void modelDataEntryFinished(OperationType operationType, boolean result, long indexId, String title)
228     {
229         //
230         Log.v(TAG, "modelDataEntryFinished : " + result + " " + title + " " + indexId);
231     }
232
233     /**
234      *
235      */
236     void closeDatabase()
237     {
238         try
239         {
240             database.close();
241         }
242         catch (Exception e)
243         {
244             e.printStackTrace();
245         }
246     }
247
248     /**
249      *    IDetailEditor.editDetailData()
250      */
251     @Override
252     public void editDetailData(final long indexId, final long dataId, final int count, final long defaultMillis)
253     {
254         activity.runOnUiThread(new Runnable()
255         {
256             @Override
257             public void run()
258             {
259                 CreateModelDataDialog dialog2 = new CreateModelDataDialog(activity);
260                 dialog2.show(false, activity.getString(R.string.information_modify_time), count, new CreateModelData(database, editCallback, null, indexId, dataId), defaultMillis);
261             }
262         });
263     }
264
265     /**
266      *
267      */
268     public void updateDatabaseRecord(@NonNull RecordDetailAdapter adapter)
269     {
270         try
271         {
272             int count = adapter.getItemCount();
273             if (count > 1)
274             {
275                 for (int index = 0; index < count; index++)
276                 {
277                     DetailRecord record = adapter.getRecord(index);
278                     long id = record.getDataId();
279                     long time = record.getTotalTime();
280                     database.updateTimeEntryData(id, time);
281                 }
282             }
283         }
284         catch (Exception e)
285         {
286             e.printStackTrace();
287         }
288     }
289
290     /**
291      *   現在のデータを共有する
292      *
293      */
294     public void shareTheData(final RecordDetailAdapter adapter)
295     {
296         Log.v(TAG, "shareTheData()");
297         if ((adapter == null) || (adapter.getItemCount() <= 0) || (activity == null))
298         {
299             // データがない場合は、何もしない
300             return;
301         }
302         shareDetailIntent(adapter);
303     }
304
305
306     /**
307      *
308      *
309      */
310     private void shareDetailIntent(RecordDetailAdapter adapter)
311     {
312         String title = "";
313         int dataCount = adapter.getItemCount();
314         StringBuilder dataToExport = new StringBuilder("");
315         dataToExport.append("; ");
316         dataToExport.append(activity.getString(R.string.app_name));
317         dataToExport.append("\r\n");
318         try
319         {
320             Cursor cursor = database.getIndexdata(indexId);
321             while (cursor.moveToNext())
322             {
323                 dataToExport.append("; ");
324                 title = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_TITLE));
325                 dataToExport.append(title);
326                 dataToExport.append(",");
327                 dataToExport.append(TimeStringConvert.getTimeString(cursor.getLong(cursor.getColumnIndex(COLUMN_NAME_TIME_DURATION))));
328                 dataToExport.append(",");
329                 dataToExport.append(cursor.getLong(cursor.getColumnIndex(COLUMN_NAME_START_TIME)));
330                 dataToExport.append(",");
331                 dataToExport.append(cursor.getString(cursor.getColumnIndex(COLUMN_NAME_MEMO)));
332                 dataToExport.append(",");
333                 dataToExport.append("\r\n");
334             }
335         }
336         catch (Exception e)
337         {
338             e.printStackTrace();
339             dataToExport.append("\r\n");
340         }
341         dataToExport.append("; \r\n");
342         dataToExport.append("; LapCount,LapTime,TotalTime,LapTime(ms),TotalTime(ms),;\r\n");
343
344         for (int index = 0; index < dataCount; index++)
345         {
346             try
347             {
348                 DetailRecord record = adapter.getRecord(index);
349                 dataToExport.append(record.getLapCount());
350                 dataToExport.append(",");
351                 dataToExport.append(record.getTitle());
352                 dataToExport.append(",");
353                 dataToExport.append(record.getOverallTime());
354                 dataToExport.append(",");
355                 dataToExport.append(record.getLapTime());
356                 dataToExport.append(",");
357                 dataToExport.append(record.getTotalTime());
358                 dataToExport.append(",");
359                 dataToExport.append(";");
360             }
361             catch (Exception e)
362             {
363                 e.printStackTrace();
364                 dataToExport.append(";;\r\n");
365                 break;
366             }
367             dataToExport.append("\r\n");
368         }
369
370         // Intent発行(ACTION_SEND)
371         try
372         {
373             Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
374             sendIntent.putExtra(Intent.EXTRA_SUBJECT, title);
375             sendIntent.setType("text/plain");
376             sendIntent.putExtra(Intent.EXTRA_TEXT, dataToExport.toString());
377             activity.startActivity(sendIntent);
378
379             Log.v(TAG, "<<< SEND INTENT >>> : " + title);
380         }
381         catch (Exception e)
382         {
383             e.printStackTrace();
384         }
385     }
386
387     class EditIndexData
388     {
389         final String title;
390         final int icon;
391
392         EditIndexData(String title, int icon)
393         {
394             this.title = title;
395             this.icon = icon;
396         }
397
398         String getTitle()
399         {
400             return (title);
401         }
402
403         int getIcon()
404         {
405             return (icon);
406         }
407     }
408
409
410     /**
411      *
412      */
413     interface IDatabaseReadyNotify
414     {
415         void databaseSetupFinished(boolean result);
416         void updatedIndexData(boolean isIconOnly);
417     }
418 }