Guest User

FingerprintUiHelper

a guest
Jun 7th, 2016
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.22 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2015 The Android Open Source Project
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License
  15.  */
  16.  
  17. package com.example.uz_system_1.lockscreen;
  18.  
  19. import android.support.v4.hardware.fingerprint.FingerprintManagerCompat;
  20. import android.support.v4.hardware.fingerprint.FingerprintManagerCompat.AuthenticationCallback;
  21. import android.support.v4.hardware.fingerprint.FingerprintManagerCompat.AuthenticationResult;
  22. import android.support.v4.hardware.fingerprint.FingerprintManagerCompat.CryptoObject;
  23. import android.support.v4.os.CancellationSignal;
  24. import android.util.Log;
  25.  
  26. import com.google.common.annotations.VisibleForTesting;
  27.  
  28. import javax.inject.Inject;
  29.  
  30. /**
  31.  * Small helper class to manage text/icon around fingerprint authentication UI.
  32.  */
  33. public class FingerprintUiHelper extends AuthenticationCallback {
  34.  
  35.     @VisibleForTesting
  36.     static final long ERROR_TIMEOUT_MILLIS = 1600;
  37.     @VisibleForTesting
  38.     static final long SUCCESS_DELAY_MILLIS = 1300;
  39.  
  40.     private final FingerprintManagerCompat mFingerprintManager;
  41.  
  42.     private final Callback mCallback;
  43.     private CancellationSignal mCancellationSignal;
  44.  
  45.     @VisibleForTesting
  46.     boolean mSelfCancelled;
  47.  
  48.     /**
  49.      * Builder class for {@link FingerprintUiHelper} in which injected fields from Dagger
  50.      * holds its fields and takes other arguments in the {@link #build} method.
  51.      */
  52.     public static class FingerprintUiHelperBuilder {
  53.         private  final FingerprintManagerCompat mFingerPrintManager;
  54.  
  55.         @Inject
  56.         public FingerprintUiHelperBuilder(FingerprintManagerCompat fingerprintManager) {
  57.             mFingerPrintManager = fingerprintManager;
  58.         }
  59.  
  60.  
  61.         public FingerprintUiHelper build(Callback callback) {
  62.             return new FingerprintUiHelper(mFingerPrintManager,
  63.                     callback);
  64.         }
  65.     }
  66.  
  67.     /**
  68.      * Constructor for {@link FingerprintUiHelper}. This method is expected to be called from
  69.      * only the {@link FingerprintUiHelperBuilder} class.
  70.      */
  71.     private FingerprintUiHelper(FingerprintManagerCompat fingerprintManager,
  72.                                 Callback callback) {
  73.         mFingerprintManager = fingerprintManager;
  74.         mCallback = callback;
  75.     }
  76.  
  77.     public boolean isFingerprintAuthAvailable() {
  78.         return mFingerprintManager.isHardwareDetected()
  79.                 && mFingerprintManager.hasEnrolledFingerprints();
  80.     }
  81.  
  82.     public void startListening(CryptoObject cryptoObject) {
  83.         if (!isFingerprintAuthAvailable()) {
  84.             return;
  85.         }
  86.         mCancellationSignal = new CancellationSignal();
  87.         mSelfCancelled = false;
  88.         mFingerprintManager
  89.                 .authenticate(cryptoObject, 0 /* flags */, mCancellationSignal, this, null);
  90.  
  91.     }
  92.  
  93.     public void stopListening() {
  94.         if (mCancellationSignal != null) {
  95.             mSelfCancelled = true;
  96.             mCancellationSignal.cancel();
  97.             mCancellationSignal = null;
  98.         }
  99.     }
  100.  
  101.     @Override
  102.     public void onAuthenticationError(int errMsgId, CharSequence errString) {
  103.         if (!mSelfCancelled) {
  104.             showError(errString);
  105.             mCallback.onError();
  106.         }
  107.     }
  108.  
  109.     @Override
  110.     public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
  111.         showError(helpString);
  112.     }
  113.  
  114.     @Override
  115.     public void onAuthenticationFailed() {
  116.         Log.i("adsa","dsa");
  117.     }
  118.  
  119.     @Override
  120.     public void onAuthenticationSucceeded(AuthenticationResult result) {
  121.         mCallback.onAuthenticated();
  122.     }
  123.  
  124.     private void showError(CharSequence error) {
  125.     }
  126.  
  127.  
  128.     public interface Callback {
  129.  
  130.         void onAuthenticated();
  131.  
  132.         void onError();
  133.     }
  134. }
Add Comment
Please, Sign In to add comment