Created
July 1, 2024 12:21
-
-
Save sajjadyousefnia/51787cff5e3d6b6bb8f49832cc60c239 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sajjady.sleepbehavior | |
import android.app.AlarmManager | |
import android.app.PendingIntent | |
import android.content.Context | |
import android.content.Intent | |
import android.net.Uri | |
import android.os.Build | |
import android.os.Bundle | |
import android.os.PowerManager | |
import android.provider.Settings | |
import androidx.activity.enableEdgeToEdge | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.appcompat.app.AppCompatDelegate | |
import androidx.core.content.ContextCompat | |
import com.sajjady.sleepbehavior.databinding.ActivityMainBinding | |
class MainActivity : AppCompatActivity() { | |
private lateinit var powerManager: PowerManager | |
private lateinit var pendingIntent: PendingIntent | |
private lateinit var alarmIntent: Intent | |
private lateinit var alarmManager: AlarmManager | |
// val isIgnoringBatteryOptimizations = powerManager.isIgnoringBatteryOptimizations(packageName) | |
lateinit var binding: ActivityMainBinding | |
private val INTERVAL_TIME = 1L * 60L * 1000L | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
enableEdgeToEdge() | |
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); | |
binding = ActivityMainBinding.inflate(layoutInflater) | |
setContentView(binding.root) | |
alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager | |
alarmIntent = Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM) | |
pendingIntent = | |
PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_MUTABLE) | |
powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager | |
binding.btnAlarmSet.setOnClickListener { | |
requestAlarmManagerType() | |
} | |
binding.btnAlarmOff.setOnClickListener { | |
cancelAlarm(this) | |
} | |
} | |
fun requestAlarmManagerType() { | |
if (requestIgnoreBatteryOptimizations(this)) { | |
if (requestExactAlarmManager()) { | |
if (getDozeModePermission()) { | |
scheduleRepeatingAlarm(this) | |
} | |
} | |
} | |
} | |
fun cancelAlarm(context: Context) { | |
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager | |
val intent = Intent(context, AlarmReceiver::class.java) | |
val pendingIntent = | |
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) | |
alarmManager.cancel(pendingIntent) | |
} | |
fun getDozeModePermission(): Boolean { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
alarmManager.setAndAllowWhileIdle( | |
AlarmManager.RTC_WAKEUP, | |
System.currentTimeMillis() + INTERVAL_TIME, | |
pendingIntent | |
) | |
return true | |
} else { | |
return true | |
} | |
} | |
fun scheduleRepeatingAlarm(context: Context) { | |
// val intervalMillis = AlarmManager.INTERVAL_FIFTEEN_MINUTES | |
val intervalMillis = INTERVAL_TIME | |
alarmManager.setRepeating( | |
AlarmManager.RTC_WAKEUP, | |
System.currentTimeMillis() + intervalMillis, | |
intervalMillis, | |
pendingIntent | |
) | |
} | |
fun requestExactAlarmManager(): Boolean { | |
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { | |
if (!alarmManager.canScheduleExactAlarms()) { | |
// Direct the user to settings to enable the permission | |
startActivity(alarmIntent) | |
return false | |
} else { | |
return true | |
} | |
} else { | |
return true | |
} | |
} | |
fun requestIgnoreBatteryOptimizations(context: Context): Boolean { | |
// val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager | |
val packageName = context.packageName | |
alarmPlanAgreed() | |
if (!powerManager.isIgnoringBatteryOptimizations(packageName)) { | |
val intent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) | |
intent.data = Uri.parse("package:$packageName") | |
context.startActivity(intent) | |
return false | |
} else { | |
return true | |
} | |
} | |
override fun onResume() { | |
super.onResume() | |
if (getSharedPreferences( | |
ContextCompat.getString(this, R.string.APP_REQUESTS), Context.MODE_PRIVATE | |
).getBoolean( | |
ContextCompat.getString(this, R.string.APP_REQUESTS_BATTERY), false | |
) | |
) { | |
val requestedBattery = getSharedPreferences( | |
ContextCompat.getString(this, R.string.APP_REQUESTS), | |
Context.MODE_PRIVATE | |
).getBoolean(ContextCompat.getString(this, R.string.APP_REQUESTS_BATTERY), false) | |
if (requestedBattery) { | |
if (!powerManager.isIgnoringBatteryOptimizations(packageName)) { | |
if (requestExactAlarmManager()) { | |
if (getDozeModePermission()) { | |
scheduleRepeatingAlarm(this) | |
} | |
alarmPlanCancelled() | |
} | |
} | |
} | |
} | |
} | |
fun alarmPlanCancelled() { | |
getSharedPreferences( | |
ContextCompat.getString(this, R.string.APP_REQUESTS), | |
MODE_PRIVATE | |
).edit() | |
.putBoolean( | |
ContextCompat.getString( | |
this, | |
R.string.APP_REQUESTS_BATTERY | |
), false | |
) | |
.apply() | |
} | |
fun alarmPlanAgreed() { | |
getSharedPreferences( | |
ContextCompat.getString(this, R.string.APP_REQUESTS), | |
MODE_PRIVATE | |
).edit() | |
.putBoolean( | |
ContextCompat.getString( | |
this, | |
R.string.APP_REQUESTS_BATTERY | |
), true | |
) | |
.apply() | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
alarmPlanCancelled() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment