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

Commit 78ba8512 authored by Ingo's avatar Ingo
Browse files

Make code handle downloads from TRWP official site plus configurable file...

Make code handle downloads from TRWP official site plus configurable file extension for checksum files
parent 0d96b5ae
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.util.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@@ -45,8 +46,8 @@ public class DownloadSrcController extends AbstractSubController {
    @FXML Label progressTitle;
    @FXML Button restartDownloadBtn;
    
    private Map<String, String> sourcesToDownload;
    private Iterator<Map.Entry<String, String>> taskIterator;
    private Map<String, Pair<String, String>> sourcesToDownload;
    private Iterator<Map.Entry<String, Pair<String, String>>> taskIterator;
    private DownloadTask currentTask;
    
    @Override    
@@ -71,8 +72,8 @@ public class DownloadSrcController extends AbstractSubController {
        logger.info("startNextDownload()");
        logger.debug("taskIterator has next ? {} "+taskIterator.hasNext());
        if(taskIterator.hasNext()){
            Map.Entry<String, String> sources = taskIterator.next();
            currentTask = new DownloadTask(sources.getKey(), sources.getValue(), i18n);
            Map.Entry<String, Pair<String, String>> sources = taskIterator.next();
            currentTask = new DownloadTask(sources.getKey(), sources.getValue().getKey(), i18n, sources.getValue().getValue());
            
            
            currentTask.setOnSucceeded((WorkerStateEvent eh)->{
+12 −5
Original line number Diff line number Diff line
@@ -56,31 +56,34 @@ public class DownloadTask extends Task<Boolean>{
    final private ResourceBundle i18n;
    final private String targetUrl;
    final private String fileName;
    final private String checkSumExt;

    /**
     * COnstruction of the download task
     * @param targetUrl the web path to the resource
     * @param fileName name of the file
     * @param resources used to send already translated message
     * @param checkSumExt (optional) file extension for the checksum file
     */
    public DownloadTask(String targetUrl, String fileName, ResourceBundle resources){
    public DownloadTask(String targetUrl, String fileName, ResourceBundle resources, String checkSumExt){
        this.targetUrl = targetUrl;
        this.fileName = fileName;
        this.i18n = resources;
        this.checkSumExt = checkSumExt;
    }
    
    
    @Override
    protected Boolean call() throws Exception {
        final String localFilePath = AppConstants.getSourcesFolderPath()+fileName;
        final String checksumFilePath = localFilePath+".sha256sum";
        final String checksumFilePath = localFilePath+checkSumExt;
        
        if(isCancelled()) return false;
        
        this.updateTitle("Downloading "+fileName+".sha256sum");
        this.updateTitle("Downloading "+fileName+checkSumExt);
        
        // Download checksum. If file not downloaded return false and stop downloadin because integrity isn't guaranteed
        if( !downloadFile(targetUrl+".sha256sum", checksumFilePath) ){
        if( !downloadFile(targetUrl+checkSumExt, checksumFilePath) ){
            this.updateMessage(i18n.getString("download_lbl_cantcheckIntegrity"));
            return false;
        }
@@ -117,6 +120,10 @@ public class DownloadTask extends Task<Boolean>{
        HttpURLConnection connect = (HttpURLConnection) new URL(fileURL).openConnection();
        connect.setReadTimeout(30000);
        connect.setConnectTimeout(30000);
        if (fileURL.contains("dl.twrp.me")) {
            // direct download from official TWRP site just redirects to itself
            connect.setRequestProperty("Referer", fileURL + ".html");
        }
        
        if(connect.getResponseCode() != HttpURLConnection.HTTP_OK){ //return false it resources is unreachable
            return false;
+9 −3
Original line number Diff line number Diff line
@@ -30,6 +30,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.HashMap;

import javafx.util.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;
@@ -44,7 +46,7 @@ import org.yaml.snakeyaml.Yaml;
public class ThreadFactory {
    private final static Logger logger = LoggerFactory.getLogger(ThreadFactory.class);
    private ProcessMould flashMould;
    private final HashMap<String, String> sourcesToDownload;
    private final HashMap<String, Pair<String, String>> sourcesToDownload;
    private Device device;
    private final String yamlFolderPath;
    
@@ -198,11 +200,15 @@ public class ThreadFactory {
            if(key.equals("twrp")){
                AppConstants.setTwrpImgPath((String) source.get("filePath"));
            }
            sourcesToDownload.put((String) source.get("url"), (String) source.get("filePath"));
            var filePathAndCheckSumExt = new Pair<String, String>(
                    (String) source.get("filePath"),
                    source.containsKey("checkSumExt") ? "."+(String) source.get("checkSumExt") : ".sha256sum"
            );
            sourcesToDownload.put((String) source.get("url"), filePathAndCheckSumExt);
        }
    }
    
    public HashMap<String, String> getSourcesToDownload(){
    public HashMap<String, Pair<String, String>> getSourcesToDownload(){
        return sourcesToDownload;
    }

+1 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ sources:
   twrp:         
        url: https://dl.twrp.me/FP2/twrp-3.4.0-0-FP2.img
        filePath: twrp-3.4.0-0-FP2.img
        checkSumExt: sha256
flash:
    f1:
        ui: