now possible to clone/create repository in a given external directory.

*issues*:
  - not possible to switch between external/local directory without closing the app. (PasswordRepository is a singleton, has to be cleaned when a change is made)
This commit is contained in:
Mohamed Zenadi 2015-05-03 12:14:50 +02:00
parent 994cb0272b
commit 62b4d78e93
4 changed files with 34 additions and 28 deletions

View file

@ -1,7 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zeapo.pwdstore">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application android:allowBackup="true" android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:theme="@style/AppTheme">
<activity android:name=".PasswordStore" android:label="@string/app_name">
@ -34,8 +36,4 @@
<activity android:name="net.rdrei.android.dirchooser.DirectoryChooserActivity" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

View file

@ -47,9 +47,31 @@ public class PasswordStore extends ActionBarActivity {
@Override
public void onResume(){
super.onResume();
// create the repository static variable in PasswordRepository
PasswordRepository.getRepository(new File(getFilesDir() + this.getResources().getString(R.string.store_git)));
File dir = null;
if (settings.getBoolean("git_external", false)) {
if (settings.getString("git_external_repo", null) == null)
{
// todo: show the main screen
} else {
dir = new File(settings.getString("git_external_repo", null));
}
} else {
dir = new File(getFilesDir() + "/store");
}
assert dir != null;
// uninitialize the repo if the dir does not exist or is absolutely empty
if (!dir.exists() || !dir.isDirectory()) {
settings.edit().putBoolean("repository_initialized", false).apply();
}
if (!PasswordRepository.getPasswords(dir).isEmpty()) {
settings.edit().putBoolean("repository_initialized", true).apply();
}
// create the repository static variable in PasswordRepository
PasswordRepository.getRepository(new File(dir.getAbsolutePath() + "/.git"));
checkLocalRepository();
}
@ -192,7 +214,8 @@ public class PasswordStore extends ActionBarActivity {
private void createRepository() {
// final String keyId = settings.getString("openpgp_key_ids", "");
File localDir = new File(getFilesDir() + "/store/");
File localDir = PasswordRepository.getWorkTree();
localDir.mkdir();
try {
PasswordRepository.createRepository(localDir);
@ -239,8 +262,7 @@ public class PasswordStore extends ActionBarActivity {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// TODO: Remove the getpaswords() as it is a temporary fix until every user has the repository_initialized set
if (settings.getBoolean("repository_initialized", false) || PasswordRepository.getPasswords(localDir).size() > 0) {
if (settings.getBoolean("repository_initialized", false)) {
// do not push the fragment if we already have it
if (fragmentManager.findFragmentByTag("PasswordsList") == null) {
@ -249,7 +271,6 @@ public class PasswordStore extends ActionBarActivity {
fragmentManager.popBackStack();
}
PasswordRepository.setInitialized(true);
plist = new PasswordFragment();
Bundle args = new Bundle();
args.putString("Path", PasswordRepository.getWorkTree().getAbsolutePath());

View file

@ -432,8 +432,7 @@ public class GitActivity extends ActionBarActivity {
* @param view
*/
public void cloneRepository(View view) {
localDir = new File(getApplicationContext().getFilesDir().getAbsoluteFile() + "/store");
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

View file

@ -5,8 +5,8 @@ import android.util.Log;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteConfig;
@ -49,11 +49,7 @@ public class PasswordRepository {
}
public static boolean isInitialized() {
return initialized;
}
public static void setInitialized(boolean v) {
initialized = v;
return repository != null;
}
public static void createRepository(File localDir) throws Exception{
@ -112,6 +108,7 @@ public class PasswordRepository {
public static void closeRepository() {
repository.close();
repository = null;
}
public static ArrayList<File> getFilesList(){
@ -130,15 +127,6 @@ public class PasswordRepository {
return repository.getWorkTree();
}
/**
* Gets a file from the working tree
* @param name the relative path of the file
* @return the file in the repository
*/
public static File getFile(String name) {
return new File(repository.getWorkTree() + "/" + name);
}
/**
* Gets the .gpg files in a directory
* @param path the directory path