From 6e789a38f10da7c75fde3bed336dbf1d904d8efc Mon Sep 17 00:00:00 2001 From: zeapo Date: Sun, 22 Nov 2015 14:19:06 +0100 Subject: Add warnings to the user about storing the passwords on the sdcard --- .../java/com/zeapo/pwdstore/UserPreference.java | 82 +++++++++++++++------- 1 file changed, 57 insertions(+), 25 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/UserPreference.java') diff --git a/app/src/main/java/com/zeapo/pwdstore/UserPreference.java b/app/src/main/java/com/zeapo/pwdstore/UserPreference.java index d1b3b360..6ffc7abc 100644 --- a/app/src/main/java/com/zeapo/pwdstore/UserPreference.java +++ b/app/src/main/java/com/zeapo/pwdstore/UserPreference.java @@ -1,6 +1,7 @@ package com.zeapo.pwdstore; import android.accessibilityservice.AccessibilityServiceInfo; +import android.app.Activity; import android.app.DialogFragment; import android.content.Context; import android.content.DialogInterface; @@ -23,14 +24,12 @@ import android.widget.Toast; import com.google.common.base.Function; import com.google.common.base.Joiner; import com.google.common.collect.Iterables; +import com.nononsenseapps.filepicker.FilePickerActivity; import com.zeapo.pwdstore.autofill.AutofillPreferenceActivity; import com.zeapo.pwdstore.crypto.PgpHandler; import com.zeapo.pwdstore.git.GitActivity; import com.zeapo.pwdstore.utils.PasswordRepository; -import net.rdrei.android.dirchooser.DirectoryChooserActivity; -import net.rdrei.android.dirchooser.DirectoryChooserConfig; - import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.openintents.openpgp.util.OpenPgpKeyPreference; @@ -126,7 +125,7 @@ public class UserPreference extends AppCompatActivity { new AlertDialog.Builder(callingActivity). setTitle(R.string.pref_dialog_delete_title). setMessage(getResources().getString(R.string.dialog_delete_msg) - + " " + PasswordRepository.getWorkTree().toString()). + + " \n" + PasswordRepository.getWorkTree().toString()). setCancelable(false). setPositiveButton(R.string.dialog_delete, new DialogInterface.OnClickListener() { @Override @@ -266,19 +265,32 @@ public class UserPreference extends AppCompatActivity { } public void selectExternalGitRepository() { - Intent intent = new Intent(this, DirectoryChooserActivity.class); - File dir = new File(Environment.getExternalStorageDirectory() + "/PasswordStore"); - if (!dir.exists()) { - dir.mkdir(); - } - DirectoryChooserConfig config = DirectoryChooserConfig.builder() - .newDirectoryName("PasswordStore") - .allowNewDirectoryNameModification(true) - .initialDirectory(Environment.getExternalStorageDirectory() + "/PasswordStore") - .build(); - intent.putExtra(DirectoryChooserActivity.EXTRA_CONFIG, config); - - startActivityForResult(intent, SELECT_GIT_DIRECTORY); + final Activity activity = this; + new AlertDialog.Builder(this). + setTitle("Choose where to store the passwords"). + setMessage("You must select a directory where to store your passwords. If you want " + + "to store your passwords within the hidden storage of the application, " + + "cancel this dialog and disable the \"External Repository\" option."). + setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + // This always works + Intent i = new Intent(activity.getApplicationContext(), FilePickerActivity.class); + // This works if you defined the intent filter + // Intent i = new Intent(Intent.ACTION_GET_CONTENT); + + // Set these depending on your use case. These are the defaults. + i.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false); + i.putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, true); + i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_DIR); + + i.putExtra(FilePickerActivity.EXTRA_START_PATH, Environment.getExternalStorageDirectory().getPath()); + + startActivityForResult(i, SELECT_GIT_DIRECTORY); + } + }). + setNegativeButton(R.string.dialog_cancel, null).show(); + } @Override @@ -381,17 +393,37 @@ public class UserPreference extends AppCompatActivity { } } break; + case SELECT_GIT_DIRECTORY: { + final Uri uri = data.getData(); + + if (uri.getPath().equals(Environment.getExternalStorageDirectory().getPath())) { + // the user wants to use the root of the sdcard as a store... + new AlertDialog.Builder(this). + setTitle("SD-Card root selected"). + setMessage("You have selected the root of your sdcard for the store. " + + "This is extremly dangerous and you will lose your data " + + "as its content will be deleted"). + setPositiveButton("Remove everything", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + PreferenceManager.getDefaultSharedPreferences(getApplicationContext()) + .edit() + .putString("git_external_repo", uri.getPath()) + .apply(); + } + }). + setNegativeButton(R.string.dialog_cancel, null).show(); + } else { + PreferenceManager.getDefaultSharedPreferences(getApplicationContext()) + .edit() + .putString("git_external_repo", uri.getPath()) + .apply(); + } + } + break; default: break; } } - - // why do they have to use a different resultCode than OK :/ - if (requestCode == SELECT_GIT_DIRECTORY && resultCode == DirectoryChooserActivity.RESULT_CODE_DIR_SELECTED) { - PreferenceManager.getDefaultSharedPreferences(getApplicationContext()) - .edit() - .putString("git_external_repo", data.getStringExtra(DirectoryChooserActivity.RESULT_SELECTED_DIR)) - .apply(); - } } } -- cgit v1.2.3