A cleaner way of testing if the repository is initialized or not

This commit is contained in:
Mohamed Zenadi 2015-01-08 23:34:46 +01:00
parent dd6155e809
commit 82f7e5190a
3 changed files with 44 additions and 73 deletions

View file

@ -32,11 +32,8 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Stack; import java.util.Stack;
public class PasswordStore extends ActionBarActivity { public class PasswordStore extends ActionBarActivity {
private Stack<Integer> scrollPositions; private static final String TAG = "PwdStrAct";
/** if we leave the activity to do something, do not add any other fragment */
public boolean leftActivity = false;
private File currentDir; private File currentDir;
private SharedPreferences settings; private SharedPreferences settings;
private Activity activity; private Activity activity;
@ -46,7 +43,6 @@ public class PasswordStore extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pwdstore); setContentView(R.layout.activity_pwdstore);
scrollPositions = new Stack<Integer>();
settings = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext()); settings = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
activity = this; activity = this;
} }
@ -54,18 +50,15 @@ public class PasswordStore extends ActionBarActivity {
@Override @Override
public void onResume(){ public void onResume(){
super.onResume(); super.onResume();
// create the repository static variable in PasswordRepository // create the repository static variable in PasswordRepository
PasswordRepository.getRepository(new File(getFilesDir() + this.getResources().getString(R.string.store_git))); PasswordRepository.getRepository(new File(getFilesDir() + this.getResources().getString(R.string.store_git)));
// re-check that there was no change with the repository state
checkLocalRepository(); checkLocalRepository();
} }
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
this.leftActivity = true;
} }
@Override @Override
@ -74,6 +67,7 @@ public class PasswordStore extends ActionBarActivity {
getMenuInflater().inflate(R.menu.pwdstore, menu); getMenuInflater().inflate(R.menu.pwdstore, menu);
MenuItem searchItem = menu.findItem(R.id.action_search); MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override @Override
public boolean onQueryTextSubmit(String s) { public boolean onQueryTextSubmit(String s) {
@ -130,7 +124,6 @@ public class PasswordStore extends ActionBarActivity {
System.out.println("Exception caught :("); System.out.println("Exception caught :(");
e.printStackTrace(); e.printStackTrace();
} }
this.leftActivity = true;
return true; return true;
case R.id.menu_add_password: case R.id.menu_add_password:
@ -154,7 +147,6 @@ public class PasswordStore extends ActionBarActivity {
intent = new Intent(this, GitActivity.class); intent = new Intent(this, GitActivity.class);
intent.putExtra("Operation", GitActivity.REQUEST_PUSH); intent.putExtra("Operation", GitActivity.REQUEST_PUSH);
startActivityForResult(intent, GitActivity.REQUEST_PUSH); startActivityForResult(intent, GitActivity.REQUEST_PUSH);
this.leftActivity = true;
return true; return true;
case R.id.git_pull: case R.id.git_pull:
@ -166,7 +158,6 @@ public class PasswordStore extends ActionBarActivity {
intent = new Intent(this, GitActivity.class); intent = new Intent(this, GitActivity.class);
intent.putExtra("Operation", GitActivity.REQUEST_PULL); intent.putExtra("Operation", GitActivity.REQUEST_PULL);
startActivityForResult(intent, GitActivity.REQUEST_PULL); startActivityForResult(intent, GitActivity.REQUEST_PULL);
this.leftActivity = true;
return true; return true;
case R.id.refresh: case R.id.refresh:
@ -210,6 +201,7 @@ public class PasswordStore extends ActionBarActivity {
git.add().addFilepattern("."), git.add().addFilepattern("."),
git.commit().setMessage(R.string.initialization_commit_text + keyId) git.commit().setMessage(R.string.initialization_commit_text + keyId)
); );
settings.edit().putBoolean("repository_initialized", true).commit();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
localDir.delete(); localDir.delete();
@ -273,42 +265,9 @@ public class PasswordStore extends ActionBarActivity {
FragmentManager fragmentManager = getFragmentManager(); FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
int status = 0;
if (localDir.exists()) {
// if we are coming back from gpg do not anything
if (this.leftActivity) {
this.leftActivity = false;
return;
}
File[] folders = localDir.listFiles();
status = folders.length;
// this means that the repository has been correctly cloned
// if this file does not exist, well... this will not work if there is no other files
if ((new File(localDir.getAbsolutePath() + "/.gpg-id")).exists())
status++;
}
// either the repo is empty or it was not correctly cloned
switch (status) {
case 0:
if(!localDir.equals(PasswordRepository.getWorkTree()) && localDir.exists())
break;
PasswordRepository.setInitialized(false);
// if we still have the pass list (after deleting for instance) remove it
if (fragmentManager.findFragmentByTag("PasswordsList") != null) {
fragmentManager.popBackStack();
}
ToCloneOrNot cloneFrag = new ToCloneOrNot();
fragmentTransaction.replace(R.id.main_layout, cloneFrag, "ToCloneOrNot");
fragmentTransaction.commit();
break;
default:
if (settings.getBoolean("repository_initialized", false)) {
// do not push the fragment if we already have it
if (fragmentManager.findFragmentByTag("PasswordsList") == null) { if (fragmentManager.findFragmentByTag("PasswordsList") == null) {
// clean things up // clean things up
@ -319,7 +278,7 @@ public class PasswordStore extends ActionBarActivity {
PasswordRepository.setInitialized(true); PasswordRepository.setInitialized(true);
plist = new PasswordFragment(); plist = new PasswordFragment();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putString("Path", localDir.getAbsolutePath()); args.putString("Path", PasswordRepository.getWorkTree().getAbsolutePath());
plist.setArguments(args); plist.setArguments(args);
@ -328,8 +287,16 @@ public class PasswordStore extends ActionBarActivity {
fragmentTransaction.replace(R.id.main_layout, plist, "PasswordsList"); fragmentTransaction.replace(R.id.main_layout, plist, "PasswordsList");
fragmentTransaction.commit(); fragmentTransaction.commit();
} }
} else {
// if we still have the pass list (after deleting the repository for instance) remove it
if (fragmentManager.findFragmentByTag("PasswordsList") != null) {
fragmentManager.popBackStack();
}
ToCloneOrNot cloneFrag = new ToCloneOrNot();
fragmentTransaction.replace(R.id.main_layout, cloneFrag, "ToCloneOrNot");
fragmentTransaction.commit();
} }
this.leftActivity = false;
} }
@ -348,8 +315,6 @@ public class PasswordStore extends ActionBarActivity {
} }
public void decryptPassword(PasswordItem item) { public void decryptPassword(PasswordItem item) {
this.leftActivity = true;
Intent intent = new Intent(this, PgpHandler.class); Intent intent = new Intent(this, PgpHandler.class);
intent.putExtra("NAME", item.toString()); intent.putExtra("NAME", item.toString());
intent.putExtra("FILE_PATH", item.getFile().getAbsolutePath()); intent.putExtra("FILE_PATH", item.getFile().getAbsolutePath());
@ -360,7 +325,6 @@ public class PasswordStore extends ActionBarActivity {
public void createPassword(View v) { public void createPassword(View v) {
this.currentDir = getCurrentDir(); this.currentDir = getCurrentDir();
Log.i("PWDSTR", "Adding file to : " + this.currentDir.getAbsolutePath()); Log.i("PWDSTR", "Adding file to : " + this.currentDir.getAbsolutePath());
this.leftActivity = true;
Intent intent = new Intent(this, PgpHandler.class); Intent intent = new Intent(this, PgpHandler.class);
intent.putExtra("FILE_PATH", getCurrentDir().getAbsolutePath()); intent.putExtra("FILE_PATH", getCurrentDir().getAbsolutePath());
@ -433,10 +397,11 @@ public class PasswordStore extends ActionBarActivity {
protected void onActivityResult(int requestCode, int resultCode, protected void onActivityResult(int requestCode, int resultCode,
Intent data) { Intent data) {
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
if (requestCode == GitActivity.REQUEST_CLONE)
checkLocalRepository();
switch (requestCode) { switch (requestCode) {
case GitActivity.REQUEST_CLONE:
// if we get here with a RESULT_OK then it's probably OK :)
settings.edit().putBoolean("repository_initialized", true).commit();
break;
case PgpHandler.REQUEST_CODE_ENCRYPT : case PgpHandler.REQUEST_CODE_ENCRYPT :
Git git = new Git(PasswordRepository.getRepository(new File(""))); Git git = new Git(PasswordRepository.getRepository(new File("")));
GitAsyncTask tasks = new GitAsyncTask(this, false, false, CommitCommand.class); GitAsyncTask tasks = new GitAsyncTask(this, false, false, CommitCommand.class);

View file

@ -7,6 +7,7 @@ import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBarActivity;
import android.util.Log; import android.util.Log;
import android.view.MenuItem; import android.view.MenuItem;
@ -123,10 +124,10 @@ public class UserPreference extends ActionBarActivity implements Preference.OnPr
try { try {
FileUtils.deleteDirectory(PasswordRepository.getWorkTree()); FileUtils.deleteDirectory(PasswordRepository.getWorkTree());
} catch (Exception e) { } catch (Exception e) {
//This is what happens when jgit fails :(
//TODO Handle the diffent cases of exceptions //TODO Handle the diffent cases of exceptions
} }
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean("repository_initialized", false).commit();
dialog.cancel(); dialog.cancel();
finish(); finish();
} }

View file

@ -130,6 +130,13 @@ public class GitActivity extends ActionBarActivity {
connection_mode_spinner.setSelection(1); connection_mode_spinner.setSelection(1);
connection_mode_spinner.setEnabled(false); connection_mode_spinner.setEnabled(false);
} }
// however, if we have some saved that, that's more important!
if (connectionMode.equals("ssh-key")) {
connection_mode_spinner.setSelection(0);
} else {
connection_mode_spinner.setSelection(1);
}
} }
@Override @Override
@ -145,12 +152,6 @@ public class GitActivity extends ActionBarActivity {
protcol_spinner.setSelection(1); protcol_spinner.setSelection(1);
} }
if (connectionMode.equals("ssh-key")) {
connection_mode_spinner.setSelection(0);
} else {
connection_mode_spinner.setSelection(1);
}
// init the server information // init the server information
final EditText server_url = ((EditText) findViewById(R.id.server_url)); final EditText server_url = ((EditText) findViewById(R.id.server_url));
final EditText server_port = ((EditText) findViewById(R.id.server_port)); final EditText server_port = ((EditText) findViewById(R.id.server_port));
@ -594,6 +595,10 @@ public class GitActivity extends ActionBarActivity {
GitOperation op; GitOperation op;
switch (requestCode) { switch (requestCode) {
case REQUEST_CLONE:
setResult(RESULT_OK);
finish();
return;
case REQUEST_PULL: case REQUEST_PULL:
op = new PullOperation(localDir, activity).setCommand(); op = new PullOperation(localDir, activity).setCommand();
break; break;