Android Open Source - android-fragment-collection Android Beam Fragment






From Project

Back to project page android-fragment-collection.

License

The source code is released under:

MIT License

If you think the Android project android-fragment-collection 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 net.vvakame.fragmentcollection;
/*from  w  w  w  . j a  v  a 2  s . c  o m*/
import java.nio.charset.Charset;
import java.util.List;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcAdapter.OnNdefPushCompleteCallback;
import android.nfc.NfcEvent;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Parcelable;
import android.provider.Settings;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import net.vvakame.fragmentcollection.androidbeam.R;

/**
 * Android Beam??????????????????????Fragment.
 *
 * @author vvakame
 */
public class AndroidBeamFragment extends Fragment implements
        CreateNdefMessageCallback, OnNdefPushCompleteCallback {

    static final String TAG = AndroidBeamFragment.class.getSimpleName();

    /**
     * {@link AndroidBeamFragment.BeamActionCallback} ?????????????????Picker.
     *
     * @author vvakame
     */
    public static interface BeamActionCallbackPicker {
        public BeamActionCallback getBeamActionCallback();
    }

    /**
     * Beam????????????????????????????????????????????????.
     *
     * @author vvakame
     */
    public static interface BeamActionCallback {
        /**
         * NFC ???????????????????????????????????.
         */
        public void onNfcNotSupported();

        /**
         * NFC ?????????????????????????????????????.
         */
        public void onNfcDisabled();

        /**
         * Beam???????????????????????????????????.
         */
        public void onBeamReceived(byte[] msg);

        /**
         * Beam???????????????????.
         */
        public byte[] onBeamSendPreprocess();

        /**
         * Beam??????????????????????????????.
         */
        public void onBeamSendComplete();
    }

    NfcAdapter mNfcAdapter;

    BeamActionCallback mCallback;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        Context context = getActivity();
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);

            // Permission????????
            if (packageInfo.requestedPermissions == null) {
                throw new IllegalStateException(
                        "Android Beam required 1 permission and 1 feature\n" +
                                "<uses-permission android:name=\"android.permission.NFC\" />\n" +
                                "<uses-feature android:name=\"android.hardware.nfc\" />");
            }
            boolean existsNFCPermission = false;
            for (String permission : packageInfo.requestedPermissions) {
                if ("android.permission.NFC".equals(permission)) {
                    existsNFCPermission = true;
                }
            }
            if (!existsNFCPermission) {
                throw new IllegalStateException(
                        "Android Beam required 1 permission\n" +
                                "<uses-permission android:name=\"android.permission.NFC\" />");
            }

            // intent filter????????
            Intent intent = new Intent("android.nfc.action.NDEF_DISCOVERED");
            intent.setPackage(context.getPackageName());
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.setType("application/" + context.getPackageName());
            List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);
            boolean exsitsThisActivity = false;
            for (ResolveInfo info : resolveInfos) {
                if (getActivity().getClass().getCanonicalName().equals(info.activityInfo.name)) {
                    exsitsThisActivity = true;
                }
            }
            if (!exsitsThisActivity) {
                throw new IllegalStateException(
                        "activity required has a intent-filter\n" +
                                "<intent-filter>\n" +
                                "<action android:name=\"android.nfc.action.NDEF_DISCOVERED\" />\n" +
                                "<category android:name=\"android.intent.category.DEFAULT\" />\n" +
                                "<data android:mimeType=\"application/" + context.getPackageName() + "\" />" +
                                "</intent-filter>");
            }
        } catch (PackageManager.NameNotFoundException e) {
            throw new IllegalStateException("package name " + context.getPackageName() + " not exsists", e);
        }

        // ??????????????
        if (activity instanceof BeamActionCallback) {
            mCallback = (BeamActionCallback) activity;
        } else if (activity instanceof BeamActionCallbackPicker) {
            BeamActionCallbackPicker picker = (BeamActionCallbackPicker) activity;
            mCallback = picker.getBeamActionCallback();
        } else {
            throw new IllegalStateException("acitivity must implemented BeamActionCallback or BeamActionCallbackPicker");
        }

        // NFC??????????
        mNfcAdapter = NfcAdapter.getDefaultAdapter(activity);
        if (mNfcAdapter == null) {
            mCallback.onNfcNotSupported();
            return;
        } else if (mNfcAdapter.isEnabled() == false) {
            mCallback.onNfcDisabled();
        }

        // ????????????
        mNfcAdapter.setNdefPushMessageCallback(this, activity);
        mNfcAdapter.setOnNdefPushCompleteCallback(this, activity);
    }

    /**
     * NdefMessage?????????????? (=Beam???????????????????????) ?????.
     */
    @Override
    public NdefMessage createNdefMessage(NfcEvent event) {
        String packageName = getActivity().getPackageName();
        byte[] msgBytes = mCallback.onBeamSendPreprocess();
        NdefRecord msg = createMimeRecord("application/" + packageName,
                msgBytes);
        NdefRecord aar = NdefRecord.createApplicationRecord(packageName);

        // AAR????????
        NdefMessage ndefMessage = new NdefMessage(new NdefRecord[]{msg, aar});

        return ndefMessage;
    }

    /**
     * ?????????????. ??????????mimeType??????????????????????????????????.
     *
     * @param mimeType ?????mimeType
     * @param payload  ??
     * @return ????????? NdefRecord
     */
    public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
        byte[] mimeBytes = mimeType.getBytes(Charset.forName("UTF-8"));
        NdefRecord mimeRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
                mimeBytes, new byte[0], payload);
        return mimeRecord;
    }

    /**
     * Beam??????????????. ???????????? {@link AndroidBeamFragment.BeamActionCallback} ???????.<br>
     * onNdefPushComplete ???UI??????????????????????UI????????????????????????????.
     */
    @Override
    public void onNdefPushComplete(NfcEvent event) {
        new Handler(Looper.getMainLooper()) {
            @Override
            public void dispatchMessage(Message msg) {
                mCallback.onBeamSendComplete();
            }

        }.sendEmptyMessage(0);
    }

    int mIntentHashCode = 0;

    /**
     * NDEF?????Intent?????????????????????????????????????.
     */
    @Override
    public void onResume() {
        super.onResume();

        Intent intent = getActivity().getIntent();
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())
                && mIntentHashCode != intent.hashCode()) {
            processIntent(intent);
            mIntentHashCode = intent.hashCode();
        }
    }

    /**
     * NdefMessage???????????Intent????????????.
     * ?????????????????????????NFC??????????????????????????????????????????????????????????.
     */
    void processIntent(Intent intent) {
        Parcelable[] rawMsgs = intent
                .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage msg = (NdefMessage) rawMsgs[0];
        mCallback.onBeamReceived(msg.getRecords()[0].getPayload());
    }

    /**
     * ????????????. NFC??????????????????????????????????.
     */
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        if (mNfcAdapter == null) {
            return;
        }
        inflater.inflate(R.menu.android_beam, menu);
    }

    /**
     * ???????????????????????????.
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.beam_settings) {
            Intent intent;
            if (Build.VERSION.SDK_INT < 15) {
                intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
            } else {
                intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
            }
            startActivity(intent);
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }
}




Java Source Code List

net.vvakame.fragmentcollection.AndroidBeamFragment.java
net.vvakame.fragmentcollection.usage.MainActivity.java