Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

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

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
Loading
Loading
Loading
Loading
+18 −4
Original line number Diff line number Diff line
@@ -7,14 +7,28 @@
    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: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"
        android:layout_width="match_parent"

res/values/arrays.xml

0 → 100644
+24 −0
Original line number Diff line number Diff line
<?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>
+4 −0
Original line number Diff line number Diff line
@@ -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>
+8 −6
Original line number Diff line number Diff line
@@ -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);
+12 −54
Original line number Diff line number Diff line
@@ -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();
    }
}
Loading