src/main/java/de/blau/android/prefs/PresetEditorActivity.java
/**
     * Opens the dialog to edit an item
     * 
     * @param item the selected item
     */
Opens the dialog to edit an item

@param item the selected item
Opens the dialog to edit an item @param item the selected item
@SuppressLint("InflateParams")
    @Override
    protected void itemEditDialog(final ListEditItem item) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        final LayoutInflater inflater = ThemeUtils.getLayoutInflater(ctx);
        final View mainView = inflater.inflate(R.layout.listedit_presetedit, null);
        final TextView editName = (TextView) mainView.findViewById(R.id.listedit_editName);
        final TextView editValue = (TextView) mainView.findViewById(R.id.listedit_editValue);
        final TextView versionLabel = (TextView) mainView.findViewById(R.id.listedit_labelVersion);
        final TextView version = (TextView) mainView.findViewById(R.id.listedit_version);
        final CheckBox useTranslations = (CheckBox) mainView.findViewById(R.id.listedit_translations);
        final ImageButton fileButton = (ImageButton) mainView.findViewById(R.id.listedit_file_button);
        if (item != null) {
            editName.setText(item.name);
            editValue.setText(item.value);
            useTranslations.setChecked(item.boolean0);
        } else if (isAddingViaIntent()) {
            String tmpName = getIntent().getExtras().getString(EXTRA_NAME);
            String tmpValue = getIntent().getExtras().getString(EXTRA_VALUE);
            editName.setText(tmpName == null ? "" : tmpName);
            editValue.setText(tmpValue == null ? "" : tmpValue);
            useTranslations.setChecked(true);
        }
        if (item != null && item.value3 != null) {
            version.setText(item.value3);
        } else {
            versionLabel.setVisibility(View.GONE);
            version.setVisibility(View.GONE);
        }
        if (item != null && LISTITEM_ID_DEFAULT.equals(item.id)) {
            editName.setInputType(InputType.TYPE_NULL);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                editName.setBackground(null);
            }
            editValue.setEnabled(false);
            fileButton.setEnabled(false);
        }
        builder.setView(mainView);
        builder.setPositiveButton(R.string.okay, (dialog, which) -> {
        });
        builder.setNegativeButton(R.string.cancel, (dialog, which) -> {
        });
        builder.setOnCancelListener(dialog -> {
            if (isAddingViaIntent()) {
                setResult(RESULT_CANCELED);
                finish();
            }
        });
        fileButton.setOnClickListener(v -> SelectFile.read(PresetEditorActivity.this, R.string.config_presetsPreferredDir_key, new ReadFile() {
            private static final long serialVersionUID = 1L;
            @Override
            public boolean read(Uri fileUri) {
                editValue.setText(fileUri.toString());
                SelectFile.savePref(new Preferences(PresetEditorActivity.this), R.string.config_presetsPreferredDir_key, fileUri);
                return true;
            }
        }));
        final AlertDialog dialog = builder.create();
        dialog.setView(mainView);
        dialog.show();
        dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(v -> {
            String name = editName.getText().toString().trim();
            String presetURL = editValue.getText().toString().trim();
            boolean useTranslationsEnabled = useTranslations.isChecked();
            changeBackgroundColor(editValue, VALID_COLOR);
            boolean validPresetURL = Patterns.WEB_URL.matcher(presetURL).matches();
            URL url = null;
            try {
                url = new URL(presetURL);
            } catch (MalformedURLException e) {
                validPresetURL = false;
            }
            if (validPresetURL || presetURL.startsWith(Schemes.FILE) || presetURL.startsWith(Schemes.CONTENT)
                    || (url != null && "localhost".equals(url.getHost())) || (item != null && item.id.equals(LISTITEM_ID_DEFAULT))) {
                if (item == null) {
                    finishCreateItem(new ListEditItem(name, presetURL, null, null, useTranslationsEnabled));
                } else {
                    item.name = name;
                    item.value = presetURL;
                    item.boolean0 = useTranslationsEnabled;
                    finishEditItem(item);
                }
                dialog.dismiss();
            } else {
                Snack.barError(PresetEditorActivity.this, R.string.toast_invalid_preseturl);
                changeBackgroundColor(editValue, ERROR_COLOR);
            }
        });
        dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setOnClickListener(v -> dialog.dismiss());
    }
