Back to project page sodf.
The source code is released under:
Copyright (c) 2013 Lorenz Lehmann Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Sof...
If you think the Android project sodf listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package lal.apps.smartfoodenvironment.activities; //ww w . j a v a 2 s . c o m import lal.apps.smartfoodenvironment.R; import lal.apps.smartfoodenvironment.model.ProductFactory; import lal.sodf.framework.SodfCallback; import lal.sodf.framework.SodfFramework; import lal.sodf.framework.SodfWrapper; import lal.sodf.framework.ontology.SodfTree; import android.app.Activity; import android.app.AlertDialog; import android.app.PendingIntent; import android.content.DialogInterface; import android.content.Intent; import android.nfc.NfcAdapter; import android.nfc.Tag; import android.os.Bundle; import android.os.Handler; import android.view.Menu; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class FactoryActivity extends Activity implements SodfCallback{ private SodfTree writeData = null; private boolean writeOnTap = false; private Handler handler; private PendingIntent pendingIntent; private AlertDialog dialog; private FactoryActivity self; private TextView name; private TextView microwave; private TextView expiresAfter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_factory); self = this; handler = new Handler(); pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); //set the view references name = (TextView) findViewById(R.id.factory_prodName); microwave = (TextView) findViewById(R.id.factory_prodMicrowave); expiresAfter = (TextView) findViewById(R.id.factory_expiresAfter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_start, menu); return true; } /** The user tapped on the image for milk */ public void milkSelected(View v){ //load the object to write writeData = ProductFactory.getMilk(); //set the data to display name.setVisibility(View.VISIBLE); name.setText(getString(R.string.factory_productName, "Milk")); microwave.setVisibility(View.VISIBLE); microwave.setText(getString(R.string.factory_productMicrowave, "No")); expiresAfter.setVisibility(View.VISIBLE); expiresAfter.setText(getString(R.string.factory_expiresAfter, 2)); } /** The user tapped on the image for Nutella */ public void nutellaSelected(View v){ //load the object to write writeData = ProductFactory.getNutella(); name.setVisibility(View.VISIBLE); name.setText(getString(R.string.factory_productName, "Nutella")); microwave.setVisibility(View.VISIBLE); microwave.setText(getString(R.string.factory_productMicrowave, "No")); expiresAfter.setVisibility(View.VISIBLE); expiresAfter.setText(getString(R.string.factory_expiresAfter, 30)); } /** The user tapped on the image for popcorn */ public void popcornSelected(View v){ //load the object to write writeData = ProductFactory.getPopcorn(); //set the data to display name.setVisibility(View.VISIBLE); name.setText(getString(R.string.factory_productName, "Popcorn")); microwave.setVisibility(View.VISIBLE); microwave.setText(getString(R.string.factory_productMicrowave, "Yes, 800W for 3 minutes")); expiresAfter.setVisibility(View.VISIBLE); expiresAfter.setText(getString(R.string.factory_expiresAfter, 1)); } /** The user tapped on the image for noodles */ public void noodlesSelected(View v){ //load the object to write writeData = ProductFactory.getNoodles(); //set the data to display name.setVisibility(View.VISIBLE); name.setText(getString(R.string.factory_productName, "Noodles")); microwave.setVisibility(View.VISIBLE); microwave.setText(getString(R.string.factory_productMicrowave, "Yes, 1200W for 8 minutes")); expiresAfter.setVisibility(View.VISIBLE); expiresAfter.setText(getString(R.string.factory_expiresAfter, 4)); } /** This function is called when the user wants to write the data on a NFC tag */ public void writeIntention(View v){ writeOnTap = true; //register to get all NFC tags NfcAdapter.getDefaultAdapter(this).enableForegroundDispatch(this, pendingIntent, null, null); //show a message saying the user should tap a NFC tag to write AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Please tap a NFC tag to write the data on it") .setTitle("Write Product Information") .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { //turn the write mode off writeOnTap = false; } }); dialog = builder.create(); dialog.show(); } @Override public void readComplete(SodfWrapper data) { //not needed and thus ignored here } @Override public void writeComplete(final int resultCode) { handler.post(new Runnable() { @Override public void run() { //show a toast based on the read result Toast toast; switch (resultCode){ case SodfCallback.WRITE_IO_EXCEPTION: toast = Toast.makeText(self, "IO exception when writing. Please try againg!", Toast.LENGTH_LONG); break; case SodfCallback.WRITE_FORMAT_EXCEPTION: toast = Toast.makeText(self, "Format exception when writing. Please try againg!", Toast.LENGTH_LONG); break; default: toast = Toast.makeText(self, "Write successful", Toast.LENGTH_LONG); } toast.show(); } }); } /* (non-Javadoc) * @see android.app.Activity#onPause() */ @Override protected void onPause() { //has to be disabled because the app is not in foreground anymore NfcAdapter.getDefaultAdapter(this).disableForegroundDispatch(this); super.onPause(); } /* (non-Javadoc) * @see android.app.Activity#onNewIntent(android.content.Intent) */ @Override protected void onNewIntent(Intent intent) { Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); //ensure that a tag was tapped and the user wants to write data if (tag != null && writeData != null && writeOnTap == true){ //switch the write mode off writeOnTap = false; //hide the dialog if (dialog != null) dialog.cancel(); //tell the framework to write the data SodfFramework.writeData(tag, writeData, "lal.apps.smartfoodenvironment.factory", this); } } }