Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3be47241 authored by Gabriele M's avatar Gabriele M
Browse files

Remove LegacySupport class

This was meant to provide an upgrade path from the old app. It's
been almost a year now, this class is no longer needed.

Change-Id: I17c82e5877b275c5ceb3caefa28066abc170f095
parent 852d7761
Loading
Loading
Loading
Loading
+0 −10
Original line number Diff line number Diff line
@@ -55,7 +55,6 @@ import org.lineageos.updater.controller.UpdaterService;
import org.lineageos.updater.download.DownloadClient;
import org.lineageos.updater.misc.BuildInfoUtils;
import org.lineageos.updater.misc.Constants;
import org.lineageos.updater.misc.LegacySupport;
import org.lineageos.updater.misc.StringGenerator;
import org.lineageos.updater.misc.Utils;
import org.lineageos.updater.model.UpdateInfo;
@@ -243,20 +242,11 @@ public class UpdatesActivity extends UpdatesListActivity {
        boolean newUpdates = false;

        List<UpdateInfo> updates = Utils.parseJson(jsonFile, true);

        List<String> importedNotAvailableOnline = LegacySupport.importDownloads(this, updates);

        List<String> updatesOnline = new ArrayList<>();
        for (UpdateInfo update : updates) {
            newUpdates |= controller.addUpdate(update);
            updatesOnline.add(update.getDownloadId());
        }

        if (importedNotAvailableOnline != null) {
            updatesOnline.removeAll(importedNotAvailableOnline);
            controller.setUpdatesNotAvailableOnline(importedNotAvailableOnline);
        }

        controller.setUpdatesAvailableOnline(updatesOnline, true);

        if (manualRefresh) {
+0 −159
Original line number Diff line number Diff line
/*
 * 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.UpdatesDbHelper;
import org.lineageos.updater.model.Update;
import org.lineageos.updater.model.UpdateInfo;
import org.lineageos.updater.model.UpdateStatus;

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.Locale;
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 method imports the updates downloaded with CMUpdater and it adds them to the
     * updates database. It accepts in input a list of updates which it updates with the
     * data of matching imported updates (i.e. same filename). If for a given imported
     * update this method can't find any matching update, it adds a new entry to the
     * given list.
     *
     * @param context
     * @param updatesJson List of updates to be downloaded
     * @return A list with the IDs of the imported updates with no matching updates
     */
    public static List<String> importDownloads(Context context,
            List<UpdateInfo> updatesJson) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        if (preferences.getBoolean(IMPORT_DONE, false)) {
            return null;
        }

        Log.d(TAG, "Importing downloads");

        List<String> notReplacing = new ArrayList<>();
        File updatesDir = new File(context.getDataDir(), "app_updates/");
        if (updatesDir.isDirectory()) {
            UpdatesDbHelper dbHelper = new UpdatesDbHelper(context);
            File[] files = updatesDir.listFiles();
            if (files != null) {

                Map<String, Integer> updatesMap = new HashMap<>();
                for (UpdateInfo 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) {
                            Update update = new 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 {
                                Update update = createUpdateFromFile(file);
                                notReplacing.add(update.getDownloadId());
                                updatesJson.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 Update createUpdateFromFile(File file) throws IllegalFilenameException {
        Update update = new Update();
        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(Locale.ROOT);
    }
}