Search in sources :

Example 6 with AppCompatDialog

use of android.support.v7.app.AppCompatDialog in project Rashr by DsLNeXuS.

the class BackupRestoreFragment method showPopup.

public void showPopup(final View v) {
    PopupMenu popup = new PopupMenu(mActivity, v);
    popup.getMenuInflater().inflate(R.menu.bakmgr_menu, popup.getMenu());
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(final MenuItem menuItem) {
            final CharSequence text = ((AppCompatTextView) v).getText();
            final String FileName = text.toString();
            final AppCompatDialog dialog = new AppCompatDialog(mActivity);
            dialog.setTitle(R.string.setname);
            dialog.setContentView(R.layout.dialog_input);
            final AppCompatButton bGo = (AppCompatButton) dialog.findViewById(R.id.bGoBackup);
            final AppCompatEditText etFileName = (AppCompatEditText) dialog.findViewById(R.id.etFileName);
            if (bGo == null || etFileName == null)
                return false;
            //If current item is 0 (first item) a recovery backup will be edited or created
            final File path = mPager.getCurrentItem() == 0 ? Const.PathToRecoveryBackups : Const.PathToKernelBackups;
            try {
                switch(menuItem.getItemId()) {
                    case R.id.iRestore:
                        File backup = new File(path, FileName);
                        FlashUtil RestoreUtil = new FlashUtil(mActivity, backup, mPager.getCurrentItem() == 0 ? FlashUtil.JOB_RESTORE_RECOVERY : FlashUtil.JOB_RESTORE_KERNEL);
                        RestoreUtil.execute();
                        return true;
                    case R.id.iRename:
                        etFileName.setHint(FileName);
                        bGo.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                String Name;
                                if (etFileName.getText() != null && etFileName.isEnabled() && !etFileName.getText().toString().equals("")) {
                                    //User has defined a name for the backup. Use it.
                                    Name = etFileName.getText().toString();
                                } else {
                                    //Use hint as backup name. Normally the correct version
                                    //and Recovery System (if Rashr could read it out of
                                    //"/cache/recovery/last_log"
                                    Name = String.valueOf(etFileName.getHint());
                                }
                                if (!Name.endsWith(RashrApp.DEVICE.getRecoveryExt())) {
                                    //Append extension
                                    Name += RashrApp.DEVICE.getRecoveryExt();
                                }
                                File renamedBackup = new File(path, Name);
                                if (renamedBackup.exists()) {
                                    Toast.makeText(mActivity, R.string.backupalready, Toast.LENGTH_SHORT).show();
                                    // if backup already exists, let the user chose a new name
                                    onMenuItemClick(menuItem);
                                } else {
                                    File Backup = new File(path, FileName);
                                    if (Backup.renameTo(renamedBackup)) {
                                        mAdapter.reload();
                                    } else {
                                        Toast.makeText(mActivity, R.string.rename_failed, Toast.LENGTH_SHORT).show();
                                    }
                                }
                                dialog.dismiss();
                            }
                        });
                        dialog.show();
                        return true;
                    case R.id.iDeleteBackup:
                        backup = new File(path, text.toString());
                        if (backup.delete()) {
                            Toast.makeText(mActivity, mContext.getString(R.string.bak_deleted), Toast.LENGTH_SHORT).show();
                        }
                        mAdapter.reload();
                        return true;
                    default:
                        return false;
                }
            } catch (Exception e) {
                if (e.getMessage().contains("EINVAL") && text.toString().contains(":")) {
                    AlertDialog.Builder adialog = new AlertDialog.Builder(mContext);
                    adialog.setMessage(R.string.check_name);
                    adialog.setMessage(R.string.ok);
                    adialog.show();
                }
                RashrApp.ERRORS.add(Const.RASHR_TAG + " " + e);
                return false;
            }
        }
    });
    popup.show();
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) MenuItem(android.view.MenuItem) View(android.view.View) AdapterView(android.widget.AdapterView) AppCompatTextView(android.support.v7.widget.AppCompatTextView) ListView(android.widget.ListView) AppCompatButton(android.support.v7.widget.AppCompatButton) AppCompatEditText(android.support.v7.widget.AppCompatEditText) AppCompatDialog(android.support.v7.app.AppCompatDialog) File(java.io.File) PopupMenu(android.support.v7.widget.PopupMenu)

Aggregations

AppCompatDialog (android.support.v7.app.AppCompatDialog)6 View (android.view.View)4 AppCompatButton (android.support.v7.widget.AppCompatButton)3 AdapterView (android.widget.AdapterView)3 ListView (android.widget.ListView)3 NonNull (android.support.annotation.NonNull)2 AppCompatEditText (android.support.v7.widget.AppCompatEditText)2 AppCompatTextView (android.support.v7.widget.AppCompatTextView)2 File (java.io.File)2 SuppressLint (android.annotation.SuppressLint)1 ProgressDialog (android.app.ProgressDialog)1 DialogInterface (android.content.DialogInterface)1 Uri (android.net.Uri)1 AlertDialog (android.support.v7.app.AlertDialog)1 AppCompatCheckBox (android.support.v7.widget.AppCompatCheckBox)1 PopupMenu (android.support.v7.widget.PopupMenu)1 MenuItem (android.view.MenuItem)1 AbsListView (android.widget.AbsListView)1 ArrayAdapter (android.widget.ArrayAdapter)1 TextView (android.widget.TextView)1