AI2 Component  (Version nb184)
GingerbreadUtil.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime.util;
8 
9 import android.app.Activity;
10 import android.content.Intent;
11 import android.content.Context;
12 import android.nfc.NdefMessage;
13 import android.nfc.NdefRecord;
14 import android.nfc.NfcAdapter;
15 import android.nfc.Tag;
16 import android.nfc.tech.Ndef;
17 import android.nfc.tech.NdefFormatable;
18 import android.os.Parcelable;
19 import android.util.Log;
20 
22 
23 import java.net.CookieHandler;
24 import java.net.CookieManager;
25 import java.net.CookieStore;
26 
27 import java.io.IOException;
28 import java.nio.charset.Charset;
29 import java.util.Locale;
30 
36 public class GingerbreadUtil {
37  private GingerbreadUtil() {
38  }
39 
43  public static CookieHandler newCookieManager() {
44  return new CookieManager();
45  }
46 
54  public static boolean clearCookies(CookieHandler cookieHandler) {
55  if (cookieHandler instanceof CookieManager) {
56  CookieManager cookieManager = (CookieManager) cookieHandler;
57  CookieStore cookieStore = cookieManager.getCookieStore();
58  if (cookieStore != null) {
59  cookieStore.removeAll();
60  return true;
61  }
62  }
63  return false;
64  }
65 
69  public static NfcAdapter newNfcAdapter(Context context) {
70  return NfcAdapter.getDefaultAdapter(context);
71  }
72 
73  public static void enableNFCWriteMode(Activity activity, NfcAdapter nfcAdapter, String textToWrite){
74  NdefRecord textRecord = createTextRecord(textToWrite,true);
75  NdefMessage msg = new NdefMessage(new NdefRecord[] { textRecord });
76  nfcAdapter.enableForegroundNdefPush(activity, msg);
77  }
78 
79  public static void disableNFCAdapter(Activity activity, NfcAdapter nfcAdapter){
80  nfcAdapter.disableForegroundNdefPush(activity);
81  }
82 
83  public static NdefRecord createTextRecord(String payload, boolean encodeInUtf8) {
84  byte[] langBytes = Locale.getDefault().getLanguage().getBytes(Charset.forName("US-ASCII"));
85  Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
86  byte[] textBytes = payload.getBytes(utfEncoding);
87  int utfBit = encodeInUtf8 ? 0 : (1 << 7);
88  char status = (char) (utfBit + langBytes.length);
89  byte[] data = new byte[1 + langBytes.length + textBytes.length];
90  data[0] = (byte) status;
91  System.arraycopy(langBytes, 0, data, 1, langBytes.length);
92  System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);
93  NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
94  NdefRecord.RTD_TEXT, new byte[0], data);
95  return record;
96  }
97 
98  public static void resolveNFCIntent(Intent intent, NearField nfc){
99  String action = intent.getAction();
100  //activity.setIntent(new Intent()); // Consume this intent. Is this the right thing?
101  if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
102  // When a tag is discovered we send it to the service to be save. We
103  // include a PendingIntent for the service to call back onto. This
104  // will cause this activity to be restarted with onNewIntent(). At
105  // that time we read it from the database and view it.
106  // We'll keep this database code in here for now, but it's useless, because we
107  // can use AppInventor higher level operations to manipulate the tag data.
108 
109  if (nfc.ReadMode()) {
110 
111  Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
112  NdefMessage[] msgs;
113  if (rawMsgs != null) {
114  msgs = new NdefMessage[rawMsgs.length];
115  for (int i = 0; i < rawMsgs.length; i++) {
116  msgs[i] = (NdefMessage) rawMsgs[i];
117  }
118  } else {
119  // Unknown tag type
120  // For now, just ignore it. Later we might want to signal an error to the
121  // app user.
122  byte[] empty = new byte[] {};
123  NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
124  NdefMessage msg = new NdefMessage(new NdefRecord[] {record});
125  msgs = new NdefMessage[] {msg};
126  }
127  byte[] payload = msgs[0].getRecords()[0].getPayload();
128  //the substring chops off the two language encoding bits at the beginning
129  String message = new String(payload).substring(3);
130  nfc.TagRead(message);
131  } else {
132  Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
133  NdefMessage msg = null;
134  if(nfc.WriteType() == 1) {
135  NdefRecord textRecord = createTextRecord(nfc.TextToWrite(),true);
136  msg = new NdefMessage(new NdefRecord[] { textRecord });
137  }
138  if (writeNFCTag(msg, detectedTag)) {
139  nfc.TagWritten();
140  }
141  }
142  } else {
143  Log.e("nearfield", "Unknown intent " + intent);
144  }
145  }
146 
147  /*
148  * Writes an NdefMessage to a NFC tag
149  */
150  public static boolean writeNFCTag(NdefMessage message, Tag tag) {
151  int size = message.toByteArray().length;
152  try {
153  Ndef ndef = Ndef.get(tag);
154  if (ndef != null) {
155  ndef.connect();
156  if (!ndef.isWritable()) {
157  return false;
158  }
159  if (ndef.getMaxSize() < size) {
160  return false;
161  }
162  ndef.writeNdefMessage(message);
163  return true;
164  } else {
165  NdefFormatable format = NdefFormatable.get(tag);
166  if (format != null) {
167  try {
168  format.connect();
169  format.format(message);
170  return true;
171  } catch (IOException e) {
172  return false;
173  }
174  } else {
175  return false;
176  }
177  }
178  } catch (Exception e) {
179  return false;
180  }
181  }
182 }
com.google.appinventor.components.runtime.util.GingerbreadUtil.enableNFCWriteMode
static void enableNFCWriteMode(Activity activity, NfcAdapter nfcAdapter, String textToWrite)
Definition: GingerbreadUtil.java:73
com.google.appinventor.components.runtime.NearField.TagRead
void TagRead(String message)
Definition: NearField.java:93
com.google.appinventor.components.runtime.util.GingerbreadUtil.newNfcAdapter
static NfcAdapter newNfcAdapter(Context context)
Definition: GingerbreadUtil.java:69
com.google.appinventor.components.runtime.NearField
Definition: NearField.java:49
com.google.appinventor.components
com.google.appinventor.components.runtime.util.GingerbreadUtil.createTextRecord
static NdefRecord createTextRecord(String payload, boolean encodeInUtf8)
Definition: GingerbreadUtil.java:83
com.google.appinventor.components.runtime.NearField.TagWritten
void TagWritten()
Definition: NearField.java:103
com.google.appinventor.components.runtime.NearField.WriteType
int WriteType()
Definition: NearField.java:145
com.google.appinventor.components.runtime.NearField.TextToWrite
String TextToWrite()
Definition: NearField.java:135
com.google.appinventor.components.runtime.util.GingerbreadUtil.resolveNFCIntent
static void resolveNFCIntent(Intent intent, NearField nfc)
Definition: GingerbreadUtil.java:98
com.google.appinventor.components.runtime.util.GingerbreadUtil.newCookieManager
static CookieHandler newCookieManager()
Definition: GingerbreadUtil.java:43
com.google.appinventor.components.runtime.util.GingerbreadUtil.disableNFCAdapter
static void disableNFCAdapter(Activity activity, NfcAdapter nfcAdapter)
Definition: GingerbreadUtil.java:79
com.google.appinventor.components.runtime.util.GingerbreadUtil
Definition: GingerbreadUtil.java:36
com.google.appinventor.components.runtime.NearField.ReadMode
boolean ReadMode()
Definition: NearField.java:126
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.util.GingerbreadUtil.clearCookies
static boolean clearCookies(CookieHandler cookieHandler)
Definition: GingerbreadUtil.java:54
com.google.appinventor.components.runtime.util.GingerbreadUtil.writeNFCTag
static boolean writeNFCTag(NdefMessage message, Tag tag)
Definition: GingerbreadUtil.java:150
com.google
com
com.google.appinventor