1+ package com.kdg.toast.plugin
2+
3+ import android.app.NotificationChannel
4+ import android.app.NotificationManager
5+ import android.app.PendingIntent
6+ import android.app.Service
7+ import android.content.Intent
8+ import android.content.SharedPreferences
9+ import android.hardware.Sensor
10+ import android.hardware.SensorEvent
11+ import android.hardware.SensorEventListener
12+ import android.hardware.SensorManager
13+ import android.os.IBinder
14+ import android.preference.PreferenceManager
15+ import android.util.Log
16+ import android.widget.Toast
17+ import androidx.core.app.NotificationCompat
18+ import androidx.core.app.NotificationManagerCompat
19+ import java.util.Calendar
20+ import java.util.Date
21+
22+ class PedometerService : Service (), SensorEventListener {
23+ var sharedPreferences: SharedPreferences ? = null
24+ var TAG : String = " PEDOMETER"
25+ var sensorManager: SensorManager ? = null
26+ var running: Boolean = false
27+ var currentDate: Date ? = null
28+ var initialDate: Date ? = null
29+ override fun onBind (intent : Intent ): IBinder ? {
30+ return null
31+ }
32+
33+ private fun createNotificationChannel () {
34+ val notificationChannel = NotificationChannel (
35+ " PedometerLib" ,
36+ " Service Channel" ,
37+ NotificationManager .IMPORTANCE_DEFAULT
38+ )
39+ val notificationManager = NotificationManagerCompat .from(this )
40+ notificationManager.createNotificationChannel(notificationChannel)
41+ }
42+
43+ private fun startNotification () {
44+ val input = " Counting your steps..."
45+ val notificationIntent = Intent (this , Bridge .myActivity.javaClass)
46+ val pendingIntent = PendingIntent .getActivity(
47+ this ,
48+ 0 , notificationIntent, 0
49+ )
50+ val notification = NotificationCompat .Builder (this , " PedometerLib" )
51+ .setContentTitle(" Background Walking Service" )
52+ .setContentText(input)
53+ .setSmallIcon(R .mipmap.ic_launcher)
54+ .setContentIntent(pendingIntent)
55+ .build()
56+ startForeground(112 , notification)
57+ }
58+
59+ override fun onCreate () {
60+ Log .i(TAG , " onCreate: CREATED" + Bridge .steps)
61+ sharedPreferences = PreferenceManager .getDefaultSharedPreferences(this )
62+ loadData()
63+ saveSummarySteps(Bridge .summarySteps + Bridge .steps)
64+ }
65+
66+ override fun onTaskRemoved (rootIntent : Intent ) {
67+ super .onTaskRemoved(rootIntent)
68+ Log .i(TAG , " onTaskRemoved: REMOVED" + Bridge .steps)
69+ initSensorManager()
70+ }
71+
72+ override fun onStartCommand (intent : Intent , flags : Int , startId : Int ): Int {
73+ Log .i(TAG , " onStartCommand: STARTED" )
74+ createNotificationChannel()
75+ startNotification()
76+ super .onCreate()
77+ Bridge .initialSteps = 0
78+ initSensorManager()
79+ val editor = sharedPreferences!! .edit()
80+ initialDate = Calendar .getInstance().time
81+ editor.putString(Bridge .INIT_DATE , currentDate.toString())
82+ editor.apply ()
83+ return START_NOT_STICKY
84+ }
85+
86+ override fun onDestroy () {
87+ super .onDestroy()
88+ Log .i(TAG , " onDestroy: DESTROYED" )
89+ disposeSensorManager()
90+ loadData()
91+ saveSummarySteps(Bridge .summarySteps + Bridge .steps)
92+ }
93+
94+ override fun onSensorChanged (sensorEvent : SensorEvent ) {
95+ Log .i(TAG , " onSensorChanged!!!!!!: " + sensorEvent.values[0 ])
96+ if (Bridge .initialSteps == 0 ) {
97+ Log .i(TAG , " onSensorChanged: AWAKE" )
98+ Bridge .initialSteps = sensorEvent.values[0 ].toInt()
99+ }
100+ if (running) {
101+ Bridge .steps = sensorEvent.values[0 ].toInt() - Bridge .initialSteps
102+ Log .i(TAG , " onSensorChanged: current steps: " + Bridge .steps)
103+ saveData(Bridge .steps)
104+ }
105+ }
106+
107+ override fun onAccuracyChanged (sensor : Sensor , i : Int ) {}
108+
109+ fun initSensorManager () {
110+ sensorManager = getSystemService(SENSOR_SERVICE ) as SensorManager
111+ running = true
112+ val countSensor = sensorManager!! .getDefaultSensor(Sensor .TYPE_STEP_COUNTER )
113+ if (countSensor != null ) {
114+ sensorManager!! .registerListener(this , countSensor, SensorManager .SENSOR_DELAY_UI )
115+ } else {
116+ Toast .makeText(Bridge .myActivity, " Sensor Not Found (" , Toast .LENGTH_LONG ).show()
117+ }
118+ }
119+
120+ fun disposeSensorManager () {
121+ running = false
122+ sensorManager!! .unregisterListener(this )
123+ }
124+
125+ fun saveData (currentSteps : Int ) {
126+ val editor = sharedPreferences!! .edit()
127+ currentDate = Calendar .getInstance().time
128+ editor.putString(Bridge .DATE , currentDate.toString())
129+ Log .i(TAG , " saveData: saved! $currentSteps " )
130+ editor.putInt(Bridge .STEPS , currentSteps)
131+ editor.apply ()
132+ }
133+
134+ fun saveSummarySteps (stepsToSave : Int ) {
135+ val editor = sharedPreferences!! .edit()
136+ currentDate = Calendar .getInstance().time
137+ editor.putString(Bridge .DATE , currentDate.toString())
138+ Log .i(TAG , " saveSummarySteps: saved! $stepsToSave " )
139+ editor.putInt(" summarySteps" , stepsToSave)
140+ editor.apply ()
141+ }
142+
143+ fun loadData () {
144+ Bridge .steps = sharedPreferences!! .getInt(Bridge .STEPS , 0 )
145+ Bridge .summarySteps = sharedPreferences!! .getInt(" summarySteps" , 0 )
146+ Log .i(TAG , " loadData: steps" + Bridge .steps)
147+ Log .i(TAG , " loadData: summarySteps " + Bridge .summarySteps)
148+ }
149+ }
0 commit comments