Android Open Source - sodf Main 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.sodf.example;
//from   w w w  . j av  a  2s  .c o  m
import lal.sodf.framework.R;
import lal.sodf.framework.SodfCallback;
import lal.sodf.framework.SodfFramework;
import lal.sodf.framework.SodfWrapper;
import lal.sodf.framework.exceptions.MalformedTypeException;
import lal.sodf.framework.exceptions.TagEmptyException;
import lal.sodf.framework.exceptions.UnformattedTagException;
import lal.sodf.framework.ontology.KeyValueNode;
import lal.sodf.framework.ontology.SodfTree;
import lal.sodf.framework.parser.SodfParser;
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.widget.TextView;

public class MainActivity extends Activity implements SodfCallback{
  
  private TextView dataText;
  private Handler handler;
  private Tag tag;
  private PendingIntent pendingIntent;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    //initialize private variables
    dataText = (TextView) findViewById(R.id.dataView);
    handler = new Handler();
    pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    
    //get the data transmitted with the intent
    handleIntent(getIntent());
  }

  @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_main, menu);
    return true;
  }
  
  /* (non-Javadoc)
   * @see android.app.Activity#onResume()
   */
  @Override
  protected void onResume() {
    super.onResume();
    //get priority for all NFC tags that are discovered while this app is running
    //filters and techLists are null to get all NFC tags
    NfcAdapter.getDefaultAdapter(this).enableForegroundDispatch(this, pendingIntent, null, null);
  }

  /* (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) {
    handleIntent(intent);
  }
  
  /** Handle the intent to determine if an NFC tag was discovered*/
  private void handleIntent(Intent i){
    tag = i.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    if (tag == null){
      Log.i("SODF Example", "No NFC tag detected");
      //do nothing
    }
    else {
      Log.i("SODF Example", "NFC tag detected");
      //read the data from tag
      SodfFramework.readData(tag, this);
    }    
  }

  @Override
  public void readComplete(final SodfWrapper data) {
    //this has to run in a handler since it is called from another thread
    handler.post(new Runnable(){
      @Override
      public void run() {
        Log.i("SODF Example","Read complete");
        //ensure that there was no error
        if (data.exception != null){
          Log.w("SODF Example","Exception when reading data");
          dataText.setText("Error: ");
          dataText.append(data.exception.toString());
          //if the tag is unformatted there will be an unformatted exception in any case so we just try to write sth now
          if (data.exception instanceof MalformedTypeException
              || data.exception instanceof UnformattedTagException
              || data.exception instanceof TagEmptyException){
            Log.i("SODF Example", "Reading data failed, maybe because the tag is used for the first time with this example... trying to write data");
            writeData();
          }
        }
        else {//write all the stuff in a text box on the layout
          dataText.setText("App: "+data.app+"\n"
              + "Compression: "+data.compressionAlgorithm+"\n"
              + "Data on tag: "+SodfParser.serializeTree(data.data));
        }
      }      
    });
  }
  
  /** Write data on the tag */
  private void writeData(){
    Log.i("SODF Example","Trying to write data on tag");
    SodfTree data = new SodfTree();
    data.getPropertiesSubtree().addChild(new KeyValueNode("sodf:test", "sodf example"));
    SodfFramework.writeData(tag, data, "lal.sodf.framework.example", this);
  }

  @Override
  public void writeComplete(final int resultCode) {
    //this has to run in a handler since it is called from another thread
    handler.post(new Runnable(){
      @Override
      public void run() {
        Log.i("SODF Example","Write complete: "+resultCode);
        dataText.setText("Wrote example data on tag. Touch again to read");
      }});
  }

}




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