Android Open Source - android-nfc-lib Nfc Activity






From Project

Back to project page android-nfc-lib.

License

The source code is released under:

MIT License

If you think the Android project android-nfc-lib 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

/*
 * NfcActivity.java//  w w w  . j  a v  a 2  s .  c o  m
 * NfcLibrary project.
 *
 * Created by : Daneo van Overloop - 17/6/2014.
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 AppFoundry. All rights reserved.
 *
 * 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 Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 */

package be.appfoundry.nfclibrary.implementation;

import android.app.ProgressDialog;
import android.content.Intent;
import android.nfc.FormatException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import be.appfoundry.nfclibrary.exceptions.InsufficientCapacityException;
import be.appfoundry.nfclibrary.exceptions.ReadOnlyTagException;
import be.appfoundry.nfclibrary.exceptions.TagNotPresentException;
import be.appfoundry.nfclibrary.tasks.interfaces.AsyncOperationCallback;
import be.appfoundry.nfclibrary.tasks.interfaces.AsyncUiCallback;
import be.appfoundry.nfclibrary.utilities.async.WriteCallbackNfcAsync;
import be.appfoundry.nfclibrary.utilities.interfaces.NfcReadUtility;
import be.appfoundry.nfclibrary.utilities.interfaces.NfcWriteUtility;
import be.appfoundry.nfclibrary.utilities.sync.NfcReadUtilityImpl;


public class NfcActivity extends be.appfoundry.nfclibrary.activities.NfcActivity {

    private static final String TAG = NfcActivity.class.getName();

    NfcReadUtility mNfcReadUtility = new NfcReadUtilityImpl();
    ProgressDialog mProgressDialog;

