mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-23 02:10:44 +01:00
Update restore progress banner UI/UX and job behavior.
This commit is contained in:
committed by
Greyson Parrelli
parent
320d51707d
commit
93609106b0
@@ -0,0 +1,39 @@
|
||||
package org.thoughtcrime.securesms.jobmanager.impl
|
||||
|
||||
import android.app.job.JobInfo
|
||||
import androidx.annotation.RequiresApi
|
||||
import org.thoughtcrime.securesms.jobmanager.Constraint
|
||||
|
||||
/**
|
||||
* Job constraint for determining whether or not the device battery is not low.
|
||||
*/
|
||||
class BatteryNotLowConstraint private constructor() : Constraint {
|
||||
companion object {
|
||||
const val KEY: String = "BatteryNotLowConstraint"
|
||||
|
||||
fun isMet(): Boolean {
|
||||
return ChargingAndBatteryIsNotLowConstraintObserver.isCharging() || ChargingAndBatteryIsNotLowConstraintObserver.isBatteryNotLow()
|
||||
}
|
||||
}
|
||||
|
||||
override fun getFactoryKey(): String = KEY
|
||||
|
||||
override fun isMet(): Boolean {
|
||||
return Companion.isMet()
|
||||
}
|
||||
|
||||
@RequiresApi(26)
|
||||
override fun applyToJobInfo(jobInfoBuilder: JobInfo.Builder) {
|
||||
jobInfoBuilder.setRequiresBatteryNotLow(true)
|
||||
}
|
||||
|
||||
override fun getJobSchedulerKeyPart(): String? {
|
||||
return "BATTERY_NOT_LOW"
|
||||
}
|
||||
|
||||
class Factory : Constraint.Factory<BatteryNotLowConstraint?> {
|
||||
override fun create(): BatteryNotLowConstraint {
|
||||
return BatteryNotLowConstraint()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package org.thoughtcrime.securesms.jobmanager.impl;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.BatteryManager;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.jobmanager.ConstraintObserver;
|
||||
|
||||
/**
|
||||
* Observes the charging state and low battery state of the device and notifies the JobManager system when appropriate.
|
||||
*/
|
||||
public class ChargingAndBatteryIsNotLowConstraintObserver implements ConstraintObserver {
|
||||
|
||||
private static final String REASON = Log.tag(ChargingAndBatteryIsNotLowConstraintObserver.class);
|
||||
private static final int STATUS_BATTERY = 0;
|
||||
private static final int LOW_BATTERY_LEVEL = 20;
|
||||
|
||||
private final Application application;
|
||||
|
||||
private static volatile boolean charging;
|
||||
private static volatile boolean batteryNotLow;
|
||||
|
||||
public ChargingAndBatteryIsNotLowConstraintObserver(@NonNull Application application) {
|
||||
this.application = application;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(@NonNull Notifier notifier) {
|
||||
Intent intent = application.registerReceiver(new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
boolean wasCharging = charging;
|
||||
boolean wasBatteryNotLow = batteryNotLow;
|
||||
|
||||
charging = isCharging(intent);
|
||||
batteryNotLow = isBatteryNotLow(intent);
|
||||
|
||||
if ((charging && !wasCharging) || (batteryNotLow && !wasBatteryNotLow)) {
|
||||
notifier.onConstraintMet(REASON);
|
||||
}
|
||||
}
|
||||
}, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
|
||||
charging = isCharging(intent);
|
||||
}
|
||||
|
||||
public static boolean isCharging() {
|
||||
return charging;
|
||||
}
|
||||
|
||||
public static boolean isBatteryNotLow() {
|
||||
return batteryNotLow;
|
||||
}
|
||||
|
||||
private static boolean isCharging(@Nullable Intent intent) {
|
||||
if (intent == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int status = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, STATUS_BATTERY);
|
||||
return status != STATUS_BATTERY;
|
||||
}
|
||||
|
||||
private static boolean isBatteryNotLow(@Nullable Intent intent) {
|
||||
if (intent == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
|
||||
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
|
||||
|
||||
if (level <= 0 || scale <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ((int) Math.floor(level * 100 / (double) scale)) > LOW_BATTERY_LEVEL;
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ public class ChargingConstraint implements Constraint {
|
||||
|
||||
@Override
|
||||
public boolean isMet() {
|
||||
return ChargingConstraintObserver.isCharging();
|
||||
return ChargingAndBatteryIsNotLowConstraintObserver.isCharging();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
package org.thoughtcrime.securesms.jobmanager.impl;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.BatteryManager;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.jobmanager.ConstraintObserver;
|
||||
|
||||
/**
|
||||
* Observes the charging state of the device and notifies the JobManager system when appropriate.
|
||||
*/
|
||||
public class ChargingConstraintObserver implements ConstraintObserver {
|
||||
|
||||
private static final String REASON = Log.tag(ChargingConstraintObserver.class);
|
||||
private static final int STATUS_BATTERY = 0;
|
||||
|
||||
private final Application application;
|
||||
|
||||
private static volatile boolean charging;
|
||||
|
||||
public ChargingConstraintObserver(@NonNull Application application) {
|
||||
this.application = application;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(@NonNull Notifier notifier) {
|
||||
Intent intent = application.registerReceiver(new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
boolean wasCharging = charging;
|
||||
|
||||
charging = isCharging(intent);
|
||||
|
||||
if (charging && !wasCharging) {
|
||||
notifier.onConstraintMet(REASON);
|
||||
}
|
||||
}
|
||||
}, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
|
||||
charging = isCharging(intent);
|
||||
}
|
||||
|
||||
public static boolean isCharging() {
|
||||
return charging;
|
||||
}
|
||||
|
||||
private static boolean isCharging(@Nullable Intent intent) {
|
||||
if (intent == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int status = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, STATUS_BATTERY);
|
||||
return status != STATUS_BATTERY;
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ package org.thoughtcrime.securesms.jobmanager.impl
|
||||
|
||||
import android.app.Application
|
||||
import android.app.job.JobInfo
|
||||
import android.content.Context
|
||||
import org.thoughtcrime.securesms.jobmanager.Constraint
|
||||
import org.thoughtcrime.securesms.util.NetworkUtil
|
||||
|
||||
@@ -17,10 +18,14 @@ class WifiConstraint(private val application: Application) : Constraint {
|
||||
|
||||
companion object {
|
||||
const val KEY = "WifiConstraint"
|
||||
|
||||
fun isMet(context: Context): Boolean {
|
||||
return NetworkUtil.isConnectedWifi(context)
|
||||
}
|
||||
}
|
||||
|
||||
override fun isMet(): Boolean {
|
||||
return NetworkUtil.isConnectedWifi(application)
|
||||
return isMet(application)
|
||||
}
|
||||
|
||||
override fun getFactoryKey(): String = KEY
|
||||
|
||||
Reference in New Issue
Block a user