Define interfaces with only getters for the updates

This also changes the code to use the new interfaces wherever the
update is not supposed to change.
This commit is contained in:
Gabriele M
2017-07-21 01:17:19 +02:00
parent 11f82cfb87
commit 28b8774db6
11 changed files with 153 additions and 68 deletions

View File

@@ -52,8 +52,7 @@ import org.lineageos.updater.misc.Constants;
import org.lineageos.updater.misc.LegacySupport; import org.lineageos.updater.misc.LegacySupport;
import org.lineageos.updater.misc.StringGenerator; import org.lineageos.updater.misc.StringGenerator;
import org.lineageos.updater.misc.Utils; import org.lineageos.updater.misc.Utils;
import org.lineageos.updater.model.Update; import org.lineageos.updater.model.UpdateInfo;
import org.lineageos.updater.model.UpdateDownload;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@@ -233,12 +232,12 @@ public class UpdatesActivity extends UpdatesListActivity {
Controller controller = mUpdaterService.getUpdaterController(); Controller controller = mUpdaterService.getUpdaterController();
boolean newUpdates = false; boolean newUpdates = false;
List<UpdateDownload> updates = Utils.parseJson(jsonFile, true); List<UpdateInfo> updates = Utils.parseJson(jsonFile, true);
List<String> importedNotAvailableOnline = LegacySupport.importDownloads(this, updates); List<String> importedNotAvailableOnline = LegacySupport.importDownloads(this, updates);
List<String> updatesOnline = new ArrayList<>(); List<String> updatesOnline = new ArrayList<>();
for (UpdateDownload update : updates) { for (UpdateInfo update : updates) {
newUpdates |= controller.addUpdate(update); newUpdates |= controller.addUpdate(update);
updatesOnline.add(update.getDownloadId()); updatesOnline.add(update.getDownloadId());
} }
@@ -257,14 +256,14 @@ public class UpdatesActivity extends UpdatesListActivity {
} }
List<String> updateIds = new ArrayList<>(); List<String> updateIds = new ArrayList<>();
List<UpdateDownload> sortedUpdates = controller.getUpdates(); List<UpdateInfo> sortedUpdates = controller.getUpdates();
Collections.sort(sortedUpdates, new Comparator<UpdateDownload>() { Collections.sort(sortedUpdates, new Comparator<UpdateInfo>() {
@Override @Override
public int compare(UpdateDownload u1, UpdateDownload u2) { public int compare(UpdateInfo u1, UpdateInfo u2) {
return Long.compare(u2.getTimestamp(), u1.getTimestamp()); return Long.compare(u2.getTimestamp(), u1.getTimestamp());
} }
}); });
for (Update update : sortedUpdates) { for (UpdateInfo update : sortedUpdates) {
updateIds.add(update.getDownloadId()); updateIds.add(update.getDownloadId());
} }
mAdapter.setData(updateIds); mAdapter.setData(updateIds);
@@ -371,7 +370,7 @@ public class UpdatesActivity extends UpdatesListActivity {
} }
private void handleDownloadStatusChange(String downloadId) { private void handleDownloadStatusChange(String downloadId) {
UpdateDownload update = mUpdaterService.getUpdaterController().getUpdate(downloadId); UpdateInfo update = mUpdaterService.getUpdaterController().getUpdate(downloadId);
switch (update.getStatus()) { switch (update.getStatus()) {
case PAUSED_ERROR: case PAUSED_ERROR:
showSnackbar(R.string.snack_download_failed, Snackbar.LENGTH_LONG); showSnackbar(R.string.snack_download_failed, Snackbar.LENGTH_LONG);

View File

@@ -39,7 +39,7 @@ import org.lineageos.updater.misc.FileUtils;
import org.lineageos.updater.misc.PermissionsUtils; import org.lineageos.updater.misc.PermissionsUtils;
import org.lineageos.updater.misc.StringGenerator; import org.lineageos.updater.misc.StringGenerator;
import org.lineageos.updater.misc.Utils; import org.lineageos.updater.misc.Utils;
import org.lineageos.updater.model.UpdateDownload; import org.lineageos.updater.model.UpdateInfo;
import org.lineageos.updater.model.UpdateStatus; import org.lineageos.updater.model.UpdateStatus;
import java.io.File; import java.io.File;
@@ -117,7 +117,7 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
notifyDataSetChanged(); notifyDataSetChanged();
} }
private void handleActiveStatus(ViewHolder viewHolder, UpdateDownload update) { private void handleActiveStatus(ViewHolder viewHolder, UpdateInfo update) {
String buildDate = StringGenerator.getDateLocalizedUTC(mActivity, String buildDate = StringGenerator.getDateLocalizedUTC(mActivity,
DateFormat.MEDIUM, update.getTimestamp()); DateFormat.MEDIUM, update.getTimestamp());
String buildInfoText = mActivity.getString(R.string.list_build_version_date, String buildInfoText = mActivity.getString(R.string.list_build_version_date,
@@ -170,7 +170,7 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
viewHolder.itemView.setOnLongClickListener(getLongClickListener(update, canDelete)); viewHolder.itemView.setOnLongClickListener(getLongClickListener(update, canDelete));
} }
private void handleNotActiveStatus(ViewHolder viewHolder, UpdateDownload update) { private void handleNotActiveStatus(ViewHolder viewHolder, UpdateInfo update) {
String buildDate = StringGenerator.getDateLocalizedUTC(mActivity, String buildDate = StringGenerator.getDateLocalizedUTC(mActivity,
DateFormat.LONG, update.getTimestamp()); DateFormat.LONG, update.getTimestamp());
String buildVersion = mActivity.getString(R.string.list_build_version, String buildVersion = mActivity.getString(R.string.list_build_version,
@@ -195,7 +195,7 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
} }
final String downloadId = mDownloadIds.get(i); final String downloadId = mDownloadIds.get(i);
UpdateDownload update = mUpdaterController.getUpdate(downloadId); UpdateInfo update = mUpdaterController.getUpdate(downloadId);
if (update == null) { if (update == null) {
// The update was deleted // The update was deleted
viewHolder.mAction.setEnabled(false); viewHolder.mAction.setEnabled(false);
@@ -284,7 +284,7 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
button.setContentDescription( button.setContentDescription(
mActivity.getString(R.string.action_description_resume)); mActivity.getString(R.string.action_description_resume));
button.setEnabled(enabled); button.setEnabled(enabled);
UpdateDownload update = mUpdaterController.getUpdate(downloadId); UpdateInfo update = mUpdaterController.getUpdate(downloadId);
final boolean canInstall = Utils.canInstall(update) || final boolean canInstall = Utils.canInstall(update) ||
update.getFile().length() == update.getFileSize(); update.getFile().length() == update.getFileSize();
clickListener = !enabled ? null : new View.OnClickListener() { clickListener = !enabled ? null : new View.OnClickListener() {
@@ -305,7 +305,7 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
button.setContentDescription( button.setContentDescription(
mActivity.getString(R.string.action_description_install)); mActivity.getString(R.string.action_description_install));
button.setEnabled(enabled); button.setEnabled(enabled);
UpdateDownload update = mUpdaterController.getUpdate(downloadId); UpdateInfo update = mUpdaterController.getUpdate(downloadId);
final boolean canInstall = Utils.canInstall(update); final boolean canInstall = Utils.canInstall(update);
clickListener = !enabled ? null : new View.OnClickListener() { clickListener = !enabled ? null : new View.OnClickListener() {
@Override @Override
@@ -356,7 +356,7 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
.setNegativeButton(android.R.string.cancel, null); .setNegativeButton(android.R.string.cancel, null);
} }
private View.OnLongClickListener getLongClickListener(final UpdateDownload update, private View.OnLongClickListener getLongClickListener(final UpdateInfo update,
final boolean canDelete) { final boolean canDelete) {
return new View.OnLongClickListener() { return new View.OnLongClickListener() {
@Override @Override
@@ -370,7 +370,7 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
} }
private AlertDialog.Builder getInstallDialog(final String downloadId) { private AlertDialog.Builder getInstallDialog(final String downloadId) {
UpdateDownload update = mUpdaterController.getUpdate(downloadId); UpdateInfo update = mUpdaterController.getUpdate(downloadId);
int resId; int resId;
try { try {
if (Utils.isABUpdate(update.getFile())) { if (Utils.isABUpdate(update.getFile())) {
@@ -407,7 +407,7 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
} }
} }
private void startActionMode(final UpdateDownload update, final boolean canDelete) { private void startActionMode(final UpdateInfo update, final boolean canDelete) {
if (mActionMode != null) { if (mActionMode != null) {
Log.d(TAG, "Action mode already enabled"); Log.d(TAG, "Action mode already enabled");
return; return;
@@ -488,7 +488,7 @@ public class UpdatesListAdapter extends RecyclerView.Adapter<UpdatesListAdapter.
}); });
} }
private void exportUpdate(UpdateDownload update) { private void exportUpdate(UpdateInfo update) {
try { try {
File dest = new File(Utils.getExportPath(mActivity), update.getName()); File dest = new File(Utils.getExportPath(mActivity), update.getName());
if (dest.exists()) { if (dest.exists()) {

View File

@@ -15,20 +15,20 @@
*/ */
package org.lineageos.updater.controller; package org.lineageos.updater.controller;
import org.lineageos.updater.model.UpdateDownload; import org.lineageos.updater.model.UpdateInfo;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
public interface Controller { public interface Controller {
boolean addUpdate(UpdateDownload update); boolean addUpdate(UpdateInfo update);
List<UpdateDownload> getUpdates(); List<UpdateInfo> getUpdates();
Set<String> getIds(); Set<String> getIds();
UpdateDownload getUpdate(String downloadId); UpdateInfo getUpdate(String downloadId);
void setUpdatesAvailableOnline(List<String> downloadIds, boolean purgeList); void setUpdatesAvailableOnline(List<String> downloadIds, boolean purgeList);

View File

@@ -27,6 +27,7 @@ import org.lineageos.updater.UpdatesDbHelper;
import org.lineageos.updater.download.DownloadClient; import org.lineageos.updater.download.DownloadClient;
import org.lineageos.updater.misc.Utils; import org.lineageos.updater.misc.Utils;
import org.lineageos.updater.model.UpdateDownload; import org.lineageos.updater.model.UpdateDownload;
import org.lineageos.updater.model.UpdateInfo;
import org.lineageos.updater.model.UpdateStatus; import org.lineageos.updater.model.UpdateStatus;
import java.io.File; import java.io.File;
@@ -327,19 +328,20 @@ public class UpdaterController implements Controller {
} }
@Override @Override
public boolean addUpdate(UpdateDownload update) { public boolean addUpdate(UpdateInfo update) {
return addUpdate(update, true); return addUpdate(update, true);
} }
private boolean addUpdate(final UpdateDownload update, boolean availableOnline) { private boolean addUpdate(final UpdateInfo updateInfo, boolean availableOnline) {
Log.d(TAG, "Adding download: " + update.getDownloadId()); Log.d(TAG, "Adding download: " + updateInfo.getDownloadId());
if (mDownloads.containsKey(update.getDownloadId())) { if (mDownloads.containsKey(updateInfo.getDownloadId())) {
Log.d(TAG, "Download (" + update.getDownloadId() + ") already added"); Log.d(TAG, "Download (" + updateInfo.getDownloadId() + ") already added");
UpdateDownload updateAdded = mDownloads.get(update.getDownloadId()).mUpdate; UpdateDownload updateAdded = mDownloads.get(updateInfo.getDownloadId()).mUpdate;
updateAdded.setAvailableOnline(availableOnline && updateAdded.getAvailableOnline()); updateAdded.setAvailableOnline(availableOnline && updateAdded.getAvailableOnline());
updateAdded.setDownloadUrl(update.getDownloadUrl()); updateAdded.setDownloadUrl(updateInfo.getDownloadUrl());
return false; return false;
} }
UpdateDownload update = new UpdateDownload(updateInfo);
if (!fixUpdateStatus(update) && !availableOnline) { if (!fixUpdateStatus(update) && !availableOnline) {
update.setPersistentStatus(UpdateStatus.Persistent.UNKNOWN); update.setPersistentStatus(UpdateStatus.Persistent.UNKNOWN);
deleteUpdateAsync(update); deleteUpdateAsync(update);
@@ -347,8 +349,7 @@ public class UpdaterController implements Controller {
return false; return false;
} }
update.setAvailableOnline(availableOnline); update.setAvailableOnline(availableOnline);
UpdateDownload updateCopy = new UpdateDownload(update); mDownloads.put(update.getDownloadId(), new DownloadEntry(update));
mDownloads.put(update.getDownloadId(), new DownloadEntry(updateCopy));
return true; return true;
} }
@@ -473,18 +474,18 @@ public class UpdaterController implements Controller {
} }
@Override @Override
public List<UpdateDownload> getUpdates() { public List<UpdateInfo> getUpdates() {
List<UpdateDownload> updates = new ArrayList<>(); List<UpdateInfo> updates = new ArrayList<>();
for (DownloadEntry entry : mDownloads.values()) { for (DownloadEntry entry : mDownloads.values()) {
updates.add(new UpdateDownload(entry.mUpdate)); updates.add(entry.mUpdate);
} }
return updates; return updates;
} }
@Override @Override
public UpdateDownload getUpdate(String downloadId) { public UpdateInfo getUpdate(String downloadId) {
DownloadEntry entry = mDownloads.get(downloadId); DownloadEntry entry = mDownloads.get(downloadId);
return entry != null ? new UpdateDownload(entry.mUpdate) : null; return entry != null ? entry.mUpdate : null;
} }
UpdateDownload getActualUpdate(String downloadId) { UpdateDownload getActualUpdate(String downloadId) {

View File

@@ -36,7 +36,7 @@ import org.lineageos.updater.UpdatesActivity;
import org.lineageos.updater.misc.BuildInfoUtils; import org.lineageos.updater.misc.BuildInfoUtils;
import org.lineageos.updater.misc.StringGenerator; import org.lineageos.updater.misc.StringGenerator;
import org.lineageos.updater.misc.Utils; import org.lineageos.updater.misc.Utils;
import org.lineageos.updater.model.UpdateDownload; import org.lineageos.updater.model.UpdateInfo;
import org.lineageos.updater.model.UpdateStatus; import org.lineageos.updater.model.UpdateStatus;
import java.io.IOException; import java.io.IOException;
@@ -90,17 +90,17 @@ public class UpdaterService extends Service {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
String downloadId = intent.getStringExtra(UpdaterController.EXTRA_DOWNLOAD_ID); String downloadId = intent.getStringExtra(UpdaterController.EXTRA_DOWNLOAD_ID);
if (UpdaterController.ACTION_UPDATE_STATUS.equals(intent.getAction())) { if (UpdaterController.ACTION_UPDATE_STATUS.equals(intent.getAction())) {
UpdateDownload update = mUpdaterController.getUpdate(downloadId); UpdateInfo update = mUpdaterController.getUpdate(downloadId);
setNotificationTitle(update); setNotificationTitle(update);
Bundle extras = new Bundle(); Bundle extras = new Bundle();
extras.putString(UpdaterController.EXTRA_DOWNLOAD_ID, downloadId); extras.putString(UpdaterController.EXTRA_DOWNLOAD_ID, downloadId);
mNotificationBuilder.setExtras(extras); mNotificationBuilder.setExtras(extras);
handleUpdateStatusChange(update); handleUpdateStatusChange(update);
} else if (UpdaterController.ACTION_DOWNLOAD_PROGRESS.equals(intent.getAction())) { } else if (UpdaterController.ACTION_DOWNLOAD_PROGRESS.equals(intent.getAction())) {
UpdateDownload update = mUpdaterController.getUpdate(downloadId); UpdateInfo update = mUpdaterController.getUpdate(downloadId);
handleDownloadProgressChange(update); handleDownloadProgressChange(update);
} else if (UpdaterController.ACTION_INSTALL_PROGRESS.equals(intent.getAction())) { } else if (UpdaterController.ACTION_INSTALL_PROGRESS.equals(intent.getAction())) {
UpdateDownload update = mUpdaterController.getUpdate(downloadId); UpdateInfo update = mUpdaterController.getUpdate(downloadId);
setNotificationTitle(update); setNotificationTitle(update);
handleInstallProgress(update); handleInstallProgress(update);
} else if (UpdaterController.ACTION_UPDATE_REMOVED.equals(intent.getAction())) { } else if (UpdaterController.ACTION_UPDATE_REMOVED.equals(intent.getAction())) {
@@ -161,7 +161,7 @@ public class UpdaterService extends Service {
} }
} else if (ACTION_INSTALL_UPDATE.equals(intent.getAction())) { } else if (ACTION_INSTALL_UPDATE.equals(intent.getAction())) {
String downloadId = intent.getStringExtra(EXTRA_DOWNLOAD_ID); String downloadId = intent.getStringExtra(EXTRA_DOWNLOAD_ID);
UpdateDownload update = mUpdaterController.getUpdate(downloadId); UpdateInfo update = mUpdaterController.getUpdate(downloadId);
if (update.getPersistentStatus() != UpdateStatus.Persistent.VERIFIED) { if (update.getPersistentStatus() != UpdateStatus.Persistent.VERIFIED) {
throw new IllegalArgumentException(update.getDownloadId() + " is not verified"); throw new IllegalArgumentException(update.getDownloadId() + " is not verified");
} }
@@ -191,7 +191,7 @@ public class UpdaterService extends Service {
} }
} }
private void handleUpdateStatusChange(UpdateDownload update) { private void handleUpdateStatusChange(UpdateInfo update) {
switch (update.getStatus()) { switch (update.getStatus()) {
case DELETED: { case DELETED: {
stopForeground(STOP_FOREGROUND_DETACH); stopForeground(STOP_FOREGROUND_DETACH);
@@ -326,7 +326,7 @@ public class UpdaterService extends Service {
} }
} }
private void handleDownloadProgressChange(UpdateDownload update) { private void handleDownloadProgressChange(UpdateInfo update) {
int progress = update.getProgress(); int progress = update.getProgress();
mNotificationBuilder.setProgress(100, progress, false); mNotificationBuilder.setProgress(100, progress, false);
@@ -343,7 +343,7 @@ public class UpdaterService extends Service {
mNotificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build()); mNotificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build());
} }
private void handleInstallProgress(UpdateDownload update) { private void handleInstallProgress(UpdateInfo update) {
int progress = update.getInstallProgress(); int progress = update.getInstallProgress();
mNotificationBuilder.setProgress(100, progress, false); mNotificationBuilder.setProgress(100, progress, false);
@@ -361,7 +361,7 @@ public class UpdaterService extends Service {
mNotificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build()); mNotificationManager.notify(NOTIFICATION_ID, mNotificationBuilder.build());
} }
private void setNotificationTitle(UpdateDownload update) { private void setNotificationTitle(UpdateInfo update) {
String buildDate = StringGenerator.getDateLocalizedUTC(this, String buildDate = StringGenerator.getDateLocalizedUTC(this,
DateFormat.MEDIUM, update.getTimestamp()); DateFormat.MEDIUM, update.getTimestamp());
String buildInfo = getString(R.string.list_build_version_date, String buildInfo = getString(R.string.list_build_version_date,

View File

@@ -22,6 +22,7 @@ import android.util.Log;
import org.lineageos.updater.UpdatesDbHelper; import org.lineageos.updater.UpdatesDbHelper;
import org.lineageos.updater.model.UpdateDownload; import org.lineageos.updater.model.UpdateDownload;
import org.lineageos.updater.model.UpdateInfo;
import org.lineageos.updater.model.UpdateStatus; import org.lineageos.updater.model.UpdateStatus;
import java.io.File; import java.io.File;
@@ -57,7 +58,7 @@ public final class LegacySupport {
* @return A list with the IDs of the imported updates with no matching updates * @return A list with the IDs of the imported updates with no matching updates
*/ */
public static List<String> importDownloads(Context context, public static List<String> importDownloads(Context context,
List<UpdateDownload> updatesJson) { List<UpdateInfo> updatesJson) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (preferences.getBoolean(IMPORT_DONE, false)) { if (preferences.getBoolean(IMPORT_DONE, false)) {
return null; return null;
@@ -73,7 +74,7 @@ public final class LegacySupport {
if (files != null) { if (files != null) {
Map<String, Integer> updatesMap = new HashMap<>(); Map<String, Integer> updatesMap = new HashMap<>();
for (UpdateDownload update : updatesJson) { for (UpdateInfo update : updatesJson) {
updatesMap.put(update.getName(), updatesJson.indexOf(update)); updatesMap.put(update.getName(), updatesJson.indexOf(update));
} }
@@ -86,7 +87,7 @@ public final class LegacySupport {
Log.d(TAG, "Importing " + file.getAbsolutePath()); Log.d(TAG, "Importing " + file.getAbsolutePath());
Integer index = updatesMap.get(file.getName()); Integer index = updatesMap.get(file.getName());
if (index != null) { if (index != null) {
UpdateDownload update = updatesJson.get(index); UpdateDownload update = new UpdateDownload(updatesJson.get(index));
update.setFile(file); update.setFile(file);
update.setFileSize(file.length()); update.setFileSize(file.length());
update.setStatus(UpdateStatus.DOWNLOADED); update.setStatus(UpdateStatus.DOWNLOADED);

View File

@@ -35,8 +35,9 @@ import org.json.JSONObject;
import org.lineageos.updater.R; import org.lineageos.updater.R;
import org.lineageos.updater.UpdatesDbHelper; import org.lineageos.updater.UpdatesDbHelper;
import org.lineageos.updater.controller.UpdaterService; import org.lineageos.updater.controller.UpdaterService;
import org.lineageos.updater.model.Update; import org.lineageos.updater.model.UpdateBaseInfo;
import org.lineageos.updater.model.UpdateDownload; import org.lineageos.updater.model.UpdateDownload;
import org.lineageos.updater.model.UpdateInfo;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@@ -76,9 +77,9 @@ public class Utils {
return new File(context.getCacheDir(), "updates.json"); return new File(context.getCacheDir(), "updates.json");
} }
// This should really return an Update object, but currently this only // This should really return an UpdateBaseInfo object, but currently this only
// used to initialize UpdateDownload objects // used to initialize UpdateInfo objects
private static UpdateDownload parseJsonUpdate(JSONObject object) throws JSONException { private static UpdateInfo parseJsonUpdate(JSONObject object) throws JSONException {
UpdateDownload update = new UpdateDownload(); UpdateDownload update = new UpdateDownload();
update.setTimestamp(object.getLong("datetime")); update.setTimestamp(object.getLong("datetime"));
update.setName(object.getString("filename")); update.setName(object.getString("filename"));
@@ -89,7 +90,7 @@ public class Utils {
return update; return update;
} }
public static boolean isCompatible(Update update) { public static boolean isCompatible(UpdateBaseInfo update) {
if (update.getTimestamp() < SystemProperties.getLong(Constants.PROP_BUILD_DATE, 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;
@@ -101,15 +102,15 @@ public class Utils {
return true; return true;
} }
public static boolean canInstall(Update update) { public static boolean canInstall(UpdateBaseInfo update) {
return update.getTimestamp() >= SystemProperties.getLong(Constants.PROP_BUILD_DATE, 0) && return update.getTimestamp() >= SystemProperties.getLong(Constants.PROP_BUILD_DATE, 0) &&
update.getVersion().equalsIgnoreCase( update.getVersion().equalsIgnoreCase(
SystemProperties.get(Constants.PROP_BUILD_VERSION)); SystemProperties.get(Constants.PROP_BUILD_VERSION));
} }
public static List<UpdateDownload> parseJson(File file, boolean compatibleOnly) public static List<UpdateInfo> parseJson(File file, boolean compatibleOnly)
throws IOException, JSONException { throws IOException, JSONException {
List<UpdateDownload> updates = new ArrayList<>(); List<UpdateInfo> updates = new ArrayList<>();
String json = ""; String json = "";
try (BufferedReader br = new BufferedReader(new FileReader(file))) { try (BufferedReader br = new BufferedReader(new FileReader(file))) {
@@ -125,7 +126,7 @@ public class Utils {
continue; continue;
} }
try { try {
UpdateDownload update = parseJsonUpdate(updatesList.getJSONObject(i)); UpdateInfo update = parseJsonUpdate(updatesList.getJSONObject(i));
if (compatibleOnly && isCompatible(update)) { if (compatibleOnly && isCompatible(update)) {
updates.add(update); updates.add(update);
} else { } else {
@@ -175,15 +176,15 @@ public class Utils {
*/ */
public static boolean checkForNewUpdates(File oldJson, File newJson) public static boolean checkForNewUpdates(File oldJson, File newJson)
throws IOException, JSONException { throws IOException, JSONException {
List<UpdateDownload> oldList = parseJson(oldJson, true); List<UpdateInfo> oldList = parseJson(oldJson, true);
List<UpdateDownload> newList = parseJson(newJson, true); List<UpdateInfo> newList = parseJson(newJson, true);
Set<String> oldIds = new HashSet<>(); Set<String> oldIds = new HashSet<>();
for (Update update : oldList) { for (UpdateInfo update : oldList) {
oldIds.add(update.getDownloadId()); oldIds.add(update.getDownloadId());
} }
// In case of no new updates, the old list should // In case of no new updates, the old list should
// have all (if not more) the updates // have all (if not more) the updates
for (Update update : newList) { for (UpdateInfo update : newList) {
if (!oldIds.contains(update.getDownloadId())) { if (!oldIds.contains(update.getDownloadId())) {
return true; return true;
} }
@@ -249,7 +250,7 @@ public class Utils {
// Ideally the database is empty when we get here // Ideally the database is empty when we get here
UpdatesDbHelper dbHelper = new UpdatesDbHelper(context); UpdatesDbHelper dbHelper = new UpdatesDbHelper(context);
List<String> knownPaths = new ArrayList<>(); List<String> knownPaths = new ArrayList<>();
for (UpdateDownload update : dbHelper.getUpdates()) { for (UpdateInfo update : dbHelper.getUpdates()) {
knownPaths.add(update.getFile().getAbsolutePath()); knownPaths.add(update.getFile().getAbsolutePath());
} }
for (File file : files) { for (File file : files) {

View File

@@ -15,7 +15,7 @@
*/ */
package org.lineageos.updater.model; package org.lineageos.updater.model;
public class Update { public class Update implements UpdateBaseInfo {
private String mName; private String mName;
private String mDownloadUrl; private String mDownloadUrl;
@@ -27,7 +27,7 @@ public class Update {
public Update() { public Update() {
} }
public Update(Update update) { public Update(UpdateBaseInfo update) {
mName = update.getName(); mName = update.getName();
mDownloadUrl = update.getDownloadUrl(); mDownloadUrl = update.getDownloadUrl();
mDownloadId = update.getDownloadId(); mDownloadId = update.getDownloadId();
@@ -36,6 +36,7 @@ public class Update {
mVersion = update.getVersion(); mVersion = update.getVersion();
} }
@Override
public String getName() { public String getName() {
return mName; return mName;
} }
@@ -44,6 +45,7 @@ public class Update {
mName = name; mName = name;
} }
@Override
public String getDownloadId() { public String getDownloadId() {
return mDownloadId; return mDownloadId;
} }
@@ -52,6 +54,7 @@ public class Update {
mDownloadId = downloadId; mDownloadId = downloadId;
} }
@Override
public long getTimestamp() { public long getTimestamp() {
return mTimestamp; return mTimestamp;
} }
@@ -60,6 +63,7 @@ public class Update {
mTimestamp = timestamp; mTimestamp = timestamp;
} }
@Override
public String getType() { public String getType() {
return mType; return mType;
} }
@@ -68,6 +72,7 @@ public class Update {
mType = type; mType = type;
} }
@Override
public String getVersion() { public String getVersion() {
return mVersion; return mVersion;
} }
@@ -76,6 +81,7 @@ public class Update {
mVersion = version; mVersion = version;
} }
@Override
public String getDownloadUrl() { public String getDownloadUrl() {
return mDownloadUrl; return mDownloadUrl;
} }

View File

@@ -0,0 +1,30 @@
/*
* 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.model;
public interface UpdateBaseInfo {
String getName();
String getDownloadId();
long getTimestamp();
String getType();
String getVersion();
String getDownloadUrl();
}

View File

@@ -17,7 +17,7 @@ package org.lineageos.updater.model;
import java.io.File; import java.io.File;
public class UpdateDownload extends Update { public class UpdateDownload extends Update implements UpdateInfo {
private UpdateStatus mStatus = UpdateStatus.UNKNOWN; private UpdateStatus mStatus = UpdateStatus.UNKNOWN;
private int mPersistentStatus = UpdateStatus.Persistent.UNKNOWN; private int mPersistentStatus = UpdateStatus.Persistent.UNKNOWN;
@@ -32,11 +32,11 @@ public class UpdateDownload extends Update {
public UpdateDownload() { public UpdateDownload() {
} }
public UpdateDownload(Update update) { public UpdateDownload(UpdateBaseInfo update) {
super(update); super(update);
} }
public UpdateDownload(UpdateDownload update) { public UpdateDownload(UpdateInfo update) {
super(update); super(update);
mStatus = update.getStatus(); mStatus = update.getStatus();
mPersistentStatus = update.getPersistentStatus(); mPersistentStatus = update.getPersistentStatus();
@@ -49,6 +49,7 @@ public class UpdateDownload extends Update {
mAvailableOnline = update.getAvailableOnline(); mAvailableOnline = update.getAvailableOnline();
} }
@Override
public UpdateStatus getStatus() { public UpdateStatus getStatus() {
return mStatus; return mStatus;
} }
@@ -57,6 +58,7 @@ public class UpdateDownload extends Update {
mStatus = status; mStatus = status;
} }
@Override
public int getPersistentStatus() { public int getPersistentStatus() {
return mPersistentStatus; return mPersistentStatus;
} }
@@ -65,6 +67,7 @@ public class UpdateDownload extends Update {
mPersistentStatus = status; mPersistentStatus = status;
} }
@Override
public File getFile() { public File getFile() {
return mFile; return mFile;
} }
@@ -73,6 +76,7 @@ public class UpdateDownload extends Update {
mFile = file; mFile = file;
} }
@Override
public long getFileSize() { public long getFileSize() {
return mFileSize; return mFileSize;
} }
@@ -81,6 +85,7 @@ public class UpdateDownload extends Update {
mFileSize = fileSize; mFileSize = fileSize;
} }
@Override
public int getProgress() { public int getProgress() {
return mProgress; return mProgress;
} }
@@ -89,6 +94,7 @@ public class UpdateDownload extends Update {
mProgress = progress; mProgress = progress;
} }
@Override
public long getEta() { public long getEta() {
return mEta; return mEta;
} }
@@ -97,6 +103,7 @@ public class UpdateDownload extends Update {
mEta = eta; mEta = eta;
} }
@Override
public long getSpeed() { public long getSpeed() {
return mSpeed; return mSpeed;
} }
@@ -105,6 +112,7 @@ public class UpdateDownload extends Update {
mSpeed = speed; mSpeed = speed;
} }
@Override
public int getInstallProgress() { public int getInstallProgress() {
return mInstallProgress; return mInstallProgress;
} }
@@ -113,6 +121,7 @@ public class UpdateDownload extends Update {
mInstallProgress = progress; mInstallProgress = progress;
} }
@Override
public boolean getAvailableOnline() { public boolean getAvailableOnline() {
return mAvailableOnline; return mAvailableOnline;
} }

View File

@@ -0,0 +1,38 @@
/*
* 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.model;
import java.io.File;
public interface UpdateInfo extends UpdateBaseInfo {
UpdateStatus getStatus();
int getPersistentStatus();
File getFile();
long getFileSize();
int getProgress();
long getEta();
long getSpeed();
int getInstallProgress();
boolean getAvailableOnline();
}