    AsyncUiCallback mAsyncUiCallback = new AsyncUiCallback() {
        @Override
        public void callbackWithReturnValue(Boolean result) {
            if (mProgressDialog != null && mProgressDialog.isShowing()) {
                mProgressDialog.dismiss();
            }
            if (result) {
                Toast.makeText(NfcActivity.this, "Write has been done!", Toast.LENGTH_LONG).show();
            }

            Log.d(TAG,"Received our result : " + result);

        }

        @Override
        public void onProgressUpdate(Boolean... values) {
            if (values.length > 0 && values[0] && mProgressDialog != null) {
                mProgressDialog.setMessage("Writing");
                Log.d(TAG,"Writing !");
            }

        }

        @Override
        public void onError(Exception e) {
            if (mProgressDialog != null && mProgressDialog.isShowing()) {
                mProgressDialog.dismiss();
            }
            Log.i(TAG,"Encountered an error !",e);
            Toast.makeText(NfcActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    };

    AsyncOperationCallback mAsyncOperationCallback;
    private AsyncTask<Object, Void, Boolean> mTask;
    private boolean mRegistered;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nfc);

        Button emailButton = (Button) findViewById(R.id.btn_write_email_nfc);
        Button smsButton = (Button) findViewById(R.id.btn_write_sms_nfc);
        Button geoButton = (Button) findViewById(R.id.btn_write_geolocation_nfc);
        Button uriButton = (Button) findViewById(R.id.btn_write_uri_nfc);
        Button telButton = (Button) findViewById(R.id.btn_write_tel_nfc);
        Button bluetoothButton = (Button) findViewById(R.id.btn_write_bluetooth_nfc);
        if (emailButton != null) {
            emailButton.setOnClickListener(new Button.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final EditText emailText = ((EditText) retrieveElement(R.id.input_text_email_target));
                    if (emailText != null) {
                        final String text = emailText.getText().toString();
                        mAsyncOperationCallback = new AsyncOperationCallback() {
                            @Override
                            public boolean performWrite(NfcWriteUtility writeUtility) throws ReadOnlyTagException, InsufficientCapacityException, TagNotPresentException, FormatException {
                                return writeUtility.writeEmailToTagFromIntent(text, null, null, getIntent());
                            }
                        };
                        showDialog();
                    }
                }
            });
        }

        if (smsButton != null) {
            smsButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText smsText = (EditText) retrieveElement(R.id.input_text_sms_target);
                    if (smsText != null) {
                        final String text = smsText.getText().toString();
                        mAsyncOperationCallback = new AsyncOperationCallback() {
                            @Override
                            public boolean performWrite(NfcWriteUtility writeUtility) throws ReadOnlyTagException, InsufficientCapacityException, TagNotPresentException, FormatException {
                                return writeUtility.writeSmsToTagFromIntent(text, null, getIntent());
                            }
                        };
                        showDialog();
                    }
                }
            });
        }

        if (geoButton != null) {
            geoButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText latitudeText = (EditText) retrieveElement(R.id.input_text_geo_lat_target);
                    EditText longitudeText = (EditText) retrieveElement(R.id.input_text_geo_long_target);
                    if (latitudeText != null && longitudeText != null) {

                        final double longitude = Double.parseDouble(latitudeText.getText().toString());
                        final double latitude = Double.parseDouble(longitudeText.getText().toString());

                        mAsyncOperationCallback = new AsyncOperationCallback() {
                            @Override
                            public boolean performWrite(NfcWriteUtility writeUtility) throws ReadOnlyTagException, InsufficientCapacityException, TagNotPresentException, FormatException {
                                return writeUtility.writeGeolocationToTagFromIntent(latitude, longitude, getIntent());
                            }
                        };
                        showDialog();
                    }
                }
            });
        }
        if (uriButton != null) {
            uriButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final TextView uriText = retrieveElement(R.id.input_text_uri_target);
                    if (uriText != null) {
                        final String text = uriText.getText().toString();
                        mAsyncOperationCallback = new AsyncOperationCallback() {
                            @Override
                            public boolean performWrite(NfcWriteUtility writeUtility) throws ReadOnlyTagException, InsufficientCapacityException, TagNotPresentException, FormatException {
                                return writeUtility.writeUriToTagFromIntent(text, getIntent());
                            }
                        };
                        showDialog();
                    }

                }
            });
        }
        if (telButton != null) {
            telButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final TextView telText = retrieveElement(R.id.input_text_tel_target);
                    if (telText != null) {
                        final String text = telText.getText().toString();
                        mAsyncOperationCallback = new AsyncOperationCallback() {
                            @Override
                            public boolean performWrite(NfcWriteUtility writeUtility) throws ReadOnlyTagException, InsufficientCapacityException, TagNotPresentException, FormatException {
                                return writeUtility.writeTelToTagFromIntent(text, getIntent());
                            }
                        };
                        showDialog();
                    } else {
                        showNoInputToast();
                    }
                }
            });
        }
        if (bluetoothButton != null) {
            bluetoothButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    EditText bluetoothInput = (EditText) findViewById(R.id.input_text_bluetooth_address);
                    if (bluetoothInput != null) {
                        final String text = bluetoothInput.getText().toString();
                        mAsyncOperationCallback = new AsyncOperationCallback() {
                            @Override
                            public boolean performWrite(NfcWriteUtility writeUtility) throws ReadOnlyTagException, InsufficientCapacityException, TagNotPresentException, FormatException {
                                return writeUtility.writeBluetoothAddressToTagFromIntent(text, getIntent());
                            }
                        };
                        showDialog();
                    }
                }
            });
        }


        enableBeam();

    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (getNfcAdapter() != null) {
            getNfcAdapter().disableForegroundDispatch(this);
        }

    }

    /**
     * Launched when in foreground dispatch mode
     *
     * @param paramIntent
     *         containing found data
     */

    public void onNewIntent(final Intent paramIntent) {
        super.onNewIntent(paramIntent);

        if (mAsyncOperationCallback != null && mProgressDialog != null && mProgressDialog.isShowing()) {
            new WriteCallbackNfcAsync(mAsyncUiCallback, mAsyncOperationCallback).executeWriteOperation();
            mAsyncOperationCallback = null;
        } else {
            for (String data : mNfcReadUtility.readFromTagWithMap(paramIntent).values()) {
                Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
            }


        }

    }

    private void showNoInputToast() {
        Toast.makeText(this, getString(R.string.no_input), Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
        if (mTask != null) {
            mTask.cancel(true);
            mTask = null;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.nfc, 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();
        return id == R.id.action_settings || super.onOptionsItemSelected(item);
    }

    public boolean isRegistered() {
        return mRegistered;
    }



    public void showDialog() {
        mProgressDialog = new ProgressDialog(NfcActivity.this);
        mProgressDialog.setTitle(R.string.progressdialog_waiting_for_tag);
        mProgressDialog.setMessage(getString(R.string.progressdialog_waiting_for_tag_message));
        mProgressDialog.show();
    }

    private TextView retrieveElement(int id) {
        TextView element = (TextView) findViewById(id);
        return (element != null) && ((TextView) findViewById(id)).getText() != null && !"".equals(((TextView) findViewById(id)).getText().toString()) ? element : null;
    }

}




Java Source Code List

be.appfoundry.nfclibrary.activities.NfcActivity.java
be.appfoundry.nfclibrary.constants.NfcPayloadHeader.java
be.appfoundry.nfclibrary.constants.NfcType.java
be.appfoundry.nfclibrary.exceptions.InsufficientCapacityException.java
be.appfoundry.nfclibrary.exceptions.ReadOnlyTagException.java
be.appfoundry.nfclibrary.exceptions.TagNotPresentException.java
be.appfoundry.nfclibrary.exceptions.TagNotWritableException.java
be.appfoundry.nfclibrary.implementation.NfcActivity.java
be.appfoundry.nfclibrary.implementation.TestEmptyFieldsNfcActivity.java
be.appfoundry.nfclibrary.implementation.TestFilledFieldsNfcActivity.java
be.appfoundry.nfclibrary.tasks.GenericTask.java
be.appfoundry.nfclibrary.tasks.interfaces.AsyncOperationCallback.java
be.appfoundry.nfclibrary.tasks.interfaces.AsyncUiCallback.java
be.appfoundry.nfclibrary.utilities.TestUtilities.java
be.appfoundry.nfclibrary.utilities.async.AbstractFailsTestsAsync.java
be.appfoundry.nfclibrary.utilities.async.AbstractNfcAsync.java
be.appfoundry.nfclibrary.utilities.async.GenericTaskTestsAsync.java
be.appfoundry.nfclibrary.utilities.async.WriteBluetoothNfcAsync.java
be.appfoundry.nfclibrary.utilities.async.WriteCallbackNfcAsync.java
be.appfoundry.nfclibrary.utilities.async.WriteEmailNfcAsync.java
be.appfoundry.nfclibrary.utilities.async.WriteGeoLocationNfcAsync.java
be.appfoundry.nfclibrary.utilities.async.WritePhoneNfcAsync.java
be.appfoundry.nfclibrary.utilities.async.WriteSmsNfcAsync.java
be.appfoundry.nfclibrary.utilities.async.WriteUriNfcAsync.java
be.appfoundry.nfclibrary.utilities.interfaces.AsyncNfcWriteOperation.java
be.appfoundry.nfclibrary.utilities.interfaces.NdefWrite.java
be.appfoundry.nfclibrary.utilities.interfaces.NfcMessageUtility.java
be.appfoundry.nfclibrary.utilities.interfaces.NfcReadUtility.java
be.appfoundry.nfclibrary.utilities.interfaces.NfcWriteUtility.java
be.appfoundry.nfclibrary.utilities.interfaces.WriteUtility.java
be.appfoundry.nfclibrary.utilities.sync.NdefWriteImpl.java
be.appfoundry.nfclibrary.utilities.sync.NfcMessageUtilityImpl.java
be.appfoundry.nfclibrary.utilities.sync.NfcReadUtilityImpl.java
be.appfoundry.nfclibrary.utilities.sync.NfcWriteUtilityImpl.java
be.appfoundry.nfclibrary.utilities.sync.WriteEmailFailsTests.java
be.appfoundry.nfclibrary.utilities.sync.WriteEmailSucceedsTests.java
be.appfoundry.nfclibrary.utilities.sync.WriteGeolocationFailsTests.java
be.appfoundry.nfclibrary.utilities.sync.WriteGeolocationSucceedsTests.java
be.appfoundry.nfclibrary.utilities.sync.WritePhoneFailsTests.java
be.appfoundry.nfclibrary.utilities.sync.WritePhoneSucceedsTests.java
be.appfoundry.nfclibrary.utilities.sync.WriteUriFailsTests.java
be.appfoundry.nfclibrary.utilities.sync.WriteUriSucceedsTests.java
be.appfoundry.nfclibrary.utilities.sync.WriteUtilityImpl.java