From a0b8301a4976aaa2ed170542bbfcedca84cd57fc Mon Sep 17 00:00:00 2001 From: Zeapo Date: Sun, 27 Jul 2014 18:58:42 +0100 Subject: Some refactoring and organisation for listview.. sooooon --- app/src/main/AndroidManifest.xml | 2 +- app/src/main/java/com/zeapo/pwdstore/GitClone.java | 20 +++- app/src/main/java/com/zeapo/pwdstore/GitRepo.java | 41 +++++++ .../java/com/zeapo/pwdstore/PasswordAdapter.java | 48 ++++++++ .../java/com/zeapo/pwdstore/PasswordFragment.java | 63 +---------- .../java/com/zeapo/pwdstore/PasswordStore.java | 126 +++++++++++++++++++++ app/src/main/java/com/zeapo/pwdstore/pwdstore.java | 114 ------------------- app/src/main/res/layout/fragment_password_list.xml | 7 +- .../main/res/layout/fragment_to_clone_or_not.xml | 11 +- app/src/main/res/values/strings.xml | 7 +- 10 files changed, 254 insertions(+), 185 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 create mode 100644 app/src/main/java/com/zeapo/pwdstore/PasswordStore.java delete mode 100644 app/src/main/java/com/zeapo/pwdstore/pwdstore.java (limited to 'app/src/main') 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/PasswordStore.java b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java new file mode 100644 index 00000000..8b047626 --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java @@ -0,0 +1,126 @@ +package com.zeapo.pwdstore; + +import android.app.Activity; +import android.app.FragmentManager; +import android.app.FragmentTransaction; +import android.content.Context; +import android.content.Intent; +import android.hardware.display.DisplayManager; +import android.net.Uri; +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; +import org.apache.commons.io.filefilter.DirectoryFileFilter; +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; + +import java.io.File; +import java.io.FileFilter; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.util.Collection; +import java.util.List; + + +public class PasswordStore extends Activity implements ToCloneOrNot.OnFragmentInteractionListener, PasswordFragment.OnFragmentInteractionListener { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_pwdstore); + } + + @Override + public void onResume(){ + super.onResume(); + // re-check that there was no change with the repository state + checkLocalRepository(); + } + + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.pwdstore, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. +// int id = item.getItemId(); +// if (id == R.id.clone_setting) { +// getClone(); +// return true; +// } + return super.onOptionsItemSelected(item); + } + + public void getClone(View view){ + Intent intent = new Intent(this, GitClone.class); + startActivity(intent); + } + + @Override + public void onFragmentInteraction(Uri uri) { + + } + + @Override + public void onFragmentInteraction(String id) { + + } + + private void checkLocalRepository() { + int status = 0; + 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; + } + + // either the repo is empty or it was not correctly cloned + switch (status) { + case 0: + ToCloneOrNot cloneFrag = new ToCloneOrNot(); + fragmentTransaction.replace(R.id.main_layout, cloneFrag); + fragmentTransaction.commit(); + break; + case 1: + // empty + break; + default: + PasswordFragment passFrag = new PasswordFragment(); + fragmentTransaction.replace(R.id.main_layout, passFrag); + fragmentTransaction.commit(); + } + } + + 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/java/com/zeapo/pwdstore/pwdstore.java b/app/src/main/java/com/zeapo/pwdstore/pwdstore.java deleted file mode 100644 index 52071957..00000000 --- a/app/src/main/java/com/zeapo/pwdstore/pwdstore.java +++ /dev/null @@ -1,114 +0,0 @@ -package com.zeapo.pwdstore; - -import android.app.Activity; -import android.app.FragmentManager; -import android.app.FragmentTransaction; -import android.content.Context; -import android.content.Intent; -import android.hardware.display.DisplayManager; -import android.net.Uri; -import android.os.Bundle; -import android.view.Menu; -import android.view.MenuItem; -import android.view.View; -import android.widget.TextView; - -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.filefilter.DirectoryFileFilter; -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.lib.Repository; -import org.eclipse.jgit.lib.RepositoryBuilder; -import org.eclipse.jgit.storage.file.FileRepositoryBuilder; - -import java.io.File; -import java.io.FileFilter; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.util.Collection; - - -public class pwdstore extends Activity implements ToCloneOrNot.OnFragmentInteractionListener, PasswordFragment.OnFragmentInteractionListener { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_pwdstore); - } - - @Override - public void onResume(){ - super.onResume(); - // re-check that there was no change with the repository state - checkLocalRepository(); - } - - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.pwdstore, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. -// int id = item.getItemId(); -// if (id == R.id.clone_setting) { -// getClone(); -// return true; -// } - return super.onOptionsItemSelected(item); - } - - public void getClone(View view){ - Intent intent = new Intent(this, GitClone.class); - startActivity(intent); - } - - @Override - public void onFragmentInteraction(Uri uri) { - - } - - @Override - public void onFragmentInteraction(String id) { - - } - - private void checkLocalRepository() { - int status = 0; - final File localDir = new File(getCacheDir() + "/store"); - - FragmentManager fragmentManager = getFragmentManager(); - FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); - - - if (localDir.exists()) { - File[] folders = localDir.listFiles((FileFilter) FileFilterUtils.directoryFileFilter()); - status = folders.length; - } - - // either the repo is empty or it was not correctly cloned - switch (status) { - case 0: - ToCloneOrNot cloneFrag = new ToCloneOrNot(); - fragmentTransaction.replace(R.id.main_layout, cloneFrag); - fragmentTransaction.commit(); - break; - case 1: - // empty - break; - default: - PasswordFragment passFrag = new PasswordFragment(); - fragmentTransaction.replace(R.id.main_layout, passFrag); - fragmentTransaction.commit(); - } - } - -} 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"> +