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     private boolean mIsTagIntentAppPreferenceSupported = false;
42 
43     @Implementation
enableReaderMode(Activity activity, NfcAdapter.ReaderCallback callback, int flags, Bundle extras)44     protected void enableReaderMode(Activity activity, NfcAdapter.ReaderCallback callback,
45             int flags, Bundle extras) {
46         sReaderModeEnabled = true;
47     }
48 
49     @Implementation
getDefaultAdapter(Context context)50     protected static NfcAdapter getDefaultAdapter(Context context) {
51         return (NfcAdapter) sNfcAdapter;
52     }
53 
54     @Implementation
isEnabled()55     protected boolean isEnabled() {
56         return mIsNfcEnabled;
57     }
58 
setEnabled(boolean enable)59     public void setEnabled(boolean enable) {
60         mIsNfcEnabled = enable;
61     }
62 
63     @Implementation
getAdapterState()64     protected int getAdapterState() {
65         return mState;
66     }
67 
setAdapterState(int state)68     public void setAdapterState(int state) {
69         this.mState = state;
70     }
71 
72     @Implementation
isSecureNfcSupported()73     protected boolean isSecureNfcSupported() {
74         return mIsSecureNfcSupported;
75     }
76 
setSecureNfcSupported(boolean supported)77     public void setSecureNfcSupported(boolean supported) {
78         this.mIsSecureNfcSupported = supported;
79     }
80 
81     @Implementation
enable()82     protected boolean enable() {
83         mIsNfcEnabled = true;
84         return true;
85     }
86 
87     @Implementation
disable()88     protected boolean disable() {
89         mIsNfcEnabled = false;
90         return true;
91     }
92 
isReaderModeEnabled()93     public static boolean isReaderModeEnabled() {
94         return sReaderModeEnabled;
95     }
96 
97     @Resetter
reset()98     public static void reset() {
99         sReaderModeEnabled = false;
100     }
101 
setTagIntentAppPreferenceSupported(boolean supported)102     public void setTagIntentAppPreferenceSupported(boolean supported) {
103         mIsTagIntentAppPreferenceSupported = supported;
104     }
105 
106     @Implementation
isTagIntentAppPreferenceSupported()107     protected boolean isTagIntentAppPreferenceSupported() {
108         return mIsTagIntentAppPreferenceSupported;
109     }
110 }
111