Example usage for android.nfc NdefRecord createUri

List of usage examples for android.nfc NdefRecord createUri

Introduction

In this page you can find the example usage for android.nfc NdefRecord createUri.

Prototype

public static NdefRecord createUri(String uriString) 

Source Link

Document

Create a new NDEF Record containing a URI.

Use this method to encode a URI (or URL) into an NDEF Record.

Uses the well known URI type representation: #TNF_WELL_KNOWN and #RTD_URI .

Usage

From source file:com.irccloud.android.activity.MainActivity.java

@Override
public void onBufferSelected(int bid) {
    launchBid = -1;//from  w  ww .  j  a v a 2 s .  c o  m
    launchURI = null;
    cidToOpen = -1;
    bufferToOpen = null;
    setIntent(new Intent(this, MainActivity.class));

    if (suggestionsTimerTask != null)
        suggestionsTimerTask.cancel();
    sortedChannels = null;
    sortedUsers = null;

    if (drawerLayout != null) {
        drawerLayout.closeDrawers();
    }
    if (bid != -1 && conn != null && conn.getUserInfo() != null) {
        conn.getUserInfo().last_selected_bid = bid;
    }
    for (int i = 0; i < backStack.size(); i++) {
        if (buffer != null && backStack.get(i) == buffer.bid)
            backStack.remove(i);
    }
    if (buffer != null && buffer.bid >= 0 && bid != buffer.bid) {
        backStack.add(0, buffer.bid);
        buffer.draft = messageTxt.getText().toString();
    }
    if (buffer == null || buffer.bid == -1 || buffer.cid == -1 || buffer.bid == bid)
        shouldFadeIn = false;
    else
        shouldFadeIn = true;
    buffer = BuffersDataSource.getInstance().getBuffer(bid);
    if (buffer != null) {
        Crashlytics.log(Log.DEBUG, "IRCCloud",
                "Buffer selected: cid" + buffer.cid + " bid" + bid + " shouldFadeIn: " + shouldFadeIn);
        server = ServersDataSource.getInstance().getServer(buffer.cid);

        try {
            TreeMap<Long, EventsDataSource.Event> events = EventsDataSource.getInstance()
                    .getEventsForBuffer(buffer.bid);
            if (events != null) {
                events = (TreeMap<Long, EventsDataSource.Event>) events.clone();
                for (EventsDataSource.Event e : events.values()) {
                    if (e != null && e.highlight && e.from != null) {
                        UsersDataSource.User u = UsersDataSource.getInstance().getUser(buffer.bid, e.from);
                        if (u != null && u.last_mention < e.eid)
                            u.last_mention = e.eid;
                    }
                }
            }
        } catch (Exception e) {
            Crashlytics.logException(e);
        }

        try {
            if (Build.VERSION.SDK_INT >= 16 && buffer != null && server != null) {
                NfcAdapter nfc = NfcAdapter.getDefaultAdapter(this);
                if (nfc != null) {
                    String uri = "irc";
                    if (server.ssl > 0)
                        uri += "s";
                    uri += "://" + server.hostname + ":" + server.port;
                    if (buffer.type.equals("channel")) {
                        uri += "/" + URLEncoder.encode(buffer.name, "UTF-8");
                        ChannelsDataSource.Channel c = ChannelsDataSource.getInstance()
                                .getChannelForBuffer(buffer.bid);
                        if (c != null && c.hasMode("k"))
                            uri += "," + c.paramForMode("k");
                    }
                    nfc.setNdefPushMessage(new NdefMessage(NdefRecord.createUri(uri)), this);
                }
            }
        } catch (Exception e) {
        }
    } else {
        Crashlytics.log(Log.DEBUG, "IRCCloud",
                "Buffer selected but not found: bid" + bid + " shouldFadeIn: " + shouldFadeIn);
        server = null;
    }
    update_subtitle();
    final Bundle b = new Bundle();
    if (buffer != null)
        b.putInt("cid", buffer.cid);
    b.putInt("bid", bid);
    b.putBoolean("fade", shouldFadeIn);
    BuffersListFragment blf = (BuffersListFragment) getSupportFragmentManager()
            .findFragmentById(R.id.BuffersList);
    final MessageViewFragment mvf = (MessageViewFragment) getSupportFragmentManager()
            .findFragmentById(R.id.messageViewFragment);
    UsersListFragment ulf = (UsersListFragment) getSupportFragmentManager()
            .findFragmentById(R.id.usersListFragment);
    UsersListFragment ulf2 = (UsersListFragment) getSupportFragmentManager()
            .findFragmentById(R.id.usersListFragment2);
    if (mvf != null)
        mvf.ready = false;
    if (blf != null)
        blf.setSelectedBid(bid);
    if (ulf != null)
        ulf.setArguments(b);
    if (ulf2 != null)
        ulf2.setArguments(b);

    if (shouldFadeIn) {
        Crashlytics.log(Log.DEBUG, "IRCCloud", "Fade Out");
        if (Build.VERSION.SDK_INT < 16) {
            AlphaAnimation anim = new AlphaAnimation(1, 0);
            anim.setDuration(150);
            anim.setFillAfter(true);
            anim.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    if (mvf != null)
                        mvf.setArguments(b);
                    messageTxt.setText("");
                    if (buffer != null && buffer.draft != null)
                        messageTxt.append(buffer.draft);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            try {
                mvf.getListView().startAnimation(anim);
                ulf.getListView().startAnimation(anim);
            } catch (Exception e) {

            }
        } else {
            mvf.getListView().animate().alpha(0).withEndAction(new Runnable() {
                @Override
                public void run() {
                    if (mvf != null)
                        mvf.setArguments(b);
                    messageTxt.setText("");
                    if (buffer != null && buffer.draft != null)
                        messageTxt.append(buffer.draft);
                }
            });
            ulf.getListView().animate().alpha(0);
        }
        mvf.showSpinner(true);
    } else {
        if (mvf != null)
            mvf.setArguments(b);
        messageTxt.setText("");
        if (buffer != null && buffer.draft != null)
            messageTxt.append(buffer.draft);
    }

    updateUsersListFragmentVisibility();
    supportInvalidateOptionsMenu();
    if (excludeBIDTask != null)
        excludeBIDTask.cancel(true);
    excludeBIDTask = new ExcludeBIDTask();
    excludeBIDTask.execute(bid);
    if (drawerLayout != null)
        new RefreshUpIndicatorTask().execute((Void) null);
    if (buffer != null && buffer.cid != -1) {
        if (drawerLayout != null) {
            drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, Gravity.LEFT);
            getSupportActionBar().setHomeButtonEnabled(true);
        }
    }
    update_suggestions(false);
}