Skip to content

Commit f8e9cc6

Browse files
committed
Add Activity Recognition module
1 parent fb750b9 commit f8e9cc6

80 files changed

Lines changed: 2417 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Within each module, you will find detailed TODO comments that guide you through
66

77
Currently it contains codebases for:
88

9+
- [Activity Recognition](https://developer.android.com/develop/sensors-and-location/location/transitions): Detects changes in the user's activity( such as walking, running, or driving). By subscribes to a transition in activities of interest and the API notifies the app only when needed.
910
- [App Shortcuts](https://developer.android.com/develop/ui/views/launch/shortcuts): Shortcuts can be displayed in a supported launcher. They help users quickly start common or recommended tasks within apps.
1011
- [Autofill](https://developers.google.com/identity/sms-retriever/request): Retrieve otp from SMS automatically using Google SMS Retriever API.
1112
- [Broadcast Receiver](https://github.com/AsemLab/Samples/tree/main/broadcast_receiver): Create a custom broadcast receiver.

activity_recognition/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import com.asemlab.samples.Configuration
2+
3+
plugins {
4+
alias(libs.plugins.android.application)
5+
alias(libs.plugins.kotlin)
6+
alias(libs.plugins.kotlin.kapt)
7+
alias(libs.plugins.hilt.plugin)
8+
}
9+
10+
android {
11+
namespace = "com.asemlab.activity_recognition"
12+
compileSdk = Configuration.compileSdk
13+
14+
defaultConfig {
15+
minSdk = Configuration.minSdk
16+
targetSdk = Configuration.targetSdk
17+
versionCode = Configuration.versionCode
18+
versionName = Configuration.versionName
19+
20+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
21+
}
22+
23+
buildTypes {
24+
release {
25+
isMinifyEnabled = false
26+
proguardFiles(
27+
getDefaultProguardFile("proguard-android-optimize.txt"),
28+
"proguard-rules.pro"
29+
)
30+
}
31+
}
32+
compileOptions {
33+
sourceCompatibility = JavaVersion.VERSION_17
34+
targetCompatibility = JavaVersion.VERSION_17
35+
}
36+
kotlinOptions {
37+
jvmTarget = "17"
38+
}
39+
buildFeatures {
40+
dataBinding = true
41+
viewBinding = true
42+
}
43+
}
44+
45+
dependencies {
46+
47+
implementation(libs.androidx.core.ktx)
48+
implementation(libs.androidx.appcompat)
49+
implementation(libs.material)
50+
implementation(libs.activity)
51+
implementation(libs.androidx.constraintlayout)
52+
implementation(libs.lifecycle.livedata.ktx)
53+
implementation(libs.androidx.viewmodel)
54+
implementation(libs.androidx.navigation.fragment.ktx)
55+
implementation(libs.androidx.navigation.ui.ktx)
56+
testImplementation(libs.junit)
57+
androidTestImplementation(libs.androidx.junit)
58+
androidTestImplementation(libs.androidx.espresso.core)
59+
60+
// Hilt
61+
implementation(libs.hilt.android)
62+
kapt(libs.hilt.compiler)
63+
64+
// Room
65+
implementation(libs.androidx.room.runtime)
66+
implementation(libs.androidx.room.ktx)
67+
kapt(libs.androidx.room.compiler)
68+
69+
// TODO Add Google Play Services Location library
70+
implementation(libs.play.services.location)
71+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.asemlab.activity_recognition
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.asemlab.stepscounter", appContext.packageName)
23+
}
24+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
6+
<!-- Required for 29+. -->
7+
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" />
8+
9+
<application
10+
android:name=".ActivityRecognitionApp"
11+
android:allowBackup="true"
12+
android:dataExtractionRules="@xml/data_extraction_rules"
13+
android:fullBackupContent="@xml/backup_rules"
14+
android:icon="@mipmap/ic_launcher"
15+
android:label="@string/app_name"
16+
android:roundIcon="@mipmap/ic_launcher_round"
17+
android:supportsRtl="true"
18+
android:theme="@style/Theme.ActivityRecognition"
19+
tools:targetApi="31">
20+
<activity
21+
android:name=".ui.MainActivity"
22+
android:exported="true"
23+
android:label="@string/app_name">
24+
<intent-filter>
25+
<action android:name="android.intent.action.MAIN" />
26+
27+
<category android:name="android.intent.category.LAUNCHER" />
28+
</intent-filter>
29+
</activity>
30+
31+
<receiver
32+
android:name=".services.TransitionsReceiver"
33+
android:enabled="true"
34+
android:exported="false"
35+
android:permission="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
36+
</application>
37+
38+
</manifest>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.asemlab.activity_recognition
2+
3+
import android.app.Application
4+
import androidx.lifecycle.MutableLiveData
5+
import com.asemlab.activity_recognition.utilties.ActivityType
6+
import com.asemlab.activity_recognition.utilties.DetectingMode
7+
import dagger.hilt.android.HiltAndroidApp
8+
9+
@HiltAndroidApp
10+
class ActivityRecognitionApp : Application() {
11+
12+
val detectingMode = MutableLiveData<DetectingMode>()
13+
val activityType = MutableLiveData(ActivityType.WALKING)
14+
val currentPoints = MutableLiveData(0)
15+
16+
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.asemlab.activity_recognition.database
2+
3+
import androidx.lifecycle.LiveData
4+
import androidx.room.Dao
5+
import androidx.room.Insert
6+
import androidx.room.Query
7+
import com.asemlab.activity_recognition.model.ActivityEntry
8+
9+
@Dao
10+
interface ActivitiesDAO {
11+
12+
@Insert
13+
fun addActivity(activityEntry: ActivityEntry)
14+
15+
@Query("DELETE FROM activities")
16+
fun clearData()
17+
18+
@Query("SELECT * FROM activities order by id desc")
19+
fun getAllActivities(): LiveData<List<ActivityEntry>>
20+
21+
@Query("SELECT * FROM activities order by id desc limit 1")
22+
fun getLastActivity(): LiveData<ActivityEntry>
23+
24+
@Query("SELECT sum(points) FROM activities")
25+
fun getTotalPoints(): LiveData<Long>
26+
27+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.asemlab.activity_recognition.database
2+
3+
import androidx.room.Database
4+
import androidx.room.RoomDatabase
5+
import com.asemlab.activity_recognition.model.ActivityEntry
6+
7+
@Database(entities = [ActivityEntry::class], version = 1, exportSchema = false)
8+
abstract class ActivitiesDB : RoomDatabase() {
9+
10+
abstract fun provideActivitiesDao(): ActivitiesDAO
11+
12+
}
13+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.asemlab.activity_recognition.database
2+
3+
import androidx.lifecycle.LiveData
4+
import com.asemlab.activity_recognition.model.ActivityEntry
5+
import com.asemlab.activity_recognition.utilties.ActivityType
6+
7+
class ActivitiesRepository(private val activitiesDAO: ActivitiesDAO) {
8+
9+
suspend fun addActivity(activityEntry: ActivityEntry) {
10+
activitiesDAO.addActivity(activityEntry)
11+
}
12+
13+
suspend fun getAllActivities(): LiveData<List<ActivityEntry>> {
14+
return activitiesDAO.getAllActivities()
15+
}
16+
17+
suspend fun getLastActivity(): LiveData<ActivityEntry> {
18+
return activitiesDAO.getLastActivity()
19+
}
20+
21+
suspend fun clearData() {
22+
activitiesDAO.clearData()
23+
}
24+
25+
suspend fun getTotalPoints(): LiveData<Long> {
26+
return activitiesDAO.getTotalPoints()
27+
}
28+
29+
suspend fun addTestingActivity() {
30+
activitiesDAO.addActivity(ActivityEntry(ActivityType.WALKING.name, 500, System.currentTimeMillis()))
31+
}
32+
33+
34+
}

0 commit comments

Comments
 (0)