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
This commit is contained in:
knuthy 2014-08-15 17:00:18 +02:00
parent 390a7ec825
commit 4e8397a74c
7 changed files with 99 additions and 58 deletions

View file

@ -16,11 +16,13 @@ import org.eclipse.jgit.api.errors.TransportException;
public class GitAsyncTask extends AsyncTask<GitCommand, Integer, Integer> { public class GitAsyncTask extends AsyncTask<GitCommand, Integer, Integer> {
private Activity activity; private Activity activity;
private boolean finishOnEnd; private boolean finishOnEnd;
private boolean refreshListOnEnd;
private ProgressDialog dialog; private ProgressDialog dialog;
public GitAsyncTask(Activity activity, boolean finishOnEnd) { public GitAsyncTask(Activity activity, boolean finishOnEnd, boolean refreshListOnEnd) {
this.activity = activity; this.activity = activity;
this.finishOnEnd = finishOnEnd; this.finishOnEnd = finishOnEnd;
this.refreshListOnEnd = refreshListOnEnd;
dialog = new ProgressDialog(this.activity); dialog = new ProgressDialog(this.activity);
} }
@ -62,5 +64,13 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, Integer> {
this.activity.setResult(Activity.RESULT_OK); this.activity.setResult(Activity.RESULT_OK);
this.activity.finish(); this.activity.finish();
} }
if (refreshListOnEnd) {
try {
((PasswordStore) this.activity).refreshListAdapter();
} catch (ClassCastException e){
// oups, mistake
}
}
} }
} }

View file

@ -441,7 +441,7 @@ public class GitHandler extends Activity {
+ ":" + + ":" +
settings.getString("git_remote_location", "path/to/repository")); 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() .pull()
.setRebase(true) .setRebase(true)
.setRemote("origin") .setRemote("origin")
@ -481,7 +481,7 @@ public class GitHandler extends Activity {
+ ":" + + ":" +
settings.getString("git_remote_location", "path/to/repository")); 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() .push()
.setPushAll() .setPushAll()
.setRemote("origin") .setRemote("origin")

View file

@ -10,6 +10,7 @@ import android.view.ViewGroup;
import android.widget.AbsListView; import android.widget.AbsListView;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView; import android.widget.ExpandableListView;
import android.widget.ListAdapter; import android.widget.ListAdapter;
import android.widget.ListView; import android.widget.ListView;
@ -54,7 +55,7 @@ public class PasswordFragment extends Fragment implements ExpandableListView.OnG
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
String path = getArguments().getString("Path"); 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 @Override
@ -124,6 +125,7 @@ public class PasswordFragment extends Fragment implements ExpandableListView.OnG
public void updateAdapter() { public void updateAdapter() {
mAdapter.clear(); mAdapter.clear();
mAdapter.addAll(PasswordRepository.getPasswords(new File(getArguments().getString("Path")))); mAdapter.addAll(PasswordRepository.getPasswords(new File(getArguments().getString("Path"))));
mListView.setAdapter((ExpandableListAdapter) mAdapter);
} }
} }

View file

