1 /*
2  * Copyright (C) 2017 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.android.settings.testutils.shadow;
18 
19 import static org.robolectric.shadow.api.Shadow.newInstanceOf;
20 
21 import android.app.Activity;
22 import android.content.Context;
23 import android.nfc.NfcAdapter;
24 import android.os.Bundle;
25 
26 import org.robolectric.annotation.Implementation;
27 import org.robolectric.annotation.Implements;
28 import org.robolectric.annotation.Resetter;
29 
30 /**
31  * Shadow of {@link NfcAdapter}.
32  */
33 @Implements(value = NfcAdapter.class)
34 public class ShadowNfcAdapter extends org.robolectric.shadows.ShadowNfcAdapter {
35     private static boolean sReaderModeEnabled;
36     private static Object sNfcAdapter = newInstanceOf("android.nfc.NfcAdapter");
37 
38     private boolean mIsNfcEnabled = false;
39     private int mState = NfcAdapter.STATE_ON;
40     private boolean mIsSecureNfcSupported = false;
41 
42     @Implementation
enableReaderMode(Activity activity, NfcAdapter.ReaderCallback callback, int flags, Bundle extras)43     protected void enableReaderMode(Activity activity, NfcAdapter.ReaderCallback callback,
44             int flags, Bundle extras) {
45         sReaderModeEnabled = true;
46     }
47 
48     @Implementation
getDefaultAdapter(Context context)49     protected static NfcAdapter getDefaultAdapter(Context context) {
50         return (NfcAdapter) sNfcAdapter;
51     }
52 
53     @Implementation
isEnabled()54     protected boolean isEnabled() {
55         return mIsNfcEnabled;
56     }
57 
setEnabled(boolean enable)58     public void setEnabled(boolean enable) {
59         mIsNfcEnabled = enable;
60     }
61 
62     @Implementation
getAdapterState()63     protected int getAdapterState() {
64         return mState;
65     }
66 
setAdapterState(int state)67     public void setAdapterState(int state) {
68         this.mState = state;
69     }
70 
71     @Implementation
isSecureNfcSupported()72     protected boolean isSecureNfcSupported() {
73         return mIsSecureNfcSupported;
74     }
75 
setSecureNfcSupported(boolean supported)76     public void setSecureNfcSupported(boolean supported) {
77         this.mIsSecureNfcSupported = supported;
78     }
79 
80     @Implementation
enable()81     protected boolean enable() {
82         mIsNfcEnabled = true;
83         return true;
84     }
85 
86     @Implementation
disable()87     protected boolean disable() {
88         mIsNfcEnabled = false;
89         return true;
90     }
91 
isReaderModeEnabled()92     public static boolean isReaderModeEnabled() {
93         return sReaderModeEnabled;
94     }
95 
96     @Resetter
reset()97     public static void reset() {
98         sReaderModeEnabled = false;
99     }
100 }