Browse Source

Fix #50 deny pass keyboard from pass app, switch to other keyboard after pass is inserted

isundil 7 years ago
parent
commit
17cc5e4de7
1 changed files with 33 additions and 7 deletions
  1. 33 7
      app/src/main/java/info/knacki/pass/input/InputService.java

+ 33 - 7
app/src/main/java/info/knacki/pass/input/InputService.java

@@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
 import android.content.Context;
 import android.content.Intent;
 import android.inputmethodservice.InputMethodService;
+import android.os.Build;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.inputmethod.EditorInfo;
@@ -39,12 +40,42 @@ public class InputService extends InputMethodService implements PasswordClickLis
 
     @Override
     public void onStartInputView(EditorInfo info, boolean restarting) {
+        if (info.packageName.equals(getApplicationContext().getPackageName())) {
+            SwitchToPreviousInputMethod();
+            return;
+        }
         super.onStartInputView(info, restarting);
         if (fPasswordListView != null) {
             fPasswordListView.reset();
         }
     }
 
+    private void PromptInputService() {
+        InputMethodManager service = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
+        if (null == service) {
+            Toast.makeText(InputService.this, "Android error", Toast.LENGTH_LONG).show();
+            log.log(Level.SEVERE, "Cannot get Input method service");
+            return;
+        }
+        service.showInputMethodPicker();
+    }
+
+    private void SwitchToPreviousInputMethod() {
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
+            if (!switchToPreviousInputMethod())
+                PromptInputService();
+        } else {
+            InputMethodManager service = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
+            if (null == service) {
+                Toast.makeText(InputService.this, "Android error", Toast.LENGTH_LONG).show();
+                log.log(Level.SEVERE, "Cannot get Input method service");
+                return;
+            }
+            if (!service.switchToLastInputMethod(getWindow().getWindow().getAttributes().token))
+                PromptInputService();
+        }
+    }
+
     @SuppressLint("InflateParams")
     @Override
     public View onCreateInputView() {
@@ -52,13 +83,7 @@ public class InputService extends InputMethodService implements PasswordClickLis
         fPasswordListView = new PasswordListView<>(this, PathUtils.GetPassDir(this));
         ((ScrollView)fInputView.findViewById(R.id.passwordListContainer)).addView(fPasswordListView);
         fInputView.findViewById(R.id.prevButton).setOnClickListener(view -> {
-            InputMethodManager service = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
-            if (null == service) {
-                Toast.makeText(InputService.this, "Android error", Toast.LENGTH_LONG).show();
-                log.log(Level.SEVERE, "Cannot get Input method service");
-                return;
-            }
-            service.showInputMethodPicker();
+            SwitchToPreviousInputMethod();
         });
         fInputView.findViewById(R.id.backspaceButton).setOnClickListener(view -> getCurrentInputConnection().deleteSurroundingText(1, 0));
         fInputView.findViewById(R.id.generateButton).setOnClickListener(view -> CreatePassword());
@@ -130,6 +155,7 @@ public class InputService extends InputMethodService implements PasswordClickLis
     public void sendPassword(String passwordContent) {
         getCurrentInputConnection().commitText(passwordContent, passwordContent.length());
         onFinishInput();
+        SwitchToPreviousInputMethod();
     }
 
     @Override