MainActivity.java
8.39 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package net.plil.clubinfo.etunicorn.app;
import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import net.plil.clubinfo.etunicorn.R;
import net.plil.clubinfo.etunicorn.app.consommation.FragmentConsommation;
import net.plil.clubinfo.etunicorn.app.consommation.PaiementConsommation;
import net.plil.clubinfo.etunicorn.app.credit.Crediter;
import net.plil.clubinfo.etunicorn.app.debit.Debiter;
import net.plil.clubinfo.etunicorn.app.event.EventFragment;
import net.plil.clubinfo.etunicorn.app.event.PaiementEvent;
import net.plil.clubinfo.etunicorn.app.personne.PersonOverviewFragment;
import net.plil.clubinfo.etunicorn.app.personne.PersonneFragment;
import net.plil.clubinfo.etunicorn.data.Consommation;
import net.plil.clubinfo.etunicorn.data.Event;
import net.plil.clubinfo.etunicorn.data.Personne;
import net.plil.clubinfo.etunicorn.data.Session;
import net.plil.clubinfo.etunicorn.utils.ConvertBytesToString;
public class MainActivity extends AppCompatActivity
implements FragmentConsommation.OnListFragmentInteractionListener,
EventFragment.OnListFragmentInteractionListener,
PersonneFragment.OnListFragmentInteractionListener {
public static Session session;
NfcAdapter mAdapter;
PendingIntent mPendingIntent;
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private int nbPages = 0;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = this.getIntent();
session = (Session) intent.getSerializableExtra("session");
switch (session.getPersonne().getRole().getNom()) {
case "admin":
nbPages = 5;
Action.CONSOMATION.setValue(2);
Action.EVENT.setValue(3);
Action.PERSONNE.setValue(4);
break;
case "bde":
nbPages = 4;
Action.CONSOMATION.setValue(-1);
Action.EVENT.setValue(2);
Action.PERSONNE.setValue(3);
break;
case "bar":
nbPages = 3;
Action.CONSOMATION.setValue(2);
Action.EVENT.setValue(-1);
Action.PERSONNE.setValue(-1);
break;
default:
nbPages = 2;
}
// config other stuff
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
mAdapter = NfcAdapter.getDefaultAdapter(this);
if (mAdapter == null) {
//nfc not support your device.
return;
}
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
Tag myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NFCSupport fNFC = (NFCSupport) getSupportFragmentManager().getFragments().get(mViewPager.getCurrentItem());
fNFC.processNFC(ConvertBytesToString.bytesToHexString(myTag.getId()));
}
}
@Override
public void onResume() {
super.onResume();
mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
}
@Override
public void onPause() {
super.onPause();
if (mAdapter != null) {
mAdapter.disableForegroundDispatch(this);
}
}
@Override
public void onListFragmentInteraction(Consommation item) {
PaiementConsommation paiementConsommation = PaiementConsommation.newInstance(item);
paiementConsommation.show(getSupportFragmentManager(), "paiementConsommation");
}
@Override
public void onListFragmentInteraction(Event item) {
PaiementEvent paiementEvent = PaiementEvent.newInstance(item);
paiementEvent.show(getSupportFragmentManager(), "paiementEvent");
}
@Override
public void onListFragmentInteraction(Personne personne) {
PersonOverviewFragment personOverviewFragment = PersonOverviewFragment.newInstance(personne);
personOverviewFragment.show(getSupportFragmentManager(), "overviewPersonne");
}
private enum Action {
DEBITER(0), CREDITER(1), CONSOMATION(2), EVENT(3), PERSONNE(4);
private int value;
Action(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if (position == Action.CREDITER.getValue())
return Crediter.newInstance();
else if (position == Action.DEBITER.getValue())
return Debiter.newInstance();
else if (position == Action.CONSOMATION.getValue())
return FragmentConsommation.newInstance();
else if (position == Action.EVENT.getValue())
return EventFragment.newInstance();
else if (position == Action.PERSONNE.getValue())
return PersonneFragment.newInstance();
else
return null;
}
@Override
public int getCount() {
return nbPages;
}
@Override
public CharSequence getPageTitle(int position) {
if (position == Action.DEBITER.getValue())
return "Debiter";
else if (position == Action.CREDITER.getValue())
return "Crediter";
else if (position == Action.CONSOMATION.getValue())
return "Consommation";
else if (position == Action.EVENT.getValue())
return getString(R.string.event);
else if (position == Action.PERSONNE.getValue())
return getString(R.string.personne);
return null;
}
}
}