@ SuppressLint ( "InflateParams" ) @ Override protected void itemEditDialog ( final ListEditItem item ) { final AlertDialog . Builder builder = new AlertDialog . Builder ( ctx ) ; final LayoutInflater inflater = ThemeUtils . getLayoutInflater ( ctx ) ; final View mainView = inflater . inflate ( R . layout . listedit_presetedit , null ) ; final TextView editName = ( TextView ) mainView . findViewById ( R . id . listedit_editName ) ; final TextView editValue = ( TextView ) mainView . findViewById ( R . id . listedit_editValue ) ; final TextView versionLabel = ( TextView ) mainView . findViewById ( R . id . listedit_labelVersion ) ; final TextView version = ( TextView ) mainView . findViewById ( R . id . listedit_version ) ; final CheckBox useTranslations = ( CheckBox ) mainView . findViewById ( R . id . listedit_translations ) ; final ImageButton fileButton = ( ImageButton ) mainView . findViewById ( R . id . listedit_file_button ) ; if ( item != null ) { editName . setText ( item . name ) ; editValue . setText ( item . value ) ; useTranslations . setChecked ( item . boolean0 ) ; } else if ( isAddingViaIntent ( ) ) { String tmpName = getIntent ( ) . getExtras ( ) . getString ( EXTRA_NAME ) ; String tmpValue = getIntent ( ) . getExtras ( ) . getString ( EXTRA_VALUE ) ; editName . setText ( tmpName == null ? "" : tmpName ) ; editValue . setText ( tmpValue == null ? "" : tmpValue ) ; useTranslations . setChecked ( true ) ; } if ( item != null && item . value3 != null ) { version . setText ( item . value3 ) ; } else { versionLabel . setVisibility ( View . GONE ) ; version . setVisibility ( View . GONE ) ; } if ( item != null && LISTITEM_ID_DEFAULT . equals ( item . id ) ) { editName . setInputType ( InputType . TYPE_NULL ) ; if ( Build . VERSION . SDK_INT >= Build . VERSION_CODES . JELLY_BEAN ) { editName . setBackground ( null ) ; } editValue . setEnabled ( false ) ; fileButton . setEnabled ( false ) ; } builder . setView ( mainView ) ; builder . setPositiveButton ( R . string . okay , ( dialog , which ) -> { } ) ; builder . setNegativeButton ( R . string . cancel , ( dialog , which ) -> { } ) ; builder . setOnCancelListener ( dialog -> { if ( isAddingViaIntent ( ) ) { setResult ( RESULT_CANCELED ) ; finish ( ) ; } } ) ; fileButton . setOnClickListener ( v -> SelectFile . read ( PresetEditorActivity . this , R . string . config_presetsPreferredDir_key , new ReadFile ( ) { private static final long serialVersionUID = 1L ; @ Override public boolean read ( Uri fileUri ) { editValue . setText ( fileUri . toString ( ) ) ; SelectFile . savePref ( new Preferences ( PresetEditorActivity . this ) , R . string . config_presetsPreferredDir_key , fileUri ) ; return true ; } } ) ) ; final AlertDialog dialog = builder . create ( ) ; dialog . setView ( mainView ) ; dialog . show ( ) ; dialog . getButton ( DialogInterface . BUTTON_POSITIVE ) . setOnClickListener ( v -> { String name = editName . getText ( ) . toString ( ) . trim ( ) ; String presetURL = editValue . getText ( ) . toString ( ) . trim ( ) ; boolean useTranslationsEnabled = useTranslations . isChecked ( ) ; changeBackgroundColor ( editValue , VALID_COLOR ) ; boolean validPresetURL = Patterns . WEB_URL . matcher ( presetURL ) . matches ( ) ; URL url = null ; try { url = new URL ( presetURL ) ; } catch ( MalformedURLException e ) { validPresetURL = false ; } if ( validPresetURL || presetURL . startsWith ( Schemes . FILE ) || presetURL . startsWith ( Schemes . CONTENT ) || ( url != null && "localhost" . equals ( url . getHost ( ) ) ) || ( item != null && item . id . equals ( LISTITEM_ID_DEFAULT ) ) ) { if ( item == null ) { finishCreateItem ( new ListEditItem ( name , presetURL , null , null , useTranslationsEnabled ) ) ; } else { item . name = name ; item . value = presetURL ; item . boolean0 = useTranslationsEnabled ; finishEditItem ( item ) ; } dialog . dismiss ( ) ; } else { Snack . barError ( PresetEditorActivity . this , R . string . toast_invalid_preseturl ) ; changeBackgroundColor ( editValue , ERROR_COLOR ) ; } } ) ; dialog . getButton ( DialogInterface . BUTTON_NEGATIVE ) . setOnClickListener ( v -> dialog . dismiss ( ) ) ; }
Opens the dialog to edit an item
Opens the dialog to edit an item
// name and value are not editable // Do nothing here because we override this button later to change the close behaviour. // However, we still need this because on older versions of Android unless we // pass a handler the button doesn't get instantiated // leave empty // overriding the handlers // validate entries // save or display toast, exception for localhost is needed for testing // new item // if garbage value entered show toasts