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.android.asymmetricfingerprintdialog;
18 
19 import com.example.android.asymmetricfingerprintdialog.server.StoreBackend;
20 import com.example.android.asymmetricfingerprintdialog.server.StoreBackendImpl;
21 
22 import android.app.KeyguardManager;
23 import android.content.Context;
24 import android.content.SharedPreferences;
25 import android.hardware.fingerprint.FingerprintManager;
26 import android.preference.PreferenceManager;
27 import android.security.keystore.KeyProperties;
28 import android.view.inputmethod.InputMethodManager;
29 
30 import java.security.KeyPairGenerator;
31 import java.security.KeyStore;
32 import java.security.KeyStoreException;
33 import java.security.NoSuchAlgorithmException;
34 import java.security.NoSuchProviderException;
35 import java.security.Signature;
36 
37 import dagger.Module;
38 import dagger.Provides;
39 
40 /**
41  * Dagger module for Fingerprint APIs.
42  */
43 @Module(
44         library = true,
45         injects = {MainActivity.class}
46 )
47 public class FingerprintModule {
48 
49     private final Context mContext;
50 
FingerprintModule(Context context)51     public FingerprintModule(Context context) {
52         mContext = context;
53     }
54 
55     @Provides
providesContext()56     public Context providesContext() {
57         return mContext;
58     }
59 
60     @Provides
providesFingerprintManager(Context context)61     public FingerprintManager providesFingerprintManager(Context context) {
62         return context.getSystemService(FingerprintManager.class);
63     }
64 
65     @Provides
providesKeyguardManager(Context context)66     public KeyguardManager providesKeyguardManager(Context context) {
67         return context.getSystemService(KeyguardManager.class);
68     }
69 
70     @Provides
providesKeystore()71     public KeyStore providesKeystore() {
72         try {
73             return KeyStore.getInstance("AndroidKeyStore");
74         } catch (KeyStoreException e) {
75             throw new RuntimeException("Failed to get an instance of KeyStore", e);
76         }
77     }
78 
79     @Provides
providesKeyPairGenerator()80     public KeyPairGenerator providesKeyPairGenerator() {
81         try {
82             return KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore");
83         } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
84             throw new RuntimeException("Failed to get an instance of KeyPairGenerator", e);
85         }
86     }
87 
88     @Provides
providesSignature(KeyStore keyStore)89     public Signature providesSignature(KeyStore keyStore) {
90         try {
91             return Signature.getInstance("SHA256withECDSA");
92         } catch (NoSuchAlgorithmException e) {
93             throw new RuntimeException("Failed to get an instance of Signature", e);
94         }
95     }
96 
97     @Provides
providesInputMethodManager(Context context)98     public InputMethodManager providesInputMethodManager(Context context) {
99         return (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
100     }
101 
102     @Provides
providesSharedPreferences(Context context)103     public SharedPreferences providesSharedPreferences(Context context) {
104         return PreferenceManager.getDefaultSharedPreferences(context);
105     }
106 
107     @Provides
providesStoreBackend()108     public StoreBackend providesStoreBackend() {
109         return new StoreBackendImpl();
110     }
111 }
112