OSDN Git Service

intentで記録を受信できるようにする(準備)
authorMRSa <mrsa@myad.jp>
Thu, 8 Jun 2023 15:16:01 +0000 (00:16 +0900)
committerMRSa <mrsa@myad.jp>
Thu, 8 Jun 2023 15:16:01 +0000 (00:16 +0900)
build.gradle
wear/build.gradle
wear/src/main/AndroidManifest.xml
wear/src/main/java/net/osdn/gokigen/joggingtimer/stopwatch/IntentSendImporter.kt [new file with mode: 0644]
wear/src/main/java/net/osdn/gokigen/joggingtimer/stopwatch/MainActivity.kt

index 021979b..bba54a7 100644 (file)
@@ -10,7 +10,7 @@ buildscript {
         mavenCentral()
     }
     dependencies {
-        classpath 'com.android.tools.build:gradle:8.0.1'
+        classpath 'com.android.tools.build:gradle:8.0.2'
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
 
         // NOTE: Do not place your application dependencies here; they belong
index 11fe6b9..c2df92f 100644 (file)
@@ -7,8 +7,8 @@ android {
         applicationId "net.osdn.gokigen.joggingtimer"
         minSdkVersion 23
         targetSdkVersion 33
-        versionCode 100051
-        versionName "1.0.5"
+        versionCode 100061
+        versionName "1.0.6"
     }
     buildTypes {
         release {
index 2901ea3..a76114c 100644 (file)
                 <category android:name="android.intent.category.DEFAULT" />
                 <data android:mimeType="vnd.google.fitness.activity/other"/>
             </intent-filter>
+            <intent-filter>
+                <action android:name="android.intent.action.SEND" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <data android:mimeType="text/*" />
+            </intent-filter>
         </activity>
         <activity
             android:name=".recordlist.ListActivity"
diff --git a/wear/src/main/java/net/osdn/gokigen/joggingtimer/stopwatch/IntentSendImporter.kt b/wear/src/main/java/net/osdn/gokigen/joggingtimer/stopwatch/IntentSendImporter.kt
new file mode 100644 (file)
index 0000000..4ded537
--- /dev/null
@@ -0,0 +1,75 @@
+package net.osdn.gokigen.joggingtimer.stopwatch
+
+import android.content.Intent
+import android.util.Log
+import java.lang.Exception
+
+class IntentSendImporter
+{
+    fun handleIntent(receivedIntent: Intent)
+    {
+        try
+        {
+            val title = receivedIntent.getStringExtra(Intent.EXTRA_SUBJECT)
+            val data = receivedIntent.getStringExtra(Intent.EXTRA_TEXT)
+            if (data == null)
+            {
+                Log.v(TAG, " ===== handleIntent: data is null... do not process")
+                return
+            }
+            Log.v(TAG, " ===== handleIntent: ${receivedIntent.action} : $title")
+
+            var nofLapTime = 0
+            val timeValueList = ArrayList<String>()
+            val lines = data.split("\r?\n".toRegex())
+            for ((currentLine, line) in lines.withIndex())
+            {
+                if (currentLine == 0)
+                {
+                    if (!line.contains("JoggingTimer"))
+                    {
+                        //  imported data is not valid... return
+                        Log.v(TAG, "Imported data is not valid...")
+                        return
+                    }
+                }
+                if (currentLine > 3)
+                {
+                    val timeValue = line.split(",".toRegex())
+                    if (timeValue.size < 3)
+                    {
+                        // 正当なデータがないので抜ける
+                        break
+                    }
+                    timeValueList.add(timeValue[3])
+                    nofLapTime++
+                }
+            }
+            val dataTitle = title ?: "imported data"
+            importLapTime(dataTitle, timeValueList)
+        }
+        catch (e: Exception)
+        {
+            e.printStackTrace()
+        }
+    }
+
+    private fun importLapTime(title: String, lapTimeList: ArrayList<String>)
+    {
+        //  ここでデータベースにデータを入れる
+        //  題名 : title (String型)
+        //  ラップタイムの配列 : timeValueList (Int型、単位は ms)
+        Log.v(TAG, " ----- $title ----- ")
+        for ((index, timeValue) in lapTimeList.withIndex())
+        {
+            Log.v(TAG, "  $index $timeValue")
+        }
+        Log.v(TAG, " ---------- ")
+
+    }
+
+    companion object
+    {
+        private val TAG = IntentSendImporter::class.java.simpleName
+    }
+}
\ No newline at end of file
index cde6422..cef8992 100644 (file)
@@ -77,6 +77,26 @@ class MainActivity : AppCompatActivity(), IClickCallback, ITimeoutReceiver, ICou
         {
             e.printStackTrace()
         }
+
+        try
+        {
+            val receivedAction = intent.action
+            if (receivedAction == Intent.ACTION_SEND)
+            {
+                val thread = Thread {
+                    // 取得したSENDインテントを処理する
+                    val importer = IntentSendImporter()
+                    importer.handleIntent(intent)
+                }
+                thread.start()
+            }
+        }
+        catch (e: Exception)
+        {
+            e.printStackTrace()
+        }
+
+
     }
 
     /**