io.atrac613.AbstractNfcTagFragment.java Source code

Java tutorial

Introduction

Here is the source code for io.atrac613.AbstractNfcTagFragment.java

Source

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.atrac613;

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

import net.kazzz.nfc.NfcTag;
import net.kazzz.util.ArrayUtil;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

/**
 * NFCTag??View???????
 * 
 * @author Kazzz.
 * @date 2011/07/09
 * @since Android API Level 10
 *
 */

public abstract class AbstractNfcTagFragment extends Fragment {
    public static final String TAG = "AbstractNfcFeliCaTagFragment";
    public static CopyOnWriteArrayList<String[]> sTechList; //

    protected String[][] mTechList;
    protected Tag mNfcTag;
    protected ArrayList<INfcTagListener> mListnerList = new ArrayList<INfcTagListener>();

    /**
     *  NFC????????
     * 
     * @author Copyright c 2011-2012 All Rights Reserved.
     * @date 2011/06/24
     * @since Android API Level 9
     *
     */
    public static interface INfcTagListener {
        void onTagDiscovered(Intent intent, Parcelable nfcTag, AbstractNfcTagFragment fragment);
    }

    /**
     * NfcAdapter??TechList??? (?????)
     * @param techList ????????
     * @return String[][] ????????
     */
    private static synchronized String[][] registerTechList(String[]... techList) {
        if (sTechList == null) {
            sTechList = new CopyOnWriteArrayList<String[]>();
        }

        for (String[] filterTech : techList) {
            boolean containTechList = false;
            if (sTechList.size() > 0) {
                for (String[] source : sTechList) {
                    if (ArrayUtil.containArray(source, filterTech)) {
                        containTechList = true;
                        break;
                    }
                }
            }
            if (!containTechList) {
                sTechList.add(filterTech);
            }
        }

        return sTechList.toArray(new String[sTechList.size()][]);
    }

    /**
     * 
     * @param activity ????
     * @param tag ???????
     */
    public AbstractNfcTagFragment(FragmentActivity activity, String tag) {
        super();
        this.registerFragment(activity, tag);
    }

    /**
     * ???Activity????
     * @param activity ????
     * @param tag ?
     */
    protected void registerFragment(FragmentActivity activity, String tag) {
        this.unRegisterFragment(activity, tag);
        FragmentManager fm = activity.getSupportFragmentManager();
        FragmentTransaction trans = fm.beginTransaction();
        {
            trans.add(this, tag);
        }
        trans.commit();
        fm.executePendingTransactions();
    }

    /**
     * Activity????
     * @param activity ????
     * @param tag ?
     */
    protected void unRegisterFragment(FragmentActivity activity, String tag) {
        FragmentManager fm = activity.getSupportFragmentManager();
        Fragment fragment = fm.findFragmentByTag(tag);
        if (fragment != null) {
            FragmentTransaction trans = fm.beginTransaction();
            {
                trans.remove(fragment);
            }
            trans.commit();
            fm.executePendingTransactions();
        }
    }

    /* (non-Javadoc)
     * @see android.support.v4.app.Fragment#onPause()
     */
    @Override
    public void onPause() {
        Log.d(TAG, "*** AbstractNfcFeliCaTagFragment go Pause");
        if (this.getActivity().isFinishing()) {
            Log.d(TAG, "*** AbstractNfcFeliCaTagFragment will finishing");
            NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this.getActivity());
            adapter.disableForegroundDispatch(this.getActivity());
            sTechList = null;
        }
        super.onPause();
    }

    /* (non-Javadoc)
     * @see android.support.v4.app.Fragment#onResume()
     */
    @Override
    public void onResume() {
        Log.d(TAG, "*** AbstractNfcFeliCaTagFragment go Resume");

        //foregrandDispathch
        Activity a = this.getActivity();
        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        IntentFilter tag = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        IntentFilter tech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
        try {
            ndef.addDataType("*/*");
        } catch (MalformedMimeTypeException e) {
            throw new RuntimeException("fail", e);
        }
        IntentFilter[] filters = new IntentFilter[] { ndef, tag, tech };

        PendingIntent pendingIntent = PendingIntent.getActivity(a, 0,
                new Intent(a, a.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this.getActivity());

        adapter.enableForegroundDispatch(this.getActivity(), pendingIntent, filters, registerTechList(mTechList));

        super.onResume();
    }

    /**
     * ???
     * @param intent ?????????
     */
    public void onNewIntent(Intent intent) {
        String action = intent.getAction();
        Log.d(TAG, "incomming intent action = " + action);

        //TECHDISCOVERED
        if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)
                || NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
            mNfcTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            if (mNfcTag != null) {
                String[] tagTechs = mNfcTag.getTechList();
                for (String[] filterTechs : mTechList) {
                    if (ArrayUtil.containArray(tagTechs, filterTechs)) {
                        Log.d(TAG, "** nfcTag = " + mNfcTag.toString());
                        for (INfcTagListener listener : mListnerList) {
                            //?
                            listener.onTagDiscovered(intent, mNfcTag, this);
                        }
                    }
                }
            }
        }
    }

    /* (non-Javadoc)
     * @see android.support.v4.app.Fragment#onDestroy()
     */
    @Override
    public void onDestroy() {
        //this.unRegisterFragment(this.getActivity(), this.getTag());
        super.onDestroy();
    }

    /**
     * NfcTagLister???
     * @param listener 
     */
    public void addNfcTagListener(INfcTagListener listener) {
        mListnerList.add(listener);
    }

    /**
     * NfcTagListener???
     * @param listener 
     */
    public void removeNfcTagListener(INfcTagListener listener) {
        mListnerList.remove(listener);
    }

    /**
     * nfcTag????
     * @return Parcelable nfcTag???
     */
    public Tag getNfcTag() {
        return mNfcTag;
    }

    /**
     * _nfcTag???
     * @param nfcTag nfcTag???
     */
    public void setNfcTag(Tag nfcTag) {
        mNfcTag = nfcTag;
    }

    /**
     * techList????
     * @return String[][] techList???
     */
    public String[][] getTechList() {
        return mTechList;
    }

    /**
     * techList???
     * @param techList techList???
     */
    public void setTechList(String[]... techList) {
        mTechList = techList;
    }

    /**
     * ??NfcTag????
     * @return NfcTag ???NfcTag?
     */
    public abstract NfcTag createNfcTag();

}