src/main/java/de/blau/android/prefs/PresetEditorActivity.java
/**
     * Download data (XML, icons) for a certain preset or load it from a file
     * 
     * @param activity a PresetEditorActivity instance
     * @param db an AdvancedPrefDatabase instance
     * @param item the item containing the preset to be downloaded
     */
Download data (XML, icons) for a certain preset or load it from a file

@param activity a PresetEditorActivity instance
@param db an AdvancedPrefDatabase instance
@param item the item containing the preset to be downloaded
Download data ( XML icons ) for a certain preset or load it from a file @param activity a PresetEditorActivity instance @param db an AdvancedPrefDatabase instance @param item the item containing the preset to be downloaded
private static void retrievePresetData(@NonNull PresetEditorActivity activity, @NonNull AdvancedPrefDatabase db, @NonNull final ListEditItem item) {
        final File presetDir = db.getPresetDirectory(item.id);
        presetDir.mkdir();
        if (!presetDir.isDirectory()) {
            throw new OperationFailedException("Could not create preset directory " + presetDir.getAbsolutePath());
        }
        if (item.value.startsWith(Preset.APKPRESET_URLPREFIX)) {
            activity.sendResultIfApplicable(item);
            return;
        }
        new ExecutorTask<Void, Integer, Integer>() {
            private boolean canceled = false;
            @Override
            protected void onPreExecute() {
                Progress.showDialog(activity, Progress.PROGRESS_PRESET);
            }
            @Override
            protected Integer doInBackground(Void args) {
                Uri uri = Uri.parse(item.value);
                final String scheme = uri.getScheme();
                int loadResult;
                if (Schemes.FILE.equals(scheme) || Schemes.CONTENT.equals(scheme)) {
                    loadResult = PresetLoader.load(activity, uri, presetDir, Preset.PRESETXML);
                } else {
                    loadResult = PresetLoader.download(item.value, presetDir, Preset.PRESETXML);
                }
                if (loadResult == PresetLoader.DOWNLOADED_PRESET_ERROR) {
                    return RESULT_TOTAL_FAILURE;
                } else if (loadResult == PresetLoader.DOWNLOADED_PRESET_ZIP) {
                    return RESULT_TOTAL_SUCCESS;
                } 
                List<String> urls = Preset.parseForURLs(presetDir);
                if (urls == null) {
                    Log.e(DEBUG_TAG, "Could not parse preset for URLs");
                    return RESULT_PRESET_NOT_PARSABLE;
                }
                boolean allImagesSuccessful = true;
                for (String url : urls) {
                    if (canceled) {
                        return RESULT_DOWNLOAD_CANCELED;
                    }
                    allImagesSuccessful &= (PresetLoader.download(url, presetDir, null) == PresetLoader.DOWNLOADED_PRESET_XML);
                }
                return allImagesSuccessful ? RESULT_TOTAL_SUCCESS : RESULT_IMAGE_FAILURE;
            }
            @Override
            protected void onPostExecute(Integer result) {
                Progress.dismissDialog(activity, Progress.PROGRESS_PRESET);
                switch (result) {
                case RESULT_TOTAL_SUCCESS:
                    Snack.barInfo(activity, R.string.preset_download_successful);
                    activity.sendResultIfApplicable(item);
                    break;
                case RESULT_TOTAL_FAILURE:
                    msgbox(R.string.preset_download_failed);
                    break;
                case RESULT_PRESET_NOT_PARSABLE:
                    db.removePresetDirectory(item.id);
                    msgbox(R.string.preset_download_parse_failed);
                    break;
                case RESULT_IMAGE_FAILURE:
                    msgbox(R.string.preset_download_missing_images);
                    break;
                case RESULT_DOWNLOAD_CANCELED:
                    break; 
                default:
                    break;
                }
            }
            /**
             * Show a simple message box detailing the download result. The activity will end as soon as the box is
             * closed.
             * 
             * @param msgResID string resource id of message
             */
            private void msgbox(int msgResID) {
                AlertDialog.Builder box = new AlertDialog.Builder(activity);
                box.setMessage(activity.getResources().getString(msgResID));
                box.setOnCancelListener(dialog -> activity.sendResultIfApplicable(item));
                box.setPositiveButton(R.string.okay, (dialog, which) -> {
                    dialog.dismiss();
                    activity.sendResultIfApplicable(item);
                });
                box.show();
            }
        }.execute();
    }
