Create Constants class

This commit is contained in:
Gabriele M
2017-07-05 23:35:14 +02:00
parent 5aa2c62296
commit 3e36596127
3 changed files with 47 additions and 14 deletions

View File

@@ -28,6 +28,7 @@ import android.support.v7.app.NotificationCompat;
import android.util.Log; import android.util.Log;
import org.json.JSONException; import org.json.JSONException;
import org.lineageos.updater.misc.Constants;
import org.lineageos.updater.misc.Utils; import org.lineageos.updater.misc.Utils;
import java.io.File; import java.io.File;
@@ -37,8 +38,6 @@ public class UpdatesCheckReceiver extends BroadcastReceiver {
private static final String TAG = "UpdatesCheckReceiver"; 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 DAILY_CHECK_ACTION = "daily_check_action";
private static final String ONESHOT_CHECK_ACTION = "oneshot_check_action"; private static final String ONESHOT_CHECK_ACTION = "oneshot_check_action";
@@ -51,7 +50,7 @@ public class UpdatesCheckReceiver extends BroadcastReceiver {
final SharedPreferences preferences = final SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(context); 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(); final long currentMillis = System.currentTimeMillis();
if (currentMillis > lastCheck + AlarmManager.INTERVAL_DAY) { if (currentMillis > lastCheck + AlarmManager.INTERVAL_DAY) {
if (!Utils.isNetworkAvailable(context)) { if (!Utils.isNetworkAvailable(context)) {
@@ -82,7 +81,9 @@ public class UpdatesCheckReceiver extends BroadcastReceiver {
showNotification(context); showNotification(context);
} }
jsonNew.renameTo(json); 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) { } catch (IOException | JSONException e) {
Log.e(TAG, "Could not parse list, scheduling new check", e); Log.e(TAG, "Could not parse list, scheduling new check", e);
scheduleUpdatesCheck(context); scheduleUpdatesCheck(context);

View File

@@ -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";
}

View File

@@ -31,11 +31,8 @@ import org.lineageos.updater.UpdateStatus;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashSet; import java.util.HashSet;
@@ -48,6 +45,9 @@ public class Utils {
private static final String TAG = "Utils"; private static final String TAG = "Utils";
private Utils() {
}
public static File getDownloadPath(Context context) { public static File getDownloadPath(Context context) {
boolean useCache = context.getResources().getBoolean(R.bool.download_in_cache); boolean useCache = context.getResources().getBoolean(R.bool.download_in_cache);
int id = useCache ? R.string.download_path_cache : R.string.download_path_data; 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) { 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"); Log.d(TAG, update.getName() + " is older than current build");
return false; 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()); Log.d(TAG, update.getName() + " has type " + update.getType());
return false; return false;
} }
@@ -84,7 +84,7 @@ public class Utils {
} }
public static boolean canInstall(Update update) { 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<UpdateDownload> parseJson(File file, boolean compatibleOnly) public static List<UpdateDownload> parseJson(File file, boolean compatibleOnly)
@@ -120,13 +120,13 @@ public class Utils {
} }
public static String getServerURL(Context context) { 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()) { if (serverUrl.trim().isEmpty()) {
serverUrl = context.getString(R.string.conf_update_server_url_def); serverUrl = context.getString(R.string.conf_update_server_url_def);
} }
String incrementalVersion = SystemProperties.get("ro.build.version.incremental"); String incrementalVersion = SystemProperties.get(Constants.PROP_BUILD_VERSION_INCREMENTAL);
String device = SystemProperties.get("ro.cm.device").toLowerCase(); String device = SystemProperties.get(Constants.PROP_DEVICE).toLowerCase();
String type = SystemProperties.get("ro.cm.releasetype").toLowerCase(); String type = SystemProperties.get(Constants.PROP_RELEASE_TYPE).toLowerCase();
return serverUrl + "/v1/" + device + "/" + type + "/" + incrementalVersion; return serverUrl + "/v1/" + device + "/" + type + "/" + incrementalVersion;
} }