Get alarm working
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.example.fmassive">
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
<uses-permission android:name="android.permission.VIBRATE" />
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
<uses-permission android:name="android.permission.VIBRATE" />
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
|
||||
@@ -39,5 +39,14 @@
|
||||
<meta-data
|
||||
android:name="flutterEmbedding"
|
||||
android:value="2" />
|
||||
</application>
|
||||
|
||||
<activity
|
||||
android:name=".StopAlarm"
|
||||
android:exported="true"
|
||||
android:process=":remote" />
|
||||
|
||||
<service
|
||||
android:name=".AlarmService"
|
||||
android:exported="false" />
|
||||
</application>
|
||||
</manifest>
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
package com.example.fmassive
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.*
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.media.AudioAttributes
|
||||
import android.media.MediaPlayer
|
||||
import android.media.MediaPlayer.OnPreparedListener
|
||||
import android.net.Uri
|
||||
import android.os.*
|
||||
import android.util.Log
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.core.app.NotificationCompat
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
class AlarmService : Service(), OnPreparedListener {
|
||||
private var mediaPlayer: MediaPlayer? = null
|
||||
private var vibrator: Vibrator? = null
|
||||
|
||||
private fun getBuilder(): NotificationCompat.Builder {
|
||||
val context = applicationContext
|
||||
val contentIntent = Intent(context, MainActivity::class.java)
|
||||
val pendingContent =
|
||||
PendingIntent.getActivity(context, 0, contentIntent, PendingIntent.FLAG_IMMUTABLE)
|
||||
val addBroadcast = Intent(MainActivity.ADD_BROADCAST).apply {
|
||||
setPackage(context.packageName)
|
||||
}
|
||||
val pendingAdd =
|
||||
PendingIntent.getBroadcast(context, 0, addBroadcast, PendingIntent.FLAG_MUTABLE)
|
||||
val stopBroadcast = Intent(MainActivity.STOP_BROADCAST)
|
||||
stopBroadcast.setPackage(context.packageName)
|
||||
val pendingStop =
|
||||
PendingIntent.getBroadcast(context, 0, stopBroadcast, PendingIntent.FLAG_IMMUTABLE)
|
||||
return NotificationCompat.Builder(context, MainActivity.CHANNEL_ID_PENDING)
|
||||
.setSmallIcon(R.drawable.ic_baseline_hourglass_bottom_24).setContentTitle("Resting")
|
||||
.setContentIntent(pendingContent)
|
||||
.addAction(R.drawable.ic_baseline_stop_24, "Stop", pendingStop)
|
||||
.addAction(R.drawable.ic_baseline_stop_24, "Add 1 min", pendingAdd)
|
||||
.setDeleteIntent(pendingStop)
|
||||
}
|
||||
|
||||
|
||||
@SuppressLint("Range")
|
||||
private fun getSettings(): Settings {
|
||||
val db = DatabaseHelper(applicationContext).readableDatabase
|
||||
val cursor = db.rawQuery("SELECT sound, no_sound, vibrate FROM settings", null)
|
||||
cursor.moveToFirst()
|
||||
val sound = cursor.getString(cursor.getColumnIndex("sound"))
|
||||
val noSound = cursor.getInt(cursor.getColumnIndex("no_sound")) == 1
|
||||
val vibrate = cursor.getInt(cursor.getColumnIndex("vibrate")) == 1
|
||||
Log.d("AlarmService", "vibrate=$vibrate")
|
||||
cursor.close()
|
||||
return Settings(sound, noSound, vibrate)
|
||||
}
|
||||
|
||||
private fun playSound(settings: Settings) {
|
||||
if (settings.sound == null && !settings.noSound) {
|
||||
Log.d("AlarmService", "Playing default alarm sound...")
|
||||
mediaPlayer = MediaPlayer.create(applicationContext, R.raw.argon)
|
||||
mediaPlayer?.start()
|
||||
mediaPlayer?.setOnCompletionListener { vibrator?.cancel() }
|
||||
} else if (settings.sound != null && !settings.noSound) {
|
||||
Log.d("AlarmService", "Playing custom alarm sound ${settings.sound}...")
|
||||
mediaPlayer = MediaPlayer().apply {
|
||||
setAudioAttributes(
|
||||
AudioAttributes.Builder()
|
||||
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
|
||||
.setUsage(AudioAttributes.USAGE_MEDIA)
|
||||
.build()
|
||||
)
|
||||
setDataSource(applicationContext, Uri.parse(settings.sound))
|
||||
prepare()
|
||||
start()
|
||||
setOnCompletionListener { vibrator?.cancel() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun doNotify(): Notification {
|
||||
Log.d("AlarmService", "doNotify")
|
||||
val alarmsChannel = NotificationChannel(
|
||||
CHANNEL_ID_DONE,
|
||||
CHANNEL_ID_DONE,
|
||||
NotificationManager.IMPORTANCE_HIGH
|
||||
)
|
||||
alarmsChannel.description = "Alarms for rest timers."
|
||||
alarmsChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
|
||||
alarmsChannel.setSound(null, null)
|
||||
val manager = applicationContext.getSystemService(
|
||||
NotificationManager::class.java
|
||||
)
|
||||
manager.createNotificationChannel(alarmsChannel)
|
||||
val builder = getBuilder()
|
||||
val context = applicationContext
|
||||
val finishIntent = Intent(context, StopAlarm::class.java)
|
||||
val finishPending = PendingIntent.getActivity(
|
||||
context, 0, finishIntent, PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
builder.setContentText("Timer finished.").setProgress(0, 0, false)
|
||||
.setAutoCancel(true).setOngoing(true)
|
||||
.setContentIntent(finishPending).setChannelId(CHANNEL_ID_DONE)
|
||||
.setCategory(NotificationCompat.CATEGORY_ALARM).priority =
|
||||
NotificationCompat.PRIORITY_HIGH
|
||||
val notification = builder.build()
|
||||
manager.notify(NOTIFICATION_ID_DONE, notification)
|
||||
manager.cancel(MainActivity.NOTIFICATION_ID_PENDING)
|
||||
return notification
|
||||
}
|
||||
|
||||
@SuppressLint("Recycle")
|
||||
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
|
||||
Log.d("AlarmService", "onStartCommand")
|
||||
val notification = doNotify()
|
||||
startForeground(NOTIFICATION_ID_DONE, notification)
|
||||
val settings = getSettings()
|
||||
playSound(settings)
|
||||
if (!settings.vibrate) return START_STICKY
|
||||
val pattern = longArrayOf(0, 300, 1300, 300, 1300, 300)
|
||||
vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
val vibratorManager =
|
||||
getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
|
||||
vibratorManager.defaultVibrator
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
getSystemService(VIBRATOR_SERVICE) as Vibrator
|
||||
}
|
||||
val audioAttributes = AudioAttributes.Builder()
|
||||
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
|
||||
.setUsage(AudioAttributes.USAGE_ALARM)
|
||||
.build()
|
||||
vibrator!!.vibrate(VibrationEffect.createWaveform(pattern, 1), audioAttributes)
|
||||
return START_STICKY
|
||||
}
|
||||
|
||||
override fun onBind(intent: Intent): IBinder? {
|
||||
return null
|
||||
}
|
||||
|
||||
override fun onPrepared(player: MediaPlayer) {
|
||||
player.start()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
mediaPlayer?.stop()
|
||||
mediaPlayer?.release()
|
||||
vibrator?.cancel()
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val CHANNEL_ID_DONE = "Alarm"
|
||||
const val NOTIFICATION_ID_DONE = 2
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.example.fmassive
|
||||
|
||||
import android.content.Context
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import android.database.sqlite.SQLiteOpenHelper
|
||||
|
||||
class DatabaseHelper(context: Context) :
|
||||
SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {
|
||||
companion object {
|
||||
private const val DATABASE_NAME = "massive.db"
|
||||
private const val DATABASE_VERSION = 1
|
||||
}
|
||||
|
||||
override fun onCreate(db: SQLiteDatabase) {
|
||||
}
|
||||
|
||||
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
|
||||
}
|
||||
|
||||
override fun onDowngrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
|
||||
}
|
||||
}
|
||||
@@ -19,16 +19,14 @@ import io.flutter.plugin.common.MethodChannel
|
||||
import kotlin.math.floor
|
||||
|
||||
class MainActivity : FlutterActivity() {
|
||||
private val CHANNEL = "com.massive/android"
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
|
||||
super.configureFlutterEngine(flutterEngine)
|
||||
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
|
||||
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, FLUTTER_CHANNEL).setMethodCallHandler {
|
||||
call, result ->
|
||||
if (call.method == "timer") {
|
||||
val args = call.arguments as ArrayList<*>
|
||||
timer(args[0] as Long)
|
||||
timer(args[0] as Int)
|
||||
} else {
|
||||
result.notImplemented()
|
||||
}
|
||||
@@ -42,7 +40,7 @@ class MainActivity : FlutterActivity() {
|
||||
private val stopReceiver = object : BroadcastReceiver() {
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
Log.d("AlarmModule", "Received stop broadcast intent")
|
||||
Log.d("MainActivity", "Received stop broadcast intent")
|
||||
stop()
|
||||
}
|
||||
}
|
||||
@@ -59,59 +57,51 @@ class MainActivity : FlutterActivity() {
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
fun add() {
|
||||
Log.d("AlarmModule", "Add 1 min to alarm.")
|
||||
Log.d("MainActivity", "Add 1 min to alarm.")
|
||||
countdownTimer?.cancel()
|
||||
val newMs = if (running) currentMs.toInt().plus(60000) else 60000
|
||||
countdownTimer = getTimer(newMs.toLong())
|
||||
countdownTimer = getTimer(newMs)
|
||||
countdownTimer?.start()
|
||||
running = true
|
||||
//val manager = getManager()
|
||||
//manager.cancel(AlarmService.NOTIFICATION_ID_DONE)
|
||||
//val intent = Intent(context, AlarmService::class.java)
|
||||
//context.stopService(intent)
|
||||
val manager = getManager()
|
||||
manager.cancel(AlarmService.NOTIFICATION_ID_DONE)
|
||||
val intent = Intent(context, AlarmService::class.java)
|
||||
context.stopService(intent)
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
fun stop() {
|
||||
Log.d("AlarmModule", "Stop alarm.")
|
||||
Log.d("MainActivity", "Stop alarm.")
|
||||
countdownTimer?.cancel()
|
||||
running = false
|
||||
//val intent = Intent(context, AlarmService::class.java)
|
||||
//reactApplicationContext?.stopService(intent)
|
||||
//val manager = getManager()
|
||||
//manager.cancel(AlarmService.NOTIFICATION_ID_DONE)
|
||||
//manager.cancel(NOTIFICATION_ID_PENDING)
|
||||
//val params = Arguments.createMap().apply {
|
||||
// putString("minutes", "00")
|
||||
// putString("seconds", "00")
|
||||
//}
|
||||
//reactApplicationContext
|
||||
// .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
|
||||
// .emit("tick", params)
|
||||
val intent = Intent(context, AlarmService::class.java)
|
||||
context.stopService(intent)
|
||||
val manager = getManager()
|
||||
manager.cancel(AlarmService.NOTIFICATION_ID_DONE)
|
||||
manager.cancel(NOTIFICATION_ID_PENDING)
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
fun timer(milliseconds: Long) {
|
||||
fun timer(milliseconds: Int) {
|
||||
context.registerReceiver(stopReceiver, IntentFilter(STOP_BROADCAST))
|
||||
context.registerReceiver(addReceiver, IntentFilter(ADD_BROADCAST))
|
||||
Log.d("AlarmModule", "Queue alarm for $milliseconds delay")
|
||||
//val manager = getManager()
|
||||
//manager.cancel(AlarmService.NOTIFICATION_ID_DONE)
|
||||
//val intent = Intent(reactApplicationContext, AlarmService::class.java)
|
||||
//reactApplicationContext.stopService(intent)
|
||||
Log.d("MainActivity", "Queue alarm for $milliseconds delay")
|
||||
val manager = getManager()
|
||||
manager.cancel(AlarmService.NOTIFICATION_ID_DONE)
|
||||
val intent = Intent(context, AlarmService::class.java)
|
||||
context.stopService(intent)
|
||||
countdownTimer?.cancel()
|
||||
countdownTimer = getTimer(milliseconds)
|
||||
countdownTimer?.start()
|
||||
running = true
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.M)
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
private fun getTimer(
|
||||
endMs: Long,
|
||||
endMs: Int,
|
||||
): CountDownTimer {
|
||||
val builder = getBuilder()
|
||||
return object : CountDownTimer(endMs.toLong(), 1000) {
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
override fun onTick(current: Long) {
|
||||
currentMs = current
|
||||
val seconds =
|
||||
@@ -123,27 +113,13 @@ class MainActivity : FlutterActivity() {
|
||||
.setCategory(NotificationCompat.CATEGORY_PROGRESS).priority =
|
||||
NotificationCompat.PRIORITY_LOW
|
||||
val manager = getManager()
|
||||
Log.d("AlarmModule", "Notify $NOTIFICATION_ID_PENDING")
|
||||
Log.d("MainActivity", "current=$current")
|
||||
manager.notify(NOTIFICATION_ID_PENDING, builder.build())
|
||||
//val params = Arguments.createMap().apply {
|
||||
// putString("minutes", minutes)
|
||||
// putString("seconds", seconds)
|
||||
//}
|
||||
//reactApplicationContext
|
||||
// .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
|
||||
// .emit("tick", params)
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
override fun onFinish() {
|
||||
//val context = reactApplicationContext
|
||||
//context.startForegroundService(Intent(context, AlarmService::class.java))
|
||||
//context
|
||||
// .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
|
||||
// .emit("finish", Arguments.createMap().apply {
|
||||
// putString("minutes", "00")
|
||||
// putString("seconds", "00")
|
||||
// })
|
||||
Log.d("MainActivity", "Finish")
|
||||
context.startForegroundService(Intent(context, AlarmService::class.java))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -190,5 +166,6 @@ class MainActivity : FlutterActivity() {
|
||||
const val ADD_BROADCAST = "add-timer-event"
|
||||
const val CHANNEL_ID_PENDING = "Timer"
|
||||
const val NOTIFICATION_ID_PENDING = 1
|
||||
const val FLUTTER_CHANNEL = "com.massive/android"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.example.fmassive
|
||||
|
||||
class Settings(val sound: String?, val noSound: Boolean, val vibrate: Boolean)
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.example.fmassive
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.annotation.RequiresApi
|
||||
|
||||
class StopAlarm : Activity() {
|
||||
@RequiresApi(Build.VERSION_CODES.O_MR1)
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
Log.d("AlarmActivity", "Call to AlarmActivity")
|
||||
super.onCreate(savedInstanceState)
|
||||
val context = applicationContext
|
||||
context.stopService(Intent(context, AlarmService::class.java))
|
||||
savedInstanceState.apply { setShowWhenLocked(true) }
|
||||
val intent = Intent(context, MainActivity::class.java)
|
||||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
context.startActivity(intent)
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user