-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathColinTreeFirstRun.java
More file actions
89 lines (76 loc) · 3.4 KB
/
ColinTreeFirstRun.java
File metadata and controls
89 lines (76 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package cn.colintree.aix.ColinTreeFirstRun;
import android.content.Context;
import android.util.Log;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.runtime.util.*;
import com.google.appinventor.components.runtime.errors.YailRuntimeError;
import android.content.SharedPreferences;
import org.json.JSONException;
@DesignerComponent(version = ColinTreeFirstRun.VERSION,
description = "by ColinTree at http://aix.colintree.cn/",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "images/extension.png")
@SimpleObject(external = true)
public class ColinTreeFirstRun extends AndroidNonvisibleComponent implements Component {
public static final int VERSION = 1;
private ComponentContainer container;
private Context context;
private SharedPreferences sharedPreferences;
private static final String LOG_TAG = "ColinTreeFirstRun";
private FirstRun firstRun;
public ColinTreeFirstRun(ComponentContainer container) {
super(container.$form());
this.container = container;
context = (Context) container.$context();
Log.d(LOG_TAG, "FirstRun Created" );
firstRun=new FirstRun(container);
}
@SimpleFunction(description = "return if it is first time to run, if it is true, set a flag.")
public boolean IsFirstRun() {
return firstRun.IsFirstRun();
}
@SimpleFunction(description = "")
public void ClearFirstRunFlag() {
firstRun.ClearFirstRunFlag();
}
public class FirstRun{
final String tag="ColinTreeFirstRun";
final Object valueToStore="false";
private SharedPreferences sharedPreferences;
public FirstRun(ComponentContainer container) {
sharedPreferences = context.getSharedPreferences("ColinTreeFirstRun", Context.MODE_PRIVATE);
}
public boolean IsFirstRun() {
if (GetValue(tag,"FirstRun!!!!!")=="FirstRun!!!!!"||GetValue(tag,"FirstRun!!!!!")==valueToStore){
StoreValue(tag,valueToStore);
return true;
}
return false;
}
public void ClearFirstRunFlag() {
final SharedPreferences.Editor sharedPrefsEditor = sharedPreferences.edit();
sharedPrefsEditor.clear();
sharedPrefsEditor.commit();
}
private void StoreValue(final String tag, final Object valueToStore) {
final SharedPreferences.Editor sharedPrefsEditor = sharedPreferences.edit();
try {
sharedPrefsEditor.putString(tag, JsonUtil.getJsonRepresentation(valueToStore));
sharedPrefsEditor.commit();
} catch (JSONException e) {
throw new YailRuntimeError("Value failed to convert to JSON.", "JSON Creation Error.");
}
}
private Object GetValue(final String tag, final Object valueIfTagNotThere) {
try {
String value = sharedPreferences.getString(tag, "");
return (value.length() == 0) ? valueIfTagNotThere : JsonUtil.getObjectFromJson(value);
} catch (JSONException e) {
throw new YailRuntimeError("Value failed to convert from JSON.", "JSON Creation Error.");
}
}
}
}