AI2 Component  (Version nb184)
NearField.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2012-2014 MIT, All rights reserved
3 // Released under the Apache License, Version 2.0
4 // http://www.apache.org/licenses/LICENSE-2.0
5 
6 package com.google.appinventor.components.runtime;
7 
8 import android.app.Activity;
9 import android.content.Intent;
10 import android.nfc.NfcAdapter;
11 import android.util.Log;
12 
25 
35 @DesignerComponent(version = YaVersion.NEARFIELD_COMPONENT_VERSION,
36  description = "<p>Non-visible component to provide NFC capabilities. " +
37  "For now this component supports the reading and writing of text tags only " +
38  "(if supported by the device)</p>" +
39  "<p>In order to read and write text tags, the component must have its " +
40  "<code>ReadMode</code> property set to True or False respectively.</p>" +
41  "<p><strong>Note:</strong> This component will only work on Screen1 of" +
42  " any App Inventor app.</p>",
43  category = ComponentCategory.SENSORS,
44  nonVisible = true,
45  iconName = "images/nearfield.png")
46 
47 @SimpleObject
48 @UsesPermissions(permissionNames = "android.permission.NFC")
51  private static final String TAG = "nearfield";
52  private Activity activity;
53 
54  private NfcAdapter nfcAdapter;
55  private boolean readMode = true;
56  private int writeType;
57  private String tagContent = "";
58  private String textToWrite = "";
59 
60  /* Used to identify the call to startActivityForResult. Will be passed back into the
61  resultReturned() callback method. */
62  protected int requestCode;
63 
68  public NearField(ComponentContainer container) {
69  super(container.$form());
70  activity = container.$context();
71  writeType = 1;
72  nfcAdapter = (SdkLevel.getLevel() >= SdkLevel.LEVEL_GINGERBREAD)
73  ? GingerbreadUtil.newNfcAdapter(activity)
74  : null;
75  // register with the forms to that OnResume and OnNewIntent
76  // messages get sent to this component
77  form.registerForOnResume(this);
78  form.registerForOnNewIntent(this);
79  form.registerForOnPause(this);
80  Log.d(TAG, "Nearfield component created");
81  }
82 
83  // Events
84 
93  public void TagRead(String message) {
94  Log.d(TAG, "Tag read: got message " + message);
95  tagContent = message;
96  EventDispatcher.dispatchEvent(this, "TagRead", message);
97  }
98 
102  @SimpleEvent
103  public void TagWritten() {
104  Log.d(TAG, "Tag written: " + textToWrite);
105  EventDispatcher.dispatchEvent(this, "TagWritten");
106  }
107 
108  // Properties
109 
113  @SimpleProperty(category = PropertyCategory.BEHAVIOR)//what does this mean?
114  public String LastMessage() {
115  Log.d(TAG, "String message method stared");
116  return tagContent;
117  }
118 
123  defaultValue = "True")
124 
126  public boolean ReadMode() {
127  Log.d(TAG, "boolean method stared");
128  return readMode;
129  }
130 
134  @SimpleProperty(category = PropertyCategory.BEHAVIOR)//what does this mean?
135  public String TextToWrite() {
136  Log.d(TAG, "String message method stared");
137  return textToWrite;
138  }
139 
145  public int WriteType() {
146  return writeType;
147  }
148 
154  description = "Specifies whether the NFC hardware should operate in read or write mode.")
155  public void ReadMode(boolean newMode) {
156  Log.d(TAG, "Read mode set to" + newMode);
157  readMode = newMode;
158  if(!readMode && SdkLevel.getLevel() >= SdkLevel.LEVEL_GINGERBREAD){
159  GingerbreadUtil.enableNFCWriteMode(activity, nfcAdapter, textToWrite);
160  }
161  }
162 
168  description = "Specifies the content that will be written to the tag when in write mode. "
169  + "This method has no effect if ReadMode is true.")
170  public void TextToWrite(String newText) {
171  Log.d(TAG, "Text to write set to" + newText);
172  textToWrite = newText;
173  if(!readMode && writeType == 1){
175  GingerbreadUtil.enableNFCWriteMode(activity, nfcAdapter, textToWrite);
176  }
177  }
178  }
179 
180  // When NFC is detected, the form's onNewIntent method is triggered (because of the
181  // specification in the manifest. The form then sends that intent here.
182  @Override
183  public void onNewIntent(Intent intent) {
184  Log.d(TAG, "Nearfield on onNewIntent. Intent is: " + intent);
185  resolveIntent(intent);
186  }
187 
188  // TODO: Re-enable NFC communication if it had been disabled
189  @Override
190  public void onResume() {
191  Intent intent = activity.getIntent();
192  Log.d(TAG, "Nearfield on onResume. Intent is: " + intent);
193  }
194 
195  void resolveIntent(Intent intent) {
196  Log.d(TAG, "resolve intent. Intent is: " + intent);
197  // Parse the intent
199  GingerbreadUtil.resolveNFCIntent(intent, this);
200  }
201  }
202 
203  // TODO: Disable NFC communication in onPause and onDelete
204  // and restore it in onResume
205 
206  public void onPause() {
207  Log.d(TAG, "OnPause method started.");
208  if (nfcAdapter != null) {
209  GingerbreadUtil.disableNFCAdapter(activity, nfcAdapter);
210  }
211  //nfcAdapter.disableForegroundDispatch(activity);
212  }
213 
214  @Override
215  public void onDelete() {
216  // TODO Auto-generated method stub
217  // need to delete the nearfieldActivity
218 
219  }
220 
221  @Override
222  public void onStop() {
223  // TODO Auto-generated method stub
224  }
225 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.runtime.util.SdkLevel.LEVEL_GINGERBREAD
static final int LEVEL_GINGERBREAD
Definition: SdkLevel.java:26
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
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.onResume
void onResume()
Definition: NearField.java:190
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
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.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.runtime.NearField
Definition: NearField.java:49
com.google.appinventor.components
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN
static final String PROPERTY_TYPE_BOOLEAN
Definition: PropertyTypeConstants.java:35
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.annotations.SimpleEvent
Definition: SimpleEvent.java:20
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.runtime.NearField.onPause
void onPause()
Definition: NearField.java:206
com.google.appinventor.components.runtime.OnNewIntentListener
Definition: OnNewIntentListener.java:13
com.google.appinventor.components.runtime.NearField.TagWritten
void TagWritten()
Definition: NearField.java:103
com.google.appinventor.components.annotations.UsesPermissions
Definition: UsesPermissions.java:21
com.google.appinventor.components.runtime.OnResumeListener
Definition: OnResumeListener.java:14
com.google.appinventor.components.runtime.util.GingerbreadUtil.resolveNFCIntent
static void resolveNFCIntent(Intent intent, NearField nfc)
Definition: GingerbreadUtil.java:98
com.google.appinventor.components.runtime.EventDispatcher.dispatchEvent
static boolean dispatchEvent(Component component, String eventName, Object...args)
Definition: EventDispatcher.java:188
com.google.appinventor.components.runtime.AndroidNonvisibleComponent
Definition: AndroidNonvisibleComponent.java:17
com.google.appinventor.components.runtime.util.SdkLevel
Definition: SdkLevel.java:19
com.google.appinventor.components.runtime.util.GingerbreadUtil.disableNFCAdapter
static void disableNFCAdapter(Activity activity, NfcAdapter nfcAdapter)
Definition: GingerbreadUtil.java:79
com.google.appinventor.components.runtime.OnPauseListener
Definition: OnPauseListener.java:14
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.util.GingerbreadUtil
Definition: GingerbreadUtil.java:36
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.NearField.onStop
void onStop()
Definition: NearField.java:222
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime.util.SdkLevel.getLevel
static int getLevel()
Definition: SdkLevel.java:45
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.Deleteable
Definition: Deleteable.java:15
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.runtime.OnStopListener
Definition: OnStopListener.java:15
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google
com
com.google.appinventor.components.runtime.NearField.onNewIntent
void onNewIntent(Intent intent)
Definition: NearField.java:183
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.runtime.NearField.onDelete
void onDelete()
Definition: NearField.java:215
com.google.appinventor.components.runtime.NearField.NearField
NearField(ComponentContainer container)
Definition: NearField.java:68
com.google.appinventor.components.runtime.NearField.requestCode
int requestCode
Definition: NearField.java:62
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor