OSDN Git Service

データ送信時のintentの変更(強化)検討中。
[gokigen/JoggingTimer.git] / wear / src / main / java / net / osdn / gokigen / joggingtimer / recorddetail / DetailActivity.kt
index 0b80a94..a1cb4a5 100644 (file)
@@ -1,5 +1,6 @@
 package net.osdn.gokigen.joggingtimer.recorddetail
 
+import android.annotation.SuppressLint
 import android.os.Bundle
 import android.util.Log
 import android.view.MenuItem
@@ -23,37 +24,33 @@ import net.osdn.gokigen.joggingtimer.utilities.SetReferenceDialog
 import net.osdn.gokigen.joggingtimer.utilities.SetReferenceDialog.SetReferenceCallback
 import kotlin.math.roundToInt
 
-/**
- *
- *
- */
 class DetailActivity : AppCompatActivity(),
     RecordDetailSetup.IDatabaseReadyNotify, MenuItem.OnMenuItemClickListener,
     DataEditDialog.Callback, IEditedModelDataCallback, DetailSelectionMenuAdapter.ISelectedMenu,
-    AmbientModeSupport.AmbientCallbackProvider, SetReferenceCallback {
-    private val TAG = toString()
+    AmbientModeSupport.AmbientCallbackProvider, SetReferenceCallback
+{
     private var detailAdapter: RecordDetailAdapter? = null
     private var setupper: RecordDetailSetup? = null
     private var actionDrawerView: WearableActionDrawerView? = null
 
-    /**
-     *
-     */
-    override fun onCreate(savedInstanceState: Bundle?) {
+    override fun onCreate(savedInstanceState: Bundle?)
+    {
         super.onCreate(savedInstanceState)
         Log.v(TAG, "onCreate()")
         setContentView(R.layout.activity_detail)
 
-        // Enables Always-on
-        //setAmbientEnabled();
-        try {
+        try
+        {
             val ambientController = AmbientModeSupport.attach(this)
             ambientController.setAutoResumeEnabled(true)
-            //boolean isAmbient = ambientController.isAmbient();
-        } catch (e: Exception) {
+        }
+        catch (e: Exception)
+        {
             e.printStackTrace()
         }
-        try {
+
+        try
+        {
             val naviView = findViewById<WearableNavigationDrawerView>(R.id.top_navigation_drawer)
             val menuAdapter = DetailSelectionMenuAdapter(this, this)
             naviView.setAdapter(menuAdapter)
@@ -62,108 +59,74 @@ class DetailActivity : AppCompatActivity(),
             detailAdapter = RecordDetailAdapter()
             val layoutManager = WearableLinearLayoutManager(this)
 
-            //view.setCircularScrollingGestureEnabled(getResources().getConfiguration().isScreenRound());
             view.isCircularScrollingGestureEnabled = false
             val dividerDecoration = DividerItemDecoration(view.context, layoutManager.orientation)
             view.addItemDecoration(dividerDecoration)
             view.layoutManager = layoutManager
             view.adapter = detailAdapter
-        } catch (e: Exception) {
+        }
+        catch (e: Exception)
+        {
             e.printStackTrace()
             detailAdapter = null
         }
 
-        // Bottom Action Drawer
-        actionDrawerView = findViewById(R.id.bottom_action_drawer)
-        actionDrawerView?.controller?.peekDrawer()
-        actionDrawerView?.setOnMenuItemClickListener(this)
-    }
-
-    /**
-     *
-     */
-    override fun onSaveInstanceState(outState: Bundle) {
-        super.onSaveInstanceState(outState)
-    }
-
-    /**
-     *
-     */
-    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
-        super.onRestoreInstanceState(savedInstanceState)
+        try
+        {
+            // Bottom Action Drawer
+            actionDrawerView = findViewById(R.id.bottom_action_drawer)
+            actionDrawerView?.controller?.peekDrawer()
+            actionDrawerView?.setOnMenuItemClickListener(this)
+        }
+        catch (e: Exception)
+        {
+            e.printStackTrace()
+        }
     }
 
-    /**
-     *
-     */
-    override fun onResume() {
+    override fun onResume()
+    {
         super.onResume()
         Log.v(TAG, "onResume() ")
     }
 
-    /**
-     *
-     */
-    override fun onPause() {
+    override fun onPause()
+    {
         super.onPause()
         Log.v(TAG, "onPause()")
     }
 
-    /**
-     *
-     *
-     */
-    public override fun onStart() {
+    public override fun onStart()
+    {
         super.onStart()
-        try {
+        try
+        {
             val indexId = intent.getLongExtra(INTENT_EXTRA_DATA_ID, -1)
             Log.v(TAG, "onResume() $indexId")
             setupper = RecordDetailSetup(this, indexId, this, detailAdapter, this)
-            setupper!!.setup()
-        } catch (e: Exception) {
+            setupper?.setup()
+        }
+        catch (e: Exception)
+        {
             e.printStackTrace()
         }
     }
 
-    /**
-     *
-     *
-     */
-    public override fun onStop() {
+    public override fun onStop()
+    {
         super.onStop()
         Log.v(TAG, "onStop()")
-        try {
-            if (setupper != null) {
-                setupper!!.closeDatabase()
-                setupper = null
-            }
-        } catch (e: Exception) {
+        try
+        {
+            setupper?.closeDatabase()
+            setupper = null
+        }
+        catch (e: Exception)
+        {
             e.printStackTrace()
         }
         System.gc()
     }
-    /*
-    @Override
-    public void onEnterAmbient(Bundle ambientDetails)
-    {
-        super.onEnterAmbient(ambientDetails);
-        Log.v(TAG, "onEnterAmbient()");
-    }
-
-    @Override
-    public void onExitAmbient()
-    {
-        super.onExitAmbient();
-        Log.v(TAG, "onExitAmbient()");
-    }
-
-    @Override
-    public void onUpdateAmbient()
-    {
-        super.onUpdateAmbient();
-        Log.v(TAG, "onUpdateAmbient()");
-    }
-*/
 
     override fun onGenericMotionEvent(ev: MotionEvent?): Boolean
     {
@@ -187,25 +150,18 @@ class DetailActivity : AppCompatActivity(),
         return (super.onGenericMotionEvent(ev))
     }
 
-
-    /**
-     *
-     *
-     */
-    override fun databaseSetupFinished(result: Boolean) {
+    override fun databaseSetupFinished(result: Boolean)
+    {
         Log.v(TAG, "databaseSetupFinished() : $result")
     }
 
-    /**
-     *
-     *
-     */
-    override fun updatedIndexData(isIconOnly: Boolean) {
+    override fun updatedIndexData(isIconOnly: Boolean)
+    {
         Log.v(TAG, "selectedReferenceData() : $isIconOnly")
         runOnUiThread {
-            try {
-                val title: String
-                title = if (isIconOnly) {
+            try
+            {
+                val title: String = if (isIconOnly) {
                     getString(R.string.action_set_reference)
                 } else {
                     getString(R.string.action_edited_data)
@@ -213,187 +169,187 @@ class DetailActivity : AppCompatActivity(),
                 val toast =
                     Toast.makeText(applicationContext, title, Toast.LENGTH_SHORT)
                 toast.show()
-            } catch (e: Exception) {
+            }
+            catch (e: Exception)
+            {
                 e.printStackTrace()
             }
         }
     }
 
-    /**
-     *
-     *
-     */
-    override fun onMenuItemClick(item: MenuItem): Boolean {
+    override fun onMenuItemClick(item: MenuItem): Boolean
+    {
         Log.v(TAG, "onMenuItemClick(): $item")
         val itemId = item.itemId
-        try {
-            actionDrawerView!!.controller.closeDrawer()
-        } catch (e: Exception) {
+        try
+        {
+            actionDrawerView?.controller?.closeDrawer()
+        }
+        catch (e: Exception)
+        {
             e.printStackTrace()
         }
-        return itemSelected(itemId)
+        return (itemSelected(itemId))
     }
 
-    private fun itemSelected(itemId: Int): Boolean {
+    private fun itemSelected(itemId: Int): Boolean
+    {
         var ret = false
-        //String toastMessage = "";
-        if (itemId == R.id.menu_edit_title) {
-            // タイトルの編集
-            var title = ""
-            var iconId = R.drawable.ic_android_black_24dp
-            val data = setupper!!.editIndexData
-            if (data != null) {
-                iconId = data.getIcon()
-                title = data.getTitle()
+        when (itemId) {
+            R.id.menu_edit_title -> {
+                // タイトルの編集
+                var title = ""
+                var iconId = R.drawable.ic_android_black_24dp
+                val data = setupper?.getEditIndexData()
+                if (data != null)
+                {
+                    iconId = data.icon()
+                    title = data.title()
+                }
+                val dialog = DataEditDialog.newInstance(iconId, title, this)
+                val manager = supportFragmentManager
+                dialog.show(manager, "dialog")
+                true.also { ret = it }
             }
-            val dialog = DataEditDialog.newInstance(iconId, title, this)
-            val manager = supportFragmentManager
-            dialog.show(manager, "dialog")
-            ret = true
-        } else if (itemId == R.id.menu_set_reference) {
-            // 基準値の設定ダイアログを表示する
-            val callback: SetReferenceCallback = this
-            runOnUiThread {
-                try {
-                    // 基準値設定ダイアログを表示する
-                    val dialog = SetReferenceDialog.newInstance(callback)
-                    val manager = supportFragmentManager
-                    dialog.show(manager, "dialog")
-                } catch (e: Exception) {
-                    e.printStackTrace()
+            R.id.menu_set_reference -> {
+                // 基準値の設定ダイアログを表示する
+                val callback: SetReferenceCallback = this
+                runOnUiThread {
+                    try {
+                        // 基準値設定ダイアログを表示する
+                        val dialog = SetReferenceDialog.newInstance(callback)
+                        val manager = supportFragmentManager
+                        dialog.show(manager, "dialog")
+                    } catch (e: Exception) {
+                        e.printStackTrace()
+                    }
                 }
+                true.also { ret = it }
             }
-            ret = true
-        } else if (itemId == R.id.menu_share_data) {
-            // 現在のデータを共有する
-            setupper!!.shareTheData(detailAdapter)
-            ret = true
-        }
-        /*
-        try
-        {
-            if (toastMessage.length() > 0)
-            {
-                Toast toast = Toast.makeText(getApplicationContext(), toastMessage, Toast.LENGTH_SHORT);
-                toast.show();
+            R.id.menu_share_data -> {
+                // 現在のデータを共有する
+                setupper?.shareTheData(detailAdapter)
+                true.also { ret = it }
             }
         }
-        catch (Exception e)
-        {
-            e.printStackTrace();
-        }
-*/return ret
+        return ret
     }
 
-    /**
-     *
-     */
-    override fun dataEdited(iconId: Int, title: String) {
+    override fun dataEdited(iconId: Int, title: String)
+    {
         Log.v(TAG, "iconId : $iconId title : '$title'")
-        try {
-            setupper!!.setEditIndexData(title, iconId)
+        try
+        {
+            setupper?.setEditIndexData(title, iconId)
             val view = findViewById<WearableRecyclerView>(R.id.recycler_detail_view)
             view.postInvalidate()
-        } catch (e: Exception) {
+        }
+        catch (e: Exception)
+        {
             e.printStackTrace()
         }
     }
 
-    /**
-     *
-     */
-    private fun updateScreen() {
-        try {
+    private fun updateScreen()
+    {
+        try
+        {
             val view = findViewById<WearableRecyclerView>(R.id.recycler_detail_view)
             view.postInvalidate()
-        } catch (e: Exception) {
+        }
+        catch (e: Exception)
+        {
             e.printStackTrace()
         }
     }
 
-    /**
-     *
-     */
-    override fun cancelled() {
+    override fun cancelled()
+    {
         updateScreen()
     }
 
-    /**
-     *
-     */
+    @SuppressLint("NotifyDataSetChanged")
     override fun editedModelData(
         indexId: Long,
         detailId: Long,
         lapCount: Int,
         prevTime: Long,
         newTime: Long
-    ) {
+    )
+    {
         Log.v(
             TAG,
             "editedModelData() $indexId $detailId $lapCount ($prevTime -> $newTime)"
         )
-        if (detailAdapter == null) {
+        if (detailAdapter == null)
+        {
             return
         }
         val diffTime = newTime - prevTime
-        val count = detailAdapter!!.itemCount
-        if (count > 1) {
+        val count = detailAdapter?.itemCount ?: 0
+        if (count > 1)
+        {
             var totalTime: Long = 0
             val modTime = diffTime * -1 / (count - 1)
-            for (index in 1..count) {
-                val record = detailAdapter!!.getRecord(index - 1)
+            for (index in 1..count)
+            {
+                val record = detailAdapter?.getRecord(index - 1)
                 totalTime = if (lapCount == index) {
-                    record.addModifiedTime(diffTime, totalTime)
+                    record?.addModifiedTime(diffTime, totalTime) ?: 0
                 } else {
-                    record.addModifiedTime(modTime, totalTime)
+                    record?.addModifiedTime(modTime, totalTime) ?: 0
                 }
-                detailAdapter!!.notifyItemChanged(index - 1)
+                detailAdapter?.notifyItemChanged(index - 1)
             }
-            try {
+            try
+            {
                 val thread = Thread {
-                    if (setupper != null) {
-                        setupper!!.updateDatabaseRecord(detailAdapter!!)
+                    if (setupper != null)
+                    {
+                        setupper?.updateDatabaseRecord(detailAdapter!!)
                     }
                 }
                 thread.start()
-            } catch (e: Exception) {
+            }
+            catch (e: Exception)
+            {
                 e.printStackTrace()
             }
         }
-        try {
+        try
+        {
             detailAdapter?.notifyDataSetChanged()
-        } catch (e: Exception) {
+        }
+        catch (e: Exception)
+        {
             e.printStackTrace()
         }
     }
 
-    override fun selectedMenu(itemId: Int) {
+    override fun selectedMenu(itemId: Int)
+    {
         itemSelected(itemId)
     }
 
-    override fun getAmbientCallback(): AmbientModeSupport.AmbientCallback {
+    override fun getAmbientCallback(): AmbientModeSupport.AmbientCallback
+    {
         return object : AmbientModeSupport.AmbientCallback() {
-            override fun onEnterAmbient(ambientDetails: Bundle) {
+            override fun onEnterAmbient(ambientDetails: Bundle)
+            {
                 Log.v(TAG, "onEnterAmbient()")
             }
-
-            fun onExitAmbient(ambientDetails: Bundle?) {
-                Log.v(TAG, "onExitAmbient()")
-            }
         }
     }
 
-    override fun onPointerCaptureChanged(hasCapture: Boolean) {
-        super.onPointerCaptureChanged(hasCapture)
-    }
-
-    override fun confirmed(id: Int) {
+    override fun confirmed(id: Int)
+    {
         // 現在のデータを基準値として設定する
         Log.v(TAG, " SET REFERENCE DATA ID: $id")
-        setupper!!.setReferenceData(id)
+        setupper?.setReferenceData(id)
     }
 
     companion object {
+        private val TAG = DetailActivity::class.java.simpleName
         const val INTENT_EXTRA_DATA_ID = "Detail.dataId"
     }
-}
\ No newline at end of file
+}