From 5cb380bf471c6c7db1d165183296176d564c1ad3 Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Sun, 27 Dec 2015 06:00:06 -0500 Subject: Don't use ResolveInfo for app settings ui --- .../zeapo/pwdstore/autofill/AutofillFragment.java | 3 + .../autofill/AutofillPreferenceActivity.java | 37 +++++++++--- .../pwdstore/autofill/AutofillRecyclerAdapter.java | 68 ++++++++++++---------- .../zeapo/pwdstore/autofill/AutofillService.java | 3 + app/src/main/res/layout/autofill_recycler_view.xml | 20 ++++++- 5 files changed, 90 insertions(+), 41 deletions(-) (limited to 'app') diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java index e4986354..4a5a37cd 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java @@ -38,6 +38,8 @@ public class AutofillFragment extends DialogFragment { // need to interact with the recyclerAdapter which is a member of activity final AutofillPreferenceActivity callingActivity = (AutofillPreferenceActivity) getActivity(); LayoutInflater inflater = callingActivity.getLayoutInflater(); + + // if... hide textview final View view = inflater.inflate(R.layout.fragment_autofill, null); builder.setView(view); @@ -74,6 +76,7 @@ public class AutofillFragment extends DialogFragment { } }); + // if... autofill_web SharedPreferences prefs = getActivity().getApplicationContext().getSharedPreferences("autofill", Context.MODE_PRIVATE); String preference = prefs.getString(packageName, ""); 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 adfce0b1..0c721012 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java @@ -1,12 +1,14 @@ package com.zeapo.pwdstore.autofill; import android.app.DialogFragment; +import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; 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.design.widget.FloatingActionButton; import android.support.v4.app.NavUtils; import android.support.v4.app.TaskStackBuilder; import android.support.v4.view.MenuItemCompat; @@ -14,15 +16,15 @@ 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.ArrayList; import java.util.List; +import java.util.Map; public class AutofillPreferenceActivity extends AppCompatActivity { @@ -58,6 +60,13 @@ public class AutofillPreferenceActivity extends AppCompatActivity { } setTitle("Autofill Apps"); + + final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); + fab.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + } + }); } private class populateTask extends AsyncTask { @@ -70,15 +79,25 @@ public class AutofillPreferenceActivity extends AppCompatActivity { protected Void doInBackground(Void... params) { Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); - List allApps = pm.queryIntentActivities(intent, 0); + List allAppsResolveInfo = pm.queryIntentActivities(intent, 0); + List allApps = new ArrayList<>(); - HashMap> iconMap = new HashMap<>(allApps.size()); - for (ResolveInfo app : allApps) { - iconMap.put(app.activityInfo.packageName - , Pair.create(app.loadIcon(pm), app.loadLabel(pm).toString())); + for (ResolveInfo app : allAppsResolveInfo) { + allApps.add(new AutofillRecyclerAdapter.AppInfo(app.loadLabel(pm).toString() + , app.activityInfo.packageName, app.loadIcon(pm))); + } + + SharedPreferences prefs = getSharedPreferences("autofill_web", Context.MODE_PRIVATE); + Map prefsMap = prefs.getAll(); + for (String key : prefsMap.keySet()) { + try { + allApps.add(new AutofillRecyclerAdapter.AppInfo(null, key, pm.getApplicationIcon("com.android.browser"))); + } catch (PackageManager.NameNotFoundException e) { + allApps.add(new AutofillRecyclerAdapter.AppInfo(null, key, null)); + } } - recyclerAdapter = new AutofillRecyclerAdapter(allApps, iconMap, pm, AutofillPreferenceActivity.this); + recyclerAdapter = new AutofillRecyclerAdapter(allApps, pm, AutofillPreferenceActivity.this); return null; } 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 eddf075e..e00f1c5b 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java @@ -3,12 +3,10 @@ package com.zeapo.pwdstore.autofill; 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; @@ -18,13 +16,12 @@ 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 { - private SortedList apps; - private ArrayList allApps; - private HashMap> iconMap; + + private SortedList apps; + private ArrayList allApps; private PackageManager pm; private AutofillPreferenceActivity activity; @@ -51,28 +48,39 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter allApps, HashMap> iconMap - , final PackageManager pm, AutofillPreferenceActivity activity) { - SortedList.Callback callback = new SortedListAdapterCallback(this) { - @Override - public int compare(ResolveInfo o1, ResolveInfo o2) { - return o1.loadLabel(pm).toString().toLowerCase().compareTo(o2.loadLabel(pm).toString().toLowerCase()); - } + public static class AppInfo { + public String label; + public String packageName; + public Drawable icon; - @Override - public boolean areContentsTheSame(ResolveInfo oldItem, ResolveInfo newItem) { - return oldItem.loadLabel(pm).toString().equals(newItem.loadLabel(pm).toString()); - } + public AppInfo(String label, String packageName, Drawable icon) { + this.label = label; + this.packageName = packageName; + this.icon = icon; + } + } - @Override - public boolean areItemsTheSame(ResolveInfo item1, ResolveInfo item2) { - return item1.loadLabel(pm).toString().equals(item2.loadLabel(pm).toString()); - } + public AutofillRecyclerAdapter(List allApps, final PackageManager pm + , AutofillPreferenceActivity activity) { + SortedList.Callback callback = new SortedListAdapterCallback(this) { + @Override + public int compare(AppInfo o1, AppInfo o2) { + return o1.label.toLowerCase().compareTo(o2.label.toLowerCase()); + } + + @Override + public boolean areContentsTheSame(AppInfo oldItem, AppInfo newItem) { + return oldItem.label.equals(newItem.label); + } + + @Override + public boolean areItemsTheSame(AppInfo item1, AppInfo item2) { + return item1.packageName.equals(item2.packageName); + } }; - this.apps = new SortedList<>(ResolveInfo.class, callback); + this.apps = new SortedList<>(AppInfo.class, callback); this.apps.addAll(allApps); this.allApps = new ArrayList<>(allApps); - this.iconMap = new HashMap<>(iconMap); this.pm = pm; this.activity = activity; } @@ -86,11 +94,11 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter + android:layout_width="match_parent" + android:layout_height="match_parent" + xmlns:app="http://schemas.android.com/apk/res-auto"> + + \ No newline at end of file -- cgit v1.2.3