From 4e8397a74c35f20b35a84c0bbf17f82be9d57dd7 Mon Sep 17 00:00:00 2001 From: knuthy Date: Fri, 15 Aug 2014 17:00:18 +0200 Subject: [PATCH] more improvements to the expandable list - Deletion is working flawlessly - refresh works better now, especially after add - Added the possibility to refresh list after gitasync --- .../java/com/zeapo/pwdstore/GitAsyncTask.java | 12 +++- .../java/com/zeapo/pwdstore/GitHandler.java | 4 +- .../com/zeapo/pwdstore/PasswordFragment.java | 4 +- .../com/zeapo/pwdstore/PasswordStore.java | 64 ++++++++++++++----- .../com/zeapo/pwdstore/crypto/PgpHandler.java | 27 +------- .../zeapo/pwdstore/utils/PasswordAdapter.java | 44 ++++++++++--- app/src/main/res/layout/child_row_layout.xml | 2 - 7 files changed, 99 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/com/zeapo/pwdstore/GitAsyncTask.java b/app/src/main/java/com/zeapo/pwdstore/GitAsyncTask.java index 8db11c43..11071018 100644 --- a/app/src/main/java/com/zeapo/pwdstore/GitAsyncTask.java +++ b/app/src/main/java/com/zeapo/pwdstore/GitAsyncTask.java @@ -16,11 +16,13 @@ import org.eclipse.jgit.api.errors.TransportException; public class GitAsyncTask extends AsyncTask { private Activity activity; private boolean finishOnEnd; + private boolean refreshListOnEnd; private ProgressDialog dialog; - public GitAsyncTask(Activity activity, boolean finishOnEnd) { + public GitAsyncTask(Activity activity, boolean finishOnEnd, boolean refreshListOnEnd) { this.activity = activity; this.finishOnEnd = finishOnEnd; + this.refreshListOnEnd = refreshListOnEnd; dialog = new ProgressDialog(this.activity); } @@ -62,5 +64,13 @@ public class GitAsyncTask extends AsyncTask { this.activity.setResult(Activity.RESULT_OK); this.activity.finish(); } + + if (refreshListOnEnd) { + try { + ((PasswordStore) this.activity).refreshListAdapter(); + } catch (ClassCastException e){ + // oups, mistake + } + } } } diff --git a/app/src/main/java/com/zeapo/pwdstore/GitHandler.java b/app/src/main/java/com/zeapo/pwdstore/GitHandler.java index aed6b776..7dc25567 100644 --- a/app/src/main/java/com/zeapo/pwdstore/GitHandler.java +++ b/app/src/main/java/com/zeapo/pwdstore/GitHandler.java @@ -441,7 +441,7 @@ public class GitHandler extends Activity { + ":" + settings.getString("git_remote_location", "path/to/repository")); - new GitAsyncTask(activity, true).execute(new Git(PasswordRepository.getRepository(new File(""))) + new GitAsyncTask(activity, true, false).execute(new Git(PasswordRepository.getRepository(new File(""))) .pull() .setRebase(true) .setRemote("origin") @@ -481,7 +481,7 @@ public class GitHandler extends Activity { + ":" + settings.getString("git_remote_location", "path/to/repository")); - new GitAsyncTask(activity, true).execute(new Git(PasswordRepository.getRepository(new File(""))) + new GitAsyncTask(activity, true, false).execute(new Git(PasswordRepository.getRepository(new File(""))) .push() .setPushAll() .setRemote("origin") diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java index a8dc67bb..db214cc5 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java @@ -10,6 +10,7 @@ import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.ArrayAdapter; +import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ListAdapter; import android.widget.ListView; @@ -54,7 +55,7 @@ public class PasswordFragment extends Fragment implements ExpandableListView.OnG super.onCreate(savedInstanceState); String path = getArguments().getString("Path"); - mAdapter = new PasswordAdapter(getActivity(), PasswordRepository.getPasswords(new File(path))); + mAdapter = new PasswordAdapter((PasswordStore) getActivity(), PasswordRepository.getPasswords(new File(path))); } @Override @@ -124,6 +125,7 @@ public class PasswordFragment extends Fragment implements ExpandableListView.OnG public void updateAdapter() { mAdapter.clear(); mAdapter.addAll(PasswordRepository.getPasswords(new File(getArguments().getString("Path")))); + mListView.setAdapter((ExpandableListAdapter) mAdapter); } } diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java index c5059289..62475867 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java @@ -16,6 +16,7 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; +import android.widget.ExpandableListView; import android.widget.LinearLayout; import com.jcraft.jsch.JSch; @@ -42,7 +43,7 @@ import java.util.Stack; public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentInteractionListener, PasswordFragment.OnFragmentInteractionListener { private Stack scrollPositions; /** if we leave the activity to do something, do not add any other fragment */ - private boolean leftActivity = false; + public boolean leftActivity = false; private File currentDir; private SharedPreferences settings; private Activity activity; @@ -250,19 +251,6 @@ public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentI checkLocalRepository(item.getFile()); } else { try { - try { - this.leftActivity = true; - - Intent intent = new Intent(this, PgpHandler.class); - intent.putExtra("PGP-ID", FileUtils.readFileToString(PasswordRepository.getFile("/.gpg-id"))); - intent.putExtra("NAME", item.toString()); - intent.putExtra("FILE_PATH", item.getFile().getAbsolutePath()); - intent.putExtra("Operation", "DECRYPT"); - startActivityForResult(intent, PgpHandler.REQUEST_CODE_DECRYPT_AND_VERIFY); - - } catch (IOException e) { - e.printStackTrace(); - } } catch (Exception e) { @@ -271,6 +259,21 @@ public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentI } } } + public void decryptPassword(PasswordItem item) { + try { + this.leftActivity = true; + + Intent intent = new Intent(this, PgpHandler.class); + intent.putExtra("PGP-ID", FileUtils.readFileToString(PasswordRepository.getFile("/.gpg-id"))); + intent.putExtra("NAME", item.toString()); + intent.putExtra("FILE_PATH", item.getFile().getAbsolutePath()); + intent.putExtra("Operation", "DECRYPT"); + startActivityForResult(intent, PgpHandler.REQUEST_CODE_DECRYPT_AND_VERIFY); + + } catch (IOException e) { + e.printStackTrace(); + } + } public void createPassword(View v) { this.currentDir = getCurrentDir(); @@ -288,7 +291,36 @@ public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentI } } - private void refreshListAdapter() { + public void deletePassword(final PasswordItem item) { + new AlertDialog.Builder(this). + setMessage("Are you sure you want to delete the password \"" + + item + "\"") + .setPositiveButton("YES", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + String path = item.getFile().getAbsolutePath(); + item.getFile().delete(); + + setResult(RESULT_CANCELED); + Git git = new Git(PasswordRepository.getRepository(new File(""))); + GitAsyncTask tasks = new GitAsyncTask(activity, false, true); + System.out.println(tasks); + tasks.execute( + git.rm().addFilepattern(path.replace(PasswordRepository.getWorkTree() + "/", "")), + git.commit().setMessage("[ANDROID PwdStore] Remove " + item + " from store.") + ); + } + }) + .setNegativeButton("NO", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) { + + } + }) + .show(); + } + + public void refreshListAdapter() { PasswordFragment plist; if (null != (plist = (PasswordFragment) getFragmentManager().findFragmentByTag("PasswordsList"))) { @@ -311,7 +343,7 @@ public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentI switch (requestCode) { case PgpHandler.REQUEST_CODE_ENCRYPT : Git git = new Git(PasswordRepository.getRepository(new File(""))); - GitAsyncTask tasks = new GitAsyncTask(this, false); + GitAsyncTask tasks = new GitAsyncTask(this, false, false); tasks.execute( git.add().addFilepattern("."), git.commit().setMessage("[ANDROID PwdStore] Add " + data.getExtras().getString("NAME") + " from store.") diff --git a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpHandler.java b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpHandler.java index 774264ad..68c71c4b 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/PgpHandler.java +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/PgpHandler.java @@ -155,7 +155,7 @@ public class PgpHandler extends Activity { finish(); break; case R.id.crypto_delete_button: - deletePassword(); +// deletePassword(); break; case R.id.crypto_get_key_ids: getKeyIds(new Intent()); @@ -462,30 +462,5 @@ public class PgpHandler extends Activity { } - private void deletePassword() { - new AlertDialog.Builder(this). - setMessage("Are you sure you want to delete the password \"" + - getIntent().getExtras().getString("NAME") + "\"") - .setPositiveButton("YES", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - (new File(getIntent().getExtras().getString("FILE_PATH"))).delete(); - setResult(RESULT_CANCELED); - Git git = new Git(PasswordRepository.getRepository(new File(""))); - GitAsyncTask tasks = new GitAsyncTask(activity, true); - tasks.execute( - git.rm().addFilepattern(getIntent().getExtras().getString("FILE_PATH").replace(PasswordRepository.getWorkTree() + "/", "")), - git.commit().setMessage("[ANDROID PwdStore] Remove " + getIntent().getExtras().getString("FILE_PATH") + " from store.") - ); - } - }) - .setNegativeButton("NO", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - - } - }) - .show(); - } } diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordAdapter.java b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordAdapter.java index c18b1ab8..fbc7d817 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordAdapter.java +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordAdapter.java @@ -1,6 +1,8 @@ package com.zeapo.pwdstore.utils; import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; import android.database.DataSetObserver; import android.graphics.Typeface; import android.util.Log; @@ -8,15 +10,21 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; +import android.widget.Button; import android.widget.ExpandableListAdapter; +import android.widget.ImageButton; import android.widget.TextView; +import com.zeapo.pwdstore.PasswordStore; import com.zeapo.pwdstore.R; +import com.zeapo.pwdstore.crypto.PgpHandler; + +import org.apache.commons.io.FileUtils; import java.util.ArrayList; public class PasswordAdapter extends ArrayAdapter implements ExpandableListAdapter{ - private final Context context; + private final PasswordStore activity; private final ArrayList values; static class ViewHolder { @@ -24,10 +32,10 @@ public class PasswordAdapter extends ArrayAdapter implements Exp public TextView type; } - public PasswordAdapter(Context context, ArrayList values) { - super(context, R.layout.password_row_layout, values); - this.context = context; + public PasswordAdapter(PasswordStore activity, ArrayList values) { + super(activity, R.layout.password_row_layout, values); this.values = values; + this.activity = activity; } @Override @@ -85,7 +93,7 @@ public class PasswordAdapter extends ArrayAdapter implements Exp // reuse for performance, holder pattern! if (rowView == null) { - LayoutInflater inflater = (LayoutInflater) context + LayoutInflater inflater = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); rowView = inflater.inflate(R.layout.password_row_layout, null); @@ -100,12 +108,12 @@ public class PasswordAdapter extends ArrayAdapter implements Exp holder.name.setText(pass.toString()); if (pass.getType() == PasswordItem.TYPE_CATEGORY) { - holder.name.setTextColor(this.context.getResources().getColor(android.R.color.holo_blue_dark)); + holder.name.setTextColor(this.activity.getResources().getColor(android.R.color.holo_blue_dark)); holder.name.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD)); holder.type.setText("Category: "); } else { holder.type.setText("Password: "); - holder.name.setTextColor(this.context.getResources().getColor(android.R.color.holo_orange_dark)); + holder.name.setTextColor(this.activity.getResources().getColor(android.R.color.holo_orange_dark)); holder.name.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL)); } @@ -114,12 +122,28 @@ public class PasswordAdapter extends ArrayAdapter implements Exp @Override public View getChildView(int i, int i2, boolean b, View view, ViewGroup viewGroup) { - PasswordItem pass = values.get(i); + final PasswordItem pass = values.get(i); - LayoutInflater inflater = (LayoutInflater) context + LayoutInflater inflater = (LayoutInflater) this.activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.child_row_layout, null); - Log.i("ADAPTER", "Child clicked"); + + View.OnClickListener onClickListener = new View.OnClickListener() { + @Override + public void onClick(View view) { + switch (view.getId()) { + case R.id.crypto_show_button: + activity.decryptPassword(pass); + break; + case R.id.crypto_delete_button: + activity.deletePassword(pass); + break; + } + } + }; + + ((ImageButton) view.findViewById(R.id.crypto_show_button)).setOnClickListener(onClickListener); + ((ImageButton) view.findViewById(R.id.crypto_delete_button)).setOnClickListener(onClickListener); return view; } diff --git a/app/src/main/res/layout/child_row_layout.xml b/app/src/main/res/layout/child_row_layout.xml index a60ead53..0fc5e33d 100644 --- a/app/src/main/res/layout/child_row_layout.xml +++ b/app/src/main/res/layout/child_row_layout.xml @@ -11,7 +11,6 @@ android:src="@drawable/ico_del" android:background="@drawable/red_rectangle" android:layout_gravity="center_vertical" - android:onClick="handleClick" android:layout_column="1" android:layout_row="0"/> @@ -23,7 +22,6 @@ android:background="@drawable/blue_rectangle" android:layout_gravity="center_vertical" android:layout_marginLeft="8dp" - android:onClick="handleClick" android:layout_column="2" android:layout_row="0"/>