OSDN Git Service

intentで記録を受信できるようにする(実装中)
authorMRSa <mrsa@myad.jp>
Fri, 9 Jun 2023 14:45:15 +0000 (23:45 +0900)
committerMRSa <mrsa@myad.jp>
Fri, 9 Jun 2023 14:45:15 +0000 (23:45 +0900)
wear/build.gradle
wear/src/main/java/net/osdn/gokigen/joggingtimer/recordlist/ListActivity.kt
wear/src/main/java/net/osdn/gokigen/joggingtimer/stopwatch/IntentSendImporter.kt
wear/src/main/java/net/osdn/gokigen/joggingtimer/stopwatch/MainActivity.kt
wear/src/main/java/net/osdn/gokigen/joggingtimer/storage/ITimeEntryDatabase.java
wear/src/main/java/net/osdn/gokigen/joggingtimer/storage/TimeEntryDatabase.java
wear/src/main/res/values-ja/strings.xml
wear/src/main/res/values/strings.xml

index c2df92f..7fef049 100644 (file)
@@ -26,7 +26,6 @@ android {
 dependencies {
     implementation fileTree(dir: 'libs', include: ['*.jar'])
     implementation 'com.google.android.support:wearable:2.9.0'
-    //implementation 'com.google.android.gms:play-services-wearable:18.0.0'
 
     implementation 'androidx.preference:preference-ktx:1.2.0'
     implementation "androidx.core:core-splashscreen:1.0.1"
index 25da2ee..d543232 100644 (file)
@@ -1,5 +1,6 @@
 package net.osdn.gokigen.joggingtimer.recordlist
 
+import android.annotation.SuppressLint
 import android.content.Intent
 import android.os.Bundle
 import android.util.Log
@@ -29,29 +30,34 @@ import kotlin.math.roundToInt
  */
 class ListActivity : AppCompatActivity(),
     IDetailLauncher, RecordSummarySetup.IDatabaseReadyNotify, ICreatedModelDataCallback,
-    ListSelectionMenuAdapter.ISelectedMenu, AmbientModeSupport.AmbientCallbackProvider {
-    private val TAG = toString()
+    ListSelectionMenuAdapter.ISelectedMenu, AmbientModeSupport.AmbientCallbackProvider
+{
     private var summaryAdapter: RecordSummaryAdapter? = null
     private var setupper: RecordSummarySetup? = null
 
     /**
      *
      */
-    override fun onCreate(savedInstanceState: Bundle?) {
+    override fun onCreate(savedInstanceState: Bundle?)
+    {
         super.onCreate(savedInstanceState)
         Log.v(TAG, "onCreate()")
         setContentView(R.layout.activity_list)
 
         // 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.list_top_navigation_drawer)
             val menuAdapter = ListSelectionMenuAdapter(this, this)
@@ -66,7 +72,9 @@ class ListActivity : AppCompatActivity(),
             view.addItemDecoration(dividerDecoration)
             view.layoutManager = layoutManager
             view.adapter = summaryAdapter
-        } catch (e: Exception) {
+        }
+        catch (e: Exception)
+        {
             e.printStackTrace()
             summaryAdapter = null
         }
@@ -75,27 +83,17 @@ class ListActivity : AppCompatActivity(),
     /**
      *
      */
-    override fun onSaveInstanceState(outState: Bundle) {
-        super.onSaveInstanceState(outState)
-    }
-
-    /**
-     *
-     */
-    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
-        super.onRestoreInstanceState(savedInstanceState)
-    }
-
-    /**
-     *
-     */
-    override fun onResume() {
+    override fun onResume()
+    {
         super.onResume()
         Log.v(TAG, "onResume()")
-        try {
+        try
+        {
             setupper = RecordSummarySetup(this, this, this, summaryAdapter, this)
-            setupper!!.setup()
-        } catch (e: Exception) {
+            setupper?.setup()
+        }
+        catch (e: Exception)
+        {
             e.printStackTrace()
         }
     }
@@ -103,15 +101,20 @@ class ListActivity : AppCompatActivity(),
     /**
      *
      */
-    override fun onPause() {
+    override fun onPause()
+    {
         super.onPause()
         Log.v(TAG, "onPause()")
-        try {
-            if (setupper != null) {
-                setupper!!.closeDatabase()
+        try
+        {
+            if (setupper != null)
+            {
+                setupper?.closeDatabase()
                 setupper = null
             }
-        } catch (e: Exception) {
+        }
+        catch (e: Exception)
+        {
             e.printStackTrace()
         }
         System.gc()
@@ -121,7 +124,8 @@ class ListActivity : AppCompatActivity(),
      *
      *
      */
-    public override fun onStart() {
+    public override fun onStart()
+    {
         super.onStart()
         Log.v(TAG, "onStart()")
     }
@@ -130,32 +134,11 @@ class ListActivity : AppCompatActivity(),
      *
      *
      */
-    public override fun onStop() {
+    public override fun onStop()
+    {
         super.onStop()
         Log.v(TAG, "onStop()")
     }
-    /*
-    @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
     {
@@ -183,13 +166,17 @@ class ListActivity : AppCompatActivity(),
      *
      *
      */
-    override fun launchDetail(recordId: Long) {
+    override fun launchDetail(recordId: Long)
+    {
         Log.v(TAG, "launchDetail() id:$recordId")
-        try {
+        try
+        {
             val intent = Intent(this, DetailActivity::class.java)
             intent.putExtra(DetailActivity.INTENT_EXTRA_DATA_ID, recordId)
             startActivity(intent)
-        } catch (e: Exception) {
+        }
+        catch (e: Exception)
+        {
             e.printStackTrace()
         }
     }
@@ -198,15 +185,16 @@ class ListActivity : AppCompatActivity(),
      *
      *
      */
-    private fun itemSelected(itemId: Int) {
-        //String toastMessage = "";
-        if (itemId == R.id.menu_create_model) {
+    private fun itemSelected(itemId: Int)
+    {
+        if (itemId == R.id.menu_create_model)
+        {
             // モデルデータの作成
             val dialog2 = CreateModelDataDialog.newInstance(
                 true,
                 getString(R.string.information_time_picker),
                 0,
-                setupper!!.getCreateModelDataCallback(
+                setupper?.getCreateModelDataCallback(
                     ITimeEntryDatabase.DONT_USE_ID,
                     ITimeEntryDatabase.DONT_USE_ID
                 ),
@@ -214,28 +202,16 @@ class ListActivity : AppCompatActivity(),
             )
             dialog2.show(supportFragmentManager, "dialog2")
         }
-        /*
-        try
-        {
-            if (toastMessage.length() > 0)
-            {
-                Toast toast = Toast.makeText(getApplicationContext(), toastMessage, Toast.LENGTH_SHORT);
-                toast.show();
-            }
-        }
-        catch (Exception e)
-        {
-            e.printStackTrace();
-        }
- */
     }
 
     /**
      *
      *
      */
-    override fun deleteRecord(targetRecord: DataRecord) {
-        try {
+    override fun deleteRecord(targetRecord: DataRecord)
+    {
+        try
+        {
             val positionId = targetRecord.positionId
             val title = targetRecord.title
             Log.v(TAG, "deleteRecord() : $title")
@@ -262,7 +238,9 @@ class ListActivity : AppCompatActivity(),
                 }
             }
             dialog.show(supportFragmentManager, "dialog")
-        } catch (e: Exception) {
+        }
+        catch (e: Exception)
+        {
             e.printStackTrace()
         }
     }
@@ -271,7 +249,8 @@ class ListActivity : AppCompatActivity(),
      *
      *
      */
-    override fun databaseSetupFinished(result: Boolean) {
+    override fun databaseSetupFinished(result: Boolean)
+    {
         Log.v(TAG, "databaseSetupFinished() : $result")
     }
 
@@ -279,7 +258,8 @@ class ListActivity : AppCompatActivity(),
      *
      *
      */
-    override fun selectedMenu(itemId: Int) {
+    override fun selectedMenu(itemId: Int)
+    {
         itemSelected(itemId)
     }
 
@@ -287,42 +267,50 @@ class ListActivity : AppCompatActivity(),
      *
      *
      */
-    override fun createdModelData(indexId: Long) {
-        // データの登録
-        setupper!!.setIndexData(indexId)
+    @SuppressLint("NotifyDataSetChanged")
+    override fun createdModelData(indexId: Long)
+    {
+        try
+        {
+            // データの登録
+            setupper?.setIndexData(indexId)
 
-        // 一覧の更新
-        runOnUiThread {
-            if (summaryAdapter != null) {
-                val count = summaryAdapter!!.itemCount
-                summaryAdapter!!.notifyItemChanged(count - 1)
-                summaryAdapter!!.notifyDataSetChanged()
-            }
+            // 一覧の更新
+            runOnUiThread {
+                if (summaryAdapter != null)
+                {
+                    val count = summaryAdapter?.itemCount ?: 1
+                    summaryAdapter?.notifyItemChanged(count - 1)
+                    summaryAdapter?.notifyDataSetChanged()
+                }
 
-            // Toastで作成を通知する
-            val toast = Toast.makeText(
-                applicationContext,
-                getString(R.string.created_model_data),
-                Toast.LENGTH_SHORT
-            )
-            toast.show()
+                // Toastで作成を通知する
+                val toast = Toast.makeText(
+                    applicationContext,
+                    getString(R.string.created_model_data),
+                    Toast.LENGTH_SHORT
+                )
+                toast.show()
+            }
+        }
+        catch (e: Exception)
+        {
+            e.printStackTrace()
         }
     }
 
-    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()")
-                //updateTimerLabel();
-            }
         }
     }
 
-    override fun onPointerCaptureChanged(hasCapture: Boolean) {
-        super.onPointerCaptureChanged(hasCapture)
+    companion object
+    {
+        private val TAG = ListActivity::class.java.simpleName
     }
-}
\ No newline at end of file
+}
index 4ded537..0227bdb 100644 (file)
@@ -1,12 +1,21 @@
 package net.osdn.gokigen.joggingtimer.stopwatch
 
+import android.content.Context
 import android.content.Intent
 import android.util.Log
+import net.osdn.gokigen.joggingtimer.storage.ITimeEntryDatabaseCallback
+import net.osdn.gokigen.joggingtimer.storage.TimeEntryDatabaseFactory
 import java.lang.Exception
 
-class IntentSendImporter
+class IntentSendImporter(context: Context, val intent: Intent) : ITimeEntryDatabaseCallback
 {
-    fun handleIntent(receivedIntent: Intent)
+    val database =  TimeEntryDatabaseFactory(context, this).entryDatabase
+
+    fun start()
+    {
+        database.prepare()
+    }
+    private fun handleIntent(receivedIntent: Intent)
     {
         try
         {
@@ -19,8 +28,8 @@ class IntentSendImporter
             }
             Log.v(TAG, " ===== handleIntent: ${receivedIntent.action} : $title")
 
-            var nofLapTime = 0
-            val timeValueList = ArrayList<String>()
+            var totalTime: Long = 0
+            val lapTimeList = ArrayList<Long>()
             val lines = data.split("\r?\n".toRegex())
             for ((currentLine, line) in lines.withIndex())
             {
@@ -41,35 +50,72 @@ class IntentSendImporter
                         // 正当なデータがないので抜ける
                         break
                     }
-                    timeValueList.add(timeValue[3])
-                    nofLapTime++
+                    val lapTime = timeValue[3].toLong()
+                    totalTime += lapTime
+                    lapTimeList.add(lapTime)
                 }
             }
             val dataTitle = title ?: "imported data"
-            importLapTime(dataTitle, timeValueList)
+            importLapTime(dataTitle, totalTime, lapTimeList)
         }
         catch (e: Exception)
         {
             e.printStackTrace()
         }
     }
-
-    private fun importLapTime(title: String, lapTimeList: ArrayList<String>)
+    private fun importLapTime(dataTitle: String, totalTime: Long, lapTimeList: ArrayList<Long>)
     {
-        //  ここでデータベースにデータを入れる
-        //  題名 : title (String型)
-        //  ラップタイムの配列 : timeValueList (Int型、単位は ms)
-        Log.v(TAG, " ----- $title ----- ")
+        //  ここでデータベースにデータを入れる (入れたあとはコールバックで戻る)
+        database.createImportedTimeEntryData(dataTitle, "imported data", totalTime, lapTimeList)
+/*
+        //  題名 : dataTitle (String型)
+        //  ラップタイムの配列 : lapTimeList (Int型、単位は ms)
+        Log.v(TAG, " ----- $dataTitle ----- ")
         for ((index, timeValue) in lapTimeList.withIndex())
         {
             Log.v(TAG, "  $index $timeValue")
         }
         Log.v(TAG, " ---------- ")
-
+*/
     }
 
     companion object
     {
         private val TAG = IntentSendImporter::class.java.simpleName
     }
+
+    override fun prepareFinished(isReady: Boolean)
+    {
+        handleIntent(intent)
+    }
+
+    override fun dataEntryFinished(
+        operationType: ITimeEntryDatabaseCallback.OperationType?,
+        result: Boolean,
+        id: Long,
+        title: String?
+    )
+    {
+        Log.v(TAG, "dataEntryFinished()")
+    }
+
+    override fun timeEntryFinished(
+        operationType: ITimeEntryDatabaseCallback.OperationType?,
+        result: Boolean,
+        indexId: Long,
+        dataId: Long
+    )
+    {
+        Log.v(TAG, "timeEntryFinished()")
+    }
+
+    override fun modelDataEntryFinished(
+        operationType: ITimeEntryDatabaseCallback.OperationType?,
+        result: Boolean,
+        indexId: Long,
+        title: String?
+    )
+    {
+        Log.v(TAG, "modelDataEntryFinished()")
+    }
 }
\ No newline at end of file
index cef8992..3048715 100644 (file)
@@ -9,6 +9,7 @@ import android.view.View
 import android.widget.ImageButton
 import android.widget.ListView
 import android.widget.TextView
+import android.widget.Toast
 import androidx.appcompat.app.AppCompatActivity
 import androidx.constraintlayout.widget.ConstraintLayout
 import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
@@ -80,24 +81,38 @@ class MainActivity : AppCompatActivity(), IClickCallback, ITimeoutReceiver, ICou
 
         try
         {
-            val receivedAction = intent.action
-            if (receivedAction == Intent.ACTION_SEND)
+            if (Intent.ACTION_SEND == intent.action)
             {
-                val thread = Thread {
-                    // 取得したSENDインテントを処理する
-                    val importer = IntentSendImporter()
-                    importer.handleIntent(intent)
+                importReceivedIntent()
+            }
+        }
+        catch (e: Exception)
+        {
+            e.printStackTrace()
+        }
+    }
+
+    private fun importReceivedIntent()
+    {
+        try
+        {
+            val thread = Thread {
+                // 取得したSENDインテントを処理する
+                IntentSendImporter(this.applicationContext, intent).start()
+                val title = intent.getStringExtra(Intent.EXTRA_SUBJECT)
+                runOnUiThread {
+                    Toast.makeText(this, getString(R.string.data_imported) + title, Toast.LENGTH_SHORT).show()
                 }
-                thread.start()
             }
+            thread.start()
         }
         catch (e: Exception)
         {
             e.printStackTrace()
         }
+    }
 
 
-    }
 
     /**
      *
index 0dc5205..9d949a9 100644 (file)
@@ -2,6 +2,8 @@ package net.osdn.gokigen.joggingtimer.storage;
 
 import android.database.Cursor;
 
+import java.util.ArrayList;
+
 /**
  *    ITimeEntryDatabase
  *
@@ -30,5 +32,8 @@ public interface ITimeEntryDatabase
     void finishTimeData(long indexId, long startTime, long endTime);
 
     long createTimeEntryModelData(int lap, long totalTime, String memo);
+
+    long createImportedTimeEntryData(String title, String memo, long totalTime, ArrayList<Long> lapTimeList);
+
     int updateTimeEntryData(long detailId, long totalTime);
 }
index e0705f7..2d75e14 100644 (file)
@@ -13,6 +13,8 @@ import static android.provider.BaseColumns._ID;
 
 import androidx.annotation.NonNull;
 
+import java.util.ArrayList;
+
 /**
  *
  *
@@ -308,6 +310,30 @@ class TimeEntryDatabase implements ITimeEntryDatabase
         return (-1);
     }
 
+    @Override
+    public long createImportedTimeEntryData(String title, String memo, long totalTime, ArrayList<Long> lapTimeList)
+    {
+        try
+        {
+            long lapTime = 0;
+            long indexId = createIndexDataImpl(false, title, memo, MODEL_DATA_ICON_ID, 0, totalTime);
+            for (long currentTime :lapTimeList)
+            {
+                lapTime = lapTime + currentTime;
+                appendTimeDataImpl(false, indexId, lapTime, EDITABLE_RECORD_TYPE);
+            }
+            callback.modelDataEntryFinished(ITimeEntryDatabaseCallback.OperationType.FINISHED, true, indexId, title);
+            return (indexId);
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+        callback.modelDataEntryFinished(ITimeEntryDatabaseCallback.OperationType.FINISHED, false, -1, title);
+        return (-1);
+    }
+
+
     /**
      *
      *
index 7510b29..1ad121c 100644 (file)
@@ -8,6 +8,7 @@
     <string name="app_title">ジョギングタイマ</string>
     <string name="blank"> </string>
     <string name="clock_dummy">HH:MM</string>
+    <string name="data_imported">登録: </string>
     <string name="counter">[88] 00\'00\'00\"0</string>
     <string name="counter_sub">[88] 00\'00\'00\"0</string>
     <string name="dialog_title_delete">記録の削除</string>
index 21ee7b0..2a27b43 100644 (file)
@@ -8,6 +8,7 @@
     <string name="app_title">Jogging Timer</string>
     <string name="blank"> </string>
     <string name="clock_dummy">HH:MM</string>
+    <string name="data_imported">Imported: </string>
     <string name="counter">[88] 00\'00\'00\"0</string>
     <string name="counter_sub">[88] 00\'00\'00\"0</string>
     <string name="dialog_title_delete">REC. DELETE</string>