merges triedEmptyUpdate and lastUpdateCheck prefs into one useful one

This merges the triedEmptyUpdate preference into the lastUpdateCheck pref,
and uses that to determine whether the index update has ever run.  It seems
that lastUpdateCheck used to be used for that, but was semi-disabled. Then
triedEmptyUpdate was added. This merges the two into lastUpdateCheck, which
also tracks the timestamp of the last index update.
This commit is contained in:
Hans-Christoph Steiner 2018-07-19 13:42:49 +02:00
parent e44ca193dd
commit 0d386b824f
5 changed files with 28 additions and 24 deletions

View File

@ -403,7 +403,6 @@ public class FDroidApp extends Application {
CleanCacheService.schedule(this); CleanCacheService.schedule(this);
notificationHelper = new NotificationHelper(getApplicationContext()); notificationHelper = new NotificationHelper(getApplicationContext());
UpdateService.schedule(getApplicationContext());
bluetoothAdapter = getBluetoothAdapter(); bluetoothAdapter = getBluetoothAdapter();
// There are a couple things to pay attention to with this config: memory usage, // There are a couple things to pay attention to with this config: memory usage,
@ -452,7 +451,12 @@ public class FDroidApp extends Application {
.build(); .build();
ImageLoader.getInstance().init(config); ImageLoader.getInstance().init(config);
if (preferences.isIndexNeverUpdated()) {
// force this check to ensure it starts fetching the index on initial runs
networkState = ConnectivityMonitorService.getNetworkState(this);
}
ConnectivityMonitorService.registerAndStart(this); ConnectivityMonitorService.registerAndStart(this);
UpdateService.schedule(getApplicationContext());
FDroidApp.initWifiSettings(); FDroidApp.initWifiSettings();
WifiStateChangeService.start(this, null); WifiStateChangeService.start(this, null);

View File

@ -101,7 +101,6 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
public static final String PREF_PROXY_PORT = "proxyPort"; public static final String PREF_PROXY_PORT = "proxyPort";
public static final String PREF_SHOW_NFC_DURING_SWAP = "showNfcDuringSwap"; public static final String PREF_SHOW_NFC_DURING_SWAP = "showNfcDuringSwap";
public static final String PREF_POST_PRIVILEGED_INSTALL = "postPrivilegedInstall"; public static final String PREF_POST_PRIVILEGED_INSTALL = "postPrivilegedInstall";
public static final String PREF_TRIED_EMPTY_UPDATE = "triedEmptyUpdate";
public static final String PREF_PREVENT_SCREENSHOTS = "preventScreenshots"; public static final String PREF_PREVENT_SCREENSHOTS = "preventScreenshots";
public static final String PREF_PANIC_EXIT = "pref_panic_exit"; public static final String PREF_PANIC_EXIT = "pref_panic_exit";
public static final String PREF_PANIC_HIDE = "pref_panic_hide"; public static final String PREF_PANIC_HIDE = "pref_panic_hide";
@ -114,10 +113,14 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
public static final int OVER_NETWORK_ON_DEMAND = 1; public static final int OVER_NETWORK_ON_DEMAND = 1;
public static final int OVER_NETWORK_ALWAYS = 2; public static final int OVER_NETWORK_ALWAYS = 2;
// not shown in Settings
private static final String PREF_LAST_UPDATE_CHECK = "lastUpdateCheck";
// these preferences are not listed in preferences.xml so the defaults are set here // these preferences are not listed in preferences.xml so the defaults are set here
@SuppressWarnings("PMD.AvoidUsingHardCodedIP") @SuppressWarnings("PMD.AvoidUsingHardCodedIP")
public static final String DEFAULT_PROXY_HOST = "127.0.0.1"; // TODO move to preferences.xml public static final String DEFAULT_PROXY_HOST = "127.0.0.1"; // TODO move to preferences.xml
public static final int DEFAULT_PROXY_PORT = 8118; // TODO move to preferences.xml public static final int DEFAULT_PROXY_PORT = 8118; // TODO move to preferences.xml
private static final int DEFAULT_LAST_UPDATE_CHECK = -1;
private static final boolean DEFAULT_SHOW_NFC_DURING_SWAP = true; private static final boolean DEFAULT_SHOW_NFC_DURING_SWAP = true;
private static final boolean DEFAULT_POST_PRIVILEGED_INSTALL = false; private static final boolean DEFAULT_POST_PRIVILEGED_INSTALL = false;
private static final boolean DEFAULT_PANIC_EXIT = true; private static final boolean DEFAULT_PANIC_EXIT = true;
@ -321,18 +324,23 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
} }
} }
/** public long getLastUpdateCheck() {
* Used the first time F-Droid is installed to flag whether or not we have tried to request return preferences.getLong(PREF_LAST_UPDATE_CHECK, DEFAULT_LAST_UPDATE_CHECK);
* apps from the repo. This is used so that when there is no apps available, we can differentiate
* between whether the repos actually have no apps (in which case we don't need to continue
* asking), or whether there is no apps because we have never actually asked to update the repos.
*/
public boolean hasTriedEmptyUpdate() {
return preferences.getBoolean(PREF_TRIED_EMPTY_UPDATE, IGNORED_B);
} }
public void setTriedEmptyUpdate(boolean value) { public void setLastUpdateCheck(long lastUpdateCheck) {
preferences.edit().putBoolean(PREF_TRIED_EMPTY_UPDATE, value).apply(); preferences.edit().putLong(PREF_LAST_UPDATE_CHECK, lastUpdateCheck).apply();
}
public void resetLastUpdateCheck() {
setLastUpdateCheck(DEFAULT_LAST_UPDATE_CHECK);
}
/**
* The first time the app has been run since fresh install or clearing all data.
*/
public boolean isIndexNeverUpdated() {
return getLastUpdateCheck() == DEFAULT_LAST_UPDATE_CHECK;
} }
public boolean getUnstableUpdates() { public boolean getUnstableUpdates() {

View File

@ -29,7 +29,6 @@ import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
@ -41,7 +40,6 @@ import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService; import android.support.v4.app.JobIntentService;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.preference.PreferenceManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
@ -82,7 +80,6 @@ public class UpdateService extends JobIntentService {
public static final int STATUS_ERROR_LOCAL_SMALL = 4; public static final int STATUS_ERROR_LOCAL_SMALL = 4;
public static final int STATUS_INFO = 5; public static final int STATUS_INFO = 5;
private static final String STATE_LAST_UPDATED = "lastUpdateCheck";
private static final int JOB_ID = 0xfedcba; private static final int JOB_ID = 0xfedcba;
private static final int NOTIFY_ID_UPDATING = 0; private static final int NOTIFY_ID_UPDATING = 0;
@ -498,10 +495,7 @@ public class UpdateService extends JobIntentService {
} }
} }
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); fdroidPrefs.setLastUpdateCheck(System.currentTimeMillis());
SharedPreferences.Editor e = prefs.edit();
e.putLong(STATE_LAST_UPDATED, System.currentTimeMillis());
e.apply();
if (errorRepos == 0) { if (errorRepos == 0) {
if (changes) { if (changes) {

View File

@ -1095,7 +1095,7 @@ public class DBHelper extends SQLiteOpenHelper {
private static void resetTransient(SQLiteDatabase db) { private static void resetTransient(SQLiteDatabase db) {
Utils.debugLog(TAG, "Removing all index tables, they will be recreated next time F-Droid updates."); Utils.debugLog(TAG, "Removing all index tables, they will be recreated next time F-Droid updates.");
Preferences.get().setTriedEmptyUpdate(false); Preferences.get().resetLastUpdateCheck();
db.beginTransaction(); db.beginTransaction();
try { try {
@ -1147,7 +1147,7 @@ public class DBHelper extends SQLiteOpenHelper {
return; return;
} }
Preferences.get().setTriedEmptyUpdate(false); Preferences.get().resetLastUpdateCheck();
db.execSQL("drop table " + AppMetadataTable.NAME); db.execSQL("drop table " + AppMetadataTable.NAME);
db.execSQL("drop table " + ApkTable.NAME); db.execSQL("drop table " + ApkTable.NAME);

View File

@ -164,10 +164,8 @@ public class MainActivity extends AppCompatActivity implements BottomNavigationB
* don't try to do it automatically again. * don't try to do it automatically again.
*/ */
private void initialRepoUpdateIfRequired() { private void initialRepoUpdateIfRequired() {
Preferences prefs = Preferences.get(); if (!Preferences.get().isIndexNeverUpdated()) {
if (!prefs.hasTriedEmptyUpdate()) {
Utils.debugLog(TAG, "We haven't done an update yet. Forcing repo update."); Utils.debugLog(TAG, "We haven't done an update yet. Forcing repo update.");
prefs.setTriedEmptyUpdate(true);
UpdateService.updateNow(this); UpdateService.updateNow(this);
} }
} }