From 2d1a9f7a44154035b5a47b0a9843c365dadcb62c Mon Sep 17 00:00:00 2001 From: shanavas Date: Thu, 27 Apr 2017 19:12:50 +0300 Subject: Make git username and email configurable (#289) * Gather git config data and save in preferences * Align text box properly * Apply git configs from settings * Validate email address --- .../java/com/zeapo/pwdstore/UserPreference.java | 13 +++++- .../java/com/zeapo/pwdstore/git/GitActivity.java | 48 ++++++++++++++++++++++ .../zeapo/pwdstore/utils/PasswordRepository.java | 35 ++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/com/zeapo/pwdstore/UserPreference.java b/app/src/main/java/com/zeapo/pwdstore/UserPreference.java index 126aa18d..f17f52a4 100644 --- a/app/src/main/java/com/zeapo/pwdstore/UserPreference.java +++ b/app/src/main/java/com/zeapo/pwdstore/UserPreference.java @@ -59,6 +59,7 @@ public class UserPreference extends AppCompatActivity { private final static int EDIT_GIT_INFO = 3; private final static int SELECT_GIT_DIRECTORY = 4; private final static int EXPORT_PASSWORDS = 5; + private final static int EDIT_GIT_CONFIG = 6; private final static int REQUEST_EXTERNAL_STORAGE = 50; private PrefsFragment prefsFragment; @@ -117,7 +118,17 @@ public class UserPreference extends AppCompatActivity { } }); - findPreference("git_delete_repo").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + findPreference("git_config").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + Intent intent = new Intent(callingActivity, GitActivity.class); + intent.putExtra("Operation", GitActivity.EDIT_GIT_CONFIG); + startActivityForResult(intent, EDIT_GIT_CONFIG); + return true; + } + }); + + findPreference("git_delete_repo").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { new AlertDialog.Builder(callingActivity). 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 faea01aa..48dd9602 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java @@ -36,6 +36,8 @@ import java.util.regex.Pattern; public class GitActivity extends AppCompatActivity { private static final String TAG = "GitAct"; + // copied from http://stackoverflow.com/a/16058059/4247851 + private static final String emailPattern = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$"; private Activity activity; private Context context; @@ -56,6 +58,7 @@ public class GitActivity extends AppCompatActivity { public static final int EDIT_SERVER = 105; public static final int REQUEST_SYNC = 106; public static final int REQUEST_CREATE = 107; + public static final int EDIT_GIT_CONFIG = 108; @Override protected void onCreate(Bundle savedInstanceState) { @@ -255,6 +258,18 @@ public class GitActivity extends AppCompatActivity { updateURI(); + break; + case EDIT_GIT_CONFIG: + setContentView(R.layout.activity_git_config); + setTitle(R.string.title_activity_git_config); + + // init the server information + final EditText git_user_name = ((EditText) findViewById(R.id.git_user_name)); + final EditText git_user_email = ((EditText) findViewById(R.id.git_user_email)); + + git_user_name.setText(settings.getString("git_config_user_name", "")); + git_user_email.setText(settings.getString("git_config_user_email", "")); + break; case REQUEST_PULL: syncRepository(REQUEST_PULL); @@ -460,6 +475,39 @@ public class GitActivity extends AppCompatActivity { finish(); } + private boolean saveGitConfigs() { + // remember the settings + SharedPreferences.Editor editor = settings.edit(); + + String email = ((EditText) findViewById(R.id.git_user_email)).getText().toString(); + editor.putString("git_config_user_email", email); + editor.putString("git_config_user_name", ((EditText) findViewById(R.id.git_user_name)).getText().toString()); + + if (!email.matches(emailPattern)) { + new AlertDialog.Builder(this). + setMessage(activity.getResources().getString(R.string.invalid_email_dialog_text)). + setPositiveButton(activity.getResources().getString(R.string.dialog_oops), null). + show(); + return false; + } + + editor.apply(); + return true; + } + + public void applyGitConfigs(View view) { + if(!saveGitConfigs()) + return; + + String git_user_name = settings.getString("git_config_user_name", ""); + String git_user_email = settings.getString("git_config_user_email", ""); + + PasswordRepository.setUserName(git_user_name); + PasswordRepository.setUserEmail(git_user_email); + + finish(); + } + /** * Clones the repository, the directory exists, deletes it * diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java index 90cd5ed0..b1c99232 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java @@ -200,4 +200,39 @@ public class PasswordRepository { sort(passwordList); return passwordList; } + + /** + * Sets the git user name + * @param String username username + */ + public static void setUserName(String username) { + setStringConfig("user", null, "name", username); + } + + /** + * Sets the git user email + * @param String email email + */ + public static void setUserEmail(String email) { + setStringConfig("user", null, "email", email); + } + + /** + * Sets a git config value + * @param String section config section name + * @param String subsection config subsection name + * @param String name config name + * @param String value the value to be set + */ + private static void setStringConfig(String section, String subsection, String name, String value) { + if (isInitialized()) { + StoredConfig config = repository.getConfig(); + config.setString(section, subsection, name, value); + try { + config.save(); + } catch(Exception e) { + e.printStackTrace(); + } + } + } } -- cgit v1.2.3