private static void retrievePresetData ( @ NonNull PresetEditorActivity activity , @ NonNull AdvancedPrefDatabase db , @ NonNull final ListEditItem item ) { final File presetDir = db . getPresetDirectory ( item . id ) ; presetDir . mkdir ( ) ; if ( ! presetDir . isDirectory ( ) ) { throw new OperationFailedException ( "Could not create preset directory " + presetDir . getAbsolutePath ( ) ) ; } if ( item . value . startsWith ( Preset . APKPRESET_URLPREFIX ) ) { activity . sendResultIfApplicable ( item ) ; return ; } new ExecutorTask < Void , Integer , Integer > ( ) { private boolean canceled = false ; @ Override protected void onPreExecute ( ) { Progress . showDialog ( activity , Progress . PROGRESS_PRESET ) ; } @ Override protected Integer doInBackground ( Void args ) { Uri uri = Uri . parse ( item . value ) ; final String scheme = uri . getScheme ( ) ; int loadResult ; if ( Schemes . FILE . equals ( scheme ) || Schemes . CONTENT . equals ( scheme ) ) { loadResult = PresetLoader . load ( activity , uri , presetDir , Preset . PRESETXML ) ; } else { loadResult = PresetLoader . download ( item . value , presetDir , Preset . PRESETXML ) ; } if ( loadResult == PresetLoader . DOWNLOADED_PRESET_ERROR ) { return RESULT_TOTAL_FAILURE ; } else if ( loadResult == PresetLoader . DOWNLOADED_PRESET_ZIP ) { return RESULT_TOTAL_SUCCESS ; } List < String > urls = Preset . parseForURLs ( presetDir ) ; if ( urls == null ) { Log . e ( DEBUG_TAG , "Could not parse preset for URLs" ) ; return RESULT_PRESET_NOT_PARSABLE ; } boolean allImagesSuccessful = true ; for ( String url : urls ) { if ( canceled ) { return RESULT_DOWNLOAD_CANCELED ; } allImagesSuccessful &= ( PresetLoader . download ( url , presetDir , null ) == PresetLoader . DOWNLOADED_PRESET_XML ) ; } return allImagesSuccessful ? RESULT_TOTAL_SUCCESS : RESULT_IMAGE_FAILURE ; } @ Override protected void onPostExecute ( Integer result ) { Progress . dismissDialog ( activity , Progress . PROGRESS_PRESET ) ; switch ( result ) { case RESULT_TOTAL_SUCCESS : Snack . barInfo ( activity , R . string . preset_download_successful ) ; activity . sendResultIfApplicable ( item ) ; break ; case RESULT_TOTAL_FAILURE : msgbox ( R . string . preset_download_failed ) ; break ; case RESULT_PRESET_NOT_PARSABLE : db . removePresetDirectory ( item . id ) ; msgbox ( R . string . preset_download_parse_failed ) ; break ; case RESULT_IMAGE_FAILURE : msgbox ( R . string . preset_download_missing_images ) ; break ; case RESULT_DOWNLOAD_CANCELED : break ; default : break ; } } /**
             * Show a simple message box detailing the download result. The activity will end as soon as the box is
             * closed.
             * 
             * @param msgResID string resource id of message
             */ private void msgbox ( int msgResID ) { AlertDialog . Builder box = new AlertDialog . Builder ( activity ) ; box . setMessage ( activity . getResources ( ) . getString ( msgResID ) ) ; box . setOnCancelListener ( dialog -> activity . sendResultIfApplicable ( item ) ) ; box . setPositiveButton ( R . string . okay , ( dialog , which ) -> { dialog . dismiss ( ) ; activity . sendResultIfApplicable ( item ) ; } ) ; box . show ( ) ; } } . execute ( ) ; }
Download data (XML, icons) for a certain preset or load it from a file
Download data ( XML icons ) for a certain preset or load it from a file
// noinspection ResultOfMethodCallIgnored // fall through to further processing // do nothing