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.database.Cursor;
4 import android.support.wearable.activity.WearableActivity;
5 import android.util.Log;
6
7 import net.osdn.gokigen.joggingtimer.storage.ITimeEntryDatabase;
8 import net.osdn.gokigen.joggingtimer.storage.ITimeEntryDatabaseCallback;
9 import net.osdn.gokigen.joggingtimer.storage.TimeEntryDatabaseFactory;
10 import net.osdn.gokigen.joggingtimer.storage.contract.TimeEntryData;
11 import net.osdn.gokigen.joggingtimer.utilities.IconIdProvider;
12 import net.osdn.gokigen.joggingtimer.utilities.TimeStringConvert;
13
14 import static android.provider.BaseColumns._ID;
15 import static net.osdn.gokigen.joggingtimer.storage.contract.TimeEntryData.EntryData.COLUMN_NAME_ICON_ID;
16
17 /**
18  *
19  *
20  */
21 public class RecordDetailSetup  implements ITimeEntryDatabaseCallback
22 {
23     private final String TAG = toString();
24     private final WearableActivity activity;
25     private final long indexId;
26     private final IDatabaseReadyNotify callback;
27     private final IRecordOperation operation;
28     private ITimeEntryDatabase database = null;
29
30     /**
31      *
32      *
33      */
34     RecordDetailSetup(WearableActivity activity, long indexId, IDatabaseReadyNotify callback, IRecordOperation operation)
35     {
36         this.activity = activity;
37         this.indexId = indexId;
38         this.callback = callback;
39         this.operation = operation;
40     }
41
42     /**
43      *
44      *
45      */
46     void setup()
47     {
48         Log.v(TAG, "setup()");
49         database = new TimeEntryDatabaseFactory(activity, this).getEntryDatabase();
50         Thread thread = new Thread(new Runnable() {
51             @Override
52             public void run()
53             {
54                 try
55                 {
56                     database.prepare();
57                 }
58                 catch (Exception e)
59                 {
60                     e.printStackTrace();
61                 }
62             }
63         });
64         thread.start();
65     }
66
67     /**
68      *
69      *
70      */
71     @Override
72     public void prepareFinished(boolean isReady)
73     {
74         if (!isReady)
75         {
76             callback.databaseSetupFinished(false);
77             return;
78         }
79         Thread thread = new Thread(new Runnable() {
80             @Override
81             public void run()
82             {
83                 boolean ret = false;
84                 try
85                 {
86                     operation.clearRecord();
87                     Cursor cursor = database.getAllDetailData(indexId);
88                     int index = 0;
89                     long startTime = 0;
90                     long previousLapTime = 0;
91                     long morePreviousTime = 0;
92                     while (cursor.moveToNext())
93                     {
94                         long dataId = cursor.getLong(cursor.getColumnIndex(_ID));
95                         int iconId = IconIdProvider.getIconResourceId(cursor.getInt(cursor.getColumnIndex(COLUMN_NAME_ICON_ID)));
96                         long entryTime = cursor.getLong(cursor.getColumnIndex(TimeEntryData.EntryData.COLUMN_NAME_TIME_ENTRY));
97
98                         if (index == 0)
99                         {
100                             // first record
101                             startTime = entryTime;
102                             previousLapTime = entryTime;
103                             morePreviousTime = entryTime;
104                         }
105                         else
106                         {
107                             long lapTime = entryTime - previousLapTime;
108                             long overallTime = entryTime - startTime;
109                             long differenceTime = (lapTime) - (previousLapTime - morePreviousTime);
110                             String lapTimeString = "[" + index + "] " + TimeStringConvert.getTimeString(lapTime).toString();
111                             String overallTimeString = TimeStringConvert.getTimeString(overallTime).toString() + " (" + TimeStringConvert.getDiffTimeString(differenceTime).toString() +") ";
112                             operation.addRecord(new DetailRecord(dataId, iconId, lapTimeString, overallTimeString));
113                             morePreviousTime = previousLapTime;
114                             previousLapTime = entryTime;
115                         }
116                         index++;
117                     }
118                     activity.runOnUiThread(new Runnable()
119                     {
120                         @Override
121                         public void run()
122                         {
123                             operation.dataSetChangeFinished();
124                         }
125                     });
126                     ret = true;
127                 }
128                 catch (Exception e)
129                 {
130                     e.printStackTrace();
131                 }
132                 callback.databaseSetupFinished(ret);
133             }
134         });
135         thread.start();
136     }
137
138
139     /**
140      *
141      *
142      */
143     @Override
144     public void dataEntryFinished(OperationType operationType, boolean result, long id, String title)
145     {
146          //
147     }
148
149     /**
150      *
151      *
152      */
153     @Override
154     public void timeEntryFinished(OperationType operationType, boolean result, long indexId, long dataId)
155     {
156         //
157     }
158
159     /**
160      *
161      */
162     void closeDatabase()
163     {
164         try
165         {
166             database.close();
167         }
168         catch (Exception e)
169         {
170             e.printStackTrace();
171         }
172     }
173
174     /**
175      *
176      */
177     interface IDatabaseReadyNotify
178     {
179         void databaseSetupFinished(boolean result);
180     }
181 }