From 7ee5ff76ba3468467a2b0396704e5c21962ba046 Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Sat, 21 Nov 2015 04:18:07 -0500 Subject: default to reasonable external directory --- app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java') diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java b/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java index 00d5be35..d5cba03e 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java @@ -481,7 +481,7 @@ public class GitActivity extends AppCompatActivity { if (localDir.exists() && localDir.listFiles().length != 0) { new AlertDialog.Builder(this). setTitle(R.string.dialog_delete_title). - setMessage(R.string.dialog_delete_msg). + setMessage(getResources().getString(R.string.dialog_delete_msg) + " " + localDir.toString()). setCancelable(false). setPositiveButton(R.string.dialog_delete, new DialogInterface.OnClickListener() { -- cgit v1.2.3 From 60f65818f97e99384e8b59733943508ddc926ae3 Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Tue, 22 Dec 2015 21:13:59 -0500 Subject: Update hostname variable with needed protocol prefix when hitting SAVE not just CLONE (addresses #94, maybe some of the oddities observed in #137) --- .../java/com/zeapo/pwdstore/git/GitActivity.java | 82 +++++++++++++--------- 1 file changed, 49 insertions(+), 33 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java') diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java b/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java index d5cba03e..132083d1 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java @@ -45,7 +45,6 @@ public class GitActivity extends AppCompatActivity { private File localDir; private String hostname; - private String username; private String port; private SharedPreferences settings; @@ -401,6 +400,45 @@ public class GitActivity extends AppCompatActivity { return super.onOptionsItemSelected(item); } + /** + * Prepends the proper protocol to the hostname expected by jGit addRemote(). + */ + private void prependProtocol() { + hostname = ((EditText) findViewById(R.id.clone_uri)).getText().toString(); + port = ((EditText) findViewById(R.id.server_port)).getText().toString(); + // don't ask the user, take off the protocol that he puts in + hostname = hostname.replaceFirst("^.+://", ""); + ((TextView) findViewById(R.id.clone_uri)).setText(hostname); + + // now cheat a little and prepend the real protocol + // jGit does not accept a ssh:// but requires https:// + if (!protocol.equals("ssh://")) { + hostname = protocol + hostname; + } else { + // if the port is explicitly given, jgit requires the ssh:// + if (!port.isEmpty()) + hostname = protocol + hostname; + } + } + + private boolean forgotUsername() { + if (protocol.equals("ssh://")) { + // did he forget the username? + if (!hostname.matches("^.+@.+")) { + new AlertDialog.Builder(this). + setMessage(activity.getResources().getString(R.string.forget_username_dialog_text)). + setPositiveButton(activity.getResources().getString(R.string.dialog_oops), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + + } + }). + show(); + return true; + } + } + return false; + } /** * Saves the configuration found in the form */ @@ -416,8 +454,14 @@ public class GitActivity extends AppCompatActivity { editor.putString("git_remote_port", ((EditText) findViewById(R.id.server_port)).getText().toString()); editor.putString("git_remote_uri", ((EditText) findViewById(R.id.clone_uri)).getText().toString()); + // update hostname variable for use by addRemote() either here or later + // in syncRepository() + prependProtocol(); if (PasswordRepository.isInitialized()) { - PasswordRepository.addRemote("origin", ((EditText) findViewById(R.id.clone_uri)).getText().toString(), true); + // don't just use the clone_uri text, need to use hostname which has + // had the proper protocol prepended + Log.d("hostname", hostname); + PasswordRepository.addRemote("origin", hostname, true); } editor.apply(); @@ -429,6 +473,8 @@ public class GitActivity extends AppCompatActivity { * @param view */ public void saveConfiguration(View view) { + if (forgotUsername()) return; + saveConfiguration(); finish(); } @@ -443,38 +489,8 @@ public class GitActivity extends AppCompatActivity { PasswordRepository.initialize(this); } localDir = PasswordRepository.getWorkTree(); - hostname = ((EditText) findViewById(R.id.clone_uri)).getText().toString(); - port = ((EditText) findViewById(R.id.server_port)).getText().toString(); - // don't ask the user, take off the protocol that he puts in - hostname = hostname.replaceFirst("^.+://", ""); - ((TextView) findViewById(R.id.clone_uri)).setText(hostname); - - // now cheat a little and prepend the real protocol - // jGit does not accept a ssh:// but requires https:// - if (!protocol.equals("ssh://")) { - hostname = protocol + hostname; - } else { - - // if the port is explicitly given, jgit requires the ssh:// - if (!port.isEmpty()) - hostname = protocol + hostname; - - // did he forget the username? - if (!hostname.matches("^.+@.+")) { - new AlertDialog.Builder(this). - setMessage(activity.getResources().getString(R.string.forget_username_dialog_text)). - setPositiveButton(activity.getResources().getString(R.string.dialog_oops), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - - } - }). - show(); - return; - } - username = hostname.split("@")[0]; - } + if (forgotUsername()) return; saveConfiguration(); -- cgit v1.2.3 From 24b89992903ccc52dbe3191ff501028f4839762c Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Wed, 23 Dec 2015 00:20:09 -0500 Subject: Don't crash if no storage permission and it's needed --- .../java/com/zeapo/pwdstore/PasswordStore.java | 70 +++++++++++++++++++++- .../java/com/zeapo/pwdstore/UserPreference.java | 1 + .../java/com/zeapo/pwdstore/crypto/PgpHandler.java | 1 + .../java/com/zeapo/pwdstore/git/GitActivity.java | 1 - 4 files changed, 69 insertions(+), 4 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java') diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java index cca8493b..96e58d89 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java @@ -1,13 +1,19 @@ package com.zeapo.pwdstore; +import android.Manifest; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageManager; +import android.graphics.Color; import android.os.Bundle; import android.preference.PreferenceManager; +import android.support.design.widget.Snackbar; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; import android.support.v4.view.MenuItemCompat; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; @@ -16,6 +22,7 @@ import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.widget.TextView; import com.zeapo.pwdstore.crypto.PgpHandler; import com.zeapo.pwdstore.git.GitActivity; @@ -47,19 +54,76 @@ public class PasswordStore extends AppCompatActivity { private final static int NEW_REPO_BUTTON = 402; private final static int HOME = 403; + private final static int REQUEST_EXTERNAL_STORAGE = 50; @Override protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_pwdstore); settings = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()); activity = this; PRNGFixes.apply(); + + // If user opens app with permission granted then revokes and returns, + // prevent attempt to create password list fragment + if (savedInstanceState != null && (!settings.getBoolean("git_external", false) + || ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) { + savedInstanceState = null; + } + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_pwdstore); } @Override public void onResume(){ super.onResume(); - checkLocalRepository(); + // do not attempt to checkLocalRepository() if no storage permission: immediate crash + if (settings.getBoolean("git_external", false)) { + if (ContextCompat.checkSelfPermission(activity, + Manifest.permission.READ_EXTERNAL_STORAGE) + != PackageManager.PERMISSION_GRANTED) { + + if (ActivityCompat.shouldShowRequestPermissionRationale(activity, + Manifest.permission.READ_EXTERNAL_STORAGE)) { + Snackbar snack = Snackbar.make(findViewById(R.id.main_layout), "The store is on the sdcard but the app does not have permission to access it. Please give permission.", + Snackbar.LENGTH_INDEFINITE) + .setAction(R.string.dialog_ok, new View.OnClickListener() { + @Override + public void onClick(View view) { + ActivityCompat.requestPermissions(activity, + new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, + REQUEST_EXTERNAL_STORAGE); + } + }); + snack.show(); + View view = snack.getView(); + TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text); + tv.setTextColor(Color.WHITE); + tv.setMaxLines(10); + } else { + // No explanation needed, we can request the permission. + ActivityCompat.requestPermissions(activity, + new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, + REQUEST_EXTERNAL_STORAGE); + } + } else { + checkLocalRepository(); + } + + } else { + checkLocalRepository(); + } + } + + @Override + public void onRequestPermissionsResult(int requestCode, + String permissions[], int[] grantResults) { + switch (requestCode) { + case REQUEST_EXTERNAL_STORAGE: { + // If request is cancelled, the result arrays are empty. + if (grantResults.length > 0 + && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + checkLocalRepository(); + } + } + } } @Override diff --git a/app/src/main/java/com/zeapo/pwdstore/UserPreference.java b/app/src/main/java/com/zeapo/pwdstore/UserPreference.java index dd743b64..6311a7ac 100644 --- a/app/src/main/java/com/zeapo/pwdstore/UserPreference.java +++ b/app/src/main/java/com/zeapo/pwdstore/UserPreference.java @@ -231,6 +231,7 @@ public class UserPreference extends AppCompatActivity { public void onStart() { super.onStart(); final SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences(); + findPreference("pref_select_external").setSummary(getPreferenceManager().getSharedPreferences().getString("git_external_repo", "No external repository selected")); findPreference("ssh_see_key").setEnabled(sharedPreferences.getBoolean("use_generated_key", false)); // see if the autofill service is enabled and check the preference accordingly diff --git a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpHandler.java b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpHandler.java index a620bd33..fd92a1f7 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpHandler.java +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpHandler.java @@ -560,6 +560,7 @@ public class PgpHandler extends AppCompatActivity implements OpenPgpServiceConne } + // TODO (low priority but still...) android M potential permissions crashes @Override public void onBound(IOpenPgpService2 service) { Log.i("PGP", "ISBOUND!!"); diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java b/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java index 132083d1..94589ed1 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java @@ -460,7 +460,6 @@ public class GitActivity extends AppCompatActivity { if (PasswordRepository.isInitialized()) { // don't just use the clone_uri text, need to use hostname which has // had the proper protocol prepended - Log.d("hostname", hostname); PasswordRepository.addRemote("origin", hostname, true); } -- cgit v1.2.3 From 406d6c04255bbd76915f6beb71a237f101737045 Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Wed, 23 Dec 2015 18:19:25 -0500 Subject: check for blank username & update 'hostname' all in saveConfiguration() --- .../java/com/zeapo/pwdstore/git/GitActivity.java | 64 ++++++++-------------- 1 file changed, 23 insertions(+), 41 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java') diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java b/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java index 94589ed1..a1ca9519 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java @@ -401,62 +401,45 @@ public class GitActivity extends AppCompatActivity { } /** - * Prepends the proper protocol to the hostname expected by jGit addRemote(). + * Saves the configuration found in the form */ - private void prependProtocol() { + private boolean saveConfiguration() { + // remember the settings + SharedPreferences.Editor editor = settings.edit(); + + editor.putString("git_remote_server", ((EditText) findViewById(R.id.server_url)).getText().toString()); + editor.putString("git_remote_location", ((EditText) findViewById(R.id.server_path)).getText().toString()); + editor.putString("git_remote_username", ((EditText) findViewById(R.id.server_user)).getText().toString()); + editor.putString("git_remote_protocol", protocol); + editor.putString("git_remote_auth", connectionMode); + editor.putString("git_remote_port", ((EditText) findViewById(R.id.server_port)).getText().toString()); + editor.putString("git_remote_uri", ((EditText) findViewById(R.id.clone_uri)).getText().toString()); + + // 'save' hostname variable for use by addRemote() either here or later + // in syncRepository() hostname = ((EditText) findViewById(R.id.clone_uri)).getText().toString(); port = ((EditText) findViewById(R.id.server_port)).getText().toString(); // don't ask the user, take off the protocol that he puts in hostname = hostname.replaceFirst("^.+://", ""); ((TextView) findViewById(R.id.clone_uri)).setText(hostname); - // now cheat a little and prepend the real protocol - // jGit does not accept a ssh:// but requires https:// if (!protocol.equals("ssh://")) { hostname = protocol + hostname; } else { + // if the port is explicitly given, jgit requires the ssh:// if (!port.isEmpty()) hostname = protocol + hostname; - } - } - private boolean forgotUsername() { - if (protocol.equals("ssh://")) { // did he forget the username? if (!hostname.matches("^.+@.+")) { new AlertDialog.Builder(this). setMessage(activity.getResources().getString(R.string.forget_username_dialog_text)). - setPositiveButton(activity.getResources().getString(R.string.dialog_oops), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - - } - }). + setPositiveButton(activity.getResources().getString(R.string.dialog_oops), null). show(); - return true; + return false; } } - return false; - } - /** - * Saves the configuration found in the form - */ - private void saveConfiguration() { - // remember the settings - SharedPreferences.Editor editor = settings.edit(); - - editor.putString("git_remote_server", ((EditText) findViewById(R.id.server_url)).getText().toString()); - editor.putString("git_remote_location", ((EditText) findViewById(R.id.server_path)).getText().toString()); - editor.putString("git_remote_username", ((EditText) findViewById(R.id.server_user)).getText().toString()); - editor.putString("git_remote_protocol", protocol); - editor.putString("git_remote_auth", connectionMode); - editor.putString("git_remote_port", ((EditText) findViewById(R.id.server_port)).getText().toString()); - editor.putString("git_remote_uri", ((EditText) findViewById(R.id.clone_uri)).getText().toString()); - - // update hostname variable for use by addRemote() either here or later - // in syncRepository() - prependProtocol(); if (PasswordRepository.isInitialized()) { // don't just use the clone_uri text, need to use hostname which has // had the proper protocol prepended @@ -464,6 +447,7 @@ public class GitActivity extends AppCompatActivity { } editor.apply(); + return true; } /** @@ -472,9 +456,8 @@ public class GitActivity extends AppCompatActivity { * @param view */ public void saveConfiguration(View view) { - if (forgotUsername()) return; - - saveConfiguration(); + if (!saveConfiguration()) + return; finish(); } @@ -489,9 +472,8 @@ public class GitActivity extends AppCompatActivity { } localDir = PasswordRepository.getWorkTree(); - if (forgotUsername()) return; - - saveConfiguration(); + if (!saveConfiguration()) + return; if (localDir.exists() && localDir.listFiles().length != 0) { new AlertDialog.Builder(this). -- cgit v1.2.3