AI2 Component  (Version nb184)
TinyDB.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 package com.google.appinventor.components.runtime;
7 
19 
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Map;
24 
25 import android.content.Context;
26 import android.content.SharedPreferences;
27 
28 import org.json.JSONException;
29 
53 @DesignerComponent(version = YaVersion.TINYDB_COMPONENT_VERSION,
54  description = "TinyDB is a non-visible component that stores data for an app. " +
55  "<p> Apps created with App Inventor are initialized each time they run: " +
56  "If an app sets the value of a variable and the user then quits the app, " +
57  "the value of that variable will not be remembered the next time the app is run. " +
58  "In contrast, TinyDB is a <em> persistent </em> data store for the app, " +
59  "that is, the data stored there will be available each time the app is " +
60  "run. An example might be a game that saves the high score and " +
61  "retrieves it each time the game is played. </<p> " +
62  "<p> Data items are strings stored under <em>tags</em> . To store a data " +
63  "item, you specify the tag it should be stored under. Subsequently, you " +
64  "can retrieve the data that was stored under a given tag. </p>" +
65  "<p> There is only one data store per app. Even if you have multiple TinyDB " +
66  "components, they will use the same data store. To get the effect of " +
67  "separate stores, use different keys. Also each app has its own data " +
68  "store. You cannot use TinyDB to pass data between two different apps on " +
69  "the phone, although you <em>can</em> use TinyDb to shares data between the " +
70  "different screens of a multi-screen app. </p> " +
71  "<p>When you are developing apps using the AI Companion, all the apps " +
72  "using that companion will share the same TinyDb. That sharing will disappear " +
73  "once the apps are packaged. But, during development, you should be careful to clear " +
74  "the TinyDb each time you start working on a new app.</p>",
75  category = ComponentCategory.STORAGE,
76  nonVisible = true,
77  iconName = "images/tinyDB.png")
78 
79 @SimpleObject
80 public class TinyDB extends AndroidNonvisibleComponent implements Component, Deleteable {
81 
82  public static final String DEFAULT_NAMESPACE="TinyDB1";
83 
84  private SharedPreferences sharedPreferences;
85  private String namespace;
86 
87  private Context context; // this was a local in constructor and final not private
88 
89 
95  public TinyDB(ComponentContainer container) {
96  super(container.$form());
97  context = (Context) container.$context();
99  }
100 
112  @SimpleProperty(description = "Namespace for storing data.", category = PropertyCategory.BEHAVIOR)
114  public void Namespace(String namespace) {
115  this.namespace = namespace;
116  sharedPreferences = context.getSharedPreferences(namespace, Context.MODE_PRIVATE);
117  }
118 
119  @SimpleProperty(description = "Namespace for storing data.")
120  public String Namespace() {
121  return namespace;
122  }
123 
132  @SimpleFunction(description = "Store the given value under the given tag. The storage persists "
133  + "on the phone when the app is restarted.")
134  public void StoreValue(final String tag, final Object valueToStore) {
135  final SharedPreferences.Editor sharedPrefsEditor = sharedPreferences.edit();
136  try {
137  sharedPrefsEditor.putString(tag, JsonUtil.getJsonRepresentation(valueToStore));
138  sharedPrefsEditor.commit();
139  } catch (JSONException e) {
140  throw new YailRuntimeError("Value failed to convert to JSON.", "JSON Creation Error.");
141  }
142  }
143 
153  @SimpleFunction(description = "Retrieve the value stored under the given tag. If there's no "
154  + "such tag, then return valueIfTagNotThere.")
155  public Object GetValue(final String tag, final Object valueIfTagNotThere) {
156  try {
157  String value = sharedPreferences.getString(tag, "");
158  // If there's no entry with tag as a key then return the empty string.
159  // was return (value.length() == 0) ? "" : JsonUtil.getObjectFromJson(value);
160  return (value.length() == 0) ? valueIfTagNotThere : JsonUtil.getObjectFromJson(value, true);
161  } catch (JSONException e) {
162  throw new YailRuntimeError("Value failed to convert from JSON.", "JSON Creation Error.");
163  }
164  }
165 
171  @SimpleFunction(description = "Return a list of all the tags in the data store.")
172  public Object GetTags() {
173  List<String> keyList = new ArrayList<String>();
174  Map<String,?> keyValues = sharedPreferences.getAll();
175  // here is the simple way to get keys
176  keyList.addAll(keyValues.keySet());
177  java.util.Collections.sort(keyList);
178  return keyList;
179  }
180 
185  @SimpleFunction(description = "Clear the entire data store.")
186  public void ClearAll() {
187  final SharedPreferences.Editor sharedPrefsEditor = sharedPreferences.edit();
188  sharedPrefsEditor.clear();
189  sharedPrefsEditor.commit();
190  }
191 
197  @SimpleFunction(description = "Clear the entry with the given tag.")
198  public void ClearTag(final String tag) {
199  final SharedPreferences.Editor sharedPrefsEditor = sharedPreferences.edit();
200  sharedPrefsEditor.remove(tag);
201  sharedPrefsEditor.commit();
202  }
203 
204  @Override
205  public void onDelete() {
206  final SharedPreferences.Editor sharedPrefsEditor = sharedPreferences.edit();
207  sharedPrefsEditor.clear();
208  sharedPrefsEditor.commit();
209  }
210 }
com.google.appinventor.components.runtime.TinyDB.StoreValue
void StoreValue(final String tag, final Object valueToStore)
Definition: TinyDB.java:134
com.google.appinventor.components.runtime.TinyDB.Namespace
String Namespace()
Definition: TinyDB.java:120
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_STRING
static final String PROPERTY_TYPE_STRING
Definition: PropertyTypeConstants.java:237
com.google.appinventor.components
com.google.appinventor.components.runtime.TinyDB.GetTags
Object GetTags()
Definition: TinyDB.java:172
com.google.appinventor.components.runtime.util.JsonUtil
Definition: JsonUtil.java:42
com.google.appinventor.components.runtime.TinyDB.DEFAULT_NAMESPACE
static final String DEFAULT_NAMESPACE
Definition: TinyDB.java:82
com.google.appinventor.components.runtime.TinyDB.ClearAll
void ClearAll()
Definition: TinyDB.java:186
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.runtime.TinyDB.onDelete
void onDelete()
Definition: TinyDB.java:205
com.google.appinventor.components.runtime.TinyDB
Definition: TinyDB.java:80
com.google.appinventor.components.runtime.AndroidNonvisibleComponent
Definition: AndroidNonvisibleComponent.java:17
com.google.appinventor.components.runtime.errors.YailRuntimeError
Definition: YailRuntimeError.java:14
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.Component
Definition: Component.java:17
com.google.appinventor.components.runtime.Map
Definition: Map.java:84
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.util.JsonUtil.getObjectFromJson
static Object getObjectFromJson(String jsonString)
Definition: JsonUtil.java:317
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google
com
com.google.appinventor.components.runtime.errors
Definition: ArrayIndexOutOfBoundsError.java:7
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.runtime.TinyDB.TinyDB
TinyDB(ComponentContainer container)
Definition: TinyDB.java:95
com.google.appinventor.components.runtime.util.JsonUtil.getJsonRepresentation
static String getJsonRepresentation(Object value)
Definition: JsonUtil.java:246
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor.components.runtime.TinyDB.ClearTag
void ClearTag(final String tag)
Definition: TinyDB.java:198
com.google.appinventor
com.google.appinventor.components.runtime.TinyDB.GetValue
Object GetValue(final String tag, final Object valueIfTagNotThere)
Definition: TinyDB.java:155