From ca45e739dadbacfe76ccce3e2c79b308d292061a Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Fri, 14 Aug 2015 00:35:00 -0400 Subject: Async populate the app list --- .../autofill/AutofillPreferenceActivity.java | 64 +++++++++++++++------- .../pwdstore/autofill/AutofillRecyclerAdapter.java | 7 ++- app/src/main/res/layout/autofill_recycler_view.xml | 18 ++++-- 3 files changed, 62 insertions(+), 27 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java index 6f507fa5..788442a0 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java @@ -4,6 +4,7 @@ import android.app.DialogFragment; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; +import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.NavUtils; import android.support.v4.app.TaskStackBuilder; @@ -14,6 +15,7 @@ import android.support.v7.widget.RecyclerView; import android.support.v7.widget.SearchView; import android.view.Menu; import android.view.MenuItem; +import android.view.View; import com.zeapo.pwdstore.R; @@ -28,6 +30,8 @@ public class AutofillPreferenceActivity extends AppCompatActivity { AutofillRecyclerAdapter recyclerAdapter; // let fragment have access private RecyclerView.LayoutManager layoutManager; + private PackageManager pm; + private boolean recreate; @Override @@ -41,27 +45,47 @@ public class AutofillPreferenceActivity extends AppCompatActivity { recyclerView.setLayoutManager(layoutManager); recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST)); - Intent intent = new Intent(Intent.ACTION_MAIN); - intent.addCategory(Intent.CATEGORY_LAUNCHER); - List allApps = getPackageManager().queryIntentActivities(intent, 0); - final PackageManager pm = getPackageManager(); - Collections.sort(allApps, new Comparator() { - @Override - public int compare(ResolveInfo lhs, ResolveInfo rhs) { - return lhs.loadLabel(pm).toString().compareTo(rhs.loadLabel(pm).toString()); - } - }); - recyclerAdapter = new AutofillRecyclerAdapter(new ArrayList<>(allApps), pm, this); - recyclerView.setAdapter(recyclerAdapter); + pm = getPackageManager(); + + new populateTask().execute(); setTitle("Autofill Apps"); + } + + private class populateTask extends AsyncTask> { + @Override + protected void onPreExecute() { + findViewById(R.id.progress_bar).setVisibility(View.VISIBLE); + } - recreate = false; - Bundle extras = getIntent().getExtras(); - if (extras != null) { - recreate = true; - recyclerView.scrollToPosition(recyclerAdapter.getPosition(extras.getString("packageName"))); - showDialog(extras.getString("packageName"), extras.getString("appName")); + @Override + protected List doInBackground(Void... params) { + Intent intent = new Intent(Intent.ACTION_MAIN); + intent.addCategory(Intent.CATEGORY_LAUNCHER); + List allApps = pm.queryIntentActivities(intent, 0); + Collections.sort(allApps, new Comparator() { + @Override + public int compare(ResolveInfo lhs, ResolveInfo rhs) { + return lhs.loadLabel(pm).toString().compareTo(rhs.loadLabel(pm).toString()); + } + }); + return allApps; + } + + @Override + protected void onPostExecute(List allApps) { + findViewById(R.id.progress_bar).setVisibility(View.GONE); + + recyclerAdapter = new AutofillRecyclerAdapter(new ArrayList<>(allApps), pm, AutofillPreferenceActivity.this); + recyclerView.setAdapter(recyclerAdapter); + + recreate = false; + Bundle extras = getIntent().getExtras(); + if (extras != null) { + recreate = true; + recyclerView.scrollToPosition(recyclerAdapter.getPosition(extras.getString("packageName"))); + showDialog(extras.getString("packageName"), extras.getString("appName")); + } } } @@ -75,11 +99,12 @@ public class AutofillPreferenceActivity extends AppCompatActivity { searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String s) { - return true; + return false; } @Override public boolean onQueryTextChange(String s) { + recyclerAdapter.filter(s); return true; } }); @@ -99,6 +124,7 @@ public class AutofillPreferenceActivity extends AppCompatActivity { }); return super.onCreateOptionsMenu(menu); } + @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java index 7c735f18..485ef288 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java @@ -4,7 +4,6 @@ import android.content.Context; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; -import android.support.v7.view.ActionMode; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; @@ -20,7 +19,6 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter apps; private PackageManager pm; private AutofillPreferenceActivity activity; - private ActionMode actionMode; public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public View view; @@ -61,7 +59,7 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter - + - \ No newline at end of file + + + \ No newline at end of file -- cgit v1.2.3