Load icons & labels async beforehand as well

This commit is contained in:
Matthew Wong
2015-08-14 13:31:23 -04:00
parent b260a19be7
commit 8388d8530a
2 changed files with 22 additions and 5 deletions

View File

@@ -4,6 +4,7 @@ import android.app.DialogFragment;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
@@ -13,12 +14,14 @@ import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.util.Pair;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.zeapo.pwdstore.R;
import java.util.HashMap;
import java.util.List;
public class AutofillPreferenceActivity extends AppCompatActivity {
@@ -60,7 +63,14 @@ public class AutofillPreferenceActivity extends AppCompatActivity {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> allApps = pm.queryIntentActivities(intent, 0);
recyclerAdapter = new AutofillRecyclerAdapter(allApps, pm, AutofillPreferenceActivity.this);
HashMap<String, Pair<Drawable, String>> iconMap = new HashMap<>(allApps.size());
for (ResolveInfo app : allApps) {
iconMap.put(app.activityInfo.packageName
, Pair.create(app.loadIcon(pm), app.loadLabel(pm).toString()));
}
recyclerAdapter = new AutofillRecyclerAdapter(allApps, iconMap, pm, AutofillPreferenceActivity.this);
return null;
}

View File

@@ -4,9 +4,11 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.support.v7.util.SortedList;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.util.SortedListAdapterCallback;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -16,11 +18,13 @@ import android.widget.TextView;
import com.zeapo.pwdstore.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class AutofillRecyclerAdapter extends RecyclerView.Adapter<AutofillRecyclerAdapter.ViewHolder> {
private SortedList<ResolveInfo> apps;
private ArrayList<ResolveInfo> allApps;
private HashMap<String, Pair<Drawable, String>> iconMap;
private PackageManager pm;
private AutofillPreferenceActivity activity;
@@ -47,11 +51,12 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter<AutofillRecycl
}
public AutofillRecyclerAdapter(List<ResolveInfo> allApps, final PackageManager pm, AutofillPreferenceActivity activity) {
public AutofillRecyclerAdapter(List<ResolveInfo> allApps, HashMap<String, Pair<Drawable, String>> iconMap
, final PackageManager pm, AutofillPreferenceActivity activity) {
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());
return o1.loadLabel(pm).toString().toLowerCase().compareTo(o2.loadLabel(pm).toString().toLowerCase());
}
@Override
@@ -67,6 +72,7 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter<AutofillRecycl
this.apps = new SortedList<>(ResolveInfo.class, callback);
this.apps.addAll(allApps);
this.allApps = new ArrayList<>(allApps);
this.iconMap = new HashMap<>(iconMap);
this.pm = pm;
this.activity = activity;
}
@@ -81,10 +87,11 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter<AutofillRecycl
@Override
public void onBindViewHolder(AutofillRecyclerAdapter.ViewHolder holder, int position) {
ResolveInfo app = apps.get(position);
holder.name.setText(app.loadLabel(pm)); // 'No package identifier when getting value for resource number 0x00000000' 5+
holder.icon.setImageDrawable(app.loadIcon(pm));
holder.packageName = app.activityInfo.packageName;
holder.icon.setImageDrawable(iconMap.get(holder.packageName).first);
holder.name.setText(iconMap.get(holder.packageName).second);
holder.secondary.setVisibility(View.VISIBLE);
holder.view.setBackgroundResource(R.color.grey_white_1000);