package net.plil.clubinfo.etunicorn.app; import android.app.PendingIntent; import android.content.Intent; import android.net.Uri; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.os.Bundle; import android.support.design.widget.AppBarLayout; 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.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.consommation.FragmentConsommation; import net.plil.clubinfo.etunicorn.app.consommation.PaiementConsommation; 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, PersonOverviewFragment.OnFragmentInteractionListener { /** * 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 SectionsPagerAdapter mSectionsPagerAdapter; private int nbPages = 0; /** * The {@link ViewPager} that will host the section contents. */ private ViewPager mViewPager; private Toolbar toolbar; private AppBarLayout appBarLayout; public static Session session; NfcAdapter mAdapter; PendingIntent mPendingIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = this.getIntent(); session = (Session) intent.getSerializableExtra("session"); if (session.getPersonne().getRole().getNom().equals("admin")){ nbPages = 5; Action.CONSOMATION.setValue(2); Action.EVENT.setValue(3); Action.PERSONNE.setValue(4); } else if (session.getPersonne().getRole().getNom().equals("bde")){ nbPages = 4; Action.CONSOMATION.setValue(-1); Action.EVENT.setValue(2); Action.PERSONNE.setValue(3); } else if (session.getPersonne().getRole().getNom().equals("bar")){ nbPages = 3; Action.CONSOMATION.setValue(2); Action.EVENT.setValue(-1); Action.PERSONNE.setValue(-1); } else { nbPages = 2; } // config other stuff setContentView(R.layout.activity_main); appBarLayout = (AppBarLayout) findViewById(R.id.appbar); 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. 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 onListFragmentInteractionLong(Consommation consommation) { } @Override public void onListFragmentInteraction(Event item) { PaiementEvent paiementEvent = PaiementEvent.newInstance(item); paiementEvent.show(getSupportFragmentManager(), "paiementEvent"); } @Override public void onListFragmentInteractionLong(Event item) { } @Override public void onListFragmentInteraction(Personne personne) { PersonOverviewFragment personOverviewFragment = PersonOverviewFragment.newInstance(personne); personOverviewFragment.show(getSupportFragmentManager(), "overviewPersonne"); } @Override public void onListFragmentInteractionLong(Personne personne) { } @Override public void onFragmentInteraction(Uri uri) { } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentPagerAdapter { public 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; } } 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; } } }