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" const val CHANNEL_DEFAULT_ID = "default"
} }
private var manager: NotificationManager? = null
init {
manager = ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
}
fun createNotificationChannels() { fun createNotificationChannels() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
val notificationManager = manager?.createNotificationChannel(
ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(
NotificationChannel( NotificationChannel(
CHANNEL_DEFAULT_ID, CHANNEL_DEFAULT_ID,
ctx.getString(R.string.notification_channel_default_name), ctx.getString(R.string.notification_channel_default_name),

View File

@ -9,7 +9,8 @@ class CodeReceiver : BroadcastReceiver() {
const val KEY = "code" const val KEY = "code"
const val ACTION = "me.lucky.wasted.action.TRIGGER" 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 prefs = Preferences(context)
val code = prefs.code val code = prefs.code
if (!prefs.isServiceEnabled || 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) panic(context, intent)
} }
} }

View File

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

View File

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

View File

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

View File

@ -6,7 +6,8 @@ import android.content.Intent
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
class RestartReceiver : BroadcastReceiver() { 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 && if (intent.action != Intent.ACTION_BOOT_COMPLETED &&
intent.action != Intent.ACTION_MY_PACKAGE_REPLACED) return intent.action != Intent.ACTION_MY_PACKAGE_REPLACED) return
val prefs = Preferences(context) val prefs = Preferences(context)

View File

@ -19,13 +19,17 @@ class UnlockService : Service() {
private lateinit var receiver: BroadcastReceiver private lateinit var receiver: BroadcastReceiver
private class UnlockReceiver : 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 val keyguardManager = context
.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager .getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager?
if (!keyguardManager.isDeviceSecure) return if (keyguardManager?.isDeviceSecure != true) return
val manager = WipeJobManager(context) val manager = WipeJobManager(context)
while (manager.schedule() != JobScheduler.RESULT_SUCCESS) var delay = 1000L
SystemClock.sleep(1000) 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 const val JOB_ID = 1000
} }
private val prefs by lazy { Preferences(ctx) } private val prefs by lazy { Preferences(ctx) }
private val jobScheduler by lazy { private var scheduler: JobScheduler? = null
ctx.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
init {
scheduler = ctx.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler?
} }
fun schedule(): Int { fun schedule(): Int {
return jobScheduler.schedule( return scheduler?.schedule(
JobInfo.Builder(JOB_ID, ComponentName(ctx, WipeJobService::class.java)) JobInfo.Builder(JOB_ID, ComponentName(ctx, WipeJobService::class.java))
.setMinimumLatency(TimeUnit.DAYS.toMillis(prefs.wipeOnInactivityDays.toLong())) .setMinimumLatency(TimeUnit.DAYS.toMillis(prefs.wipeOnInactivityDays.toLong()))
.setBackoffCriteria(0, JobInfo.BACKOFF_POLICY_LINEAR) .setBackoffCriteria(0, JobInfo.BACKOFF_POLICY_LINEAR)
.setPersisted(true) .setPersisted(true)
.build() .build()
) ) ?: JobScheduler.RESULT_FAILURE
} }
fun setState(value: Boolean): Boolean { fun setState(value: Boolean): Boolean {
if (value) { if (value) {
if (schedule() == JobScheduler.RESULT_FAILURE) return false if (schedule() == JobScheduler.RESULT_FAILURE) return false
} else { jobScheduler.cancel(JOB_ID) } } else { scheduler?.cancel(JOB_ID) }
return true return true
} }
} }