src/main/java/de/blau/android/prefs/PresetLoader.java
/**
     * Download a Preset
     * 
     * @param url the url to download from
     * @param presetDir the directory to save the preset to
     * @param filename A filename where to save the file. If null, the URL will be hashed using the PresetIconManager
     *            hash function and the file will be saved to hashvalue.png (where "hashvalue" will be replaced with the
     *            URL hash).
     * @return code indicating result
     */
Download a Preset

@param url the url to download from
@param presetDir the directory to save the preset to
@param filename A filename where to save the file. If null, the URL will be hashed using the PresetIconManager
hash function and the file will be saved to hashvalue.png (where "hashvalue" will be replaced with the
URL hash).
@return code indicating result
Download a Preset @param url the url to download from @param presetDir the directory to save the preset to @param filename A filename where to save the file . If null the URL will be hashed using the PresetIconManager hash function and the file will be saved to hashvalue . png ( where " hashvalue " will be replaced with the URL hash ) . @return code indicating result
public static int download(@NonNull String url, @NonNull File presetDir, @Nullable String filename) {
        if (filename == null) {
            filename = PresetIconManager.hash(url) + ".png";
        }
        Log.d(DEBUG_TAG, "Downloading " + url + " to " + presetDir + "/" + filename);
        Request request = new Request.Builder().url(url).build();
        OkHttpClient client = App.getHttpClient().newBuilder().connectTimeout(Server.TIMEOUT, TimeUnit.MILLISECONDS)
                .readTimeout(Server.TIMEOUT, TimeUnit.MILLISECONDS).build();
        Call presetCall = client.newCall(request);
        try (Response presetCallResponse = presetCall.execute()) {
            if (presetCallResponse.isSuccessful()) {
                ResponseBody responseBody = presetCallResponse.body();
                InputStream downloadStream = responseBody.byteStream();
                String contentType = responseBody.contentType().toString();
                boolean zip = (contentType != null && MimeTypes.ZIP.equalsIgnoreCase(contentType))
                        || url.toLowerCase(Locale.US).endsWith("." + FileExtensions.ZIP);
                if (zip) {
                    Log.d(DEBUG_TAG, "detected zip file");
                    filename = System.currentTimeMillis() + FILE_NAME_TEMPORARY_ARCHIVE;
                }
                final File destinationFile = new File(presetDir, filename);
                try (OutputStream fileStream = new FileOutputStream(destinationFile)) {
                    StreamUtils.copy(downloadStream, fileStream);
                    if (zip && FileUtil.unpackZip(presetDir.getPath() + Paths.DELIMITER, filename)) {
                        if (!destinationFile.delete()) { 
                            Log.e(DEBUG_TAG, "Could not delete " + filename);
                        }
                        return DOWNLOADED_PRESET_ZIP;
                    }
                    return DOWNLOADED_PRESET_XML;
                }
            } else {
                Log.w(DEBUG_TAG, "Could not download file " + url + " respose code " + presetCallResponse.code());
                return DOWNLOADED_PRESET_ERROR;
            }
        } catch (Exception e) {
            Log.e(DEBUG_TAG, "Could not download file " + url + " " + e.getMessage());
            return DOWNLOADED_PRESET_ERROR;
        }
    }
public static int download ( @ NonNull String url , @ NonNull File presetDir , @ Nullable String filename ) { if ( filename == null ) { filename = PresetIconManager . hash ( url ) + ".png" ; } Log . d ( DEBUG_TAG , "Downloading " + url + " to " + presetDir + "/" + filename ) ; Request request = new Request . Builder ( ) . url ( url ) . build ( ) ; OkHttpClient client = App . getHttpClient ( ) . newBuilder ( ) . connectTimeout ( Server . TIMEOUT , TimeUnit . MILLISECONDS ) . readTimeout ( Server . TIMEOUT , TimeUnit . MILLISECONDS ) . build ( ) ; Call presetCall = client . newCall ( request ) ; try ( Response presetCallResponse = presetCall . execute ( ) ) { if ( presetCallResponse . isSuccessful ( ) ) { ResponseBody responseBody = presetCallResponse . body ( ) ; InputStream downloadStream = responseBody . byteStream ( ) ; String contentType = responseBody . contentType ( ) . toString ( ) ; boolean zip = ( contentType != null && MimeTypes . ZIP . equalsIgnoreCase ( contentType ) ) || url . toLowerCase ( Locale . US ) . endsWith ( "." + FileExtensions . ZIP ) ; if ( zip ) { Log . d ( DEBUG_TAG , "detected zip file" ) ; filename = System . currentTimeMillis ( ) + FILE_NAME_TEMPORARY_ARCHIVE ; } final File destinationFile = new File ( presetDir , filename ) ; try ( OutputStream fileStream = new FileOutputStream ( destinationFile ) ) { StreamUtils . copy ( downloadStream , fileStream ) ; if ( zip && FileUtil . unpackZip ( presetDir . getPath ( ) + Paths . DELIMITER , filename ) ) { if ( ! destinationFile . delete ( ) ) { Log . e ( DEBUG_TAG , "Could not delete " + filename ) ; } return DOWNLOADED_PRESET_ZIP ; } return DOWNLOADED_PRESET_XML ; } } else { Log . w ( DEBUG_TAG , "Could not download file " + url + " respose code " + presetCallResponse . code ( ) ) ; return DOWNLOADED_PRESET_ERROR ; } } catch ( Exception e ) { Log . e ( DEBUG_TAG , "Could not download file " + url + " " + e . getMessage ( ) ) ; return DOWNLOADED_PRESET_ERROR ; } }
Download a Preset
Download a Preset
// NOSONAR requires API 26