Commit 2454a339 authored by LuK1337's avatar LuK1337 Committed by Luca Stefani

Updater: Implement auto update check interval preference

* This replaces auto update check switch with a dropdown with 4 options:
  * Never
  * Once a day
  * Once a week (default)
  * Once a month

Change-Id: I4bcae4c013a5d44958f9c54d641e64aac3062a8b
parent ee251456
......@@ -7,13 +7,27 @@
android:paddingStart="24dp"
android:paddingTop="16dp">
<Switch
android:id="@+id/preferences_auto_updates_check"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="@string/menu_auto_updates_check"
android:textSize="16sp" />
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/menu_auto_updates_check"
android:textColor="@color/inverted"
android:textSize="16sp" />
<Spinner
android:id="@+id/preferences_auto_updates_check_interval"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="@array/menu_auto_updates_check_interval_entries" />
</LinearLayout>
<Switch
android:id="@+id/preferences_auto_delete_updates"
......
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2019 The LineageOS Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources>
<string-array name="menu_auto_updates_check_interval_entries" translatable="false">
<item>@string/menu_auto_updates_check_interval_never</item>
<item>@string/menu_auto_updates_check_interval_daily</item>
<item>@string/menu_auto_updates_check_interval_weekly</item>
<item>@string/menu_auto_updates_check_interval_monthly</item>
</string-array>
</resources>
......@@ -64,6 +64,10 @@
<string name="menu_refresh">Refresh</string>
<string name="menu_preferences">Preferences</string>
<string name="menu_auto_updates_check">Auto updates check</string>
<string name="menu_auto_updates_check_interval_daily">Once a day</string>
<string name="menu_auto_updates_check_interval_weekly">Once a week</string>
<string name="menu_auto_updates_check_interval_monthly">Once a month</string>
<string name="menu_auto_updates_check_interval_never">Never</string>
<string name="menu_auto_delete_updates">Delete updates when installed</string>
<string name="menu_delete_update">Delete</string>
<string name="menu_copy_url">Copy URL</string>
......
......@@ -45,6 +45,7 @@ import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
......@@ -292,7 +293,7 @@ public class UpdatesActivity extends UpdatesListActivity {
long millis = System.currentTimeMillis();
preferences.edit().putLong(Constants.PREF_LAST_UPDATE_CHECK, millis).apply();
updateLastCheckedString();
if (json.exists() && preferences.getBoolean(Constants.PREF_AUTO_UPDATES_CHECK, true) &&
if (json.exists() && Utils.isUpdateCheckEnabled(this) &&
Utils.checkForNewUpdates(json, jsonNew)) {
UpdatesCheckReceiver.updateRepeatingUpdatesCheck(this);
}
......@@ -406,7 +407,8 @@ public class UpdatesActivity extends UpdatesListActivity {
private void showPreferencesDialog() {
View view = LayoutInflater.from(this).inflate(R.layout.preferences_dialog, null);
Switch autoCheck = view.findViewById(R.id.preferences_auto_updates_check);
Spinner autoCheckInterval =
view.findViewById(R.id.preferences_auto_updates_check_interval);
Switch autoDelete = view.findViewById(R.id.preferences_auto_delete_updates);
Switch dataWarning = view.findViewById(R.id.preferences_mobile_data_warning);
Switch abPerfMode = view.findViewById(R.id.preferences_ab_perf_mode);
......@@ -416,7 +418,7 @@ public class UpdatesActivity extends UpdatesListActivity {
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
autoCheck.setChecked(prefs.getBoolean(Constants.PREF_AUTO_UPDATES_CHECK, true));
autoCheckInterval.setSelection(Utils.getUpdateCheckSetting(this));
autoDelete.setChecked(prefs.getBoolean(Constants.PREF_AUTO_DELETE_UPDATES, false));
dataWarning.setChecked(prefs.getBoolean(Constants.PREF_MOBILE_DATA_WARNING, true));
abPerfMode.setChecked(prefs.getBoolean(Constants.PREF_AB_PERF_MODE, false));
......@@ -426,8 +428,8 @@ public class UpdatesActivity extends UpdatesListActivity {
.setView(view)
.setOnDismissListener(dialogInterface -> {
prefs.edit()
.putBoolean(Constants.PREF_AUTO_UPDATES_CHECK,
autoCheck.isChecked())
.putInt(Constants.PREF_AUTO_UPDATES_CHECK_INTERVAL,
autoCheckInterval.getSelectedItemPosition())
.putBoolean(Constants.PREF_AUTO_DELETE_UPDATES,
autoDelete.isChecked())
.putBoolean(Constants.PREF_MOBILE_DATA_WARNING,
......@@ -436,7 +438,7 @@ public class UpdatesActivity extends UpdatesListActivity {
abPerfMode.isChecked())
.apply();
if (autoCheck.isChecked()) {
if (Utils.isUpdateCheckEnabled(this)) {
UpdatesCheckReceiver.scheduleRepeatingUpdatesCheck(this);
} else {
UpdatesCheckReceiver.cancelRepeatingUpdatesCheck(this);
......
......@@ -59,7 +59,8 @@ public class UpdatesCheckReceiver extends BroadcastReceiver {
final SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(context);
if (!preferences.getBoolean(Constants.PREF_AUTO_UPDATES_CHECK, true)) {
if (!Utils.isUpdateCheckEnabled(context)) {
return;
}
......@@ -155,15 +156,19 @@ public class UpdatesCheckReceiver extends BroadcastReceiver {
}
public static void scheduleRepeatingUpdatesCheck(Context context) {
long millisToNextRelease = millisToNextRelease(context);
if (!Utils.isUpdateCheckEnabled(context)) {
return;
}
PendingIntent updateCheckIntent = getRepeatingUpdatesCheckIntent(context);
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + millisToNextRelease,
AlarmManager.INTERVAL_DAY, updateCheckIntent);
alarmMgr.setRepeating(AlarmManager.RTC, System.currentTimeMillis() +
Utils.getUpdateCheckInterval(context), Utils.getUpdateCheckInterval(context),
updateCheckIntent);
Date nextCheckDate = new Date(System.currentTimeMillis() + millisToNextRelease);
Log.d(TAG, "Setting daily updates check: " + nextCheckDate);
Date nextCheckDate = new Date(System.currentTimeMillis() +
Utils.getUpdateCheckInterval(context));
Log.d(TAG, "Setting automatic updates check: " + nextCheckDate);
}
public static void cancelRepeatingUpdatesCheck(Context context) {
......@@ -194,51 +199,4 @@ public class UpdatesCheckReceiver extends BroadcastReceiver {
alarmMgr.cancel(getUpdatesCheckIntent(context));
Log.d(TAG, "Cancelling pending one-shot check");
}
private static long millisToNextRelease(Context context) {
final long extraMillis = 3 * AlarmManager.INTERVAL_HOUR;
List<UpdateInfo> updates = null;
try {
updates = Utils.parseJson(Utils.getCachedUpdateList(context), false);
} catch (IOException | JSONException ignored) {
}
if (updates == null || updates.size() == 0) {
return SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY;
}
long buildTimestamp = 0;
for (UpdateInfo update : updates) {
if (update.getTimestamp() > buildTimestamp) {
buildTimestamp = update.getTimestamp();
}
}
buildTimestamp *= 1000;
Calendar c = Calendar.getInstance();
long now = c.getTimeInMillis();
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
c.setTimeInMillis(c.getTimeInMillis() + millisSinceMidnight(buildTimestamp));
long millisToNextRelease = (c.getTimeInMillis() - now);
millisToNextRelease += extraMillis;
if (c.getTimeInMillis() < now) {
millisToNextRelease += AlarmManager.INTERVAL_DAY;
}
return millisToNextRelease;
}
private static long millisSinceMidnight(long millis) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(millis);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return millis - c.getTimeInMillis();
}
}
......@@ -23,8 +23,13 @@ public final class Constants {
public static final String AB_PAYLOAD_BIN_PATH = "payload.bin";
public static final String AB_PAYLOAD_PROPERTIES_PATH = "payload_properties.txt";
public static final int AUTO_UPDATES_CHECK_INTERVAL_NEVER = 0;
public static final int AUTO_UPDATES_CHECK_INTERVAL_DAILY = 1;
public static final int AUTO_UPDATES_CHECK_INTERVAL_WEEKLY = 2;
public static final int AUTO_UPDATES_CHECK_INTERVAL_MONTHLY = 3;
public static final String PREF_LAST_UPDATE_CHECK = "last_update_check";
public static final String PREF_AUTO_UPDATES_CHECK = "auto_updates_check";
public static final String PREF_AUTO_UPDATES_CHECK_INTERVAL = "auto_updates_check_interval";
public static final String PREF_AUTO_DELETE_UPDATES = "auto_delete_updates";
public static final String PREF_AB_PERF_MODE = "ab_perf_mode";
public static final String PREF_MOBILE_DATA_WARNING = "pref_mobile_data_warning";
......
......@@ -15,6 +15,7 @@
*/
package org.lineageos.updater.misc;
import android.app.AlarmManager;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
......@@ -368,4 +369,26 @@ public class Utils {
StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
return sm.isEncrypted(file);
}
public static int getUpdateCheckSetting(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getInt(Constants.PREF_AUTO_UPDATES_CHECK_INTERVAL,
Constants.AUTO_UPDATES_CHECK_INTERVAL_WEEKLY);
}
public static boolean isUpdateCheckEnabled(Context context) {
return getUpdateCheckSetting(context) != Constants.AUTO_UPDATES_CHECK_INTERVAL_NEVER;
}
public static long getUpdateCheckInterval(Context context) {
switch (Utils.getUpdateCheckSetting(context)) {
case Constants.AUTO_UPDATES_CHECK_INTERVAL_DAILY:
return AlarmManager.INTERVAL_DAY;
case Constants.AUTO_UPDATES_CHECK_INTERVAL_WEEKLY:
default:
return AlarmManager.INTERVAL_DAY * 7;
case Constants.AUTO_UPDATES_CHECK_INTERVAL_MONTHLY:
return AlarmManager.INTERVAL_DAY * 30;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment