From 3e3659612744a7a1604a6c35bc3daeea02df5f37 Mon Sep 17 00:00:00 2001 From: Gabriele M Date: Wed, 5 Jul 2017 23:35:14 +0200 Subject: [PATCH] Create Constants class --- .../updater/UpdatesCheckReceiver.java | 9 +++--- src/org/lineageos/updater/misc/Constants.java | 32 +++++++++++++++++++ src/org/lineageos/updater/misc/Utils.java | 20 ++++++------ 3 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 src/org/lineageos/updater/misc/Constants.java diff --git a/src/org/lineageos/updater/UpdatesCheckReceiver.java b/src/org/lineageos/updater/UpdatesCheckReceiver.java index 1a294336..3ccd5fbd 100644 --- a/src/org/lineageos/updater/UpdatesCheckReceiver.java +++ b/src/org/lineageos/updater/UpdatesCheckReceiver.java @@ -28,6 +28,7 @@ import android.support.v7.app.NotificationCompat; import android.util.Log; import org.json.JSONException; +import org.lineageos.updater.misc.Constants; import org.lineageos.updater.misc.Utils; import java.io.File; @@ -37,8 +38,6 @@ public class UpdatesCheckReceiver extends BroadcastReceiver { private static final String TAG = "UpdatesCheckReceiver"; - private static final String LAST_UPDATES_CHECK_PREF = "last_update_check"; - private static final String DAILY_CHECK_ACTION = "daily_check_action"; private static final String ONESHOT_CHECK_ACTION = "oneshot_check_action"; @@ -51,7 +50,7 @@ public class UpdatesCheckReceiver extends BroadcastReceiver { final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - long lastCheck = preferences.getLong(LAST_UPDATES_CHECK_PREF, -1); + long lastCheck = preferences.getLong(Constants.PREF_LAST_UPDATE_CHECK, -1); final long currentMillis = System.currentTimeMillis(); if (currentMillis > lastCheck + AlarmManager.INTERVAL_DAY) { if (!Utils.isNetworkAvailable(context)) { @@ -82,7 +81,9 @@ public class UpdatesCheckReceiver extends BroadcastReceiver { showNotification(context); } jsonNew.renameTo(json); - preferences.edit().putLong(LAST_UPDATES_CHECK_PREF, currentMillis).apply(); + preferences.edit() + .putLong(Constants.PREF_LAST_UPDATE_CHECK, currentMillis) + .apply(); } catch (IOException | JSONException e) { Log.e(TAG, "Could not parse list, scheduling new check", e); scheduleUpdatesCheck(context); diff --git a/src/org/lineageos/updater/misc/Constants.java b/src/org/lineageos/updater/misc/Constants.java new file mode 100644 index 00000000..dd5b3a32 --- /dev/null +++ b/src/org/lineageos/updater/misc/Constants.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2017 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. + */ +package org.lineageos.updater.misc; + +public final class Constants { + + private Constants() { + } + + public static final String PREF_LAST_UPDATE_CHECK = "last_update_check"; + + public static final String PROP_BUILD_DATE = "ro.build.date.utc"; + public static final String PROP_BUILD_VERSION = "ro.cm.build.version"; + public static final String PROP_BUILD_VERSION_INCREMENTAL = "ro.build.version.incremental"; + public static final String PROP_DEVICE = "ro.cm.device"; + public static final String PROP_RELEASE_TYPE = "ro.cm.releasetype"; + public static final String PROP_UPDATER_URI = "cm.updater.uri"; + +} diff --git a/src/org/lineageos/updater/misc/Utils.java b/src/org/lineageos/updater/misc/Utils.java index 79707023..04f7cac7 100644 --- a/src/org/lineageos/updater/misc/Utils.java +++ b/src/org/lineageos/updater/misc/Utils.java @@ -31,11 +31,8 @@ import org.lineageos.updater.UpdateStatus; import java.io.BufferedReader; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; -import java.nio.channels.FileChannel; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashSet; @@ -48,6 +45,9 @@ public class Utils { private static final String TAG = "Utils"; + private Utils() { + } + public static File getDownloadPath(Context context) { boolean useCache = context.getResources().getBoolean(R.bool.download_in_cache); int id = useCache ? R.string.download_path_cache : R.string.download_path_data; @@ -72,11 +72,11 @@ public class Utils { } public static boolean isCompatible(Update update) { - if (update.getTimestamp() < SystemProperties.getLong("ro.build.date.utc", 0)) { + if (update.getTimestamp() < SystemProperties.getLong(Constants.PROP_BUILD_DATE, 0)) { Log.d(TAG, update.getName() + " is older than current build"); return false; } - if (!update.getType().equalsIgnoreCase(SystemProperties.get("ro.cm.releasetype"))) { + if (!update.getType().equalsIgnoreCase(SystemProperties.get(Constants.PROP_RELEASE_TYPE))) { Log.d(TAG, update.getName() + " has type " + update.getType()); return false; } @@ -84,7 +84,7 @@ public class Utils { } public static boolean canInstall(Update update) { - return update.getVersion().equalsIgnoreCase(SystemProperties.get("ro.cm.build.version")); + return update.getVersion().equalsIgnoreCase(SystemProperties.get(Constants.PROP_BUILD_VERSION)); } public static List parseJson(File file, boolean compatibleOnly) @@ -120,13 +120,13 @@ public class Utils { } public static String getServerURL(Context context) { - String serverUrl = SystemProperties.get("cm.updater.uri"); + String serverUrl = SystemProperties.get(Constants.PROP_UPDATER_URI); if (serverUrl.trim().isEmpty()) { serverUrl = context.getString(R.string.conf_update_server_url_def); } - String incrementalVersion = SystemProperties.get("ro.build.version.incremental"); - String device = SystemProperties.get("ro.cm.device").toLowerCase(); - String type = SystemProperties.get("ro.cm.releasetype").toLowerCase(); + String incrementalVersion = SystemProperties.get(Constants.PROP_BUILD_VERSION_INCREMENTAL); + String device = SystemProperties.get(Constants.PROP_DEVICE).toLowerCase(); + String type = SystemProperties.get(Constants.PROP_RELEASE_TYPE).toLowerCase(); return serverUrl + "/v1/" + device + "/" + type + "/" + incrementalVersion; }