Refactoring

pull/177/head
M66B 5 years ago
parent 29e6fb7076
commit fceefc1b4b

@ -19,9 +19,6 @@ package eu.faircode.email;
Copyright 2018-2020 by Marcel Bokhorst (M66B)
*/
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Paint;
@ -44,8 +41,6 @@ import androidx.preference.PreferenceManager;
import com.google.android.material.snackbar.Snackbar;
import java.util.Date;
public class FragmentPro extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
private TextView tvPending;
private TextView tvActivated;
@ -90,23 +85,7 @@ public class FragmentPro extends FragmentBase implements SharedPreferences.OnSha
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
prefs.edit().putBoolean("banner", !isChecked).apply();
Intent banner = new Intent(getContext(), ServiceUI.class);
banner.setAction("banner");
PendingIntent pi = PendingIntent.getService(getContext(), ServiceUI.PI_BANNER, banner, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
if (isChecked) {
long now = new Date().getTime();
long interval = AlarmManager.INTERVAL_DAY * 7;
long due = interval - (now % interval);
long trigger = now + due;
Log.i("Set banner alarm at " + new Date(trigger) + " due=" + due);
am.set(AlarmManager.RTC, trigger, pi);
} else {
Log.i("Cancel banner alarm");
am.cancel(pi);
}
ServiceUI.schedule(getContext(), isChecked);
}
});

@ -505,4 +505,23 @@ public class ServiceUI extends IntentService {
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, next, piSync);
}
}
static void scheduleBanner(Context context, boolean set) {
Intent banner = new Intent(context, ServiceUI.class);
banner.setAction("banner");
PendingIntent pi = PendingIntent.getService(context, ServiceUI.PI_BANNER, banner, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (set) {
long now = new Date().getTime();
long interval = AlarmManager.INTERVAL_DAY * 7;
long due = interval - (now % interval);
long trigger = now + due;
Log.i("Set banner alarm at " + new Date(trigger) + " due=" + due);
am.set(AlarmManager.RTC, trigger, pi);
} else {
Log.i("Cancel banner alarm");
am.cancel(pi);
}
}
}

Loading…
Cancel
Save