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
class AlarmReceiver: BroadcastReceiver() { | |
val TAG = "AlarmManager" | |
override fun onReceive(context: Context, intent: Intent) { | |
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) | |
as NotificationManager | |
val intentFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED) | |
val batteryStatus = context.registerReceiver(null, intentFilter) |
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
val alarmIntent = Intent(context, AlarmReceiver::class.java) | |
val pendingIntent = PendingIntent.getBroadcast(context, JOB_ID, alarmIntent, 0) | |
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager | |
if (batteryStatus.shouldShowNotification()) { | |
Log.d(TAG, "onReceive: device is charging or unknown, scheduling job.") | |
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 0, JOB_INTERVAL, | |
pendingIntent) |
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
if (isCharging || isUnknown) { | |
jobScheduler.schedule(jobInfo(context)) | |
} else if (hasBeenDisconnected) { | |
jobScheduler.cancelAll() | |
// FIXME this is a pretty hacky way of getting the job done :( | |
(context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager) | |
.cancel(NOTIFICATION_ID) | |
} |
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
private fun buildNotification(batteryStatus: Intent): Notification { | |
val level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) | |
return NotificationCompat.Builder(this) | |
.setContentTitle(getString(R.string.app_name)) | |
.setContentText("Phone is $level% charged.") | |
.setSmallIcon(R.mipmap.ic_launcher) | |
.setOnlyAlertOnce(true) | |
.build() | |
} |
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
private fun buildNotification(batteryStatus: Intent): Notification { | |
val level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) | |
return NotificationCompat.Builder(this) | |
.setContentTitle(getString(R.string.app_name)) | |
.setContentText("Phone is $level% charged.") | |
.setSmallIcon(R.mipmap.ic_launcher) | |
.setOnlyAlertOnce(true) | |
.build() | |
} |
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
override fun onStartJob(jobParams: JobParameters): Boolean { | |
val intentFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED) | |
val batteryStatus = registerReceiver(null, intentFilter) | |
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | |
notificationManager.notify(Constants.NOTIFICATION_ID, buildNotification(batteryStatus)) | |
jobFinished(jobParams, false) | |
return false | |
} |
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
<service android:name=".PowerService" | |
android:permission="android.permission.BIND_JOB_SERVICE" | |
/> |
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
private fun jobInfo(context: Context): JobInfo { | |
val serviceName = ComponentName(context, PowerService::class.java) | |
return JobInfo.Builder(JOB_ID, serviceName) | |
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_NONE) | |
.setRequiresCharging(true) | |
.setPeriodic(JOB_INTERVAL) | |
.build() | |
} |
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
if (isCharging) { | |
jobScheduler.schedule(jobInfo(context)) | |
} else if (hasBeenDisconnected) { | |
jobScheduler.cancelAll() | |
} |
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
val intentFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED) | |
val batteryStatus = context.registerReceiver(null, intentFilter) | |
val status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1) |
NewerOlder