Search in sources :

Example 1 with BasicActivity

use of com.amaze.filemanager.activities.superclasses.BasicActivity in project AmazeFileManager by TeamAmaze.

the class RenameBookmark method onCreateDialog.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Context c = getActivity();
    if (getActivity() instanceof BookmarkCallback)
        bookmarkCallback = (BookmarkCallback) getActivity();
    title = getArguments().getString("title");
    path = getArguments().getString("path");
    int accentColor = getArguments().getInt("accentColor");
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(c);
    studiomode = sp.getInt("studio", 0);
    if (dataUtils.containsBooks(new String[] { title, path }) != -1) {
        final MaterialDialog materialDialog;
        String pa = path;
        MaterialDialog.Builder builder = new MaterialDialog.Builder(c);
        builder.title(R.string.renamebookmark);
        builder.positiveColor(accentColor);
        builder.negativeColor(accentColor);
        builder.neutralColor(accentColor);
        builder.positiveText(R.string.save);
        builder.neutralText(R.string.cancel);
        builder.negativeText(R.string.delete);
        builder.theme(((BasicActivity) getActivity()).getAppTheme().getMaterialDialogTheme());
        builder.autoDismiss(false);
        View v2 = getActivity().getLayoutInflater().inflate(R.layout.rename, null);
        builder.customView(v2, true);
        final TextInputLayout t1 = v2.findViewById(R.id.t1);
        final TextInputLayout t2 = v2.findViewById(R.id.t2);
        final AppCompatEditText conName = v2.findViewById(R.id.editText4);
        conName.setText(title);
        final String s1 = String.format(getString(R.string.cantbeempty), c.getResources().getString(R.string.name));
        final String s2 = String.format(getString(R.string.cantbeempty), c.getResources().getString(R.string.path));
        conName.addTextChangedListener(new SimpleTextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {
                if (conName.getText().toString().length() == 0)
                    t1.setError(s2);
                else
                    t1.setError("");
            }
        });
        final AppCompatEditText ip = v2.findViewById(R.id.editText);
        if (studiomode != 0) {
            if (path.startsWith("smb:/")) {
                try {
                    jcifs.Config.registerSmbURLHandler();
                    URL a = new URL(path);
                    String userinfo = a.getUserInfo();
                    if (userinfo != null) {
                        String inf = URLDecoder.decode(userinfo, "UTF-8");
                        user = inf.substring(0, inf.indexOf(":"));
                        pass = inf.substring(inf.indexOf(":") + 1, inf.length());
                        String ipp = a.getHost();
                        pa = "smb://" + ipp + a.getPath();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            ip.addTextChangedListener(new SimpleTextWatcher() {

                @Override
                public void afterTextChanged(Editable s) {
                    if (ip.getText().toString().length() == 0)
                        t2.setError(s1);
                    else
                        t2.setError("");
                }
            });
        } else
            t2.setVisibility(View.GONE);
        ip.setText(pa);
        builder.onNeutral((dialog, which) -> dialog.dismiss());
        materialDialog = builder.build();
        materialDialog.getActionButton(DialogAction.POSITIVE).setOnClickListener(v -> {
            String t = ip.getText().toString();
            String name = conName.getText().toString();
            if (studiomode != 0 && t.startsWith("smb://")) {
                try {
                    URL a = new URL(t);
                    String userinfo = a.getUserInfo();
                    if (userinfo == null && user.length() > 0) {
                        t = "smb://" + ((URLEncoder.encode(user, "UTF-8") + ":" + URLEncoder.encode(pass, "UTF-8") + "@")) + a.getHost() + a.getPath();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            int i = -1;
            if ((i = dataUtils.containsBooks(new String[] { title, path })) != -1) {
                if (!t.equals(title) && t.length() >= 1) {
                    dataUtils.removeBook(i);
                    dataUtils.addBook(new String[] { name, t });
                    dataUtils.sortBook();
                    if (bookmarkCallback != null) {
                        bookmarkCallback.modify(path, title, t, name);
                    }
                }
            }
            materialDialog.dismiss();
        });
        materialDialog.getActionButton(DialogAction.NEGATIVE).setOnClickListener(v -> {
            int i;
            if ((i = dataUtils.containsBooks(new String[] { title, path })) != -1) {
                dataUtils.removeBook(i);
                if (bookmarkCallback != null) {
                    bookmarkCallback.delete(title, path);
                }
            }
            materialDialog.dismiss();
        });
        return materialDialog;
    }
    return null;
}
Also used : Context(android.content.Context) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) SharedPreferences(android.content.SharedPreferences) BasicActivity(com.amaze.filemanager.activities.superclasses.BasicActivity) View(android.view.View) URL(java.net.URL) AppCompatEditText(android.support.v7.widget.AppCompatEditText) SimpleTextWatcher(com.amaze.filemanager.utils.SimpleTextWatcher) Editable(android.text.Editable) TextInputLayout(android.support.design.widget.TextInputLayout)

Aggregations

Context (android.content.Context)1 SharedPreferences (android.content.SharedPreferences)1 TextInputLayout (android.support.design.widget.TextInputLayout)1 AppCompatEditText (android.support.v7.widget.AppCompatEditText)1 Editable (android.text.Editable)1 View (android.view.View)1 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1 BasicActivity (com.amaze.filemanager.activities.superclasses.BasicActivity)1 SimpleTextWatcher (com.amaze.filemanager.utils.SimpleTextWatcher)1 URL (java.net.URL)1