From 02bfcf6c3f5ddd806dd9a92823926eced139842f Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Tue, 28 Jul 2015 03:11:02 -0400 Subject: Create app autofill service: a dialog pops up for all password fields & has a button to paste/set a password found in the store with name matching app's --- app/src/main/res/xml/autofill_config.xml | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 app/src/main/res/xml/autofill_config.xml (limited to 'app/src/main/res/xml') diff --git a/app/src/main/res/xml/autofill_config.xml b/app/src/main/res/xml/autofill_config.xml new file mode 100644 index 00000000..618442ab --- /dev/null +++ b/app/src/main/res/xml/autofill_config.xml @@ -0,0 +1,8 @@ + \ No newline at end of file -- cgit v1.2.3 From 8c266187cb1a1264034fe2637d3b22e67284b0b5 Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Tue, 28 Jul 2015 09:15:41 -0400 Subject: Fill in field immediately after user unlocks --- .../java/com/zeapo/pwdstore/AutofillActivity.java | 6 +- .../java/com/zeapo/pwdstore/AutofillService.java | 65 ++++++++++++---------- app/src/main/res/xml/autofill_config.xml | 2 +- 3 files changed, 40 insertions(+), 33 deletions(-) (limited to 'app/src/main/res/xml') diff --git a/app/src/main/java/com/zeapo/pwdstore/AutofillActivity.java b/app/src/main/java/com/zeapo/pwdstore/AutofillActivity.java index f52fe0a2..2a1227b9 100644 --- a/app/src/main/java/com/zeapo/pwdstore/AutofillActivity.java +++ b/app/src/main/java/com/zeapo/pwdstore/AutofillActivity.java @@ -7,7 +7,7 @@ import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; - +// 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; @@ -27,9 +27,9 @@ public class AutofillActivity extends AppCompatActivity { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { - finish(); + finish(); // go back to the password field app if (resultCode == RESULT_OK) { - AutofillService.getService().decryptAndVerify(); + AutofillService.setUnlockOK(); // report the result to service } } } diff --git a/app/src/main/java/com/zeapo/pwdstore/AutofillService.java b/app/src/main/java/com/zeapo/pwdstore/AutofillService.java index ca738c67..58bb0994 100644 --- a/app/src/main/java/com/zeapo/pwdstore/AutofillService.java +++ b/app/src/main/java/com/zeapo/pwdstore/AutofillService.java @@ -40,29 +40,40 @@ 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; - private static AutofillService service; + private ArrayList items; // password choices + private AlertDialog dialog; + private static boolean unlockOK = false; // if openkeychain user interaction was successful + private static CharSequence packageName; + private static 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); - service = this; - } - - public static AutofillService getService() { - return service; } @Override public void onAccessibilityEvent(AccessibilityEvent event) { - if (!event.isPassword() || Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) { + 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 + || (dialog != null && dialog.isShowing()) + || event.getPackageName().equals("org.sufficientlysecure.keychain")) { + return; + } + if (ignoreActionFocus) { + ignoreActionFocus = false; return; } info = event.getSource(); @@ -74,29 +85,23 @@ public class AutofillService extends AccessibilityService { applicationInfo = null; } String appName = (applicationInfo != null ? packageManager.getApplicationLabel(applicationInfo) : "").toString(); - if (appName.equals("OpenKeychain")) { - return; - } items = recursiveFilter(appName, null); if (items.isEmpty()) { return; } - ArrayList itemNames = new ArrayList<>(); - for (PasswordItem item : items) { - itemNames.add(item.toString()); - } - AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.Theme_AppCompat_Light_Dialog_Alert); - builder.setNegativeButton("Cancel", null); - builder.setView(R.layout.autofill_layout); - final AlertDialog dialog = builder.create(); - - dialog.setTitle("Fill with Password Store"); - dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); - dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); - dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); + if (dialog == null) { + AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.Theme_AppCompat_Light_Dialog_Alert); + builder.setNegativeButton("Cancel", null); + builder.setView(R.layout.autofill_layout); + dialog = builder.create(); + dialog.setTitle("Fill with Password Store"); + 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.show(); - ((Button) dialog.findViewById(R.id.button)).setText(itemNames.get(0).toString()); + ((Button) dialog.findViewById(R.id.button)).setText(items.get(0).getName()); dialog.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -131,6 +136,8 @@ public class AutofillService extends AccessibilityService { } public void decryptAndVerify() { + unlockOK = false; + packageName = info.getPackageName(); Intent data = new Intent(); data.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY); InputStream is = null; @@ -147,16 +154,16 @@ public class AutofillService extends AccessibilityService { 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 = true; + info.performAction(AccessibilityNodeInfo.ACTION_FOCUS); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - // if the user focused on something else, take focus back - // but this will open another dialog... - info.performAction(AccessibilityNodeInfo.ACTION_FOCUS); Bundle args = new Bundle(); args.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, passContent[0]); info.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, args); } else { - info.performAction(AccessibilityNodeInfo.ACTION_FOCUS); ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText("autofill_pm", passContent[0]); clipboard.setPrimaryClip(clip); diff --git a/app/src/main/res/xml/autofill_config.xml b/app/src/main/res/xml/autofill_config.xml index 618442ab..93125a51 100644 --- a/app/src/main/res/xml/autofill_config.xml +++ b/app/src/main/res/xml/autofill_config.xml @@ -1,6 +1,6 @@ Date: Fri, 31 Jul 2015 09:00:06 -0400 Subject: Add per-app settings page --- app/src/main/AndroidManifest.xml | 4 +- .../java/com/zeapo/pwdstore/AutofillActivity.java | 35 --- .../java/com/zeapo/pwdstore/AutofillService.java | 246 -------------------- .../java/com/zeapo/pwdstore/UserPreference.java | 10 + .../zeapo/pwdstore/autofill/AutofillActivity.java | 84 +++++++ .../pwdstore/autofill/AutofillRecyclerAdapter.java | 62 +++++ .../zeapo/pwdstore/autofill/AutofillService.java | 254 +++++++++++++++++++++ app/src/main/res/layout/autofill_recycler_view.xml | 12 + app/src/main/res/layout/autofill_row_layout.xml | 49 ++++ app/src/main/res/xml/preference.xml | 8 + 10 files changed, 481 insertions(+), 283 deletions(-) delete mode 100644 app/src/main/java/com/zeapo/pwdstore/AutofillActivity.java delete mode 100644 app/src/main/java/com/zeapo/pwdstore/AutofillService.java create mode 100644 app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java create mode 100644 app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java create mode 100644 app/src/main/java/com/zeapo/pwdstore/autofill/AutofillService.java create mode 100644 app/src/main/res/layout/autofill_recycler_view.xml create mode 100644 app/src/main/res/layout/autofill_row_layout.xml (limited to 'app/src/main/res/xml') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d45583c9..b3600c53 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -45,7 +45,7 @@ android:value="com.zeapo.pwdstore.PasswordStore" /> - @@ -54,7 +54,7 @@ android:resource="@xml/autofill_config" /> - + diff --git a/app/src/main/java/com/zeapo/pwdstore/AutofillActivity.java b/app/src/main/java/com/zeapo/pwdstore/AutofillActivity.java deleted file mode 100644 index 2a1227b9..00000000 --- a/app/src/main/java/com/zeapo/pwdstore/AutofillActivity.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.zeapo.pwdstore; - -import android.app.PendingIntent; -import android.content.Intent; -import android.content.IntentSender; -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; -import android.util.Log; - -// 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; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - Intent intent = getIntent(); - PendingIntent pi = intent.getExtras().getParcelable("pending_intent"); - try { - startIntentSenderForResult(pi.getIntentSender() - , REQUEST_CODE_DECRYPT_AND_VERIFY, null, 0, 0, 0); - } catch (IntentSender.SendIntentException e) { - Log.e(AutofillService.Constants.TAG, "SendIntentException", e); - } - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - finish(); // go back to the password field app - if (resultCode == RESULT_OK) { - AutofillService.setUnlockOK(); // report the result to service - } - } -} diff --git a/app/src/main/java/com/zeapo/pwdstore/AutofillService.java b/app/src/main/java/com/zeapo/pwdstore/AutofillService.java deleted file mode 100644 index 887e100e..00000000 --- a/app/src/main/java/com/zeapo/pwdstore/AutofillService.java +++ /dev/null @@ -1,246 +0,0 @@ -package com.zeapo.pwdstore; - -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.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(); - } - }); - 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; - } - } - } -} diff --git a/app/src/main/java/com/zeapo/pwdstore/UserPreference.java b/app/src/main/java/com/zeapo/pwdstore/UserPreference.java index 9a5c83d4..e2a9e5a7 100644 --- a/app/src/main/java/com/zeapo/pwdstore/UserPreference.java +++ b/app/src/main/java/com/zeapo/pwdstore/UserPreference.java @@ -17,6 +17,7 @@ import android.widget.Toast; import com.google.common.base.Function; import com.google.common.base.Joiner; import com.google.common.collect.Iterables; +import com.zeapo.pwdstore.autofill.AutofillActivity; import com.zeapo.pwdstore.crypto.PgpHandler; import com.zeapo.pwdstore.git.GitActivity; import com.zeapo.pwdstore.utils.PasswordRepository; @@ -182,6 +183,15 @@ public class UserPreference extends AppCompatActivity { findPreference("pref_select_external").setOnPreferenceChangeListener(resetRepo); findPreference("git_external").setOnPreferenceChangeListener(resetRepo); + + findPreference("autofill_apps").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + Intent intent = new Intent(callingActivity, AutofillActivity.class); + startActivity(intent); + return false; + } + }); } @Override diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java new file mode 100644 index 00000000..5d8fac5e --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillActivity.java @@ -0,0 +1,84 @@ +package com.zeapo.pwdstore.autofill; + +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.content.IntentSender; +import android.content.SharedPreferences; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +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 com.zeapo.pwdstore.R; + +import java.util.ArrayList; +import java.util.List; +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; + private AutofillRecyclerAdapter recyclerAdapter; + private RecyclerView.LayoutManager layoutManager; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + Intent intent = getIntent(); + Bundle extras = intent.getExtras(); + // if called by service just for startIntentSenderForResult + if (extras != null) { + try { + PendingIntent pi = intent.getExtras().getParcelable("pending_intent"); + if (pi == null) { + return; + } + startIntentSenderForResult(pi.getIntentSender() + , REQUEST_CODE_DECRYPT_AND_VERIFY, null, 0, 0, 0); + } catch (IntentSender.SendIntentException e) { + Log.e(AutofillService.Constants.TAG, "SendIntentException", e); + } + return; + } + // otherwise if called from settings + final PackageManager pm = getPackageManager(); + 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); + } + } + + setContentView(R.layout.autofill_recycler_view); + recyclerView = (RecyclerView) findViewById(R.id.autofill_recycler); + + layoutManager = new LinearLayoutManager(this); + recyclerView.setLayoutManager(layoutManager); + + recyclerAdapter = new AutofillRecyclerAdapter(apps, pm); + recyclerView.setAdapter(recyclerAdapter); + + setTitle("Autofill Apps"); + + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + finish(); // go back to the password field app + if (resultCode == RESULT_OK) { + AutofillService.setUnlockOK(); // report the result to service + } + } +} diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java new file mode 100644 index 00000000..b59223bd --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java @@ -0,0 +1,62 @@ +package com.zeapo.pwdstore.autofill; + +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import com.zeapo.pwdstore.R; + +import java.util.ArrayList; + +public class AutofillRecyclerAdapter extends RecyclerView.Adapter { + private ArrayList apps; + private PackageManager pm; + + public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { + public View view; + public TextView name; + public ImageView icon; + + public ViewHolder(View view) { + super(view); + this.view = view; + name = (TextView) view.findViewById(R.id.app_name); + icon = (ImageView) view.findViewById(R.id.app_icon); + view.setOnClickListener(this); + } + + @Override + public void onClick(View v) { + + } + } + + public AutofillRecyclerAdapter(ArrayList apps, PackageManager pm) { + this.apps = apps; + this.pm = pm; + } + + @Override + public AutofillRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View v = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.autofill_row_layout, parent, false); + return new ViewHolder(v); + } + + @Override + public void onBindViewHolder(AutofillRecyclerAdapter.ViewHolder holder, int position) { + ApplicationInfo app = apps.get(position); + holder.name.setText(pm.getApplicationLabel(app)); + holder.icon.setImageDrawable(pm.getApplicationIcon(app)); + } + + @Override + public int getItemCount() { + return apps.size(); + } +} 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; + } + } + } +} diff --git a/app/src/main/res/layout/autofill_recycler_view.xml b/app/src/main/res/layout/autofill_recycler_view.xml new file mode 100644 index 00000000..27afa77d --- /dev/null +++ b/app/src/main/res/layout/autofill_recycler_view.xml @@ -0,0 +1,12 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/autofill_row_layout.xml b/app/src/main/res/layout/autofill_row_layout.xml new file mode 100644 index 00000000..3f94a8ad --- /dev/null +++ b/app/src/main/res/layout/autofill_row_layout.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + \ 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 e4071c96..3a8c4fdf 100644 --- a/app/src/main/res/xml/preference.xml +++ b/app/src/main/res/xml/preference.xml @@ -70,6 +70,14 @@ android:summary="@string/pref_recursive_filter_hint" android:title="@string/pref_recursive_filter" /> + + + + + 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/res/xml') 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/res/xml') 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/res/xml') 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/res/xml') 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: Tue, 11 Aug 2015 13:34:14 -0400 Subject: passwordfragment changes --- .../main/java/com/zeapo/pwdstore/PasswordFragment.java | 6 +++++- app/src/main/java/com/zeapo/pwdstore/PasswordStore.java | 15 +++++++++++++++ .../com/zeapo/pwdstore/autofill/AutofillFragment.java | 13 ++++++++++++- .../zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java | 2 -- app/src/main/res/xml/preference.xml | 2 +- 5 files changed, 33 insertions(+), 5 deletions(-) (limited to 'app/src/main/res/xml') diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java index 72636043..1daa4f90 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java @@ -112,7 +112,11 @@ public class PasswordFragment extends Fragment{ ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); } else { - ((PasswordStore) getActivity()).decryptPassword(item); + if (getArguments().getBoolean("matchWith", false)) { + ((PasswordStore) getActivity()).matchPasswordWithApp(item); + } else { + ((PasswordStore) getActivity()).decryptPassword(item); + } } } diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java index e51ef201..39d918c8 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.java @@ -289,6 +289,12 @@ public class PasswordStore extends AppCompatActivity { Bundle args = new Bundle(); args.putString("Path", PasswordRepository.getWorkTree().getAbsolutePath()); + // if the activity was started from the autofill settings, the + // intent is to match a clicked pwd with app. pass this to fragment + if (getIntent().getBooleanExtra("matchWith", false)) { + args.putBoolean("matchWith", true); + } + plist.setArguments(args); fragmentTransaction.addToBackStack("passlist"); @@ -531,4 +537,13 @@ public class PasswordStore extends AppCompatActivity { }) .show(); } + + public void matchPasswordWithApp(PasswordItem item) { + String path = item.getFile().getAbsolutePath(); + path = path.replace(PasswordRepository.getWorkTree() + "/", "").replace(".gpg", ""); + Intent data = new Intent(); + data.putExtra("path", path); + setResult(RESULT_OK, data); + finish(); + } } 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 b85dec6a..3e00edfb 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.java @@ -1,5 +1,6 @@ package com.zeapo.pwdstore.autofill; +import android.app.Activity; import android.content.DialogInterface; import android.content.Intent; import android.support.v7.app.AlertDialog; @@ -18,6 +19,7 @@ import com.zeapo.pwdstore.PasswordStore; import com.zeapo.pwdstore.R; public class AutofillFragment extends DialogFragment { + private static final int MATCH_WITH = 777; public AutofillFragment() { } @@ -57,7 +59,8 @@ public class AutofillFragment extends DialogFragment { public void onClick(View v) { // TODO figure out UI for this Intent intent = new Intent(getActivity(), PasswordStore.class); - startActivity(intent); + intent.putExtra("matchWith", true); + startActivityForResult(intent, MATCH_WITH); } }; view.findViewById(R.id.match).setOnClickListener(matchPassword); @@ -92,4 +95,12 @@ public class AutofillFragment extends DialogFragment { builder.setNegativeButton("Cancel", null); return builder.create(); } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + if (resultCode == Activity.RESULT_OK) { + ((EditText) getDialog().findViewById(R.id.matched)).setText(data.getStringExtra("path")); + } + + } } 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 26680152..1b5e63b7 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillRecyclerAdapter.java @@ -1,11 +1,9 @@ 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; diff --git a/app/src/main/res/xml/preference.xml b/app/src/main/res/xml/preference.xml index 538f0387..9a42bb3e 100644 --- a/app/src/main/res/xml/preference.xml +++ b/app/src/main/res/xml/preference.xml @@ -75,7 +75,7 @@ + android:title="Per-app settings"/> -- cgit v1.2.3 From b75190fcd7926c3994f8191996661c19b7b12466 Mon Sep 17 00:00:00 2001 From: Matthew Wong 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/res/xml') 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 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/res/xml') 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 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/res/xml') 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 a73e8625f55e869ba0d74bfa370178c54be3662d Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Sun, 16 Aug 2015 16:58:03 -0400 Subject: Settings dependent on service enabled --- app/src/main/res/xml/preference.xml | 2 ++ 1 file changed, 2 insertions(+) (limited to 'app/src/main/res/xml') diff --git a/app/src/main/res/xml/preference.xml b/app/src/main/res/xml/preference.xml index 1c977992..639fdfe1 100644 --- a/app/src/main/res/xml/preference.xml +++ b/app/src/main/res/xml/preference.xml @@ -77,10 +77,12 @@ android:key="autofill_enable" android:title="@string/pref_autofill_enable_title"/>