2828import com .zeapo .pwdstore .crypto .PgpHandler ;
2929import com .zeapo .pwdstore .git .GitActivity ;
3030import com .zeapo .pwdstore .git .GitAsyncTask ;
31+ import com .zeapo .pwdstore .git .GitOperation ;
3132import com .zeapo .pwdstore .pwgen .PRNGFixes ;
3233import com .zeapo .pwdstore .utils .PasswordItem ;
3334import com .zeapo .pwdstore .utils .PasswordRecyclerAdapter ;
3435import com .zeapo .pwdstore .utils .PasswordRepository ;
3536
3637import org .apache .commons .io .FileUtils ;
37- import org .eclipse .jgit .api .CommitCommand ;
3838import org .eclipse .jgit .api .Git ;
3939import org .eclipse .jgit .lib .Repository ;
4040
@@ -57,6 +57,7 @@ public class PasswordStore extends AppCompatActivity {
5757 private final static int HOME = 403 ;
5858
5959 private final static int REQUEST_EXTERNAL_STORAGE = 50 ;
60+
6061 @ Override
6162 protected void onCreate (Bundle savedInstanceState ) {
6263 settings = PreferenceManager .getDefaultSharedPreferences (this .getApplicationContext ());
@@ -74,7 +75,7 @@ protected void onCreate(Bundle savedInstanceState) {
7475 }
7576
7677 @ Override
77- public void onResume (){
78+ public void onResume () {
7879 super .onResume ();
7980 // do not attempt to checkLocalRepository() if no storage permission: immediate crash
8081 if (settings .getBoolean ("git_external" , false )) {
@@ -373,10 +374,9 @@ private void checkLocalRepository(File localDir) {
373374 }
374375
375376
376-
377377 @ Override
378378 public void onBackPressed () {
379- if ((null != plist ) && plist .isNotEmpty ()) {
379+ if ((null != plist ) && plist .isNotEmpty ()) {
380380 plist .popBack ();
381381 } else {
382382 super .onBackPressed ();
@@ -438,20 +438,12 @@ public void deletePasswords(final PasswordRecyclerAdapter adapter, final Set<Int
438438 .setPositiveButton (this .getResources ().getString (R .string .dialog_yes ), new DialogInterface .OnClickListener () {
439439 @ Override
440440 public void onClick (DialogInterface dialogInterface , int i ) {
441- String path = item .getFile ().getAbsolutePath ();
442441 item .getFile ().delete ();
443442 adapter .remove (position );
444443 it .remove ();
445444 adapter .updateSelectedItems (position , selectedItems );
446445
447- setResult (RESULT_CANCELED );
448- Repository repo = PasswordRepository .getRepository (PasswordRepository .getRepositoryDirectory (activity ));
449- Git git = new Git (repo );
450- GitAsyncTask tasks = new GitAsyncTask (activity , false , true , CommitCommand .class );
451- tasks .execute (
452- git .rm ().addFilepattern (path .replace (PasswordRepository .getWorkTree () + "/" , "" )),
453- git .commit ().setMessage ("[ANDROID PwdStore] Remove " + item + " from store." )
454- );
446+ commit ("[ANDROID PwdStore] Remove " + item + " from store." );
455447 deletePasswords (adapter , selectedItems );
456448 }
457449 })
@@ -468,10 +460,10 @@ public void onClick(DialogInterface dialogInterface, int i) {
468460 public void movePasswords (ArrayList <PasswordItem > values ) {
469461 Intent intent = new Intent (this , PgpHandler .class );
470462 ArrayList <String > fileLocations = new ArrayList <>();
471- for (PasswordItem passwordItem : values ){
463+ for (PasswordItem passwordItem : values ) {
472464 fileLocations .add (passwordItem .getFile ().getAbsolutePath ());
473465 }
474- intent .putExtra ("Files" ,fileLocations );
466+ intent .putExtra ("Files" , fileLocations );
475467 intent .putExtra ("Operation" , "SELECTFOLDER" );
476468 startActivityForResult (intent , PgpHandler .REQUEST_CODE_SELECT_FOLDER );
477469 }
@@ -480,7 +472,7 @@ public void movePasswords(ArrayList<PasswordItem> values) {
480472 * clears adapter's content and updates it with a fresh list of passwords from the root
481473 */
482474 public void updateListAdapter () {
483- if ((null != plist )) {
475+ if ((null != plist )) {
484476 plist .updateAdapter ();
485477 }
486478 }
@@ -489,31 +481,36 @@ public void updateListAdapter() {
489481 * Updates the adapter with the current view of passwords
490482 */
491483 public void refreshListAdapter () {
492- if ((null != plist )) {
484+ if ((null != plist )) {
493485 plist .refreshAdapter ();
494486 }
495487 }
496488
497489 public void filterListAdapter (String filter ) {
498- if ((null != plist )) {
490+ if ((null != plist )) {
499491 plist .filterAdapter (filter );
500492 }
501493 }
502494
503495 private File getCurrentDir () {
504- if ((null != plist )) {
496+ if ((null != plist )) {
505497 return plist .getCurrentDir ();
506498 }
507499 return PasswordRepository .getWorkTree ();
508500 }
509501
510- private void commit (String message ) {
511- Git git = new Git (PasswordRepository .getRepository (new File ("" )));
512- GitAsyncTask tasks = new GitAsyncTask (this , false , false , CommitCommand .class );
513- tasks .execute (
514- git .add ().addFilepattern ("." ),
515- git .commit ().setMessage (message )
516- );
502+ private void commit (final String message ) {
503+ new GitOperation (PasswordRepository .getRepositoryDirectory (activity ), activity ) {
504+ @ Override
505+ public void execute () {
506+ Git git = new Git (this .repository );
507+ GitAsyncTask tasks = new GitAsyncTask (activity , false , true , this );
508+ tasks .execute (
509+ git .add ().addFilepattern ("." ),
510+ git .commit ().setMessage (message )
511+ );
512+ }
513+ };
517514 }
518515
519516 protected void onActivityResult (int requestCode , int resultCode ,
@@ -573,31 +570,29 @@ protected void onActivityResult(int requestCode, int resultCode,
573570 startActivityForResult (intent , GitActivity .REQUEST_CLONE );
574571 break ;
575572 case PgpHandler .REQUEST_CODE_SELECT_FOLDER :
576- Log .d ("Moving" ,"Moving passwords to " + data .getStringExtra ("SELECTED_FOLDER_PATH" ));
573+ Log .d ("Moving" , "Moving passwords to " + data .getStringExtra ("SELECTED_FOLDER_PATH" ));
577574 Log .d ("Moving" , TextUtils .join (", " , data .getStringArrayListExtra ("Files" )));
578575 File target = new File (data .getStringExtra ("SELECTED_FOLDER_PATH" ));
579- if (!target .isDirectory ()){
580- Log .e ("Moving" ,"Tried moving passwords to a non-existing folder." );
576+ if (!target .isDirectory ()) {
577+ Log .e ("Moving" , "Tried moving passwords to a non-existing folder." );
581578 break ;
582579 }
583580
584- Repository repo = PasswordRepository .getRepository (PasswordRepository .getRepositoryDirectory (activity ));
585- Git git = new Git (repo );
586- GitAsyncTask tasks = new GitAsyncTask (activity , false , true , CommitCommand .class );
587-
588- for (String string : data .getStringArrayListExtra ("Files" )){
581+ for (String string : data .getStringArrayListExtra ("Files" )) {
589582 File source = new File (string );
590- if (!source .exists ()){
591- Log .e ("Moving" ,"Tried moving something that appears non-existent." );
583+ if (!source .exists ()) {
584+ Log .e ("Moving" , "Tried moving something that appears non-existent." );
592585 continue ;
593586 }
594- if (!source .renameTo (new File (target .getAbsolutePath ()+"/" +source .getName ()))){
595- Log .e ("Moving" ,"Something went wrong while moving." );
596- }else {
597- tasks .execute (
598- git .add ().addFilepattern (source .getAbsolutePath ().replace (PasswordRepository .getWorkTree () + "/" , "" )),
599- git .commit ().setMessage ("[ANDROID PwdStore] Moved " +string .replace (PasswordRepository .getWorkTree () + "/" , "" )+" to " +target .getAbsolutePath ().replace (PasswordRepository .getWorkTree () + "/" ,"" )+target .getAbsolutePath ()+"/" +source .getName ()+"." )
600- );
587+ if (!source .renameTo (new File (target .getAbsolutePath () + "/" + source .getName ()))) {
588+ // TODO this should show a warning to the user
589+ Log .e ("Moving" , "Something went wrong while moving." );
590+ } else {
591+ commit ("[ANDROID PwdStore] Moved "
592+ + string .replace (PasswordRepository .getWorkTree () + "/" , "" )
593+ + " to "
594+ + target .getAbsolutePath ().replace (PasswordRepository .getWorkTree () + "/" , "" )
595+ + target .getAbsolutePath () + "/" + source .getName () + "." );
601596 }
602597 }
603598 updateListAdapter ();
0 commit comments