App filtering

This commit is contained in:
Matthew Wong
2015-08-14 01:43:40 -04:00
parent ca45e739da
commit b260a19be7
2 changed files with 46 additions and 32 deletions

View File

@@ -19,9 +19,6 @@ import android.view.View;
import com.zeapo.pwdstore.R; import com.zeapo.pwdstore.R;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
public class AutofillPreferenceActivity extends AppCompatActivity { public class AutofillPreferenceActivity extends AppCompatActivity {
@@ -52,31 +49,25 @@ public class AutofillPreferenceActivity extends AppCompatActivity {
setTitle("Autofill Apps"); setTitle("Autofill Apps");
} }
private class populateTask extends AsyncTask<Void, Void, List<ResolveInfo>> { private class populateTask extends AsyncTask<Void, Void, Void> {
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
findViewById(R.id.progress_bar).setVisibility(View.VISIBLE); findViewById(R.id.progress_bar).setVisibility(View.VISIBLE);
} }
@Override @Override
protected List<ResolveInfo> doInBackground(Void... params) { protected Void doInBackground(Void... params) {
Intent intent = new Intent(Intent.ACTION_MAIN); Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> allApps = pm.queryIntentActivities(intent, 0); List<ResolveInfo> allApps = pm.queryIntentActivities(intent, 0);
Collections.sort(allApps, new Comparator<ResolveInfo>() { recyclerAdapter = new AutofillRecyclerAdapter(allApps, pm, AutofillPreferenceActivity.this);
@Override return null;
public int compare(ResolveInfo lhs, ResolveInfo rhs) {
return lhs.loadLabel(pm).toString().compareTo(rhs.loadLabel(pm).toString());
}
});
return allApps;
} }
@Override @Override
protected void onPostExecute(List<ResolveInfo> allApps) { protected void onPostExecute(Void aVoid) {
findViewById(R.id.progress_bar).setVisibility(View.GONE); findViewById(R.id.progress_bar).setVisibility(View.GONE);
recyclerAdapter = new AutofillRecyclerAdapter(new ArrayList<>(allApps), pm, AutofillPreferenceActivity.this);
recyclerView.setAdapter(recyclerAdapter); recyclerView.setAdapter(recyclerAdapter);
recreate = false; recreate = false;
@@ -104,24 +95,13 @@ public class AutofillPreferenceActivity extends AppCompatActivity {
@Override @Override
public boolean onQueryTextChange(String s) { public boolean onQueryTextChange(String s) {
recyclerAdapter.filter(s); if (recyclerAdapter != null) {
recyclerAdapter.filter(s);
}
return true; return true;
} }
}); });
// When using the support library, the setOnActionExpandListener() method is
// static and accepts the MenuItem object as an argument
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
});
return super.onCreateOptionsMenu(menu); return super.onCreateOptionsMenu(menu);
} }

View File

@@ -4,7 +4,9 @@ import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.support.v7.util.SortedList;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.util.SortedListAdapterCallback;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@@ -14,9 +16,11 @@ import android.widget.TextView;
import com.zeapo.pwdstore.R; import com.zeapo.pwdstore.R;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
public class AutofillRecyclerAdapter extends RecyclerView.Adapter<AutofillRecyclerAdapter.ViewHolder> { public class AutofillRecyclerAdapter extends RecyclerView.Adapter<AutofillRecyclerAdapter.ViewHolder> {
private ArrayList<ResolveInfo> apps; private SortedList<ResolveInfo> apps;
private ArrayList<ResolveInfo> allApps;
private PackageManager pm; private PackageManager pm;
private AutofillPreferenceActivity activity; private AutofillPreferenceActivity activity;
@@ -43,8 +47,26 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter<AutofillRecycl
} }
public AutofillRecyclerAdapter(ArrayList<ResolveInfo> apps, PackageManager pm, AutofillPreferenceActivity activity) { public AutofillRecyclerAdapter(List<ResolveInfo> allApps, final PackageManager pm, AutofillPreferenceActivity activity) {
this.apps = apps; SortedList.Callback<ResolveInfo> callback = new SortedListAdapterCallback<ResolveInfo>(this) {
@Override
public int compare(ResolveInfo o1, ResolveInfo o2) {
return o1.loadLabel(pm).toString().compareTo(o2.loadLabel(pm).toString());
}
@Override
public boolean areContentsTheSame(ResolveInfo oldItem, ResolveInfo newItem) {
return oldItem.loadLabel(pm).toString().equals(newItem.loadLabel(pm).toString());
}
@Override
public boolean areItemsTheSame(ResolveInfo item1, ResolveInfo item2) {
return item1.loadLabel(pm).toString().equals(item2.loadLabel(pm).toString());
}
};
this.apps = new SortedList<>(ResolveInfo.class, callback);
this.apps.addAll(allApps);
this.allApps = new ArrayList<>(allApps);
this.pm = pm; this.pm = pm;
this.activity = activity; this.activity = activity;
} }
@@ -102,6 +124,18 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter<AutofillRecycl
} }
public void filter(String s) { public void filter(String s) {
if (s.isEmpty()) {
apps.addAll(allApps);
return;
}
apps.beginBatchedUpdates();
for (ResolveInfo app : allApps) {
if (app.loadLabel(pm).toString().toLowerCase().contains(s.toLowerCase())) {
apps.add(app);
} else {
apps.remove(app);
}
}
apps.endBatchedUpdates();
} }
} }