Browse Source

Refs #1 password UI

isundil 7 years ago
parent
commit
4ea6ca9bc7

+ 1 - 1
app/src/main/java/info/knacki/pass/ui/alertPrompt/AlertPrompt.java

@@ -83,7 +83,7 @@ public class AlertPrompt {
 
     public AlertPrompt close() {
         if (fDialog != null) {
-            fDialog.hide();
+            fDialog.dismiss();
             fDialog = null;
         }
         return this;

+ 5 - 1
app/src/main/java/info/knacki/pass/ui/alertPrompt/views/FingerprintView.java

@@ -2,6 +2,7 @@ package info.knacki.pass.ui.alertPrompt.views;
 
 import android.annotation.SuppressLint;
 import android.content.Context;
+import android.support.v7.widget.AppCompatImageView;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.LinearLayout;
@@ -9,13 +10,16 @@ import android.widget.LinearLayout;
 import info.knacki.pass.R;
 
 public class FingerprintView extends LinearLayout {
+    private final AppCompatImageView image;
+
     public FingerprintView(Context context) {
         super(context);
         @SuppressLint("InflateParams") View v = LayoutInflater.from(context).inflate(R.layout.fingerprint_picker, null);
         addView(v);
+        image = v.findViewById(R.id.fingerprint_icon);
     }
 
     public void onAuthFailed() {
-        // FIXME
+        image.setBackgroundResource(R.drawable.ic_fingerprint_fail);
     }
 }

+ 36 - 12
app/src/main/java/info/knacki/pass/ui/passwordPicker/FingerprintPicker.java

@@ -9,6 +9,7 @@ import android.support.v4.hardware.fingerprint.FingerprintManagerCompat;
 import android.support.v4.os.CancellationSignal;
 import android.util.Base64;
 import android.util.Base64DataException;
+import android.widget.Toast;
 
 import java.io.File;
 import java.io.FileWriter;
@@ -59,6 +60,7 @@ class FingerprintPicker extends PasswordPicker {
     private IvAndPayload fEncrypted;
     private Queue<String> fPassword;
     private Queue<String> fPasswordIterator;
+    private AlertPrompt fDisplayingPrompt;
 
     private static final class IvAndPayload {
         static final char SEPARATOR = '-';
@@ -181,6 +183,10 @@ class FingerprintPicker extends PasswordPicker {
     private void DoWriteFile(FingerprintManagerCompat.CryptoObject cryptoObject, String content) {
         try {
             Cipher cipher = cryptoObject.getCipher();
+            if (cipher == null) {
+                log.severe("Cannot write content: cipher is null");
+                return;
+            }
             byte[] encrypted = cipher.doFinal(content.getBytes());
             Writer writer = new FileWriter(fFile);
             writer.write((new IvAndPayload(cipher, encrypted)).toString());
@@ -194,10 +200,11 @@ class FingerprintPicker extends PasswordPicker {
     private void DisplayPrompt(IvAndPayload ivForDecoding, OnResponseListener<FingerprintManagerCompat.CryptoObject> resp) {
         final CancellationSignal signal = new CancellationSignal();
         final FingerprintView fingerprintView = new FingerprintView(fContext);
-        final AlertPrompt prompt = fAlertFactory.Generate(fContext)
+        fDisplayingPrompt = fAlertFactory.Generate(fContext)
                 .setView(fingerprintView)
                 .setTitle(R.string.pushfinger)
                 .setNegativeButton(R.string.cancel, (dialogInterface, view) -> {
+                    fDisplayingPrompt = null;
                     signal.cancel();
                     resp.onError(fContext.getString(R.string.cancelled), null);
                 })
@@ -209,7 +216,9 @@ class FingerprintPicker extends PasswordPicker {
         GetFingerprintManager().authenticate(cryptoObject, 0, signal, new FingerprintManagerCompat.AuthenticationCallback() {
             @Override
             public void onAuthenticationError(int errMsgId, CharSequence errString) {
-                resp.onError(errString.toString(), null);
+                ClosePrompt();
+                if (errMsgId != 5) // cancelled
+                    resp.onError(errString.toString(), null);
             }
 
             @Override
@@ -220,24 +229,31 @@ class FingerprintPicker extends PasswordPicker {
 
             @Override
             public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
-                prompt.close();
+                ClosePrompt();
                 resp.onResponse(cryptoObject);
             }
 
             @Override
             public void onAuthenticationFailed() {
-                ((FingerprintView)(prompt.getView())).onAuthFailed();
+                ((FingerprintView)(fDisplayingPrompt.getView())).onAuthFailed();
             }
         }, null);
     }
 
+    private void ClosePrompt() {
+        if (fDisplayingPrompt != null) {
+            fDisplayingPrompt.close();
+            fDisplayingPrompt = null;
+        }
+    }
+
     private void ReadAllPasswords(final FileInterfaceFactory.OnPasswordEnteredListener onResp) {
         try {
             String raw = FileUtils.ReadAllFile(fFile);
             fEncrypted = new IvAndPayload(raw);
         } catch (IOException e) {
             // Fallback password
-            SuperGetPassword(onResp);
+            SuperGetPassword(true, onResp);
             return;
         }
 
@@ -245,7 +261,14 @@ class FingerprintPicker extends PasswordPicker {
             @Override
             public void onResponse(FingerprintManagerCompat.CryptoObject cryptoObject) {
                 try {
-                    byte[] decoded = cryptoObject.getCipher().doFinal(fEncrypted.fPayload);
+                    Cipher cipher = cryptoObject.getCipher();
+                    if (cipher == null) {
+                        log.severe("Cannot read content: cipher is null");
+                        Toast.makeText(fContext, R.string.fingerprint_init_error, Toast.LENGTH_LONG).show();
+                        SuperGetPassword(false, onResp);
+                        return;
+                    }
+                    byte[] decoded = cipher.doFinal(fEncrypted.fPayload);
                     String[] passwords = (new String(decoded, "UTF-8")).split("\n");
                     fPassword = new ArrayDeque<>();
                     fPasswordIterator = new ArrayDeque<>();
@@ -255,7 +278,7 @@ class FingerprintPicker extends PasswordPicker {
                             fPassword.add(i);
                     fPasswordIterator.addAll(fPassword);
                     if (fPasswordIterator.isEmpty())
-                        SuperGetPassword(onResp);
+                        SuperGetPassword(true, onResp);
                     else
                         onResp.onResponse(fPasswordIterator.remove());
                 }
@@ -270,13 +293,14 @@ class FingerprintPicker extends PasswordPicker {
                 // Fallback password
                 fPassword = new ArrayDeque<>();
                 fPasswordIterator = new ArrayDeque<>();
-                SuperGetPassword(onResp);
+                SuperGetPassword(false, onResp);
             }
         });
     }
 
-    private void SuperGetPassword(FileInterfaceFactory.OnPasswordEnteredListener onResp) {
-        FingerprintPicker.super.GetPassword(new FileInterfaceFactory.OnPasswordEnteredListener() {
+    private void SuperGetPassword(boolean savePassword, FileInterfaceFactory.OnPasswordEnteredListener onResp) {
+        ClosePrompt();
+        FingerprintPicker.super.GetPassword(savePassword ? new FileInterfaceFactory.OnPasswordEnteredListener() {
             @Override
             public boolean onResponse(String result) {
                 // TODO checkbox save password ?
@@ -290,7 +314,7 @@ class FingerprintPicker extends PasswordPicker {
             public void onError(String msg, Throwable e) {
                 onResp.onError(msg, e);
             }
-        });
+        } : onResp);
     }
 
     @Override
@@ -298,7 +322,7 @@ class FingerprintPicker extends PasswordPicker {
         if (fPassword == null)
             ReadAllPasswords(onResp);
         else if (fPasswordIterator.isEmpty())
-            SuperGetPassword(onResp);
+            SuperGetPassword(true, onResp);
         else
             onResp.onResponse(fPasswordIterator.remove());
     }

File diff suppressed because it is too large
+ 6 - 0
app/src/main/res/drawable/ic_fingerprint.xml


File diff suppressed because it is too large
+ 6 - 0
app/src/main/res/drawable/ic_fingerprint_fail.xml


+ 10 - 1
app/src/main/res/layout/fingerprint_picker.xml

@@ -3,5 +3,14 @@
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="horizontal">
-
+    <android.support.v7.widget.AppCompatImageView
+        android:layout_width="@dimen/big_icon_height"
+        android:layout_height="@dimen/big_icon_height"
+        android:background="@drawable/ic_fingerprint"
+        android:id="@+id/fingerprint_icon"/>
+    <android.support.v7.widget.AppCompatTextView
+        android:layout_width="wrap_content"
+        android:layout_height="@dimen/big_icon_height"
+        android:gravity="center_vertical"
+        android:text="@string/pushfinger_desc"/>
 </LinearLayout>

+ 3 - 1
app/src/main/res/values-fr/lang.xml

@@ -80,5 +80,7 @@
     <string name="pref_header_gpg_export">Export GPG key</string>
     <string name="sync">Synchroniser</string>
     <string name="close">Fermer</string>
-    <string name="pushfinger">Posez votre doigt</string>
+    <string name="pushfinger">Lecture d\'empreinte</string>
+    <string name="pushfinger_desc">Touchez le capteur avec le bout de votre doigt</string>
+    <string name="fingerprint_init_error">Erreur lors de l\'initialisation du lecteur d\'empreintes.</string>
 </resources>

+ 1 - 0
app/src/main/res/values/dimens.xml

@@ -1,4 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
     <dimen name="keyboard_key_height">32dp</dimen>
+    <dimen name="big_icon_height">64dp</dimen>
 </resources>

+ 2 - 0
app/src/main/res/values/lang.xml

@@ -81,4 +81,6 @@
     <string name="sync">Synchronize</string>
     <string name="close">Close</string>
     <string name="pushfinger">Fingerprint reader</string>
+    <string name="pushfinger_desc">Touch the fingerprint reader with the tip or your finger</string>
+    <string name="fingerprint_init_error">Error during fingerprint initialization</string>
 </resources>

Some files were not shown because too many files changed in this diff