prompt for an external dir if none is selected

This commit is contained in:
Mohamed Zenadi 2015-07-26 14:53:31 +02:00
parent e45afa3392
commit 294f068991
3 changed files with 20 additions and 15 deletions

View file

@ -28,6 +28,7 @@ import com.zeapo.pwdstore.utils.PasswordRepository;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.CommitCommand; import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Repository;
import java.io.File; import java.io.File;
import java.util.HashSet; import java.util.HashSet;
@ -43,6 +44,7 @@ public class PasswordStore extends AppCompatActivity {
private final static int CLONE_REPO_BUTTON = 401; private final static int CLONE_REPO_BUTTON = 401;
private final static int NEW_REPO_BUTTON = 402; private final static int NEW_REPO_BUTTON = 402;
private final static int HOME = 403;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -253,15 +255,21 @@ public class PasswordStore extends AppCompatActivity {
} }
private void checkLocalRepository() { private void checkLocalRepository() {
PasswordRepository.initialize(this); Repository repo = PasswordRepository.initialize(this);
if (repo == null) {
Intent intent = new Intent(activity, UserPreference.class);
intent.putExtra("operation", "git_external");
startActivityForResult(intent, HOME);
} else {
checkLocalRepository(PasswordRepository.getWorkTree()); checkLocalRepository(PasswordRepository.getWorkTree());
} }
}
private void checkLocalRepository(File localDir) { private void checkLocalRepository(File localDir) {
Log.d("PASS", "Check, dir: " + localDir.getAbsolutePath());
FragmentManager fragmentManager = getFragmentManager(); FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (settings.getBoolean("repository_initialized", false)) { if (localDir != null && settings.getBoolean("repository_initialized", false)) {
Log.d("PASS", "Check, dir: " + localDir.getAbsolutePath());
// do not push the fragment if we already have it // do not push the fragment if we already have it
if (fragmentManager.findFragmentByTag("PasswordsList") == null || settings.getBoolean("repo_changed", false)) { if (fragmentManager.findFragmentByTag("PasswordsList") == null || settings.getBoolean("repo_changed", false)) {
settings.edit().putBoolean("repo_changed", false).apply(); settings.edit().putBoolean("repo_changed", false).apply();
@ -432,6 +440,9 @@ public class PasswordStore extends AppCompatActivity {
case GitActivity.REQUEST_PULL: case GitActivity.REQUEST_PULL:
updateListAdapter(); updateListAdapter();
break; break;
case HOME:
checkLocalRepository();
break;
case NEW_REPO_BUTTON: case NEW_REPO_BUTTON:
initializeRepositoryInfo(); initializeRepositoryInfo();
break; break;

View file

@ -318,7 +318,7 @@ public class UserPreference extends AppCompatActivity {
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()) PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
.edit() .edit()
.putString("git_external_repo", data.getStringExtra(DirectoryChooserActivity.RESULT_SELECTED_DIR)) .putString("git_external_repo", data.getStringExtra(DirectoryChooserActivity.RESULT_SELECTED_DIR))
.commit(); .apply();
} }
} }
} }

View file

@ -1,13 +1,10 @@
package com.zeapo.pwdstore.utils; package com.zeapo.pwdstore.utils;
import android.app.Activity; import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import com.zeapo.pwdstore.UserPreference;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils; import org.apache.commons.io.filefilter.FileFilterUtils;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
@ -117,7 +114,7 @@ public class PasswordRepository {
repository = null; repository = null;
} }
public static void initialize(Activity callingActivity) { public static Repository initialize(Activity callingActivity) {
File dir = null; File dir = null;
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(callingActivity.getApplicationContext()); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(callingActivity.getApplicationContext());
@ -128,12 +125,9 @@ public class PasswordRepository {
} else { } else {
dir = new File(callingActivity.getFilesDir() + "/store"); dir = new File(callingActivity.getFilesDir() + "/store");
} }
// temp for debug
if (dir == null) { if (dir == null) {
Intent intent = new Intent(callingActivity, UserPreference.class); return null;
intent.putExtra("operation", "git_external");
callingActivity.startActivity(intent);
return;
} }
// uninitialize the repo if the dir does not exist or is absolutely empty // uninitialize the repo if the dir does not exist or is absolutely empty
@ -146,7 +140,7 @@ public class PasswordRepository {
} }
// create the repository static variable in PasswordRepository // create the repository static variable in PasswordRepository
PasswordRepository.getRepository(new File(dir.getAbsolutePath() + "/.git")); return PasswordRepository.getRepository(new File(dir.getAbsolutePath() + "/.git"));
} }
public static ArrayList<File> getFilesList(){ public static ArrayList<File> getFilesList(){