Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Engine.java

Issue 29449601: Issue 5235 - Refactoring on SharedPrefs (Closed)
Patch Set: Created May 26, 2017, 8:49 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Engine.java
===================================================================
--- a/adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Engine.java
+++ b/adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/engine/Engine.java
@@ -40,29 +40,28 @@ import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Pattern;
import org.adblockplus.adblockplussbrowser.R;
import org.adblockplus.sbrowser.contentblocker.MainPreferences;
+import org.adblockplus.sbrowser.contentblocker.util.SharedPrefsUtils;
import android.content.Context;
import android.content.Intent;
-import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
-import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
public final class Engine
{
private static final String TAG = Engine.class.getSimpleName();
// TODO make use of this regex's
@@ -83,18 +82,16 @@ public final class Engine
public static final String SBROWSER_APP_ID = "com.sec.android.app.sbrowser";
public static final String ACTION_OPEN_SETTINGS = "com.samsung.android.sbrowser.contentBlocker.ACTION_SETTING";
public static final String ACTION_UPDATE = "com.samsung.android.sbrowser.contentBlocker.ACTION_UPDATE";
public static final String EASYLIST_URL = "https://easylist-downloads.adblockplus.org/easylist.txt";
public static final String SUBSCRIPTIONS_EXCEPTIONSURL = "subscriptions_exceptionsurl";
- private static final String PREFS_KEY_PREVIOUS_VERSION = "key_previous_version";
-
// The value below specifies an interval of [x, 2*x[, where x =
// INITIAL_UPDATE_CHECK_DELAY_SECONDS
private static final long INITIAL_UPDATE_CHECK_DELAY_SECONDS = 5;
private static final long UPDATE_CHECK_INTERVAL_MINUTES = 30;
private static final long BROADCAST_COMBINATION_DELAY_MILLIS = 2500;
public static final long MILLIS_PER_SECOND = 1000;
public static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND;
@@ -224,20 +221,18 @@ public final class Engine
final ConnectivityManager connManager = (ConnectivityManager) this.serviceContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo current = connManager.getActiveNetworkInfo();
if (current == null)
{
return false;
}
- final SharedPreferences prefs = PreferenceManager
- .getDefaultSharedPreferences(this.serviceContext);
- final boolean wifiOnly = prefs.getString(
- this.serviceContext.getString(R.string.key_automatic_updates), "1").equals("1");
+ final boolean wifiOnly = "1".equals(SharedPrefsUtils.getString(
anton 2017/05/29 05:33:03 why boolean variable is stored as string? wouldn't
diegocarloslima 2017/05/31 14:32:09 This is because the preference value is stored by
+ this.serviceContext, R.string.key_automatic_updates , "1"));
if (wifiOnly)
{
if (current.isConnected() && !current.isRoaming())
{
switch (current.getType())
{
case ConnectivityManager.TYPE_BLUETOOTH:
@@ -282,19 +277,18 @@ public final class Engine
{
this.lock();
try
{
Log.d(TAG, "Writing filters...");
final File filterFile = this.subscriptions.createAndWriteFile();
writeWhitelistedWebsites(this.serviceContext, filterFile);
- final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.serviceContext);
- final String key = this.serviceContext.getString(R.string.key_cached_filter_path);
- prefs.edit().putString(key, filterFile.getAbsolutePath()).commit();
+ SharedPrefsUtils.putString(
+ this.serviceContext, R.string.key_cached_filter_path, filterFile.getAbsolutePath());
Log.d(TAG, "Cleaning up cache...");
final File dummyFile = getDummyFilterFile(this.serviceContext);
final File[] cacheDirFiles = getFilterCacheDir(this.serviceContext).listFiles();
if (cacheDirFiles != null)
{
for (final File file : cacheDirFiles)
{
@@ -346,26 +340,28 @@ public final class Engine
}
private void migrateFromPreviousVersion(final Context context)
{
try
{
final int versionCode = context.getPackageManager().getPackageInfo(context.getPackageName(),
0).versionCode;
- final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
- int previous = prefs.getInt(PREFS_KEY_PREVIOUS_VERSION, 0);
- if (versionCode > previous)
+
+ final int previousVersionCode = SharedPrefsUtils.getInt(
+ context, R.string.key_previous_version_code, 0);
+
+ if (versionCode > previousVersionCode)
{
- if (previous > 0)
+ if (previousVersionCode > 0)
{
// We can do possible migration stuff here
// Currently we only persist the new version code
}
- prefs.edit().putInt(PREFS_KEY_PREVIOUS_VERSION, versionCode).commit();
+ SharedPrefsUtils.putInt(context, R.string.key_previous_version_code, versionCode);
}
}
catch (final Throwable t)
{
Log.e(TAG, "Failed on migration, please clear all application data", t);
}
}
@@ -510,22 +506,19 @@ public final class Engine
{
writer.write("[Adblock Plus 2.0]\n");
writer.write("! This file was automatically created.\n");
}
private static void writeWhitelistedWebsites(Context context, File filterFile) throws IOException
{
Log.d(TAG, "Writing whitelisted websites...");
- final SharedPreferences prefs =
- PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
- final String key = context.getString(R.string.key_whitelisted_websites);
-
final Set<String> whitelistedWebsites = new TreeSet<>();
- whitelistedWebsites.addAll(prefs.getStringSet(key, Collections.<String>emptySet()));
+ whitelistedWebsites.addAll(SharedPrefsUtils.getStringSet(
+ context, R.string.key_whitelisted_websites, Collections.<String>emptySet()));
try (final BufferedWriter w = new BufferedWriter( new OutputStreamWriter(
new FileOutputStream(filterFile, true), StandardCharsets.UTF_8)))
{
for (final String url : whitelistedWebsites)
{
try
{
@@ -540,18 +533,19 @@ public final class Engine
continue;
}
}
}
}
private static File getCachedFilterFile(Context context)
{
- final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
- final String cachedFilterPath = prefs.getString(context.getString(R.string.key_cached_filter_path), null);
+ final String cachedFilterPath = SharedPrefsUtils.getString(
+ context, R.string.key_cached_filter_path, null);
+
if (cachedFilterPath != null)
{
return new File(cachedFilterPath);
}
return null;
}

Powered by Google App Engine
This is Rietveld