null safety

This commit is contained in:
lucky 2022-01-12 09:38:37 +03:00
parent b1ed8b86e4
commit 44a45e77f4
8 changed files with 42 additions and 28 deletions

View File

@ -10,11 +10,15 @@ class AppNotificationManager(private val ctx: Context) {
const val CHANNEL_DEFAULT_ID = "default"
}
private var manager: NotificationManager? = null
init {
manager = ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
}
fun createNotificationChannels() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
val notificationManager =
ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(
manager?.createNotificationChannel(
NotificationChannel(
CHANNEL_DEFAULT_ID,
ctx.getString(R.string.notification_channel_default_name),

View File

@ -9,7 +9,8 @@ class CodeReceiver : BroadcastReceiver() {
const val KEY = "code"
const val ACTION = "me.lucky.wasted.action.TRIGGER"
fun panic(context: Context, intent: Intent) {
fun panic(context: Context?, intent: Intent?) {
if (context == null || intent == null) return
val prefs = Preferences(context)
val code = prefs.code
if (!prefs.isServiceEnabled ||
@ -24,7 +25,7 @@ class CodeReceiver : BroadcastReceiver() {
}
}
override fun onReceive(context: Context, intent: Intent) {
override fun onReceive(context: Context?, intent: Intent?) {
panic(context, intent)
}
}

View File

@ -7,16 +7,18 @@ import android.content.Intent
import android.os.Build
class DeviceAdminManager(private val ctx: Context) {
private val dpm by lazy {
ctx.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
}
private var dpm: DevicePolicyManager? = null
private val deviceAdmin by lazy { ComponentName(ctx, DeviceAdminReceiver::class.java) }
private val prefs by lazy { Preferences(ctx) }
fun remove() = dpm.removeActiveAdmin(deviceAdmin)
fun isActive(): Boolean = dpm.isAdminActive(deviceAdmin)
fun getCurrentFailedPasswordAttempts(): Int = dpm.currentFailedPasswordAttempts
fun lockNow() = dpm.lockNow()
init {
dpm = ctx.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager?
}
fun remove() = dpm?.removeActiveAdmin(deviceAdmin)
fun isActive(): Boolean = dpm?.isAdminActive(deviceAdmin) ?: false
fun getCurrentFailedPasswordAttempts(): Int = dpm?.currentFailedPasswordAttempts ?: 0
fun lockNow() = dpm?.lockNow()
fun wipeData() {
var flags = 0
@ -24,7 +26,7 @@ class DeviceAdminManager(private val ctx: Context) {
flags = flags.or(DevicePolicyManager.WIPE_SILENTLY)
if (prefs.isWipeESIM && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
flags = flags.or(DevicePolicyManager.WIPE_EUICC)
dpm.wipeData(flags)
dpm?.wipeData(flags)
}
fun makeRequestIntent(): Intent {

View File

@ -47,7 +47,7 @@ open class MainActivity : AppCompatActivity() {
private fun update() {
if (!admin.isActive() && prefs.isServiceEnabled)
Snackbar.make(
findViewById(R.id.toggle),
binding.toggle,
R.string.service_unavailable_popup,
Snackbar.LENGTH_SHORT,
).show()
@ -149,7 +149,7 @@ open class MainActivity : AppCompatActivity() {
private fun showWipeJobServiceStartFailedPopup() {
Snackbar.make(
findViewById(R.id.toggle),
binding.toggle,
R.string.wipe_job_service_start_failed_popup,
Snackbar.LENGTH_LONG,
).show()

View File

@ -38,8 +38,8 @@ class Preferences(ctx: Context) {
get() = prefs.getBoolean(SERVICE_ENABLED, false)
set(value) = prefs.edit { putBoolean(SERVICE_ENABLED, value) }
var code: String?
get() = prefs.getString(CODE, "")
var code: String
get() = prefs.getString(CODE, "") ?: ""
set(value) = prefs.edit { putString(CODE, value) }
var isCodeEnabled: Boolean

View File

@ -6,7 +6,8 @@ import android.content.Intent
import androidx.core.content.ContextCompat
class RestartReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
override fun onReceive(context: Context?, intent: Intent?) {
if (context == null || intent == null) return
if (intent.action != Intent.ACTION_BOOT_COMPLETED &&
intent.action != Intent.ACTION_MY_PACKAGE_REPLACED) return
val prefs = Preferences(context)

View File

@ -19,13 +19,17 @@ class UnlockService : Service() {
private lateinit var receiver: BroadcastReceiver
private class UnlockReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
override fun onReceive(context: Context?, intent: Intent?) {
if (context == null) return
val keyguardManager = context
.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
if (!keyguardManager.isDeviceSecure) return
.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager?
if (keyguardManager?.isDeviceSecure != true) return
val manager = WipeJobManager(context)
while (manager.schedule() != JobScheduler.RESULT_SUCCESS)
SystemClock.sleep(1000)
var delay = 1000L
while (manager.schedule() != JobScheduler.RESULT_SUCCESS) {
SystemClock.sleep(delay)
delay = delay.shl(1)
}
}
}

View File

@ -11,24 +11,26 @@ class WipeJobManager(private val ctx: Context) {
private const val JOB_ID = 1000
}
private val prefs by lazy { Preferences(ctx) }
private val jobScheduler by lazy {
ctx.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
private var scheduler: JobScheduler? = null
init {
scheduler = ctx.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler?
}
fun schedule(): Int {
return jobScheduler.schedule(
return scheduler?.schedule(
JobInfo.Builder(JOB_ID, ComponentName(ctx, WipeJobService::class.java))
.setMinimumLatency(TimeUnit.DAYS.toMillis(prefs.wipeOnInactivityDays.toLong()))
.setBackoffCriteria(0, JobInfo.BACKOFF_POLICY_LINEAR)
.setPersisted(true)
.build()
)
) ?: JobScheduler.RESULT_FAILURE
}
fun setState(value: Boolean): Boolean {
if (value) {
if (schedule() == JobScheduler.RESULT_FAILURE) return false
} else { jobScheduler.cancel(JOB_ID) }
} else { scheduler?.cancel(JOB_ID) }
return true
}
}