diff options
author | shanavas <shanavas.m2@gmail.com> | 2017-04-27 19:12:50 +0300 |
---|---|---|
committer | Mohamed Zenadi <zeapo@users.noreply.github.com> | 2017-04-27 18:12:50 +0200 |
commit | 2d1a9f7a44154035b5a47b0a9843c365dadcb62c (patch) | |
tree | a60fe287e38c7b4ed0fa8eddd43bfc603b46230f | |
parent | 94c9b5be645d4abcb8f3374c6a4963b5cfa14335 (diff) |
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
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/UserPreference.java | 13 | ||||
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/git/GitActivity.java | 48 | ||||
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java | 35 | ||||
-rw-r--r-- | app/src/main/res/layout/activity_git_config.xml | 94 | ||||
-rw-r--r-- | app/src/main/res/values/strings.xml | 14 | ||||
-rw-r--r-- | app/src/main/res/xml/preference.xml | 3 |
6 files changed, 204 insertions, 3 deletions
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) { @@ -256,6 +259,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); break; @@ -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(); + } + } + } } diff --git a/app/src/main/res/layout/activity_git_config.xml b/app/src/main/res/layout/activity_git_config.xml new file mode 100644 index 00000000..2955809b --- /dev/null +++ b/app/src/main/res/layout/activity_git_config.xml @@ -0,0 +1,94 @@ +<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:paddingLeft="@dimen/activity_horizontal_margin" + android:paddingRight="@dimen/activity_horizontal_margin" + android:paddingTop="@dimen/activity_vertical_margin" + android:paddingBottom="@dimen/activity_vertical_margin" + tools:context="com.zeapo.pwdstore.git.GitActivity" + android:background="@android:color/white"> + + <LinearLayout + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical"> + + <TextView + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:text="@string/git_config" + android:textStyle="bold" + style="@android:style/TextAppearance.Large" + android:gravity="left" + android:paddingBottom="6dp" + android:textColor="@color/blue_grey_500" + android:background="@drawable/bottom_line"/> + + <RelativeLayout + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="horizontal" + android:layout_gravity="center_vertical"> + + <TextView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="@string/git_user_name" + android:id="@+id/label_git_user_name" + android:layout_centerVertical="true" + android:layout_alignParentLeft="true" + android:paddingBottom="8dp" + android:layout_alignParentStart="true" /> + + <EditText + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:hint="@string/git_user_name_hint" + android:id="@+id/git_user_name" + android:layout_marginLeft="8dp" + android:layout_toEndOf="@+id/label_git_user_name" + android:layout_toRightOf="@+id/label_git_user_name" + android:layout_alignParentRight="true" + android:layout_alignParentEnd="true" + android:inputType="textWebEmailAddress"/> + </RelativeLayout> + + <RelativeLayout + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="horizontal" + android:layout_gravity="center_vertical"> + + <TextView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="@string/git_user_email" + android:id="@+id/label_git_user_email" + android:layout_centerVertical="true" + android:layout_alignParentLeft="true" + android:paddingBottom="8dp" + android:layout_alignParentStart="true" /> + + <EditText + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:hint="@string/git_user_email_hint" + android:id="@+id/git_user_email" + android:layout_marginLeft="35dp" + android:layout_toEndOf="@+id/label_git_user_email" + android:layout_toRightOf="@+id/label_git_user_email" + android:layout_alignParentRight="true" + android:layout_alignParentEnd="true" + android:inputType="textWebEmailAddress"/> + </RelativeLayout> + + <Button + android:id="@+id/save_button" + android:text="Save" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:onClick="applyGitConfigs"/> + </LinearLayout> + +</ScrollView> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1bc6d9cc..a02c1b76 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -13,7 +13,8 @@ <string name="dialog_delete">Delete directory</string> <string name="dialog_do_not_delete">Cancel</string> <string name="title_activity_git_clone">Repository information</string> - + <string name="title_activity_git_config">Git Configurations</string> + <!-- Password Store --> <string name="creation_dialog_text">Please clone or create a new repository below before trying to add a password or any synchronization operation.</string> <string name="store_git">/store/.git</string> @@ -55,7 +56,7 @@ <string name="passphrase_dialog_title">Authenticate</string> <string name="passphrase_dialog_text">Please provide the passphrase for your SSH key. Leave it empty if there is no passphrase.</string> <string name="password_dialog_text">Please provide the password for this repository</string> - + <!-- Clone fragment --> <string name="clone_fragment_text">Welcome to Password Store\n\n In this screen you can either create a new repository or clone your git repository onto your device.</string> <string name="clone">Clone from server</string> @@ -78,6 +79,14 @@ <string name="warn_malformed_url_port">When using custom ports, provide an absolute path (starts with "/")</string> + <!-- Git Config fragment --> + <string name="git_config">Git config</string> + <string name="git_user_name">Username</string> + <string name="git_user_name_hint">User name</string> + <string name="git_user_email">Email</string> + <string name="git_user_email_hint">email</string> + <string name="invalid_email_dialog_text">Please enter a valid email address</string> + <!-- PGP Handler --> <string name="title_activity_pgp_handler">PgpHandler</string> <string name="crypto_name_hint">name</string> @@ -97,6 +106,7 @@ <string name="pref_git_username_title">Username</string> <string name="pref_git_username_hint">username</string> <string name="pref_edit_server_info">Edit git server settings</string> + <string name="pref_edit_git_config">Edit git config</string> <string name="pref_ssh_title">Import SSH key</string> <string name="pref_ssh_keygen_title">Generate SSH key pair</string> <string name="pref_ssh_see_key_title">View generated public SSH key</string> diff --git a/app/src/main/res/xml/preference.xml b/app/src/main/res/xml/preference.xml index 0471e71d..67c33f8c 100644 --- a/app/src/main/res/xml/preference.xml +++ b/app/src/main/res/xml/preference.xml @@ -5,6 +5,9 @@ android:key="git_server_info" android:title="@string/pref_edit_server_info" /> <Preference + android:key="git_config" + android:title="@string/pref_edit_git_config" /> + <Preference android:key="ssh_key" android:title="@string/pref_ssh_title" /> <Preference |