From a0b8301a4976aaa2ed170542bbfcedca84cd57fc Mon Sep 17 00:00:00 2001 From: Zeapo Date: Sun, 27 Jul 2014 18:58:42 +0100 Subject: [PATCH] Some refactoring and organisation for listview.. sooooon --- app/src/main/AndroidManifest.xml | 2 +- .../java/com/zeapo/pwdstore/GitClone.java | 20 ++++-- .../main/java/com/zeapo/pwdstore/GitRepo.java | 41 ++++++++++++ .../com/zeapo/pwdstore/PasswordAdapter.java | 48 ++++++++++++++ .../com/zeapo/pwdstore/PasswordFragment.java | 63 ++----------------- .../{pwdstore.java => PasswordStore.java} | 18 +++++- .../res/layout/fragment_password_list.xml | 7 ++- .../res/layout/fragment_to_clone_or_not.xml | 11 ++-- app/src/main/res/values/strings.xml | 7 ++- 9 files changed, 143 insertions(+), 74 deletions(-) create mode 100644 app/src/main/java/com/zeapo/pwdstore/GitRepo.java create mode 100644 app/src/main/java/com/zeapo/pwdstore/PasswordAdapter.java rename app/src/main/java/com/zeapo/pwdstore/{pwdstore.java => PasswordStore.java} (85%) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b26aba3f..16210fdc 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,7 +12,7 @@ android:label="@string/app_name" android:theme="@style/AppTheme" > diff --git a/app/src/main/java/com/zeapo/pwdstore/GitClone.java b/app/src/main/java/com/zeapo/pwdstore/GitClone.java index cab56e96..6166ba2c 100644 --- a/app/src/main/java/com/zeapo/pwdstore/GitClone.java +++ b/app/src/main/java/com/zeapo/pwdstore/GitClone.java @@ -31,9 +31,17 @@ import org.eclipse.jgit.api.errors.InvalidRemoteException; import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.api.errors.TransportException; import org.eclipse.jgit.errors.NotSupportedException; +import org.eclipse.jgit.internal.storage.file.FileRepository; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.storage.file.FileRepositoryBuilder; import org.eclipse.jgit.transport.JschConfigSessionFactory; import org.eclipse.jgit.transport.OpenSshConfig; +import org.eclipse.jgit.transport.RefSpec; +import org.eclipse.jgit.transport.RemoteConfig; import org.eclipse.jgit.transport.SshSessionFactory; +import org.eclipse.jgit.transport.Transport; +import org.eclipse.jgit.transport.URIish; import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; import org.eclipse.jgit.util.FS; @@ -246,7 +254,7 @@ public class GitClone extends Activity { public void cloneRepository(View view) { - localDir = new File(getApplicationContext().getCacheDir().getAbsoluteFile() + "/store"); + localDir = new File(getApplicationContext().getFilesDir().getAbsoluteFile() + "/store"); hostname = ((TextView) findViewById(R.id.clone_uri)).getText().toString(); // don't ask the user, take off the protocol that he puts in @@ -355,10 +363,12 @@ public class GitClone extends Activity { } }).show(); } else { - CloneCommand cmd = Git.cloneRepository(). - setCloneAllBranches(true). - setDirectory(localDir). - setURI(hostname); + CloneCommand cmd = Git.cloneRepository() + .setDirectory(localDir) + .setURI(hostname) + .setBare(false) + .setNoCheckout(false) + .setCloneAllBranches(true); new CloneTask(activity).execute(cmd); } diff --git a/app/src/main/java/com/zeapo/pwdstore/GitRepo.java b/app/src/main/java/com/zeapo/pwdstore/GitRepo.java new file mode 100644 index 00000000..34757b88 --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/GitRepo.java @@ -0,0 +1,41 @@ +package com.zeapo.pwdstore; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.internal.storage.file.FileRepository; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.lib.RepositoryBuilder; +import org.eclipse.jgit.storage.file.FileRepositoryBuilder; + +import java.io.File; +import java.io.IOException; + +/** + * Created by zeapo on 7/27/14. + */ +public class GitRepo { + + private static Repository repository; + + protected GitRepo(){ } + + public static Repository getRepository(File localDir) { + if (repository == null) { + FileRepositoryBuilder builder = new FileRepositoryBuilder(); + try { + repository = builder.setGitDir(localDir) + .readEnvironment() + .findGitDir() + .build(); + + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + return repository; + } + + public static void closeRepository() { + repository.close(); + } +} diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordAdapter.java b/app/src/main/java/com/zeapo/pwdstore/PasswordAdapter.java new file mode 100644 index 00000000..023c8c8a --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordAdapter.java @@ -0,0 +1,48 @@ +package com.zeapo.pwdstore; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +import com.zeapo.pwdstore.R; + +import java.util.ArrayList; + +public class PasswordAdapter extends ArrayAdapter { + private final Context context; + private final ArrayList values; + + static class ViewHolder { + public TextView text; + } + + public PasswordAdapter(Context context, ArrayList values) { + super(context, R.layout.password_row_layout, values); + this.context = context; + this.values = values; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + View rowView = convertView; + + // reuse for performance, holder pattern! + if (rowView == null) { + LayoutInflater inflater = (LayoutInflater) context + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + rowView = inflater.inflate(R.layout.password_row_layout, null); + + ViewHolder viewHolder = new ViewHolder(); + viewHolder.text = (TextView) rowView.findViewById(R.id.label); + rowView.setTag(viewHolder); + } + + ViewHolder holder = (ViewHolder) rowView.getTag(); + holder.text.setText( values.get(position) ); + + return rowView; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java index 462324db..f6a2d822 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java @@ -11,13 +11,9 @@ import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListAdapter; -import android.widget.RelativeLayout; import android.widget.TextView; - -import com.zeapo.pwdstore.dummy.DummyContent; - -import java.util.List; +import java.util.ArrayList; /** * A fragment representing a list of Items. @@ -25,8 +21,6 @@ import java.util.List; * Large screen devices (such as tablets) are supported by replacing the ListView * with a GridView. *

- * Activities containing this fragment MUST implement the {@link Callbacks} - * interface. */ public class PasswordFragment extends Fragment implements AbsListView.OnItemClickListener { @@ -43,31 +37,17 @@ public class PasswordFragment extends Fragment implements AbsListView.OnItemClic */ private ListAdapter mAdapter; - // TODO: Rename and change types of parameters - public static PasswordFragment newInstance(String param1, String param2) { - PasswordFragment fragment = new PasswordFragment(); - Bundle args = new Bundle(); - fragment.setArguments(args); - return fragment; - } - /** * Mandatory empty constructor for the fragment manager to instantiate the * fragment (e.g. upon screen orientation changes). */ - public PasswordFragment() { - } + public PasswordFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if (getArguments() != null) { - } - String[] values = new String[] { "NOT", "YET", "IMPLEMENTED" }; - - // TODO: Change Adapter to display your content - mAdapter = new MySimpleArrayAdapter(getActivity(), values); + mAdapter = new PasswordAdapter(getActivity(), new ArrayList()); } @Override @@ -76,7 +56,7 @@ public class PasswordFragment extends Fragment implements AbsListView.OnItemClic View view = inflater.inflate(R.layout.fragment_password, container, false); // Set the adapter - mListView = (AbsListView) view.findViewById(android.R.id.list); + mListView = (AbsListView) view.findViewById(R.id.pass_list); ((AdapterView) mListView).setAdapter(mAdapter); // Set OnItemClickListener so we can be notified on item clicks @@ -112,44 +92,9 @@ public class PasswordFragment extends Fragment implements AbsListView.OnItemClic } } - /** - * The default content for this Fragment has a TextView that is shown when - * the list is empty. If you would like to change the text, call this method - * to supply the text it should use. - */ - public void setEmptyText(CharSequence emptyText) { - View emptyView = mListView.getEmptyView(); - - if (emptyText instanceof TextView) { - ((TextView) emptyView).setText(emptyText); - } - } - public interface OnFragmentInteractionListener { // TODO: Update argument type and name public void onFragmentInteraction(String id); } - - public class MySimpleArrayAdapter extends ArrayAdapter { - private final Context context; - private final String[] values; - - public MySimpleArrayAdapter(Context context, String[] values) { - super(context, R.layout.password_row_layout, values); - this.context = context; - this.values = values; - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - LayoutInflater inflater = (LayoutInflater) context - .getSystemService(Context.LAYOUT_INFLATER_SERVICE); - View rowView = inflater.inflate(R.layout.password_row_layout, parent, false); - TextView textView = (TextView) rowView.findViewById(R.id.label); - textView.setText(values[position]); - - return rowView; - } - } } diff --git a/app/src/main/java/com/zeapo/pwdstore/pwdstore.java b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java similarity index 85% rename from app/src/main/java/com/zeapo/pwdstore/pwdstore.java rename to app/src/main/java/com/zeapo/pwdstore/PasswordStore.java index 52071957..8b047626 100644 --- a/app/src/main/java/com/zeapo/pwdstore/pwdstore.java +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java @@ -11,6 +11,8 @@ import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.ListView; import android.widget.TextView; import org.apache.commons.io.FileUtils; @@ -19,6 +21,8 @@ import org.apache.commons.io.filefilter.FileFilterUtils; import org.apache.commons.io.filefilter.NotFileFilter; import org.apache.commons.io.filefilter.TrueFileFilter; import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.ListBranchCommand; +import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryBuilder; import org.eclipse.jgit.storage.file.FileRepositoryBuilder; @@ -28,9 +32,10 @@ import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Collection; +import java.util.List; -public class pwdstore extends Activity implements ToCloneOrNot.OnFragmentInteractionListener, PasswordFragment.OnFragmentInteractionListener { +public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentInteractionListener, PasswordFragment.OnFragmentInteractionListener { @Override protected void onCreate(Bundle savedInstanceState) { @@ -83,12 +88,11 @@ public class pwdstore extends Activity implements ToCloneOrNot.OnFragmentIntera private void checkLocalRepository() { int status = 0; - final File localDir = new File(getCacheDir() + "/store"); + final File localDir = new File(getFilesDir() + "/store/.git"); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); - if (localDir.exists()) { File[] folders = localDir.listFiles((FileFilter) FileFilterUtils.directoryFileFilter()); status = folders.length; @@ -111,4 +115,12 @@ public class pwdstore extends Activity implements ToCloneOrNot.OnFragmentIntera } } + public void addItem(View view) { + ListView l = (ListView) findViewById(R.id.pass_list); + + PasswordAdapter pad = (PasswordAdapter) l.getAdapter(); + + pad.add("Something"); + } + } diff --git a/app/src/main/res/layout/fragment_password_list.xml b/app/src/main/res/layout/fragment_password_list.xml index 9ee68056..e3f04ece 100644 --- a/app/src/main/res/layout/fragment_password_list.xml +++ b/app/src/main/res/layout/fragment_password_list.xml @@ -6,10 +6,15 @@ tools:context="com.zeapo.pwdstore.PasswordFragment"> +