Android Open Source - sodf Expired Activity






From Project

Back to project page sodf.

License

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.

Java Source Code

package lal.apps.smartfoodenvironment.activities;
//  w w  w . j  av a2s. co m
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import lal.apps.smartfoodenvironment.R;
import lal.apps.smartfoodenvironment.model.ProductVocabulary;
import lal.sodf.framework.SodfCallback;
import lal.sodf.framework.SodfFramework;
import lal.sodf.framework.SodfWrapper;
import lal.sodf.framework.exceptions.CompressionAlgorithmNotFoundException;
import lal.sodf.framework.ontology.KeyValueNode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class ExpiredActivity extends Activity implements SodfCallback{
  
  private Handler handler;
  private PendingIntent pendingIntent;
  private TextView opened;
  private TextView suggestion;
  private ExpiredActivity self;
  private Tag tag;
  private String today;
  private SimpleDateFormat sdf;

  @SuppressLint("SimpleDateFormat")
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_expired);
    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
    opened = (TextView) findViewById(R.id.expired_productInfo);
    suggestion = (TextView) findViewById(R.id.expired_manufactuererSuggestion);
    //get the date of today formatted properly
    Calendar c = Calendar.getInstance();
    sdf = new SimpleDateFormat("dd-MMM-yyyy");
    today = sdf.format(c.getTime());
  }

  @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;
  }
  
  @Override
  public void readComplete(final SodfWrapper data) {
    handler.post(new Runnable(){
      @Override
      public void run() {
        if (data.exception != null || data.data == null){
          Log.e("SFE", "Error on read: "+data.exception);
          Toast toast = Toast.makeText(self, "The tag, which you tapped did not belong to a valid product", Toast.LENGTH_LONG);
          toast.show();
        }
        else {//parse the data
          //check if this is the first time the item is opened
          KeyValueNode date = data.data.getPropertiesSubtree().getKeyValueNodeChild(ProductVocabulary.PRODUCT_OPENED);
          KeyValueNode suggestedDays = data.data.getPropertiesSubtree().getKeyValueNodeChild(ProductVocabulary.PRODUCT_GOOD_AFTER_OPENING);
          if (date == null) {//the product was not opened before so the date has to be added
            //add the formatted date to the SODF tree
            data.data.getPropertiesSubtree().addChild(new KeyValueNode(ProductVocabulary.PRODUCT_OPENED, today));
            //write everything on the tag
            try {
              SodfFramework.writeData(tag, data, self);
            } catch (CompressionAlgorithmNotFoundException e) {
              Log.e("SFE", "Error: "+e);
            }
          }
          else {//write date and remaining days to the screen
            try {
              String openedOn = date.getFirstValue();
              Log.i("SFE","Product was opened on"+openedOn);
              //compare the opening date to the current date
              Date opening = sdf.parse(openedOn);
              Date now = sdf.parse(today);
              int diffInDays = (int) ( (now.getTime() - opening.getTime()) / (1000 * 60 * 60 * 24) );
              //calculate the exact expiration date
              long expiration = opening.getTime() + (1000 * 60 * 60 * 24 * Integer.valueOf(suggestedDays.getFirstValue()));
              Date expirationDate = new Date(expiration);
              opened.setText(getString(R.string.expired_productChecked, openedOn, diffInDays, sdf.format(expirationDate)));
              opened.setVisibility(View.VISIBLE);
              
            } catch (ParseException e) {
              Log.e("SFE","Error when parsing date: "+e);
            }                
          }
          //in any case show the user the suggested days
          if (suggestedDays != null){
            suggestion.setVisibility(View.VISIBLE);
            suggestion.setText(getString(R.string.expired_manufacturerSuggestion, suggestedDays.getFirstValue()));        
          }
          else Log.e("SFE", "Suggested days till expiration not found.");
        }
      }      
    });
  }

  @Override
  public void writeComplete(final int resultCode) {
    handler.post(new Runnable(){
      @Override
      public void run() {
        if (resultCode == SodfCallback.WRITE_SUCCESSFUL){
          opened.setVisibility(View.VISIBLE);
          opened.setText(R.string.expired_productOpened);          
        }
        else {
          Log.e("SFE","Error on write: "+resultCode);
          Toast toast = Toast.makeText(self, "Error when writing data on the tag. Please try again and tap the tag a bit longer", 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#onResume()
   */
  @Override
  protected void onResume() {
    super.onResume();
    //register to receive NFC tags
    NfcAdapter.getDefaultAdapter(this).enableForegroundDispatch(this, pendingIntent, null, null);
  }

  /* (non-Javadoc)
   * @see android.app.Activity#onNewIntent(android.content.Intent)
   */
  @Override
  protected void onNewIntent(Intent intent) {
    tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    //ensure that a tag was tapped
    if (tag != null){
      //try to read the data from the tag
      SodfFramework.readData(tag, this);
    }
  }
  

}




Java Source Code List

lal.apps.ontap.MainActivity.java
lal.apps.ontap.WifiVocabulary.java
lal.apps.smartfoodenvironment.activities.ExpiredActivity.java
lal.apps.smartfoodenvironment.activities.FactoryActivity.java
lal.apps.smartfoodenvironment.activities.MicrowaveActivity.java
lal.apps.smartfoodenvironment.activities.StartActivity.java
lal.apps.smartfoodenvironment.model.MicrowaveVocabulary.java
lal.apps.smartfoodenvironment.model.ProductFactory.java
lal.apps.smartfoodenvironment.model.ProductVocabulary.java
lal.sodf.example.MainActivity.java
lal.sodf.framework.SodfCallback.java
lal.sodf.framework.SodfCallback.java
lal.sodf.framework.SodfCallback.java
lal.sodf.framework.SodfFramework.java
lal.sodf.framework.SodfFramework.java
lal.sodf.framework.SodfFramework.java
lal.sodf.framework.SodfWrapper.java
lal.sodf.framework.SodfWrapper.java
lal.sodf.framework.SodfWrapper.java
lal.sodf.framework.compressor.CompressionAlgorithm.java
lal.sodf.framework.compressor.CompressionAlgorithm.java
lal.sodf.framework.compressor.CompressionAlgorithm.java
lal.sodf.framework.compressor.Compressor.java
lal.sodf.framework.compressor.Compressor.java
lal.sodf.framework.compressor.Compressor.java
lal.sodf.framework.compressor.Gzip.java
lal.sodf.framework.compressor.Gzip.java
lal.sodf.framework.compressor.Gzip.java
lal.sodf.framework.exceptions.CompressionAlgorithmNotFoundException.java
lal.sodf.framework.exceptions.CompressionAlgorithmNotFoundException.java
lal.sodf.framework.exceptions.CompressionAlgorithmNotFoundException.java
lal.sodf.framework.exceptions.DuplicateKeyException.java
lal.sodf.framework.exceptions.DuplicateKeyException.java
lal.sodf.framework.exceptions.DuplicateKeyException.java
lal.sodf.framework.exceptions.MalformedTypeException.java
lal.sodf.framework.exceptions.MalformedTypeException.java
lal.sodf.framework.exceptions.MalformedTypeException.java
lal.sodf.framework.exceptions.TagEmptyException.java
lal.sodf.framework.exceptions.TagEmptyException.java
lal.sodf.framework.exceptions.TagEmptyException.java
lal.sodf.framework.exceptions.UnformattedTagException.java
lal.sodf.framework.exceptions.UnformattedTagException.java
lal.sodf.framework.exceptions.UnformattedTagException.java
lal.sodf.framework.nfc.NfcContentWrapper.java
lal.sodf.framework.nfc.NfcContentWrapper.java
lal.sodf.framework.nfc.NfcContentWrapper.java
lal.sodf.framework.nfc.NfcHandler.java
lal.sodf.framework.nfc.NfcHandler.java
lal.sodf.framework.nfc.NfcHandler.java
lal.sodf.framework.ontology.FunctionsNode.java
lal.sodf.framework.ontology.FunctionsNode.java
lal.sodf.framework.ontology.FunctionsNode.java
lal.sodf.framework.ontology.KeyNode.java
lal.sodf.framework.ontology.KeyNode.java
lal.sodf.framework.ontology.KeyNode.java
lal.sodf.framework.ontology.KeyValueNode.java
lal.sodf.framework.ontology.KeyValueNode.java
lal.sodf.framework.ontology.KeyValueNode.java
lal.sodf.framework.ontology.MetadataNode.java
lal.sodf.framework.ontology.MetadataNode.java
lal.sodf.framework.ontology.MetadataNode.java
lal.sodf.framework.ontology.Node.java
lal.sodf.framework.ontology.Node.java
lal.sodf.framework.ontology.Node.java
lal.sodf.framework.ontology.PropertiesNode.java
lal.sodf.framework.ontology.PropertiesNode.java
lal.sodf.framework.ontology.PropertiesNode.java
lal.sodf.framework.ontology.RestInteractionNode.java
lal.sodf.framework.ontology.RestInteractionNode.java
lal.sodf.framework.ontology.RestInteractionNode.java
lal.sodf.framework.ontology.RestParameters.java
lal.sodf.framework.ontology.RestParameters.java
lal.sodf.framework.ontology.RestParameters.java
lal.sodf.framework.ontology.SodfTree.java
lal.sodf.framework.ontology.SodfTree.java
lal.sodf.framework.ontology.SodfTree.java
lal.sodf.framework.ontology.SodfVocabulary.java
lal.sodf.framework.ontology.SodfVocabulary.java
lal.sodf.framework.ontology.SodfVocabulary.java
lal.sodf.framework.parser.SodfParser.java
lal.sodf.framework.parser.SodfParser.java
lal.sodf.framework.parser.SodfParser.java