From bee81ba5f2e13ea4deb910d7f550681c88a1bcc1 Mon Sep 17 00:00:00 2001 From: knuthy Date: Sun, 10 Aug 2014 19:03:28 +0100 Subject: creates a new repository --- .../main/java/com/zeapo/pwdstore/GitHandler.java | 1 + .../java/com/zeapo/pwdstore/PasswordStore.java | 50 ++++++++++++++++++++++ .../zeapo/pwdstore/utils/PasswordRepository.java | 27 ++++++++++++ 3 files changed, 78 insertions(+) (limited to 'app/src/main') diff --git a/app/src/main/java/com/zeapo/pwdstore/GitHandler.java b/app/src/main/java/com/zeapo/pwdstore/GitHandler.java index 0eed611c..fe6cdb27 100644 --- a/app/src/main/java/com/zeapo/pwdstore/GitHandler.java +++ b/app/src/main/java/com/zeapo/pwdstore/GitHandler.java @@ -65,6 +65,7 @@ public class GitHandler extends Activity { public static final int REQUEST_PULL = 101; public static final int REQUEST_PUSH = 102; public static final int REQUEST_CLONE = 103; + public static final int REQUEST_INIT = 104; @Override protected void onCreate(Bundle savedInstanceState) { diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java index e291b8df..72df9ba8 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java @@ -6,8 +6,10 @@ import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.DialogInterface; import android.content.Intent; +import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; +import android.preference.PreferenceManager; import android.text.InputType; import android.util.Log; import android.view.Menu; @@ -41,12 +43,16 @@ public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentI /** if we leave the activity to do something, do not add any other fragment */ private boolean leftActivity = false; private File currentDir; + private SharedPreferences settings; + private Activity activity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pwdstore); scrollPositions = new Stack(); + settings = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()); + activity = this; } @Override @@ -130,6 +136,43 @@ public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentI startActivity(intent); } + public void initRepository(View view) { + String keyId = settings.getString("openpgp_key_ids", ""); + + if (keyId.isEmpty()) + new AlertDialog.Builder(this) + .setMessage("You have to set the information about the server before synchronizing with the server") + .setPositiveButton("On my way!", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + Intent intent = new Intent(activity, UserPreference.class); + startActivityForResult(intent, GitHandler.REQUEST_INIT); + } + }) + .setNegativeButton("Nah... later", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + // do nothing :( + } + }) + .show(); + + else { + File localDir = new File(getFilesDir() + "/store/"); + localDir.mkdir(); + try { + // we take only the first key-id, we have to think about how to handle multiple keys, and why should we do that... + // also, for compatibility use short-version of the key-id + FileUtils.writeStringToFile(new File(localDir.getAbsolutePath() + "/.gpg-id"), + keyId.substring(keyId.length() - 8)); + } catch (Exception e) { + localDir.delete(); + return; + } + PasswordRepository.createRepository(localDir); + } + } + @Override public void onFragmentInteraction(Uri uri) { @@ -154,6 +197,10 @@ public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentI if (localDir.exists()) { File[] folders = localDir.listFiles(); status = folders.length; + + // this means that the repository has been correctly cloned + if ((new File(localDir.getAbsolutePath() + "/.gpg-id")).exists()) + status++; } // either the repo is empty or it was not correctly cloned @@ -268,6 +315,9 @@ public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentI git.commit().setMessage("[ANDROID PwdStore] Add " + data.getExtras().getString("NAME") + " from store.") ); break; + case GitHandler.REQUEST_INIT: + initRepository(getCurrentFocus()); + break; } } 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 551b8015..1ff90436 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.java @@ -1,6 +1,7 @@ package com.zeapo.pwdstore.utils; import org.apache.commons.io.FileUtils; +import org.eclipse.jgit.api.Git; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.storage.file.FileRepositoryBuilder; @@ -34,6 +35,32 @@ public class PasswordRepository { return repository; } + public static void createRepository(File localDir) { + localDir.delete(); + + try { + Git.init() + .setDirectory(localDir) + .call(); + + getRepository(localDir); + + new Git(repository) + .branchCreate() + .setName("master") + .call(); + + } catch (Exception e) { + e.printStackTrace(); + return; + } + } + + // TODO add remote branch + public static void addRemote() { + + } + public static void closeRepository() { repository.close(); } -- cgit v1.2.3