|
| 1 | +package enotes.encryptednotepad; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.app.AlertDialog; |
| 5 | +import android.content.DialogInterface; |
| 6 | +import android.content.Intent; |
| 7 | +import android.net.Uri; |
| 8 | +import android.os.Bundle; |
| 9 | +import android.support.design.widget.FloatingActionButton; |
| 10 | +import android.support.design.widget.Snackbar; |
| 11 | +import android.support.v7.app.AppCompatActivity; |
| 12 | +import android.support.v7.widget.Toolbar; |
| 13 | +import android.text.InputType; |
| 14 | +import android.util.Log; |
| 15 | +import android.view.Menu; |
| 16 | +import android.view.MenuItem; |
| 17 | +import android.view.View; |
| 18 | +import android.widget.EditText; |
| 19 | +import enotes.doc.Doc; |
| 20 | +import enotes.doc.DocException; |
| 21 | +import enotes.doc.DocPasswordException; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStream; |
| 25 | + |
| 26 | +public class EditNoteActivity extends AppCompatActivity { |
| 27 | + |
| 28 | + private static final int ID_OPEN_NOTE = 1872; |
| 29 | + |
| 30 | + private EditText editText; |
| 31 | + |
| 32 | + @Override |
| 33 | + protected void onCreate(Bundle savedInstanceState) { |
| 34 | + super.onCreate(savedInstanceState); |
| 35 | + setContentView(R.layout.activity_edit_note); |
| 36 | + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); |
| 37 | + setSupportActionBar(toolbar); |
| 38 | + |
| 39 | + FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); |
| 40 | + fab.setOnClickListener(new View.OnClickListener() { |
| 41 | + @Override |
| 42 | + public void onClick(View view) { |
| 43 | + Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) |
| 44 | + .setAction("Action", null).show(); |
| 45 | + } |
| 46 | + }); |
| 47 | + editText = (EditText) findViewById(R.id.editText); |
| 48 | + Intent intent = getIntent(); |
| 49 | + if (intent != null && intent.getAction().equals(Intent.ACTION_VIEW)) { |
| 50 | + openNote(intent.getData()); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 56 | + getMenuInflater().inflate(R.menu.menu_edit_note, menu); |
| 57 | + return super.onCreateOptionsMenu(menu); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 62 | + int id = item.getItemId(); |
| 63 | + if (id == R.id.action_open) { |
| 64 | + chooseDocument(); |
| 65 | + return true; |
| 66 | + } |
| 67 | + return super.onOptionsItemSelected(item); |
| 68 | + } |
| 69 | + |
| 70 | + private void chooseDocument() { |
| 71 | + Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); |
| 72 | + intent.addCategory(Intent.CATEGORY_OPENABLE); |
| 73 | + intent.setType("application/*"); |
| 74 | + startActivityForResult(intent, ID_OPEN_NOTE); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 79 | + if (requestCode == ID_OPEN_NOTE && resultCode == Activity.RESULT_OK) { |
| 80 | + if (data != null) { |
| 81 | + Uri uri = data.getData(); |
| 82 | + openNote(uri); |
| 83 | + } |
| 84 | + } |
| 85 | + super.onActivityResult(requestCode, resultCode, data); |
| 86 | + } |
| 87 | + |
| 88 | + private void openNote(final Uri uri) { |
| 89 | + AlertDialog.Builder pwDialog = new AlertDialog.Builder(this); |
| 90 | + final EditText edit = new EditText(this); |
| 91 | + edit.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); |
| 92 | + pwDialog.setView(edit); |
| 93 | + pwDialog.setTitle("Enter password:"); |
| 94 | + pwDialog.setCancelable(true); |
| 95 | + pwDialog.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 96 | + @Override |
| 97 | + public void onClick(DialogInterface dialog, int i) { |
| 98 | + String pwd = edit.getText().toString(); |
| 99 | + if (pwd != null && !pwd.isEmpty()) { |
| 100 | + try (InputStream inputStream = getContentResolver().openInputStream(uri)) { |
| 101 | + Doc doc = Doc.open(inputStream, pwd); |
| 102 | + editText.setText(doc.getText()); |
| 103 | + dialog.dismiss(); |
| 104 | + } catch (DocPasswordException ignored) { |
| 105 | + // edit.setText(""); - the dialog is being dismissed anyway |
| 106 | + } catch (IOException | DocException e) { |
| 107 | + Log.e("ENOTES", e.getMessage(), e); |
| 108 | + dialog.dismiss(); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + }); |
| 113 | + pwDialog.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { |
| 114 | + @Override |
| 115 | + public void onClick(DialogInterface dialog, int i) { |
| 116 | + dialog.cancel(); |
| 117 | + } |
| 118 | + }); |
| 119 | + pwDialog.show(); |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments