Extracted triedEmptyUpdate preference.

This commit is contained in:
Peter Serwylo 2017-03-14 07:59:38 +11:00
parent a3de43ff7a
commit 259dd38ae9
4 changed files with 31 additions and 16 deletions

View File

@ -68,6 +68,7 @@ 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";
private static final boolean DEFAULT_ROOTED = true; private static final boolean DEFAULT_ROOTED = true;
private static final boolean DEFAULT_HIDE_ANTI_FEATURE_APPS = false; private static final boolean DEFAULT_HIDE_ANTI_FEATURE_APPS = false;
@ -182,6 +183,20 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
} }
} }
/**
* Used the first time F-Droid is installed to flag whether or not we have tried to request
* 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, false);
}
public void setTriedEmptyUpdate(boolean value) {
preferences.edit().putBoolean(PREF_TRIED_EMPTY_UPDATE, value).apply();
}
public boolean getUnstableUpdates() { public boolean getUnstableUpdates() {
return preferences.getBoolean(PREF_UNSTABLE_UPDATES, DEFAULT_UNSTABLE_UPDATES); return preferences.getBoolean(PREF_UNSTABLE_UPDATES, DEFAULT_UNSTABLE_UPDATES);
} }

View File

@ -28,6 +28,7 @@ import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.preference.PreferenceManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
@ -880,7 +881,8 @@ class DBHelper extends SQLiteOpenHelper {
private void resetTransient(SQLiteDatabase db) { private void resetTransient(SQLiteDatabase db) {
Utils.debugLog(TAG, "Removing app + apk tables so they can be recreated. Next time F-Droid updates it should trigger an index update."); Utils.debugLog(TAG, "Removing app + apk tables so they can be recreated. Next time F-Droid updates it should trigger an index update.");
context.getSharedPreferences("FDroid", Context.MODE_PRIVATE)
PreferenceManager.getDefaultSharedPreferences(context)
.edit() .edit()
.putBoolean("triedEmptyUpdate", false) .putBoolean("triedEmptyUpdate", false)
.apply(); .apply();
@ -924,8 +926,12 @@ class DBHelper extends SQLiteOpenHelper {
if (oldVersion >= 42) { if (oldVersion >= 42) {
return; return;
} }
context.getSharedPreferences("FDroid", Context.MODE_PRIVATE).edit()
.putBoolean("triedEmptyUpdate", false).apply(); PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putBoolean("triedEmptyUpdate", false)
.apply();
db.execSQL("drop table " + AppMetadataTable.NAME); db.execSQL("drop table " + AppMetadataTable.NAME);
db.execSQL("drop table " + ApkTable.NAME); db.execSQL("drop table " + ApkTable.NAME);
clearRepoEtags(db); clearRepoEtags(db);

View File

@ -1,8 +1,6 @@
package org.fdroid.fdroid.views.fragments; package org.fdroid.fdroid.views.fragments;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
@ -143,12 +141,10 @@ public abstract class AppListFragment extends ListFragment implements
* be bad. * be bad.
*/ */
private boolean updateEmptyRepos() { private boolean updateEmptyRepos() {
final String triedEmptyUpdate = "triedEmptyUpdate"; Preferences prefs = Preferences.get();
SharedPreferences prefs = getActivity().getPreferences(Context.MODE_PRIVATE); if (!prefs.hasTriedEmptyUpdate()) {
boolean hasTriedEmptyUpdate = prefs.getBoolean(triedEmptyUpdate, false);
if (!hasTriedEmptyUpdate) {
Utils.debugLog(TAG, "Empty app list, and we haven't done an update yet. Forcing repo update."); Utils.debugLog(TAG, "Empty app list, and we haven't done an update yet. Forcing repo update.");
prefs.edit().putBoolean(triedEmptyUpdate, true).apply(); prefs.setTriedEmptyUpdate(true);
UpdateService.updateNow(getActivity()); UpdateService.updateNow(getActivity());
return true; return true;
} }

View File

@ -3,7 +3,6 @@ package org.fdroid.fdroid.views.main;
import android.app.SearchManager; import android.app.SearchManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@ -19,6 +18,7 @@ import org.fdroid.fdroid.AppDetails;
import org.fdroid.fdroid.AppDetails2; import org.fdroid.fdroid.AppDetails2;
import org.fdroid.fdroid.FDroidApp; import org.fdroid.fdroid.FDroidApp;
import org.fdroid.fdroid.NfcHelper; import org.fdroid.fdroid.NfcHelper;
import org.fdroid.fdroid.Preferences;
import org.fdroid.fdroid.R; import org.fdroid.fdroid.R;
import org.fdroid.fdroid.UpdateService; import org.fdroid.fdroid.UpdateService;
import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.Utils;
@ -85,12 +85,10 @@ public class MainActivity extends AppCompatActivity implements BottomNavigationV
* don't try to do it automatically again. * don't try to do it automatically again.
*/ */
private void initialRepoUpdateIfRequired() { private void initialRepoUpdateIfRequired() {
final String triedEmptyUpdate = "triedEmptyUpdate"; Preferences prefs = Preferences.get();
SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE); if (!prefs.hasTriedEmptyUpdate()) {
boolean hasTriedEmptyUpdate = prefs.getBoolean(triedEmptyUpdate, false);
if (!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.edit().putBoolean(triedEmptyUpdate, true).apply(); prefs.setTriedEmptyUpdate(true);
UpdateService.updateNow(this); UpdateService.updateNow(this);
} }
} }