Import updates downloaded with CMUpdater
This commit is contained in:
@@ -41,6 +41,7 @@ import org.lineageos.updater.controller.UpdaterController;
|
|||||||
import org.lineageos.updater.controller.UpdaterControllerInt;
|
import org.lineageos.updater.controller.UpdaterControllerInt;
|
||||||
import org.lineageos.updater.controller.UpdaterService;
|
import org.lineageos.updater.controller.UpdaterService;
|
||||||
import org.lineageos.updater.misc.Constants;
|
import org.lineageos.updater.misc.Constants;
|
||||||
|
import org.lineageos.updater.misc.LegacySupport;
|
||||||
import org.lineageos.updater.misc.Utils;
|
import org.lineageos.updater.misc.Utils;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -161,12 +162,23 @@ public class UpdatesActivity extends AppCompatActivity {
|
|||||||
Log.d(TAG, "Adding remote updates");
|
Log.d(TAG, "Adding remote updates");
|
||||||
UpdaterControllerInt controller = mUpdaterService.getUpdaterController();
|
UpdaterControllerInt controller = mUpdaterService.getUpdaterController();
|
||||||
boolean newUpdates = false;
|
boolean newUpdates = false;
|
||||||
|
|
||||||
|
List<UpdateDownload> updates = Utils.parseJson(jsonFile, true);
|
||||||
|
|
||||||
|
List<UpdateDownload> updatesNotAvailable = LegacySupport.importDownloads(this, updates);
|
||||||
|
|
||||||
List<String> updatesOnline = new ArrayList<>();
|
List<String> updatesOnline = new ArrayList<>();
|
||||||
for (UpdateDownload update : Utils.parseJson(jsonFile, true)) {
|
for (UpdateDownload update : updates) {
|
||||||
newUpdates |= controller.addUpdate(update);
|
newUpdates |= controller.addUpdate(update);
|
||||||
updatesOnline.add(update.getDownloadId());
|
updatesOnline.add(update.getDownloadId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (updatesNotAvailable != null) {
|
||||||
|
for (UpdateDownload update : updatesNotAvailable) {
|
||||||
|
update.setAvailableOnline(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
controller.setUpdatesAvailableOnline(updatesOnline, true);
|
controller.setUpdatesAvailableOnline(updatesOnline, true);
|
||||||
|
|
||||||
if (manualRefresh) {
|
if (manualRefresh) {
|
||||||
|
|||||||
154
src/org/lineageos/updater/misc/LegacySupport.java
Normal file
154
src/org/lineageos/updater/misc/LegacySupport.java
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.preference.PreferenceManager;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import org.lineageos.updater.UpdateDownload;
|
||||||
|
import org.lineageos.updater.UpdateStatus;
|
||||||
|
import org.lineageos.updater.UpdatesDbHelper;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public final class LegacySupport {
|
||||||
|
|
||||||
|
private static final String TAG = "LegacySupport";
|
||||||
|
|
||||||
|
private static final String IMPORT_DONE = "import_done";
|
||||||
|
|
||||||
|
private static class IllegalFilenameException extends Exception {
|
||||||
|
IllegalFilenameException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This imports the updates downloaded with CMUpdater. It accepts in input a list of
|
||||||
|
* updates to be downloaded and updates its entries when it finds a matching imported
|
||||||
|
* update (i.e. same filename).
|
||||||
|
*
|
||||||
|
* @param context
|
||||||
|
* @param updatesJson List of updates to be downloaded
|
||||||
|
* @return List of updates that weren't used to replace entries of updatesJson
|
||||||
|
*/
|
||||||
|
public static List<UpdateDownload> importDownloads(Context context,
|
||||||
|
List<UpdateDownload> updatesJson) {
|
||||||
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
|
if (preferences.getBoolean(IMPORT_DONE, false)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.d(TAG, "Importing downloads");
|
||||||
|
|
||||||
|
List<UpdateDownload> notReplacing = new ArrayList<>();
|
||||||
|
File updatesDir = new File(context.getDataDir(), "updates/");
|
||||||
|
if (updatesDir.isDirectory()) {
|
||||||
|
UpdatesDbHelper dbHelper = new UpdatesDbHelper(context);
|
||||||
|
File[] files = updatesDir.listFiles();
|
||||||
|
if (files != null) {
|
||||||
|
|
||||||
|
Map<String, Integer> updatesMap = new HashMap<>();
|
||||||
|
for (UpdateDownload update : updatesJson) {
|
||||||
|
updatesMap.put(update.getName(), updatesJson.indexOf(update));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (File file : updatesDir.listFiles()) {
|
||||||
|
if (!file.getName().endsWith(".zip")) {
|
||||||
|
// Note: there could be complete, but unverified downloads here
|
||||||
|
Log.d(TAG, "Deleting " + file.getAbsolutePath());
|
||||||
|
file.delete();
|
||||||
|
} else {
|
||||||
|
Log.d(TAG, "Importing " + file.getAbsolutePath());
|
||||||
|
Integer index = updatesMap.get(file.getName());
|
||||||
|
if (index != null) {
|
||||||
|
UpdateDownload update = updatesJson.get(index);
|
||||||
|
update.setFile(file);
|
||||||
|
update.setFileSize(file.length());
|
||||||
|
update.setStatus(UpdateStatus.DOWNLOADED);
|
||||||
|
update.setPersistentStatus(UpdateStatus.Persistent.INCOMPLETE);
|
||||||
|
dbHelper.addUpdate(update);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
UpdateDownload update = createUpdateFromFile(file);
|
||||||
|
notReplacing.add(update);
|
||||||
|
dbHelper.addUpdate(update);
|
||||||
|
} catch (IllegalFilenameException e) {
|
||||||
|
Log.e(TAG, "Deleting " + file.getAbsolutePath(), e);
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
preferences.edit().putBoolean(IMPORT_DONE, true).apply();
|
||||||
|
return notReplacing;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static UpdateDownload createUpdateFromFile(File file) throws IllegalFilenameException {
|
||||||
|
UpdateDownload update = new UpdateDownload();
|
||||||
|
update.setDownloadId(UUID.randomUUID().toString());
|
||||||
|
update.setFile(file);
|
||||||
|
update.setFileSize(file.length());
|
||||||
|
String name = file.getName();
|
||||||
|
update.setName(name);
|
||||||
|
update.setTimestamp(getTimestampFromFileName(name));
|
||||||
|
update.setVersion(getVersionFromFileName(name));
|
||||||
|
update.setType(getTypeFromFileName(name));
|
||||||
|
update.setPersistentStatus(UpdateStatus.Persistent.INCOMPLETE);
|
||||||
|
|
||||||
|
return update;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static long getTimestampFromFileName(String fileName) throws IllegalFilenameException {
|
||||||
|
String[] subStrings = fileName.split("-");
|
||||||
|
if (subStrings.length < 3 || subStrings[2].length() < 8) {
|
||||||
|
throw new IllegalArgumentException("The given filename is not valid");
|
||||||
|
}
|
||||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
|
||||||
|
try {
|
||||||
|
return (dateFormat.parse(subStrings[2]).getTime() / 1000);
|
||||||
|
} catch (ParseException e) {
|
||||||
|
throw new IllegalFilenameException("The given filename is not valid");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getVersionFromFileName(String fileName) throws IllegalFilenameException {
|
||||||
|
String[] subStrings = fileName.split("-");
|
||||||
|
if (subStrings.length < 2 || subStrings[1].length() < 4) {
|
||||||
|
throw new IllegalFilenameException("The given filename is not valid");
|
||||||
|
}
|
||||||
|
return subStrings[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getTypeFromFileName(String fileName) throws IllegalFilenameException {
|
||||||
|
String[] subStrings = fileName.split("-");
|
||||||
|
if (subStrings.length < 4 || subStrings[3].length() < 7) {
|
||||||
|
throw new IllegalFilenameException("The given filename is not valid");
|
||||||
|
}
|
||||||
|
return subStrings[3].toLowerCase();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user