Example usage for android.app AlertDialog getButton

List of usage examples for android.app AlertDialog getButton

Introduction

In this page you can find the example usage for android.app AlertDialog getButton.

Prototype

public Button getButton(int whichButton) 

Source Link

Document

Gets one of the buttons used in the dialog.

Usage

From source file:com.pdftron.pdf.tools.Tool.java

protected void setAuthor(Markup annot) {
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mPDFView.getContext());
    boolean authorNameHasBeenAsked = pref.getBoolean("pref_author_name_has_been_asked", false);
    String authorName = pref.getString("pref_author_name", "");
    if (!authorNameHasBeenAsked && authorName.isEmpty()) {
        // Show dialog to get the author name.
        boolean askAuthor = false;
        if (mPDFView.getToolManager() instanceof ToolManager) {
            if (((ToolManager) mPDFView.getToolManager()).isShowAuthorDialog()) {
                askAuthor = true;/*from  ww w . j  a  v a2s  .  c o m*/
            }
        }

        mMarkupToAuthor = annot;

        String possibleName = "";
        // If the author name in the preferences is empty, we try to get
        // the name of the current user in the device.
        int res = mPDFView.getContext().checkCallingOrSelfPermission("android.permission.GET_ACCOUNTS");
        if (res == PackageManager.PERMISSION_GRANTED) {

            Pattern emailPattern = Patterns.EMAIL_ADDRESS;
            //Account[] accounts = AccountManager.get(mPDFView.getContext()).getAccounts();
            //                for (Account account : accounts) {
            //                    if (emailPattern.matcher(account.name).matches()) {
            //                        possibleName = account.name;
            //                        break;
            //                    }
            //                }
        }

        final SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean("pref_author_name_has_been_asked", true);
        editor.commit();

        if (askAuthor) {
            LayoutInflater inflater = (LayoutInflater) mPDFView.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View authorNameDialog = inflater.inflate(R.layout.tools_dialog_author_name, null);
            final EditText authorNameEditText = (EditText) authorNameDialog
                    .findViewById(R.id.tools_dialog_author_name_edittext);
            authorNameEditText.setText(possibleName);
            authorNameEditText.selectAll();

            AlertDialog.Builder builder = new AlertDialog.Builder(mPDFView.getContext());
            final AlertDialog authorDialog = builder.setView(authorNameDialog)
                    .setTitle(R.string.tools_dialog_author_name_title)
                    .setPositiveButton(R.string.tools_misc_ok, new OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String author = authorNameEditText.getText().toString().trim();
                            // Set author information on the markup
                            setAuthor(mMarkupToAuthor, author);
                            // Update preferences with the new name
                            //SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(mPDFView.getContext()).edit();
                            editor.putString("pref_author_name", author);
                            editor.commit();
                        }
                    }).setNegativeButton(R.string.tools_misc_skip, new OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    }).create();
            authorDialog.show();
            if (authorNameEditText.getText().length() == 0) {
                // empty, don't allow OK
                authorDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
            } else {
                authorDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
            }
            authorNameEditText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (authorDialog != null) {
                        if (s.length() == 0) {
                            // empty, don't allow OK
                            authorDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
                        } else {
                            authorDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
                        }
                    }
                }
            });
        } else {
            // Set author information on the markup
            setAuthor(mMarkupToAuthor, possibleName);
            // Update preferences with the new name
            //SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(mPDFView.getContext()).edit();
            editor.putString("pref_author_name", possibleName);
            editor.commit();
        }
    } else {
        // Use author name in the preferences
        String author = pref.getString("pref_author_name", "");
        setAuthor(annot, author);
    }
}