Add setting to automatically backup - 1.130
Every day at 6am (also immediately when toggled) we will copy the massive.db file to the Download directory.master 36156
parent
4db820f10a
commit
13b340f5be
@ -0,0 +1,87 @@
|
||||
package com.massive
|
||||
|
||||
import android.app.AlarmManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.os.Build
|
||||
import android.os.Environment
|
||||
import androidx.annotation.RequiresApi
|
||||
import com.facebook.react.bridge.ReactApplicationContext
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule
|
||||
import com.facebook.react.bridge.ReactMethod
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
import java.util.*
|
||||
|
||||
class BackupModule constructor(context: ReactApplicationContext?) :
|
||||
ReactContextBaseJavaModule(context) {
|
||||
val context: ReactApplicationContext = reactApplicationContext
|
||||
|
||||
private val copyReceiver = object : BroadcastReceiver() {
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
val sourceFile = File(context?.getDatabasePath("massive.db")!!.path)
|
||||
|
||||
val targetDir =
|
||||
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
|
||||
val targetFile = File(targetDir, "massive.db")
|
||||
|
||||
val input = FileInputStream(sourceFile)
|
||||
val output = FileOutputStream(targetFile)
|
||||
|
||||
input.copyTo(output)
|
||||
input.close()
|
||||
output.close()
|
||||
}
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.M)
|
||||
@ReactMethod
|
||||
fun start() {
|
||||
val alarmMgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
val intent = Intent(COPY_BROADCAST)
|
||||
val pendingIntent =
|
||||
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
|
||||
pendingIntent.send()
|
||||
|
||||
val calendar = Calendar.getInstance().apply {
|
||||
timeInMillis = System.currentTimeMillis()
|
||||
set(Calendar.HOUR_OF_DAY, 6)
|
||||
set(Calendar.MINUTE, 0)
|
||||
set(Calendar.SECOND, 0)
|
||||
}
|
||||
|
||||
alarmMgr.setRepeating(
|
||||
AlarmManager.RTC_WAKEUP,
|
||||
calendar.timeInMillis,
|
||||
AlarmManager.INTERVAL_DAY,
|
||||
pendingIntent
|
||||
)
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.M)
|
||||
@ReactMethod
|
||||
fun stop() {
|
||||
val alarmMgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
val intent = Intent(COPY_BROADCAST)
|
||||
val pendingIntent =
|
||||
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
|
||||
alarmMgr.cancel(pendingIntent)
|
||||
}
|
||||
|
||||
init {
|
||||
reactApplicationContext.registerReceiver(copyReceiver, IntentFilter(COPY_BROADCAST))
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val COPY_BROADCAST = "copy-event"
|
||||
}
|
||||
|
||||
override fun getName(): String {
|
||||
return "BackupModule"
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
import {MigrationInterface, QueryRunner} from 'typeorm'
|
||||
|
||||
export class addBackup1678334268359 implements MigrationInterface {
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner
|
||||
.query('ALTER TABLE settings ADD COLUMN backup BOOLEAN DEFAULT false')
|
||||
.catch(() => null)
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.dropColumn('settings', 'backup')
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue