1
/*
2
* Copyright (C) 2013 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.cardreader;
18
19
import android.app.Activity;
20
import android.nfc.NfcAdapter;
21
import android.os.Bundle;
22
import android.support.v4.app.Fragment;
23
import android.view.LayoutInflater;
24
import android.view.View;
25
import android.view.ViewGroup;
26
import android.widget.TextView;
27
28
import com.example.android.common.logger.Log;
29
30
/**
31
* Generic UI for sample discovery.
32
*/
33
public class CardReaderFragment extends Fragment implements LoyaltyCardReader.AccountCallback {
34
35
public static final String TAG = "CardReaderFragment";
36
// Recommend NfcAdapter flags for reading from other Android devices. Indicates that this
37
// activity is interested in NFC-A devices (including other Android devices), and that the
38
// system should not check for the presence of NDEF-formatted data (e.g. Android Beam).
39
public static int READER_FLAGS =
40
NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK;
41
public LoyaltyCardReader mLoyaltyCardReader;
42
private TextView mAccountField;
43
44
/** Called when sample is created. Displays generic UI with welcome text. */
45
@Override
46
public void onCreate(Bundle savedInstanceState) {
47
super.onCreate(savedInstanceState);
48
}
49
50
@Override
51
public View onCreateView(LayoutInflater inflater, ViewGroup container,
52
Bundle savedInstanceState) {
53
// Inflate the layout for this fragment
54
View v = inflater.inflate(R.layout.main_fragment, container, false);
55
if (v != null) {
56
mAccountField = (TextView) v.findViewById(R.id.card_account_field);
57
mAccountField.setText("Waiting...");
58
59
mLoyaltyCardReader = new LoyaltyCardReader(this);
60
61
// Disable Android Beam and register our card reader callback
62
enableReaderMode();
63
}
64
65
return v;
66
}
67
68
@Override
69
public void onPause() {
70
super.onPause();
71
disableReaderMode();
72
}
73
74
@Override
75
public void onResume() {
76
super.onResume();
77
enableReaderMode();
78
}
79
80
private void enableReaderMode() {
81
Log.i(TAG, "Enabling reader mode");
82
Activity activity = getActivity();
83
NfcAdapter nfc = NfcAdapter.getDefaultAdapter(activity);
84
if (nfc != null) {
85
nfc.enableReaderMode(activity, mLoyaltyCardReader, READER_FLAGS, null);
86
}
87
}
88
89
private void disableReaderMode() {
90
Log.i(TAG, "Disabling reader mode");
91
Activity activity = getActivity();
92
NfcAdapter nfc = NfcAdapter.getDefaultAdapter(activity);
93
if (nfc != null) {
94
nfc.disableReaderMode(activity);
95
}
96
}
97
98
@Override
99
public void onAccountReceived(final String account) {
100
// This callback is run on a background thread, but updates to UI elements must be performed
101
// on the UI thread.
102
getActivity().runOnUiThread(new Runnable() {
103
@Override
104
public void run() {
105
mAccountField.setText(account);
106
}
107
});
108
}
109
}