aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java
blob: 10684f458785b40069c6ac374cf52ff9b509b39a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.zeapo.pwdstore.autofill;

import android.app.DialogFragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.zeapo.pwdstore.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

public class AutofillPreferenceActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    AutofillRecyclerAdapter recyclerAdapter; // let fragment have access
    private RecyclerView.LayoutManager layoutManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // otherwise if called from settings
        setContentView(R.layout.autofill_recycler_view);
        recyclerView = (RecyclerView) findViewById(R.id.autofill_recycler);

        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        // apps for which the user has custom settings should be in the recycler
        final PackageManager pm = getPackageManager();
        SharedPreferences prefs
                = getSharedPreferences("autofill", Context.MODE_PRIVATE);
        Map<String, ?> prefApps = prefs.getAll();
        ArrayList<ApplicationInfo> apps = new ArrayList<>();
        for (String packageName : prefApps.keySet()) {
            try {
                apps.add(pm.getApplicationInfo(packageName, 0));
            } catch (PackageManager.NameNotFoundException e) {
                // remove invalid entries (from uninstalled apps?)
                SharedPreferences.Editor editor = prefs.edit();
                editor.remove(packageName).apply();
            }
        }
        Collections.sort(apps, new Comparator<ApplicationInfo>() {
            @Override
            public int compare(ApplicationInfo lhs, ApplicationInfo rhs) {
                return lhs.loadLabel(pm).toString().compareTo(rhs.loadLabel(pm).toString());
            }
        });
        recyclerAdapter = new AutofillRecyclerAdapter(apps, pm, this);
        recyclerView.setAdapter(recyclerAdapter);

        // show the search bar by default but don't open the keyboard
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        final SearchView searchView = (SearchView) findViewById(R.id.app_search);
        searchView.clearFocus();

        // create search suggestions of apps with icons & names
        final SimpleCursorAdapter.ViewBinder viewBinder = new SimpleCursorAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                if (view instanceof TextView) {
                    ((TextView) view).setText(cursor.getString(columnIndex));
                } else if (view instanceof ImageView) {
                    try {
                        ((ImageView) view).setImageDrawable(pm.getApplicationIcon(cursor.getString(columnIndex)));
                    } catch (PackageManager.NameNotFoundException e) {
                        e.printStackTrace();
                        return false;
                    }
                }
                return true;
            }
        };

        final List<ApplicationInfo> allApps = pm.getInstalledApplications(0);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // should be a better/faster way to do this?
                // TODO do this async probably. it lags.
                MatrixCursor matrixCursor = new MatrixCursor(new String[]{"_id", "package", "label"});
                for (ApplicationInfo applicationInfo : allApps) {
                    if (applicationInfo.loadLabel(pm).toString().toLowerCase().contains(newText.toLowerCase())) {
                        matrixCursor.addRow(new Object[]{0, applicationInfo.packageName, applicationInfo.loadLabel(pm)});
                    }
                }
                SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(AutofillPreferenceActivity.this
                        , R.layout.app_list_item, matrixCursor, new String[]{"package", "label"}
                        , new int[]{android.R.id.icon1, android.R.id.text1}, 0);
                simpleCursorAdapter.setViewBinder(viewBinder);
                searchView.setSuggestionsAdapter(simpleCursorAdapter);
                return false;
            }
        });

        searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
            @Override
            public boolean onSuggestionSelect(int position) {
                return false;
            }

            @Override
            public boolean onSuggestionClick(int position) {
                Cursor cursor = searchView.getSuggestionsAdapter().getCursor();
                String packageName = cursor.getString(1);
                String appName = cursor.getString(2);
                showDialog(packageName, appName);
                return true;
            }
        });

        setTitle("Autofill Apps");

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            showDialog(extras.getString("packageName"), extras.getString("appName"));
        }
    }

    public void showDialog(String packageName, String appName) {
        DialogFragment df = new AutofillFragment();
        Bundle args = new Bundle();
        args.putString("packageName", packageName);
        args.putString("appName", appName);
        args.putInt("position", recyclerAdapter.getPosition(packageName));
        df.setArguments(args);
        df.show(getFragmentManager(), "autofill_dialog");

        // TODO if called from dialog 'Settings' button, should activity finish at OK?
    }
}