diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index ea14af57ee..c046abc397 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -383,6 +383,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB drawerToggle.setDrawerIndicatorEnabled(savedInstanceState.getBoolean("fair:toggle")); checkFirst(); + checkBanner(); checkCrash(); Shortcuts.update(this, this); @@ -821,6 +822,14 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB new FragmentDialogFirst().show(getSupportFragmentManager(), "first"); } + private void checkBanner() { + long now = new Date().getTime(); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + long banner_hidden = prefs.getLong("banner_hidden", 0); + if (banner_hidden > 0 && now > banner_hidden) + prefs.edit().remove("banner_hidden").apply(); + } + private void checkCrash() { new SimpleTask() { @Override diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java index 42c3c7c7a1..fd5765a703 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsMisc.java @@ -526,7 +526,6 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { prefs.edit().putBoolean("protocol", checked).apply(); - ServiceUI.scheduleProtocol(compoundButton.getContext(), checked); } }); diff --git a/app/src/main/java/eu/faircode/email/FragmentPro.java b/app/src/main/java/eu/faircode/email/FragmentPro.java index 8ff7dfe35c..851e48ee5b 100644 --- a/app/src/main/java/eu/faircode/email/FragmentPro.java +++ b/app/src/main/java/eu/faircode/email/FragmentPro.java @@ -47,6 +47,8 @@ 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; @@ -61,6 +63,8 @@ public class FragmentPro extends FragmentBase implements SharedPreferences.OnSha private TextView tvRestoreHint; private Button btnCheck; + private static final int HIDE_BANNER = 3; // weeks + @Override @Nullable public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -97,14 +101,19 @@ public class FragmentPro extends FragmentBase implements SharedPreferences.OnSha tvInfo.setText(getString(R.string.title_pro_info) .replaceAll("^\\s+", "").replaceAll("\\s+", " ")); + long now = new Date().getTime(); long banner_hidden = prefs.getLong("banner_hidden", 0); - cbHide.setChecked(banner_hidden > 0); - cbHide.setText(getString(R.string.title_pro_hide, ServiceUI.HIDE_BANNER)); + cbHide.setChecked(banner_hidden > 0 && now < banner_hidden); + cbHide.setText(getString(R.string.title_pro_hide, HIDE_BANNER)); cbHide.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - ServiceUI.scheduleBanner(getContext(), isChecked); + if (isChecked) { + long banner_hidden = new Date().getTime() + HIDE_BANNER * 7 * 24 * 3600 * 1000L; + prefs.edit().putLong("banner_hidden", banner_hidden).apply(); + } else + prefs.edit().remove("banner_hidden").apply(); } }); @@ -294,8 +303,9 @@ public class FragmentPro extends FragmentBase implements SharedPreferences.OnSha btnBackup.setVisibility(pro ? View.VISIBLE : View.GONE); cbHide.setVisibility(pro ? View.GONE : View.VISIBLE); } else if ("banner_hidden".equals(key)) { + long now = new Date().getTime(); long banner_hidden = prefs.getLong("banner_hidden", 0); - cbHide.setChecked(banner_hidden > 0); + cbHide.setChecked(banner_hidden > 0 && now < banner_hidden); } } } diff --git a/app/src/main/java/eu/faircode/email/ReceiverAutoStart.java b/app/src/main/java/eu/faircode/email/ReceiverAutoStart.java index a3bd6ed8db..15f4d6cbde 100644 --- a/app/src/main/java/eu/faircode/email/ReceiverAutoStart.java +++ b/app/src/main/java/eu/faircode/email/ReceiverAutoStart.java @@ -35,7 +35,6 @@ public class ReceiverAutoStart extends BroadcastReceiver { ServiceSynchronize.boot(context); ServiceSend.boot(context); - ServiceUI.boot(context); } } } diff --git a/app/src/main/java/eu/faircode/email/ServiceUI.java b/app/src/main/java/eu/faircode/email/ServiceUI.java index bc3a52df04..d2fffb9949 100644 --- a/app/src/main/java/eu/faircode/email/ServiceUI.java +++ b/app/src/main/java/eu/faircode/email/ServiceUI.java @@ -19,17 +19,14 @@ package eu.faircode.email; Copyright 2018-2021 by Marcel Bokhorst (M66B) */ -import android.app.AlarmManager; import android.app.IntentService; import android.app.NotificationManager; -import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import androidx.annotation.Nullable; -import androidx.core.app.AlarmManagerCompat; import androidx.core.app.RemoteInput; import androidx.preference.PreferenceManager; @@ -62,12 +59,7 @@ public class ServiceUI extends IntentService { static final int PI_THREAD = 11; static final int PI_WAKEUP = 12; - static final int PI_BANNER = 14; static final int PI_EXISTS = 15; - static final int PI_PROTOCOL = 16; - - static final int HIDE_BANNER = 3; // weeks - static final int DISABLE_PROTOCOL = 30; // minutes public ServiceUI() { this(ServiceUI.class.getName()); @@ -176,15 +168,6 @@ public class ServiceUI extends IntentService { onExists(id); break; - case "daily": - case "banner": - onBanner(); - break; - - case "protocol": - onProtocol(); - return; // No eval - default: throw new IllegalArgumentException("Unknown UI action: " + parts[0]); } @@ -581,77 +564,8 @@ public class ServiceUI extends IntentService { } } - private void onBanner() { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - prefs.edit().remove("banner_hidden").apply(); - } - - private void onProtocol() { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - prefs.edit().remove("protocol").apply(); - } - static void sync(Context context, Long account) { context.startService(new Intent(context, ServiceUI.class) .setAction(account == null ? "sync" : "sync:" + account)); } - - private static PendingIntent getBannerIntent(Context context) { - Intent intent = new Intent(context, ServiceUI.class); - intent.setAction("banner"); - return PendingIntentCompat.getService( - context, ServiceUI.PI_BANNER, intent, PendingIntent.FLAG_UPDATE_CURRENT); - } - - static void scheduleBanner(Context context, boolean set) { - AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - if (set) { - long now = new Date().getTime(); - long interval = AlarmManager.INTERVAL_DAY * HIDE_BANNER * 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, getBannerIntent(context)); - prefs.edit().putLong("banner_hidden", trigger).apply(); - } else { - Log.i("Cancel banner alarm"); - am.cancel(getBannerIntent(context)); - prefs.edit().remove("banner_hidden").apply(); - } - } - - private static PendingIntent getProtocolIntent(Context context) { - Intent intent = new Intent(context, ServiceUI.class); - intent.setAction("protocol"); - return PendingIntentCompat.getService( - context, ServiceUI.PI_PROTOCOL, intent, PendingIntent.FLAG_UPDATE_CURRENT); - } - - static void scheduleProtocol(Context context, boolean set) { - AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); - if (set) { - long interval = DISABLE_PROTOCOL * 60 * 1000L; - long trigger = new Date().getTime() + interval; - Log.i("Set protocol alarm at " + new Date(trigger)); - AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC, trigger, getProtocolIntent(context)); - } else { - Log.i("Cancel protocol alarm"); - am.cancel(getProtocolIntent(context)); - } - } - - static void boot(Context context) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - - long banner_hidden = prefs.getLong("banner_hidden", 0); - if (banner_hidden > 0) { - Log.i("Restore banner alarm at " + new Date(banner_hidden)); - AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); - am.set(AlarmManager.RTC, banner_hidden, getBannerIntent(context)); - } - - boolean protocol = prefs.getBoolean("protocol", false); - ServiceUI.scheduleProtocol(context, protocol); - } }