From 2d7c37d379ae48fa3ff464032ad4cb1759b1dc31 Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Fri, 31 Jul 2015 09:00:06 -0400 Subject: Add per-app settings page --- .../zeapo/pwdstore/autofill/AutofillService.java | 254 +++++++++++++++++++++ 1 file changed, 254 insertions(+) create mode 100644 app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java (limited to 'app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java') diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java new file mode 100644 index 00000000..6188ef6d --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java @@ -0,0 +1,254 @@ +package com.zeapo.pwdstore.autofill; + +import android.accessibilityservice.AccessibilityService; +import android.app.PendingIntent; +import android.content.ClipData; +import android.content.ClipboardManager; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.SharedPreferences; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.os.Build; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.support.v7.app.AlertDialog; +import android.util.Log; +import android.view.WindowManager; +import android.view.accessibility.AccessibilityEvent; +import android.view.accessibility.AccessibilityNodeInfo; +import android.widget.Toast; + +import com.zeapo.pwdstore.R; +import com.zeapo.pwdstore.UserPreference; +import com.zeapo.pwdstore.utils.PasswordItem; +import com.zeapo.pwdstore.utils.PasswordRepository; + +import org.apache.commons.io.FileUtils; +import org.openintents.openpgp.OpenPgpError; +import org.openintents.openpgp.util.OpenPgpApi; +import org.openintents.openpgp.util.OpenPgpServiceConnection; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; + +public class AutofillService extends AccessibilityService { + private OpenPgpServiceConnection serviceConnection; + private SharedPreferences settings; + private AccessibilityNodeInfo info; // the original source of the event (the edittext field) + private ArrayList items; // password choices + private AlertDialog dialog; + private static boolean unlockOK = false; // if openkeychain user interaction was successful + private CharSequence packageName; + private boolean ignoreActionFocus = false; + + public final class Constants { + public static final String TAG = "Keychain"; + } + + public static void setUnlockOK() { unlockOK = true; } + + @Override + protected void onServiceConnected() { + super.onServiceConnected(); + serviceConnection = new OpenPgpServiceConnection(AutofillService.this, "org.sufficientlysecure.keychain"); + serviceConnection.bindToService(); + settings = PreferenceManager.getDefaultSharedPreferences(this); + } + + @Override + public void onAccessibilityEvent(AccessibilityEvent event) { + // if returning to the source app from a successful AutofillActivity + if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED + && event.getPackageName().equals(packageName) && unlockOK) { + decryptAndVerify(); + } + if (!event.isPassword() + || Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2 + || event.getPackageName().equals("org.sufficientlysecure.keychain")) { + if (!(event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED + && event.getPackageName().toString().contains("com.android.inputmethod")) + && dialog != null && dialog.isShowing()) { + dialog.dismiss(); + } + return; + } + if (dialog != null && dialog.isShowing()) { + dialog.dismiss(); + } + // ignore the ACTION_FOCUS from decryptAndVerify + if (ignoreActionFocus) { + ignoreActionFocus = false; + return; + } + info = event.getSource(); + PackageManager packageManager = getPackageManager(); + ApplicationInfo applicationInfo; + try { + applicationInfo = packageManager.getApplicationInfo(event.getPackageName().toString(), 0); + } catch (PackageManager.NameNotFoundException e) { + applicationInfo = null; + } + String appName = (applicationInfo != null ? packageManager.getApplicationLabel(applicationInfo) : "").toString(); + items = recursiveFilter(appName, null); + if (items.isEmpty()) { + return; + } + + if (dialog == null) { + AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.Theme_AppCompat_Dialog); + builder.setNegativeButton("Cancel", null); + builder.setPositiveButton("Fill", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + decryptAndVerify(); + } + }); + builder.setNeutralButton("Match", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + + } + }); + dialog = builder.create(); + dialog.setIcon(R.drawable.ic_launcher); + dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); + dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); + dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); + } + dialog.setTitle(items.get(0).getName()); + dialog.show(); + dialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT); + } + + private ArrayList recursiveFilter(String filter, File dir) { + ArrayList items = new ArrayList<>(); + if (!PasswordRepository.isInitialized()) { + initialize(); + } + ArrayList passwordItems = dir == null ? + PasswordRepository.getPasswords() : + PasswordRepository.getPasswords(dir); + for (PasswordItem item : passwordItems) { + if (item.getType() == PasswordItem.TYPE_CATEGORY) { + recursiveFilter(filter, item.getFile()); + } + if (item.toString().toLowerCase().contains(filter.toLowerCase())) { + items.add(item); + } + } + return items; + } + + // just like PasswordRepository.initialize(). + private void initialize() { + File dir = null; + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); + + if (settings.getBoolean("git_external", false)) { + if (settings.getString("git_external_repo", null) != null) { + dir = new File(settings.getString("git_external_repo", null)); + } + } else { + dir = new File(getFilesDir() + "/store"); + } + // temp for debug + if (dir == null) { + Intent intent = new Intent(this, UserPreference.class); + intent.putExtra("operation", "git_external"); + startActivity(intent); + return; + } + + // uninitialize the repo if the dir does not exist or is absolutely empty + if (!dir.exists() || !dir.isDirectory() || FileUtils.listFiles(dir, null, false).isEmpty()) { + settings.edit().putBoolean("repository_initialized", false).apply(); + } + + if (!PasswordRepository.getPasswords(dir).isEmpty()) { + settings.edit().putBoolean("repository_initialized", true).apply(); + } + + // create the repository static variable in PasswordRepository + PasswordRepository.getRepository(new File(dir.getAbsolutePath() + "/.git")); + } + + @Override + public void onInterrupt() { + + } + + public void decryptAndVerify() { + unlockOK = false; + packageName = info.getPackageName(); + Intent data = new Intent(); + data.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY); + InputStream is = null; + try { + is = FileUtils.openInputStream(items.get(0).getFile()); + } catch (IOException e) { + e.printStackTrace(); + } + ByteArrayOutputStream os = new ByteArrayOutputStream(); + + OpenPgpApi api = new OpenPgpApi(AutofillService.this, serviceConnection.getService()); + Intent result = api.executeApi(data, is, os); + switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) { + case OpenPgpApi.RESULT_CODE_SUCCESS: { + try { + String[] passContent = os.toString("UTF-8").split("\n"); + // if the user focused on something else, take focus back + // but this will open another dialog...hack to ignore this + ignoreActionFocus = info.performAction(AccessibilityNodeInfo.ACTION_FOCUS); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + Bundle args = new Bundle(); + args.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, + passContent[0]); + info.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, args); + } else { + ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); + ClipData clip = ClipData.newPlainText("autofill_pm", passContent[0]); + clipboard.setPrimaryClip(clip); + info.performAction(AccessibilityNodeInfo.ACTION_PASTE); + clip = ClipData.newPlainText("autofill_pm", "MyPasswordIsDaBest!"); + clipboard.setPrimaryClip(clip); + if (settings.getBoolean("clear_clipboard_20x", false)) { + for (int i = 0; i < 19; i++) { + clip = ClipData.newPlainText(String.valueOf(i), String.valueOf(i)); + clipboard.setPrimaryClip(clip); + } + } + } + } catch (UnsupportedEncodingException e) { + Log.e(Constants.TAG, "UnsupportedEncodingException", e); + } + break; + } + case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: { + Log.i("PgpHandler", "RESULT_CODE_USER_INTERACTION_REQUIRED"); + PendingIntent pi = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT); + // need to start a blank activity to call startIntentSenderForResult + Intent intent = new Intent(AutofillService.this, AutofillActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK + | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); + intent.putExtra("pending_intent", pi); + startActivity(intent); + break; + } + case OpenPgpApi.RESULT_CODE_ERROR: { + OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR); + Toast.makeText(AutofillService.this, + "Error from OpenKeyChain : " + error.getMessage(), + Toast.LENGTH_LONG).show(); + Log.e(Constants.TAG, "onError getErrorId:" + error.getErrorId()); + Log.e(Constants.TAG, "onError getMessage:" + error.getMessage()); + break; + } + } + } +} -- cgit v1.2.3 From 51a05087e5a349afa3f4e0febdc3a339179e9bbd Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Mon, 3 Aug 2015 08:31:23 -0400 Subject: Add searching for apps in autofill settings page --- app/src/main/AndroidManifest.xml | 3 + .../zeapo/pwdstore/autofill/AutofillActivity.java | 68 +++++++++++++++++++--- .../zeapo/pwdstore/autofill/AutofillService.java | 2 +- app/src/main/res/layout/app_list_item.xml | 23 ++++++++ app/src/main/res/layout/autofill_recycler_view.xml | 7 +++ 5 files changed, 93 insertions(+), 10 deletions(-) create mode 100644 app/src/main/res/layout/app_list_item.xml (limited to 'app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b3600c53..3ddc04e8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -55,7 +55,10 @@ + android:parentActivityName=".PasswordStore"> + diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java index 5d8fac5e..5c42ab8d 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java @@ -7,11 +7,19 @@ import android.content.IntentSender; 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.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; +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; @@ -48,9 +56,15 @@ public class AutofillActivity extends AppCompatActivity { return; } // otherwise if called from settings - final PackageManager pm = getPackageManager(); - List allApps = pm.getInstalledApplications(0); + 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(); + final List allApps = pm.getInstalledApplications(0); SharedPreferences prefs = getSharedPreferences("autofill", Context.MODE_PRIVATE); Map prefApps = prefs.getAll(); @@ -60,18 +74,54 @@ public class AutofillActivity extends AppCompatActivity { apps.add(applicationInfo); } } + recyclerAdapter = new AutofillRecyclerAdapter(apps, pm); + recyclerView.setAdapter(recyclerAdapter); - setContentView(R.layout.autofill_recycler_view); - recyclerView = (RecyclerView) findViewById(R.id.autofill_recycler); + getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); + final SearchView searchView = (SearchView) findViewById(R.id.app_search); - layoutManager = new LinearLayoutManager(this); - recyclerView.setLayoutManager(layoutManager); + // 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; + } + }; + searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { + @Override + public boolean onQueryTextSubmit(String query) { + return false; + } - recyclerAdapter = new AutofillRecyclerAdapter(apps, pm); - recyclerView.setAdapter(recyclerAdapter); + @Override + public boolean onQueryTextChange(String newText) { + // should be a better/faster way to do this? + 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(AutofillActivity.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; + } + }); setTitle("Autofill Apps"); - } @Override diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java index 6188ef6d..f3108733 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java @@ -60,7 +60,7 @@ public class AutofillService extends AccessibilityService { serviceConnection.bindToService(); settings = PreferenceManager.getDefaultSharedPreferences(this); } - + // TODO handle CLICKS and change search/search results (just use first result) @Override public void onAccessibilityEvent(AccessibilityEvent event) { // if returning to the source app from a successful AutofillActivity diff --git a/app/src/main/res/layout/app_list_item.xml b/app/src/main/res/layout/app_list_item.xml new file mode 100644 index 00000000..7df28cc6 --- /dev/null +++ b/app/src/main/res/layout/app_list_item.xml @@ -0,0 +1,23 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/autofill_recycler_view.xml b/app/src/main/res/layout/autofill_recycler_view.xml index 27afa77d..98ff779d 100644 --- a/app/src/main/res/layout/autofill_recycler_view.xml +++ b/app/src/main/res/layout/autofill_recycler_view.xml @@ -4,6 +4,13 @@ android:layout_width="match_parent" android:layout_height="match_parent"> + + Date: Sun, 9 Aug 2015 16:04:09 -0400 Subject: Autofill per-app settings dialog opens on click on suggestion or on existing app --- .../zeapo/pwdstore/autofill/AutofillActivity.java | 49 +++++++++++-- .../zeapo/pwdstore/autofill/AutofillFragment.java | 82 ++++++++++++++++++++++ .../pwdstore/autofill/AutofillRecyclerAdapter.java | 53 +++++++++++++- .../zeapo/pwdstore/autofill/AutofillService.java | 15 +++- app/src/main/res/layout/autofill_recycler_view.xml | 6 +- app/src/main/res/layout/fragment_autofill.xml | 52 ++++++++++++++ app/src/main/res/xml/preference.xml | 2 +- 7 files changed, 245 insertions(+), 14 deletions(-) create mode 100644 app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java create mode 100644 app/src/main/res/layout/fragment_autofill.xml (limited to 'app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java') diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java index 5c42ab8d..77a44c7a 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java @@ -1,5 +1,6 @@ package com.zeapo.pwdstore.autofill; +import android.app.DialogFragment; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; @@ -33,7 +34,7 @@ public class AutofillActivity extends AppCompatActivity { private boolean bound = false; private RecyclerView recyclerView; - private AutofillRecyclerAdapter recyclerAdapter; + AutofillRecyclerAdapter recyclerAdapter; // let fragment have access private RecyclerView.LayoutManager layoutManager; @Override @@ -41,6 +42,7 @@ public class AutofillActivity extends AppCompatActivity { super.onCreate(savedInstanceState); Intent intent = getIntent(); Bundle extras = intent.getExtras(); + // if called by service just for startIntentSenderForResult if (extras != null) { try { @@ -64,21 +66,26 @@ public class AutofillActivity extends AppCompatActivity { // apps for which the user has custom settings should be in the recycler final PackageManager pm = getPackageManager(); - final List allApps = pm.getInstalledApplications(0); SharedPreferences prefs = getSharedPreferences("autofill", Context.MODE_PRIVATE); Map prefApps = prefs.getAll(); ArrayList apps = new ArrayList<>(); - for (ApplicationInfo applicationInfo : allApps) { - if (prefApps.containsKey(applicationInfo.packageName)) { - apps.add(applicationInfo); + 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(); } } - recyclerAdapter = new AutofillRecyclerAdapter(apps, pm); + 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() { @@ -97,6 +104,8 @@ public class AutofillActivity extends AppCompatActivity { return true; } }; + + final List allApps = pm.getInstalledApplications(0); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { @@ -108,7 +117,9 @@ public class AutofillActivity extends AppCompatActivity { // should be a better/faster way to do this? MatrixCursor matrixCursor = new MatrixCursor(new String[]{"_id", "package", "label"}); for (ApplicationInfo applicationInfo : allApps) { - if (applicationInfo.loadLabel(pm).toString().toLowerCase().contains(newText.toLowerCase())) { + // exclude apps that already have settings; the search is just for adding + if (applicationInfo.loadLabel(pm).toString().toLowerCase().contains(newText.toLowerCase()) + && !recyclerAdapter.contains(applicationInfo.packageName)) { matrixCursor.addRow(new Object[]{0, applicationInfo.packageName, applicationInfo.loadLabel(pm)}); } } @@ -121,6 +132,30 @@ public class AutofillActivity extends AppCompatActivity { } }); + 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); + + // similar to what happens in ViewHolder.onClick but position -1 + DialogFragment df = new AutofillFragment(); + Bundle args = new Bundle(); + args.putString("packageName", packageName); + args.putString("appName", appName); + args.putInt("position", -1); + df.setArguments(args); + df.show(getFragmentManager(), "autofill_dialog"); + return false; + } + }); + setTitle("Autofill Apps"); } diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java new file mode 100644 index 00000000..e9aa86f0 --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java @@ -0,0 +1,82 @@ +package com.zeapo.pwdstore.autofill; + +import android.content.DialogInterface; +import android.support.v7.app.AlertDialog; +import android.app.Dialog; +import android.app.DialogFragment; +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.EditText; +import android.widget.RadioButton; +import android.widget.RadioGroup; + +import com.zeapo.pwdstore.R; + +public class AutofillFragment extends DialogFragment { + + public AutofillFragment() { + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + // this fragment is only created from the settings page (AutofillActivity) + // need to interact with the recyclerAdapter which is a member of activity + final AutofillActivity callingActivity = (AutofillActivity) getActivity(); + LayoutInflater inflater = callingActivity.getLayoutInflater(); + final View view = inflater.inflate(R.layout.fragment_autofill, null); + + builder.setView(view); + + final String packageName = getArguments().getString("packageName"); + String appName = getArguments().getString("appName"); + + builder.setTitle(appName); + SharedPreferences prefs + = getActivity().getApplicationContext().getSharedPreferences("autofill", Context.MODE_PRIVATE); + String preference = prefs.getString(packageName, "first"); + switch (preference) { + case "first": + ((RadioButton) view.findViewById(R.id.first)).toggle(); + break; + case "never": + ((RadioButton) view.findViewById(R.id.never)).toggle(); + break; + default: + ((RadioButton) view.findViewById(R.id.match)).toggle(); + ((EditText) view.findViewById(R.id.matched)).setText(preference); + } + + final SharedPreferences.Editor editor = prefs.edit(); + builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.autofill_radiogroup); + switch (radioGroup.getCheckedRadioButtonId()) { + case R.id.first: + editor.putString(packageName, "first"); + break; + case R.id.never: + editor.putString(packageName, "never"); + break; + default: + EditText matched = (EditText) view.findViewById(R.id.matched); + String path = matched.getText().toString(); + editor.putString(packageName, path); + } + editor.apply(); + int position = getArguments().getInt("position"); + if (position == -1) { + callingActivity.recyclerAdapter.add(packageName); + } else { + callingActivity.recyclerAdapter.notifyItemChanged(position); + } + } + }); + builder.setNegativeButton("Cancel", null); + return builder.create(); + } +} 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 b59223bd..eff94c51 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java @@ -1,7 +1,11 @@ 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.os.Bundle; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; @@ -16,29 +20,40 @@ import java.util.ArrayList; public class AutofillRecyclerAdapter extends RecyclerView.Adapter { private ArrayList apps; private PackageManager pm; + private AutofillActivity activity; public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public View view; public TextView name; + public TextView secondary; public ImageView icon; + public String packageName; public ViewHolder(View view) { super(view); this.view = view; name = (TextView) view.findViewById(R.id.app_name); + secondary = (TextView) view.findViewById(R.id.secondary_text); icon = (ImageView) view.findViewById(R.id.app_icon); view.setOnClickListener(this); } @Override public void onClick(View v) { - + DialogFragment df = new AutofillFragment(); + Bundle args = new Bundle(); + args.putString("packageName", packageName); + args.putString("appName", name.getText().toString()); + args.putInt("position", getAdapterPosition()); + df.setArguments(args); + df.show(activity.getFragmentManager(), "autofill_dialog"); } } - public AutofillRecyclerAdapter(ArrayList apps, PackageManager pm) { + public AutofillRecyclerAdapter(ArrayList apps, PackageManager pm, AutofillActivity activity) { this.apps = apps; this.pm = pm; + this.activity = activity; } @Override @@ -52,11 +67,45 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter + android:layout_height="match_parent" + android:focusable="true" + android:focusableInTouchMode="true"> + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/preference.xml b/app/src/main/res/xml/preference.xml index 3a8c4fdf..538f0387 100644 --- a/app/src/main/res/xml/preference.xml +++ b/app/src/main/res/xml/preference.xml @@ -74,7 +74,7 @@ -- cgit v1.2.3 From 1ceb41acb69f8cd780d44b438ff8db3410cbe225 Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Sun, 9 Aug 2015 22:05:20 -0400 Subject: Handle click events as well (useful when password field is the only focusable view) --- .../java/com/zeapo/pwdstore/autofill/AutofillFragment.java | 13 +++++++++++++ .../java/com/zeapo/pwdstore/autofill/AutofillService.java | 2 +- app/src/main/res/xml/autofill_config.xml | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java') 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 e9aa86f0..c1d75acd 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java @@ -1,6 +1,7 @@ package com.zeapo.pwdstore.autofill; import android.content.DialogInterface; +import android.content.Intent; import android.support.v7.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; @@ -13,6 +14,7 @@ import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; +import com.zeapo.pwdstore.PasswordStore; import com.zeapo.pwdstore.R; public class AutofillFragment extends DialogFragment { @@ -50,6 +52,17 @@ public class AutofillFragment extends DialogFragment { ((EditText) view.findViewById(R.id.matched)).setText(preference); } + View.OnClickListener matchPassword = new View.OnClickListener() { + @Override + public void onClick(View v) { + // TODO figure out UI for this + Intent intent = new Intent(getActivity(), PasswordStore.class); + startActivity(intent); + } + }; + view.findViewById(R.id.match).setOnClickListener(matchPassword); + view.findViewById(R.id.matched).setOnClickListener(matchPassword); + final SharedPreferences.Editor editor = prefs.edit(); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java index dbd018b6..4731f1d4 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java @@ -60,7 +60,7 @@ public class AutofillService extends AccessibilityService { serviceConnection.bindToService(); settings = PreferenceManager.getDefaultSharedPreferences(this); } - // TODO handle CLICKS and change search/search results (just use first result) + // TODO change search/search results (just use first result) @Override public void onAccessibilityEvent(AccessibilityEvent event) { // if returning to the source app from a successful AutofillActivity diff --git a/app/src/main/res/xml/autofill_config.xml b/app/src/main/res/xml/autofill_config.xml index 93125a51..36ef5aaa 100644 --- a/app/src/main/res/xml/autofill_config.xml +++ b/app/src/main/res/xml/autofill_config.xml @@ -1,6 +1,6 @@ Date: Sun, 9 Aug 2015 22:46:08 -0400 Subject: Fix recursive filter; don't act twice on focus+click event --- .../main/java/com/zeapo/pwdstore/autofill/AutofillService.java | 9 +++++++-- app/src/main/res/xml/autofill_config.xml | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java') diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java index 4731f1d4..ebe12c9c 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java @@ -83,8 +83,13 @@ public class AutofillService extends AccessibilityService { return; } - // if past this point, a new dialog will be created, so dismiss the existing if (dialog != null && dialog.isShowing()) { + // if the view was clicked, the click event follows the focus event + // since the focus event was already handled, ignore click event + if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_CLICKED) { + return; + } + // if past this point, a new dialog will be created, so dismiss the existing dialog.dismiss(); } @@ -145,7 +150,7 @@ public class AutofillService extends AccessibilityService { PasswordRepository.getPasswords(dir); for (PasswordItem item : passwordItems) { if (item.getType() == PasswordItem.TYPE_CATEGORY) { - recursiveFilter(filter, item.getFile()); + items.addAll(recursiveFilter(filter, item.getFile())); } if (item.toString().toLowerCase().contains(filter.toLowerCase())) { items.add(item); diff --git a/app/src/main/res/xml/autofill_config.xml b/app/src/main/res/xml/autofill_config.xml index 36ef5aaa..adf418e6 100644 --- a/app/src/main/res/xml/autofill_config.xml +++ b/app/src/main/res/xml/autofill_config.xml @@ -1,6 +1,6 @@ Date: Mon, 10 Aug 2015 13:11:42 -0400 Subject: More tweaks of dialog dismissal behaviour --- .../zeapo/pwdstore/autofill/AutofillActivity.java | 1 - .../autofill/AutofillPreferenceActivity.java | 4 ++++ .../zeapo/pwdstore/autofill/AutofillService.java | 27 +++++++++++++++++----- app/src/main/res/layout/autofill_recycler_view.xml | 2 +- app/src/main/res/xml/autofill_config.xml | 2 +- 5 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java (limited to 'app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java') diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java index 77a44c7a..2d538504 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java @@ -31,7 +31,6 @@ import java.util.Map; // blank activity started by service for calling startIntentSenderForResult public class AutofillActivity extends AppCompatActivity { public static final int REQUEST_CODE_DECRYPT_AND_VERIFY = 9913; - private boolean bound = false; private RecyclerView recyclerView; AutofillRecyclerAdapter recyclerAdapter; // let fragment have access diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java new file mode 100644 index 00000000..6f212149 --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java @@ -0,0 +1,4 @@ +package com.zeapo.pwdstore.autofill; + +public class AutofillPreferenceActivity { +} diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java index ebe12c9c..b30277ba 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java @@ -18,6 +18,7 @@ import android.util.Log; import android.view.WindowManager; import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.AccessibilityNodeInfo; +import android.view.accessibility.AccessibilityWindowInfo; import android.widget.Toast; import com.zeapo.pwdstore.R; @@ -43,6 +44,7 @@ public class AutofillService extends AccessibilityService { private AccessibilityNodeInfo info; // the original source of the event (the edittext field) private ArrayList items; // password choices private AlertDialog dialog; + private AccessibilityWindowInfo window; private static boolean unlockOK = false; // if openkeychain user interaction was successful private CharSequence packageName; private boolean ignoreActionFocus = false; @@ -73,11 +75,18 @@ public class AutofillService extends AccessibilityService { if (!event.isPassword() || Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2 || event.getPackageName().equals("org.sufficientlysecure.keychain")) { - // dismiss dialog if WINDOW_STATE_CHANGED unless the keyboard caused it + // the default keyboard showing/hiding is a window state changed event + // on Android 5+ we can use getWindows() to determine when the original window is not visible + // on Android 4.3 we have to use window state changed events and filter out the keyboard ones // there may be other exceptions... - if (!(event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED - && event.getPackageName().toString().contains("com.android.inputmethod")) - && dialog != null && dialog.isShowing()) { + boolean dismiss; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + dismiss = !getWindows().contains(window); + } else { + dismiss = !(event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED + && event.getPackageName().toString().contains("inputmethod")); + } + if (dismiss && dialog != null && dialog.isShowing()) { dialog.dismiss(); } return; @@ -99,8 +108,14 @@ public class AutofillService extends AccessibilityService { return; } - // get the app name and find a corresponding password info = event.getSource(); + + // save the dialog's corresponding window so we can use getWindows() above to check whether dismiss + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + window = info.getWindow(); + } + + // get the app name and find a corresponding password PackageManager packageManager = getPackageManager(); ApplicationInfo applicationInfo; try { @@ -134,10 +149,10 @@ public class AutofillService extends AccessibilityService { dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); + dialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT); } dialog.setTitle(items.get(0).toString()); dialog.show(); - dialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT); } private ArrayList recursiveFilter(String filter, File dir) { diff --git a/app/src/main/res/layout/autofill_recycler_view.xml b/app/src/main/res/layout/autofill_recycler_view.xml index 6130fd1a..836cbcf5 100644 --- a/app/src/main/res/layout/autofill_recycler_view.xml +++ b/app/src/main/res/layout/autofill_recycler_view.xml @@ -10,7 +10,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/app_search" - android:queryHint="Add an app to change its autofill setting" + android:queryHint="Add an app to change its setting" android:iconifiedByDefault="false"/> Date: Mon, 10 Aug 2015 16:58:28 -0400 Subject: Open settings from password fill dialog --- .../zeapo/pwdstore/autofill/AutofillActivity.java | 5 ++- .../autofill/AutofillPreferenceActivity.java | 40 ++++++++++++++-------- .../pwdstore/autofill/AutofillRecyclerAdapter.java | 26 ++++++-------- .../zeapo/pwdstore/autofill/AutofillService.java | 8 +++-- app/src/main/res/layout/app_list_item.xml | 9 ++--- 5 files changed, 49 insertions(+), 39 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java') diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java index 5a097d07..d8e50ebd 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java @@ -14,12 +14,11 @@ public class AutofillActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - Intent intent = getIntent(); - Bundle extras = intent.getExtras(); + Bundle extras = getIntent().getExtras(); if (extras != null) { try { - PendingIntent pi = intent.getExtras().getParcelable("pending_intent"); + PendingIntent pi = extras.getParcelable("pending_intent"); if (pi == null) { return; } 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 9dac9a07..89d3474e 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java @@ -8,7 +8,6 @@ import android.content.pm.PackageManager; import android.database.Cursor; import android.database.MatrixCursor; import android.os.Bundle; -import android.os.PersistableBundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; @@ -22,6 +21,8 @@ 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; @@ -57,6 +58,12 @@ public class AutofillPreferenceActivity extends AppCompatActivity { editor.remove(packageName).apply(); } } + Collections.sort(apps, new Comparator() { + @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); @@ -95,9 +102,7 @@ public class AutofillPreferenceActivity extends AppCompatActivity { // should be a better/faster way to do this? MatrixCursor matrixCursor = new MatrixCursor(new String[]{"_id", "package", "label"}); for (ApplicationInfo applicationInfo : allApps) { - // exclude apps that already have settings; the search is just for adding - if (applicationInfo.loadLabel(pm).toString().toLowerCase().contains(newText.toLowerCase()) - && !recyclerAdapter.contains(applicationInfo.packageName)) { + if (applicationInfo.loadLabel(pm).toString().toLowerCase().contains(newText.toLowerCase())) { matrixCursor.addRow(new Object[]{0, applicationInfo.packageName, applicationInfo.loadLabel(pm)}); } } @@ -121,19 +126,26 @@ public class AutofillPreferenceActivity extends AppCompatActivity { Cursor cursor = searchView.getSuggestionsAdapter().getCursor(); String packageName = cursor.getString(1); String appName = cursor.getString(2); - - // similar to what happens in ViewHolder.onClick but position -1 - DialogFragment df = new AutofillFragment(); - Bundle args = new Bundle(); - args.putString("packageName", packageName); - args.putString("appName", appName); - args.putInt("position", -1); - df.setArguments(args); - df.show(getFragmentManager(), "autofill_dialog"); - return false; + 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"); } } 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 b43f5d6e..26680152 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java @@ -40,13 +40,7 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter + android:paddingRight="8dp" + android:gravity="center_vertical"> + android:layout_width="24dp" + android:layout_height="24dp"/> Date: Tue, 11 Aug 2015 18:32:16 -0400 Subject: Add 'automatically match by default' setting; respect it & per-app settings when creating autofill dialog --- .../zeapo/pwdstore/autofill/AutofillFragment.java | 9 +++++++-- .../autofill/AutofillPreferenceActivity.java | 4 ++++ .../pwdstore/autofill/AutofillRecyclerAdapter.java | 7 ++++++- .../com/zeapo/pwdstore/autofill/AutofillService.java | 20 ++++++++++++++++++-- app/src/main/res/xml/preference.xml | 5 +++++ 5 files changed, 40 insertions(+), 5 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java') 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 3e00edfb..ea4b0a2f 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java @@ -3,6 +3,7 @@ package com.zeapo.pwdstore.autofill; import android.app.Activity; import android.content.DialogInterface; import android.content.Intent; +import android.preference.PreferenceManager; import android.support.v7.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; @@ -39,9 +40,14 @@ public class AutofillFragment extends DialogFragment { String appName = getArguments().getString("appName"); builder.setTitle(appName); + + // when an app is added for the first time, the radio button selection should reflect + // the autofill_default setting: hence, defValue + SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(callingActivity); + String defValue = settings.getBoolean("autofill_default", true) ? "first" : "never"; SharedPreferences prefs = getActivity().getApplicationContext().getSharedPreferences("autofill", Context.MODE_PRIVATE); - String preference = prefs.getString(packageName, "first"); + String preference = prefs.getString(packageName, defValue); switch (preference) { case "first": ((RadioButton) view.findViewById(R.id.first)).toggle(); @@ -57,7 +63,6 @@ public class AutofillFragment extends DialogFragment { View.OnClickListener matchPassword = new View.OnClickListener() { @Override public void onClick(View v) { - // TODO figure out UI for this Intent intent = new Intent(getActivity(), PasswordStore.class); intent.putExtra("matchWith", true); startActivityForResult(intent, MATCH_WITH); 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 89d3474e..10684f45 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java @@ -8,6 +8,7 @@ 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; @@ -100,6 +101,7 @@ public class AutofillPreferenceActivity extends AppCompatActivity { @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())) { @@ -147,5 +149,7 @@ public class AutofillPreferenceActivity extends AppCompatActivity { 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? } } 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 1b5e63b7..bd2835e5 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java @@ -4,6 +4,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; +import android.preference.PreferenceManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; @@ -59,9 +60,13 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter(); + items.add(PasswordItem.newPassword(file.getName(), file)); + } if (items.isEmpty()) { return; } diff --git a/app/src/main/res/xml/preference.xml b/app/src/main/res/xml/preference.xml index 9a42bb3e..44e38916 100644 --- a/app/src/main/res/xml/preference.xml +++ b/app/src/main/res/xml/preference.xml @@ -76,6 +76,11 @@ android:key="autofill_apps" android:summary="Customize autofill settings for specific apps." android:title="Per-app settings"/> + -- cgit v1.2.3 From 29b92f4a6d1dacac3e0a1154972e2659b3a536be Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Wed, 12 Aug 2015 21:17:15 -0400 Subject: Make service compatible with latest PasswordRepository changes --- .../zeapo/pwdstore/autofill/AutofillService.java | 42 +++------------------- 1 file changed, 4 insertions(+), 38 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java') diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java index 2a80a449..bb3b17f2 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java @@ -22,7 +22,6 @@ import android.view.accessibility.AccessibilityWindowInfo; import android.widget.Toast; import com.zeapo.pwdstore.R; -import com.zeapo.pwdstore.UserPreference; import com.zeapo.pwdstore.utils.PasswordItem; import com.zeapo.pwdstore.utils.PasswordRepository; @@ -139,7 +138,7 @@ public class AutofillService extends AccessibilityService { String path = PasswordRepository.getWorkTree() + "/" + preference + ".gpg"; File file = new File(path); items = new ArrayList<>(); - items.add(PasswordItem.newPassword(file.getName(), file)); + items.add(PasswordItem.newPassword(file.getName(), file, PasswordRepository.getRepositoryDirectory(this))); } if (items.isEmpty()) { return; @@ -178,11 +177,11 @@ public class AutofillService extends AccessibilityService { private ArrayList recursiveFilter(String filter, File dir) { ArrayList items = new ArrayList<>(); if (!PasswordRepository.isInitialized()) { - initialize(); + PasswordRepository.initialize(this); } ArrayList passwordItems = dir == null ? - PasswordRepository.getPasswords() : - PasswordRepository.getPasswords(dir); + PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(this)) : + PasswordRepository.getPasswords(dir, PasswordRepository.getRepositoryDirectory(this)); for (PasswordItem item : passwordItems) { if (item.getType() == PasswordItem.TYPE_CATEGORY) { items.addAll(recursiveFilter(filter, item.getFile())); @@ -194,39 +193,6 @@ public class AutofillService extends AccessibilityService { return items; } - // just like PasswordRepository.initialize(). - private void initialize() { - File dir = null; - SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); - - if (settings.getBoolean("git_external", false)) { - if (settings.getString("git_external_repo", null) != null) { - dir = new File(settings.getString("git_external_repo", null)); - } - } else { - dir = new File(getFilesDir() + "/store"); - } - // temp for debug - if (dir == null) { - Intent intent = new Intent(this, UserPreference.class); - intent.putExtra("operation", "git_external"); - startActivity(intent); - return; - } - - // uninitialize the repo if the dir does not exist or is absolutely empty - if (!dir.exists() || !dir.isDirectory() || FileUtils.listFiles(dir, null, false).isEmpty()) { - settings.edit().putBoolean("repository_initialized", false).apply(); - } - - if (!PasswordRepository.getPasswords(dir).isEmpty()) { - settings.edit().putBoolean("repository_initialized", true).apply(); - } - - // create the repository static variable in PasswordRepository - PasswordRepository.getRepository(new File(dir.getAbsolutePath() + "/.git")); - } - @Override public void onInterrupt() { -- cgit v1.2.3 From ec07e1eea69a69e9579ddfb44fdbee3535bcf6ad Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Thu, 13 Aug 2015 15:18:47 -0400 Subject: Preference activity overhaul --- .../zeapo/pwdstore/autofill/AutofillFragment.java | 31 +++-- .../autofill/AutofillPreferenceActivity.java | 122 ++++++------------- .../pwdstore/autofill/AutofillRecyclerAdapter.java | 132 ++++----------------- .../zeapo/pwdstore/autofill/AutofillService.java | 3 +- app/src/main/res/layout/autofill_recycler_view.xml | 11 +- app/src/main/res/layout/autofill_row_layout.xml | 5 +- app/src/main/res/layout/fragment_autofill.xml | 15 ++- app/src/main/res/menu/autofill_preference.xml | 12 ++ app/src/main/res/xml/preference.xml | 2 +- 9 files changed, 108 insertions(+), 225 deletions(-) create mode 100644 app/src/main/res/menu/autofill_preference.xml (limited to 'app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java') 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 ea4b0a2f..c2a30ffe 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java @@ -3,7 +3,7 @@ package com.zeapo.pwdstore.autofill; import android.app.Activity; import android.content.DialogInterface; import android.content.Intent; -import android.preference.PreferenceManager; +import android.content.pm.PackageManager; import android.support.v7.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; @@ -40,15 +40,20 @@ public class AutofillFragment extends DialogFragment { String appName = getArguments().getString("appName"); builder.setTitle(appName); + try { + // since we can't (easily?) pass the drawable as an argument + builder.setIcon(callingActivity.getPackageManager().getApplicationIcon(packageName)); + } catch (PackageManager.NameNotFoundException e) { + e.printStackTrace(); + } - // when an app is added for the first time, the radio button selection should reflect - // the autofill_default setting: hence, defValue - SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(callingActivity); - String defValue = settings.getBoolean("autofill_default", true) ? "first" : "never"; SharedPreferences prefs = getActivity().getApplicationContext().getSharedPreferences("autofill", Context.MODE_PRIVATE); - String preference = prefs.getString(packageName, defValue); + String preference = prefs.getString(packageName, ""); switch (preference) { + case "": + ((RadioButton) view.findViewById(R.id.use_default)).toggle(); + break; case "first": ((RadioButton) view.findViewById(R.id.first)).toggle(); break; @@ -77,6 +82,9 @@ public class AutofillFragment extends DialogFragment { public void onClick(DialogInterface dialog, int which) { RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.autofill_radiogroup); switch (radioGroup.getCheckedRadioButtonId()) { + case R.id.use_default: + editor.remove(packageName); + break; case R.id.first: editor.putString(packageName, "first"); break; @@ -90,10 +98,10 @@ public class AutofillFragment extends DialogFragment { } editor.apply(); int position = getArguments().getInt("position"); - if (position == -1) { - callingActivity.recyclerAdapter.add(packageName); - } else { - callingActivity.recyclerAdapter.notifyItemChanged(position); + callingActivity.recyclerAdapter.notifyItemChanged(position); + + if (getArguments().getBoolean("finish")) { + callingActivity.finish(); } } }); @@ -105,7 +113,8 @@ public class AutofillFragment extends DialogFragment { public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { ((EditText) getDialog().findViewById(R.id.matched)).setText(data.getStringExtra("path")); + } else { + ((RadioButton) getDialog().findViewById(R.id.use_default)).toggle(); } - } } 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 abf1f320..91131956 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java @@ -1,22 +1,17 @@ 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.Intent; import android.content.pm.PackageManager; -import android.database.Cursor; -import android.database.MatrixCursor; +import android.content.pm.ResolveInfo; import android.os.Bundle; +import android.support.v4.view.MenuItemCompat; 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 android.support.v7.widget.SearchView; +import android.view.Menu; +import android.view.MenuItem; import com.zeapo.pwdstore.R; @@ -24,7 +19,6 @@ 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 { @@ -43,102 +37,62 @@ public class AutofillPreferenceActivity extends AppCompatActivity { layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST)); - - // apps for which the user has custom settings should be in the recycler + + Intent intent = new Intent(Intent.ACTION_MAIN); + intent.addCategory(Intent.CATEGORY_LAUNCHER); + List allApps = getPackageManager().queryIntentActivities(intent, 0); final PackageManager pm = getPackageManager(); - SharedPreferences prefs - = getSharedPreferences("autofill", Context.MODE_PRIVATE); - Map prefApps = prefs.getAll(); - ArrayList 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() { + Collections.sort(allApps, new Comparator() { @Override - public int compare(ApplicationInfo lhs, ApplicationInfo rhs) { + public int compare(ResolveInfo lhs, ResolveInfo rhs) { return lhs.loadLabel(pm).toString().compareTo(rhs.loadLabel(pm).toString()); } }); - recyclerAdapter = new AutofillRecyclerAdapter(apps, pm, this); + recyclerAdapter = new AutofillRecyclerAdapter(new ArrayList<>(allApps), 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(); + setTitle("Autofill Apps"); - // 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; - } - }; + Bundle extras = getIntent().getExtras(); + if (extras != null) { + recyclerView.scrollToPosition(recyclerAdapter.getPosition(extras.getString("packageName"))); + showDialog(extras.getString("packageName"), extras.getString("appName")); + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.autofill_preference, menu); + MenuItem searchItem = menu.findItem(R.id.action_search); + SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); - final List allApps = pm.getInstalledApplications(0); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override - public boolean onQueryTextSubmit(String query) { - return false; + public boolean onQueryTextSubmit(String s) { + return true; } @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; + public boolean onQueryTextChange(String s) { + return true; } }); - searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() { + // 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 onSuggestionSelect(int position) { - return false; + public boolean onMenuItemActionCollapse(MenuItem item) { + return true; } @Override - public boolean onSuggestionClick(int position) { - Cursor cursor = searchView.getSuggestionsAdapter().getCursor(); - String packageName = cursor.getString(1); - String appName = cursor.getString(2); - showDialog(packageName, appName); + public boolean onMenuItemActionExpand(MenuItem item) { return true; } }); - - setTitle("Autofill Apps"); - - Bundle extras = getIntent().getExtras(); - if (extras != null) { - showDialog(extras.getString("packageName"), extras.getString("appName")); - } + return super.onCreateOptionsMenu(menu); } public void showDialog(String packageName, String appName) { 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 14530879..7c735f18 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java @@ -2,14 +2,11 @@ package com.zeapo.pwdstore.autofill; import android.content.Context; import android.content.SharedPreferences; -import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; -import android.preference.PreferenceManager; +import android.content.pm.ResolveInfo; import android.support.v7.view.ActionMode; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; @@ -18,19 +15,14 @@ import android.widget.TextView; import com.zeapo.pwdstore.R; import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.Set; -import java.util.TreeSet; public class AutofillRecyclerAdapter extends RecyclerView.Adapter { - private ArrayList apps; + private ArrayList apps; private PackageManager pm; private AutofillPreferenceActivity activity; - private final Set selectedItems; private ActionMode actionMode; - public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { + public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public View view; public TextView name; public TextView secondary; @@ -44,38 +36,19 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter apps, PackageManager pm, AutofillPreferenceActivity activity) { + public AutofillRecyclerAdapter(ArrayList apps, PackageManager pm, AutofillPreferenceActivity activity) { this.apps = apps; this.pm = pm; this.activity = activity; - this.selectedItems = new TreeSet<>(Collections.reverseOrder()); } @Override @@ -87,30 +60,33 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter - - + android:layout_height="match_parent"> + android:id="@+id/secondary_text" + android:textColor="@color/grey_600"/> diff --git a/app/src/main/res/layout/fragment_autofill.xml b/app/src/main/res/layout/fragment_autofill.xml index 6ad44a0d..e5534f41 100644 --- a/app/src/main/res/layout/fragment_autofill.xml +++ b/app/src/main/res/layout/fragment_autofill.xml @@ -13,11 +13,18 @@ android:layout_height="wrap_content" android:id="@+id/autofill_radiogroup" > + @@ -29,7 +36,7 @@ android:id="@+id/match" android:layout_gravity="center_vertical" android:checked="false" - android:layout_marginTop="8dp"/> + /> + /> diff --git a/app/src/main/res/menu/autofill_preference.xml b/app/src/main/res/menu/autofill_preference.xml new file mode 100644 index 00000000..77ce95f4 --- /dev/null +++ b/app/src/main/res/menu/autofill_preference.xml @@ -0,0 +1,12 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/preference.xml b/app/src/main/res/xml/preference.xml index 44e38916..e8ba80cc 100644 --- a/app/src/main/res/xml/preference.xml +++ b/app/src/main/res/xml/preference.xml @@ -79,7 +79,7 @@ -- cgit v1.2.3 From ac533d83aab29005f70c97ed939a6d6f2e13aa16 Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Thu, 13 Aug 2015 21:16:10 -0400 Subject: Preference activity up & back behaviour. Especially when opened with 'Settings' dialog button --- app/src/main/AndroidManifest.xml | 8 +++---- .../autofill/AutofillPreferenceActivity.java | 26 ++++++++++++++++++++-- .../zeapo/pwdstore/autofill/AutofillService.java | 2 +- 3 files changed, 29 insertions(+), 7 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a6ffa791..ecd106cd 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -54,18 +54,18 @@ android:resource="@xml/autofill_config" /> - + + android:value="com.zeapo.pwdstore.PasswordStore" /> - + + android:value="com.zeapo.pwdstore.PasswordStore" /> 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 91131956..6f507fa5 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillPreferenceActivity.java @@ -5,6 +5,8 @@ import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Bundle; +import android.support.v4.app.NavUtils; +import android.support.v4.app.TaskStackBuilder; import android.support.v4.view.MenuItemCompat; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; @@ -26,11 +28,12 @@ public class AutofillPreferenceActivity extends AppCompatActivity { AutofillRecyclerAdapter recyclerAdapter; // let fragment have access private RecyclerView.LayoutManager layoutManager; + private boolean recreate; + @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); @@ -53,8 +56,10 @@ public class AutofillPreferenceActivity extends AppCompatActivity { setTitle("Autofill Apps"); + 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")); } @@ -94,6 +99,24 @@ public class AutofillPreferenceActivity extends AppCompatActivity { }); return super.onCreateOptionsMenu(menu); } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + // in service, we CLEAR_TASK. then we set the recreate flag. + // something of a hack, but w/o CLEAR_TASK, behaviour was unpredictable + case android.R.id.home: + Intent upIntent = NavUtils.getParentActivityIntent(this); + if (recreate) { + TaskStackBuilder.create(this) + .addNextIntentWithParentStack(upIntent) + .startActivities(); + } else { + NavUtils.navigateUpTo(this, upIntent); + } + return true; + } + return super.onOptionsItemSelected(item); + } public void showDialog(String packageName, String appName) { DialogFragment df = new AutofillFragment(); @@ -103,6 +126,5 @@ public class AutofillPreferenceActivity extends AppCompatActivity { 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? } } diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java index 1b8ff740..688e22fc 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java @@ -158,7 +158,7 @@ public class AutofillService extends AccessibilityService { public void onClick(DialogInterface dialog, int which) { // the user will have to return to the app themselves. Intent intent = new Intent(AutofillService.this, AutofillPreferenceActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.putExtra("packageName", info.getPackageName()); intent.putExtra("appName", appName); startActivity(intent); -- cgit v1.2.3 From b1807197f85c4d6ceccc3ca23654bb184f9caa13 Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Fri, 14 Aug 2015 14:12:02 -0400 Subject: Fix crash on first start & use documentLaunchMode for nicer openkeychain-api behaviour (5+ only) --- app/src/main/AndroidManifest.xml | 4 +++- .../java/com/zeapo/pwdstore/autofill/AutofillService.java | 12 +++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ecd106cd..0789fd33 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -55,7 +55,9 @@ + android:parentActivityName=".PasswordStore" + android:documentLaunchMode="intoExisting" + android:excludeFromRecents="true"> diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java index 688e22fc..1e64e8c8 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java @@ -130,11 +130,17 @@ public class AutofillService extends AccessibilityService { String preference = prefs.getString(event.getPackageName().toString(), defValue); switch (preference) { case "first": + if (!PasswordRepository.isInitialized()) { + PasswordRepository.initialize(this); + } items = recursiveFilter(appName, null); break; case "never": return; default: + if (!PasswordRepository.isInitialized()) { + PasswordRepository.initialize(this); + } String path = PasswordRepository.getWorkTree() + "/" + preference + ".gpg"; File file = new File(path); items = new ArrayList<>(); @@ -177,9 +183,6 @@ public class AutofillService extends AccessibilityService { private ArrayList recursiveFilter(String filter, File dir) { ArrayList items = new ArrayList<>(); - if (!PasswordRepository.isInitialized()) { - PasswordRepository.initialize(this); - } ArrayList passwordItems = dir == null ? PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(this)) : PasswordRepository.getPasswords(dir, PasswordRepository.getRepositoryDirectory(this)); @@ -252,8 +255,7 @@ public class AutofillService extends AccessibilityService { PendingIntent pi = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT); // need to start a blank activity to call startIntentSenderForResult Intent intent = new Intent(AutofillService.this, AutofillActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK - | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.putExtra("pending_intent", pi); startActivity(intent); break; -- cgit v1.2.3 From ebe1f831e7659b63b76c2b39498b68d1fec61673 Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Fri, 14 Aug 2015 16:51:39 -0400 Subject: strings & enable service preference --- .../java/com/zeapo/pwdstore/UserPreference.java | 51 +++++++++++++++++++++- .../zeapo/pwdstore/autofill/AutofillFragment.java | 5 ++- .../pwdstore/autofill/AutofillRecyclerAdapter.java | 4 +- .../zeapo/pwdstore/autofill/AutofillService.java | 6 +-- app/src/main/res/layout/autofill_row_layout.xml | 2 - app/src/main/res/layout/fragment_autofill.xml | 8 ++-- app/src/main/res/values-cs/strings.xml | 2 +- app/src/main/res/values/strings.xml | 15 ++++++- app/src/main/res/xml/preference.xml | 14 +++--- 9 files changed, 85 insertions(+), 22 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java') diff --git a/app/src/main/java/com/zeapo/pwdstore/UserPreference.java b/app/src/main/java/com/zeapo/pwdstore/UserPreference.java index cb2d2881..283d5f47 100644 --- a/app/src/main/java/com/zeapo/pwdstore/UserPreference.java +++ b/app/src/main/java/com/zeapo/pwdstore/UserPreference.java @@ -1,17 +1,22 @@ package com.zeapo.pwdstore; -import android.app.AlertDialog; +import android.accessibilityservice.AccessibilityServiceInfo; import android.app.DialogFragment; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; +import android.preference.CheckBoxPreference; import android.preference.Preference; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; +import android.provider.Settings; +import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; +import android.view.accessibility.AccessibilityManager; import android.widget.Toast; import com.google.common.base.Function; @@ -33,6 +38,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.HashSet; +import java.util.List; import java.util.Set; public class UserPreference extends AppCompatActivity { @@ -192,6 +198,30 @@ public class UserPreference extends AppCompatActivity { return true; } }); + + findPreference("autofill_enable").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + new AlertDialog.Builder(callingActivity). + setTitle(R.string.pref_autofill_enable_title). + setMessage(R.string.pref_autofill_enable_msg). + setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS); + startActivity(intent); + } + }). + setNegativeButton(R.string.dialog_cancel,new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + ((CheckBoxPreference) findPreference("autofill_enable")) + .setChecked(((UserPreference) getActivity()).isServiceEnabled()); + } + }).show(); + return false; + } + }); } @Override @@ -199,6 +229,10 @@ public class UserPreference extends AppCompatActivity { super.onStart(); final SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences(); findPreference("ssh_see_key").setEnabled(sharedPreferences.getBoolean("use_generated_key", false)); + + // see if the autofill service is enabled and check the preference accordingly + ((CheckBoxPreference) findPreference("autofill_enable")) + .setChecked(((UserPreference) getActivity()).isServiceEnabled()); } } @@ -278,6 +312,21 @@ public class UserPreference extends AppCompatActivity { sshKey.close(); } + // Returns whether the autofill service is enabled + private boolean isServiceEnabled() { + AccessibilityManager am = (AccessibilityManager) this + .getSystemService(Context.ACCESSIBILITY_SERVICE); + List runningServices = am + .getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_GENERIC); + for (AccessibilityServiceInfo service : runningServices) { + if ("com.zeapo.pwdstore/.autofill.AutofillService".equals(service.getId())) { + return true; + } + } + return false; + } + + protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { 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 c2a30ffe..8cf58206 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java @@ -68,6 +68,7 @@ public class AutofillFragment extends DialogFragment { View.OnClickListener matchPassword = new View.OnClickListener() { @Override public void onClick(View v) { + ((RadioButton) view.findViewById(R.id.match)).toggle(); Intent intent = new Intent(getActivity(), PasswordStore.class); intent.putExtra("matchWith", true); startActivityForResult(intent, MATCH_WITH); @@ -77,7 +78,7 @@ public class AutofillFragment extends DialogFragment { view.findViewById(R.id.matched).setOnClickListener(matchPassword); final SharedPreferences.Editor editor = prefs.edit(); - builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { + builder.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.autofill_radiogroup); @@ -105,7 +106,7 @@ public class AutofillFragment extends DialogFragment { } } }); - builder.setNegativeButton("Cancel", null); + builder.setNegativeButton(R.string.dialog_cancel, null); return builder.create(); } 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 01916650..8971c0af 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java @@ -105,10 +105,10 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter - - diff --git a/app/src/main/res/layout/fragment_autofill.xml b/app/src/main/res/layout/fragment_autofill.xml index e5534f41..af2b7017 100644 --- a/app/src/main/res/layout/fragment_autofill.xml +++ b/app/src/main/res/layout/fragment_autofill.xml @@ -16,7 +16,7 @@ @@ -24,7 +24,7 @@ @@ -32,7 +32,7 @@ Zpráva : \n Rekurzivní filtrování Rekurzivní hledání hesel v aktuálním adresáři. + Zaplnit schránku 20krát Uložit dvacet náhodných textů do schránky namísto pouze jednoho. Užitečné pro telefony Samsug, které nabízejí funkci historie schránky. - Zaplnit schránku 20krát Generovat diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index fa5358e2..447139fb 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -117,8 +117,14 @@ Message : \n Recursive filtering Recursively find passwords of the current directory. + Enable autofill + Tap OK to go to Accessibility settings. Once there, tap Password Store under Services then tap the switch in the top right to turn it on or off. + Per-app settings + Customize autofill settings for specific apps. + Automatically match by default + Default to \'Automatically match\' for apps without custom settings. Otherwise, \'Never match.\' + Clear clipboard 20 times Store nonsense in the clipboard 20 times instead of just once. Useful on Samsung phones that feature clipboard history. - Clear clipboard 20 times Generate @@ -155,5 +161,10 @@ "Category: " - Auto-fills password fields in apps. Only works for Android versions 4.3 and up. Does not rely on the clipboard for Android versions 5.0 and up. + Autofills password fields in apps. Only works for Android versions 4.3 and up. Does not rely on the clipboard for Android versions 5.0 and up. + Fill + Use default setting + Automatically match + Match with… + Never match diff --git a/app/src/main/res/xml/preference.xml b/app/src/main/res/xml/preference.xml index e8ba80cc..1c977992 100644 --- a/app/src/main/res/xml/preference.xml +++ b/app/src/main/res/xml/preference.xml @@ -72,15 +72,19 @@ + + android:summary="@string/pref_autofill_apps_hint" + android:title="@string/pref_autofill_apps_title"/> + android:summary="@string/pref_autofill_default_hint" + android:title="@string/pref_autofill_default_title"/> @@ -88,6 +92,6 @@ android:defaultValue="false" android:key="clear_clipboard_20x" android:summary="@string/pref_clear_clipboard_hint" - android:title="@string/pref_clear_clipboard" /> + android:title="@string/pref_clear_clipboard_title" /> \ No newline at end of file -- cgit v1.2.3 From 640f6ff9bd2645382323b9375c57af11e5931d3b Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Sat, 15 Aug 2015 13:00:12 -0400 Subject: Prepend first/never keywords with / . file names won't begin with / --- .../main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java | 8 ++++---- .../java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java | 4 ++-- .../main/java/com/zeapo/pwdstore/autofill/AutofillService.java | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java') 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 8cf58206..e708c19d 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java @@ -54,10 +54,10 @@ public class AutofillFragment extends DialogFragment { case "": ((RadioButton) view.findViewById(R.id.use_default)).toggle(); break; - case "first": + case "/first": ((RadioButton) view.findViewById(R.id.first)).toggle(); break; - case "never": + case "/never": ((RadioButton) view.findViewById(R.id.never)).toggle(); break; default: @@ -87,10 +87,10 @@ public class AutofillFragment extends DialogFragment { editor.remove(packageName); break; case R.id.first: - editor.putString(packageName, "first"); + editor.putString(packageName, "/first"); break; case R.id.never: - editor.putString(packageName, "never"); + editor.putString(packageName, "/never"); break; default: EditText matched = (EditText) view.findViewById(R.id.matched); 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 8971c0af..a131598a 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java @@ -104,10 +104,10 @@ public class AutofillRecyclerAdapter extends RecyclerView.Adapter