OSDN Git Service

bfd296108cd6bd335cbb667b10e930f17fa8609f
[gokigen/JoggingTimer.git] / wear / src / main / java / net / osdn / gokigen / joggingtimer / recordlist / ListActivity.java
1 package net.osdn.gokigen.joggingtimer.recordlist;
2
3 import android.app.FragmentManager;
4 import android.content.Intent;
5 import android.os.Bundle;
6 import android.support.annotation.NonNull;
7 import android.support.v7.widget.DividerItemDecoration;
8 import android.support.wear.widget.WearableLinearLayoutManager;
9 import android.support.wear.widget.drawer.WearableNavigationDrawerView;
10 import android.support.wearable.activity.WearableActivity;
11 import android.support.wear.widget.WearableRecyclerView;
12 import android.util.Log;
13 import android.widget.Toast;
14
15 import net.osdn.gokigen.joggingtimer.R;
16 import net.osdn.gokigen.joggingtimer.recorddetail.DetailActivity;
17 import net.osdn.gokigen.joggingtimer.storage.ITimeEntryDatabase;
18 import net.osdn.gokigen.joggingtimer.utilities.ConfirmationDialog;
19 import net.osdn.gokigen.joggingtimer.utilities.CreateModelData;
20 import net.osdn.gokigen.joggingtimer.utilities.CreateModelDataDialog;
21
22 /**
23  *
24  *
25  */
26 public class ListActivity extends WearableActivity implements IDetailLauncher, RecordSummarySetup.IDatabaseReadyNotify, CreateModelData.ICreatedModelDataCallback, ListSelectionMenuAdapter.ISelectedMenu
27 {
28     private final String TAG = toString();
29     private RecordSummaryAdapter summaryAdapter = null;
30     private RecordSummarySetup setupper = null;
31
32     /**
33      *
34      */
35     @Override
36     protected void onCreate(Bundle savedInstanceState)
37     {
38         super.onCreate(savedInstanceState);
39         Log.v(TAG, "onCreate()");
40
41         setContentView(R.layout.activity_list);
42
43         // Enables Always-on
44         setAmbientEnabled();
45
46         try
47         {
48             WearableNavigationDrawerView naviView = findViewById(R.id.list_top_navigation_drawer);
49             ListSelectionMenuAdapter menuAdapter = new ListSelectionMenuAdapter(this, this);
50             naviView.setAdapter(menuAdapter);
51             naviView.addOnItemSelectedListener(menuAdapter);
52
53
54             WearableRecyclerView view = findViewById(R.id.recycler_list_view);
55             summaryAdapter = new RecordSummaryAdapter();
56             WearableLinearLayoutManager layoutManager = new WearableLinearLayoutManager(this);
57
58             view.setCircularScrollingGestureEnabled(getResources().getConfiguration().isScreenRound());
59
60             DividerItemDecoration dividerDecoration = new DividerItemDecoration(view.getContext(), layoutManager.getOrientation());
61
62             view.addItemDecoration(dividerDecoration);
63             view.setLayoutManager(layoutManager);
64             view.setAdapter(summaryAdapter);
65
66         }
67         catch (Exception e)
68         {
69             e.printStackTrace();
70             summaryAdapter = null;
71         }
72     }
73
74     /**
75      *
76      */
77     @Override
78     protected void onSaveInstanceState(Bundle outState)
79     {
80         super.onSaveInstanceState(outState);
81     }
82
83     /**
84      *
85      */
86     @Override
87     protected void onRestoreInstanceState(Bundle savedInstanceState)
88     {
89         super.onRestoreInstanceState(savedInstanceState);
90     }
91
92     /**
93      *
94      */
95     @Override
96     protected void onResume()
97     {
98         super.onResume();
99         Log.v(TAG, "onResume()");
100         try
101         {
102             setupper = new RecordSummarySetup(this, this, this, summaryAdapter, this);
103             setupper.setup();
104         }
105         catch (Exception e)
106         {
107             e.printStackTrace();
108         }
109
110     }
111
112     /**
113      *
114      */
115     @Override
116     protected void onPause()
117     {
118         super.onPause();
119         Log.v(TAG, "onPause()");
120         try
121         {
122             if (setupper != null)
123             {
124                 setupper.closeDatabase();
125                 setupper = null;
126             }
127         }
128         catch (Exception e)
129         {
130             e.printStackTrace();
131         }
132         System.gc();
133     }
134
135     /**
136      *
137      *
138      */
139     @Override
140     public void onStart()
141     {
142         super.onStart();
143         Log.v(TAG, "onStart()");
144     }
145
146     /**
147      *
148      *
149      */
150     @Override
151     public void onStop()
152     {
153         super.onStop();
154         Log.v(TAG, "onStop()");
155     }
156
157     /**
158      *
159      *
160      */
161     @Override
162     public void onEnterAmbient(Bundle ambientDetails)
163     {
164         super.onEnterAmbient(ambientDetails);
165         Log.v(TAG, "onEnterAmbient()");
166     }
167
168     /**
169      *
170      *
171      */
172     @Override
173     public void onExitAmbient()
174     {
175         super.onExitAmbient();
176         Log.v(TAG, "onExitAmbient()");
177     }
178
179     /**
180      *
181      *
182      */
183     @Override
184     public void onUpdateAmbient()
185     {
186         super.onUpdateAmbient();
187         Log.v(TAG, "onUpdateAmbient()");
188     }
189
190     /**
191      *
192      *
193      */
194     @Override
195     public void launchDetail(long recordId)
196     {
197         Log.v(TAG, "launchDetail() id:" + recordId);
198         try
199         {
200             Intent intent = new Intent(this, DetailActivity.class);
201             intent.putExtra(DetailActivity.INTENT_EXTRA_DATA_ID, recordId);
202             startActivity(intent);
203         }
204         catch (Exception e)
205         {
206             e.printStackTrace();
207         }
208     }
209
210     /**
211      *
212      *
213      */
214     private void itemSelected(int itemId)
215     {
216         String toastMessage = "";
217         switch (itemId)
218         {
219             case R.id.menu_create_model:
220                 // モデルデータの作成
221                 CreateModelDataDialog dialog2 = new CreateModelDataDialog(this);
222                 dialog2.show(true, getString(R.string.information_time_picker), 0, setupper.getCreateModelDataCallback(ITimeEntryDatabase.DONT_USE_ID, ITimeEntryDatabase.DONT_USE_ID), 0);
223                 break;
224
225
226             default:
227                 // 何もしない
228                 break;
229         }
230         try
231         {
232             if (toastMessage.length() > 0)
233             {
234                 Toast toast = Toast.makeText(getApplicationContext(), toastMessage, Toast.LENGTH_SHORT);
235                 toast.show();
236             }
237         }
238         catch (Exception e)
239         {
240             e.printStackTrace();
241         }
242     }
243
244     /**
245      *
246      *
247      */
248     @Override
249     public void deleteRecord(@NonNull DataRecord targetRecord)
250     {
251         try
252         {
253             final int positionId = targetRecord.getPositionId();
254             final String title = targetRecord.getTitle();
255
256             Log.v(TAG, "deleteRecord() : " + title);
257
258             String message = getString(R.string.dialog_message_delete) + " (" + title + ")";
259             ConfirmationDialog dialog = ConfirmationDialog.newInstance(getString(R.string.dialog_title_delete), message,  new ConfirmationDialog.Callback() {
260                 @Override
261                 public void confirm()
262                 {
263                     Log.v(TAG, "Delete Record Execute [" + title + "]" + " pos:" + positionId);
264                     if (summaryAdapter != null)
265                     {
266                         final long indexId = summaryAdapter.removeItem(positionId);
267                         try
268                         {
269                             Thread thread = new Thread(new Runnable()
270                             {
271                                 @Override
272                                 public void run()
273                                 {
274                                     if (indexId >= 0)
275                                     {
276                                         setupper.deleteTimeEntryData(indexId);
277                                     }
278                                 }
279                             });
280                             thread.start();
281                         }
282                         catch (Exception e)
283                         {
284                             e.printStackTrace();
285                         }
286                     }
287                 }
288             });
289             FragmentManager manager = getFragmentManager();
290             String tag = "dialog";
291             if (manager != null)
292             {
293                 dialog.show(manager, tag);
294             }
295         }
296         catch (Exception e)
297         {
298             e.printStackTrace();
299         }
300     }
301
302     /**
303      *
304      *
305      */
306     @Override
307     public void databaseSetupFinished(boolean result)
308     {
309         Log.v(TAG, "databaseSetupFinished() : " + result);
310     }
311
312     /**
313      *
314      *
315      */
316     @Override
317     public void selectedMenu(int itemId)
318     {
319         itemSelected(itemId);
320     }
321
322     /**
323      *
324      *
325      */
326     @Override
327     public void createdModelData(long indexId)
328     {
329         // データの登録
330         setupper.setIndexData(indexId);
331
332         // 一覧の更新
333         runOnUiThread(new Runnable()
334         {
335             @Override
336             public void run()
337             {
338                 if (summaryAdapter != null)
339                 {
340                     int count = summaryAdapter.getItemCount();
341                     summaryAdapter.notifyItemChanged(count - 1);
342                     summaryAdapter.notifyDataSetChanged();
343                 }
344
345                 // Toastで作成を通知する
346                 Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.created_model_data), Toast.LENGTH_SHORT);
347                 toast.show();
348             }
349         });
350     }
351 }