@ -16,6 +16,7 @@ import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSch;
@ -42,7 +43,7 @@ import java.util.Stack;
public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentInteractionListener, PasswordFragment.OnFragmentInteractionListener { public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentInteractionListener, PasswordFragment.OnFragmentInteractionListener {
private Stack<Integer> scrollPositions; private Stack<Integer> scrollPositions;
/** if we leave the activity to do something, do not add any other fragment */ /** 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 File currentDir;
private SharedPreferences settings; private SharedPreferences settings;
private Activity activity; private Activity activity;
@ -250,19 +251,6 @@ public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentI
checkLocalRepository(item.getFile()); checkLocalRepository(item.getFile());
} else { } else {
try { 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) { } 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) { public void createPassword(View v) {
this.currentDir = getCurrentDir(); 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; PasswordFragment plist;
if (null != if (null !=
(plist = (PasswordFragment) getFragmentManager().findFragmentByTag("PasswordsList"))) { (plist = (PasswordFragment) getFragmentManager().findFragmentByTag("PasswordsList"))) {
@ -311,7 +343,7 @@ public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentI
switch (requestCode) { switch (requestCode) {
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); GitAsyncTask tasks = new GitAsyncTask(this, false, false);
tasks.execute( tasks.execute(
git.add().addFilepattern("."), git.add().addFilepattern("."),
git.commit().setMessage("[ANDROID PwdStore] Add " + data.getExtras().getString("NAME") + " from store.") git.commit().setMessage("[ANDROID PwdStore] Add " + data.getExtras().getString("NAME") + " from store.")

View file

@ -155,7 +155,7 @@ public class PgpHandler extends Activity {
finish(); finish();
break; break;
case R.id.crypto_delete_button: case R.id.crypto_delete_button:
deletePassword(); // deletePassword();
break; break;
case R.id.crypto_get_key_ids: case R.id.crypto_get_key_ids:
getKeyIds(new Intent()); 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();
}
} }

View file

@ -1,6 +1,8 @@
package com.zeapo.pwdstore.utils; package com.zeapo.pwdstore.utils;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.DataSetObserver; import android.database.DataSetObserver;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.util.Log; import android.util.Log;
@ -8,15 +10,21 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ExpandableListAdapter; import android.widget.ExpandableListAdapter;
import android.widget.ImageButton;
import android.widget.TextView; import android.widget.TextView;
import com.zeapo.pwdstore.PasswordStore;
import com.zeapo.pwdstore.R; import com.zeapo.pwdstore.R;
import com.zeapo.pwdstore.crypto.PgpHandler;
import org.apache.commons.io.FileUtils;
import java.util.ArrayList; import java.util.ArrayList;
public class PasswordAdapter extends ArrayAdapter<PasswordItem> implements ExpandableListAdapter{ public class PasswordAdapter extends ArrayAdapter<PasswordItem> implements ExpandableListAdapter{
private final Context context; private final PasswordStore activity;
private final ArrayList<PasswordItem> values; private final ArrayList<PasswordItem> values;
static class ViewHolder { static class ViewHolder {
@ -24,10 +32,10 @@ public class PasswordAdapter extends ArrayAdapter<PasswordItem> implements Exp
public TextView type; public TextView type;
} }
public PasswordAdapter(Context context, ArrayList<PasswordItem> values) { public PasswordAdapter(PasswordStore activity, ArrayList<PasswordItem> values) {
super(context, R.layout.password_row_layout, values); super(activity, R.layout.password_row_layout, values);
this.context = context;
this.values = values; this.values = values;
this.activity = activity;
} }
@Override @Override
@ -85,7 +93,7 @@ public class PasswordAdapter extends ArrayAdapter<PasswordItem> implements Exp
// reuse for performance, holder pattern! // reuse for performance, holder pattern!
if (rowView == null) { if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.password_row_layout, null); rowView = inflater.inflate(R.layout.password_row_layout, null);
@ -100,12 +108,12 @@ public class PasswordAdapter extends ArrayAdapter<PasswordItem> implements Exp
holder.name.setText(pass.toString()); holder.name.setText(pass.toString());
if (pass.getType() == PasswordItem.TYPE_CATEGORY) { 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.name.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
holder.type.setText("Category: "); holder.type.setText("Category: ");
} else { } else {
holder.type.setText("Password: "); 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)); holder.name.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL));
} }
@ -114,12 +122,28 @@ public class PasswordAdapter extends ArrayAdapter<PasswordItem> implements Exp
@Override @Override
public View getChildView(int i, int i2, boolean b, View view, ViewGroup viewGroup) { 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); .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.child_row_layout, null); 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; return view;
} }

View file

@ -11,7 +11,6 @@
android:src="@drawable/ico_del" android:src="@drawable/ico_del"
android:background="@drawable/red_rectangle" android:background="@drawable/red_rectangle"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:onClick="handleClick"
android:layout_column="1" android:layout_column="1"
android:layout_row="0"/> android:layout_row="0"/>
@ -23,7 +22,6 @@
android:background="@drawable/blue_rectangle" android:background="@drawable/blue_rectangle"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp" android:layout_marginLeft="8dp"
android:onClick="handleClick"
android:layout_column="2" android:layout_column="2"
android:layout_row="0"/> android:layout_row="0"/>