forked from dotnet/android-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardReaderFragment.cs
118 lines (101 loc) · 3.2 KB
/
CardReaderFragment.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Xml;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Nfc;
using Android.Nfc.Tech;
using CommonSampleLibrary;
namespace CardReader
{
/**
* Generic UI for sample discovery.
*/
public class CardReaderFragment : Fragment, LoyaltyCardReader.AccountCallback
{
public string TAG {
get { return "CardReaderFragment"; }
}
// Recommend NfcAdapter flags for reading from other Android devices. Indicates that this
// activity is interested in NFC-A devices (including other Android devices), and that the
// system should not check for the presence of NDEF-formatted data (e.g. Android Beam).
public NfcReaderFlags READER_FLAGS = NfcReaderFlags.NfcA | NfcReaderFlags.SkipNdefCheck;
public LoyaltyCardReader mLoyaltyCardReader;
private TextView mAccountField;
public override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
}
public override View OnCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View v = inflater.Inflate(Resource.Layout.main_fragment, container, false);
if (v != null) {
mAccountField = (TextView) v.FindViewById(Resource.Id.card_account_field);
mAccountField.Text = "Waiting...";
mLoyaltyCardReader = new LoyaltyCardReader (new WeakReference<LoyaltyCardReader.AccountCallback>(this));
// Disable Android Beam and register our card reader callback
EnableReaderMode ();
}
return v;
}
public override void OnPause ()
{
base.OnPause ();
DisableReaderMode ();
}
public override void OnResume ()
{
base.OnResume ();
EnableReaderMode ();
}
private void EnableReaderMode ()
{
Log.Info (TAG, "Enabling reader mode");
Activity activity = this.Activity;
NfcAdapter nfc = NfcAdapter.GetDefaultAdapter (activity);
if (nfc != null)
{
nfc.EnableReaderMode(activity, mLoyaltyCardReader, READER_FLAGS , null);
}
}
private void DisableReaderMode ()
{
Log.Info (TAG, "Enabling reader mode");
Activity activity = this.Activity;
NfcAdapter nfc = NfcAdapter.GetDefaultAdapter (activity);
if (nfc != null)
{
nfc.DisableReaderMode(activity);
}
}
#region AccountCallback implementation
// This callback is run on a background thread, but updates to UI elements must be performed
// on the UI thread.
public void OnAccountRecieved (string account)
{
this.Activity.RunOnUiThread (() => mAccountField.Text = account);
}
#endregion
}
}