/
EntryPointActivity.java
201 lines (181 loc) · 7 KB
/
EntryPointActivity.java
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package org.ruboto;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
/**
* This Activity acts as an entry point to the app. It must initialize the
* JRuby runtime before continuing its life cycle.
* While JRuby is initializing, a progress dialog is shown.
* If R.layout.splash is defined, by adding a res/layout/splash.xml file,
* this layout is displayed instead of the progress dialog.
*/
public class EntryPointActivity extends org.ruboto.RubotoActivity {
private int splash = 0;
private ProgressDialog loadingDialog;
private boolean dialogCancelled = false;
private BroadcastReceiver receiver;
// FIXME(uwe): Remove this field? Duplicated by ScriptInfo.isLoaded() ?
protected boolean appStarted = false;
// EMXIF
public void onCreate(Bundle bundle) {
Log.d("EntryPointActivity onCreate:");
try {
splash = Class.forName(getPackageName() + ".R$layout").getField("splash").getInt(null);
} catch (Exception e) {
splash = -1;
}
if (JRubyAdapter.isInitialized()) {
appStarted = true;
} else {
showProgress();
initJRuby(true);
}
super.onCreate(bundle);
}
public void onResume() {
Log.d("onResume: ");
if(appStarted) {
Log.d("onResume: App already started!");
super.onResume();
return;
}
Log.d("onResume: Checking JRuby");
if (JRubyAdapter.isInitialized()) {
Log.d("Already initialized");
fireRubotoActivity();
} else {
Log.d("Not initialized");
super.onResume();
}
}
public void onPause() {
Log.d("onPause: ");
if (receiver != null) {
unregisterReceiver(receiver);
receiver = null;
}
super.onPause();
}
public void onDestroy() {
Log.d("onDestroy: ");
super.onDestroy();
if (dialogCancelled) {
System.runFinalizersOnExit(true);
System.exit(0);
}
}
private void initJRuby(final boolean firstTime) {
new Thread(new Runnable() {
public void run() {
final boolean jrubyOk = JRubyAdapter.setUpJRuby(EntryPointActivity.this);
if (jrubyOk) {
Log.d("onResume: JRuby OK");
fireRubotoActivity();
} else {
registerPackageInstallReceiver();
runOnUiThread(new Runnable() {
public void run() {
if (firstTime) {
Log.d("onResume: Checking JRuby - IN UI thread");
try {
setContentView(Class.forName(getPackageName() + ".R$layout").getField("get_ruboto_core").getInt(null));
} catch (Exception e) {
}
} else {
Toast.makeText(EntryPointActivity.this,"Failed to initialize Ruboto Core.",Toast.LENGTH_SHORT).show();
try {
TextView textView = (TextView) findViewById(Class.forName(getPackageName() + ".R$id").getField("text").getInt(null));
textView.setText("Woops! Ruboto Core was installed, but it failed to initialize properly! I am not sure how to proceed from here. If you can, please file an error report at http://ruboto.org/");
} catch (Exception e) {
}
}
hideProgress();
}
});
}
}
}).start();
}
private static final String RUBOTO_APK = "RubotoCore-release.apk";
private static final String RUBOTO_URL = "https://raw.github.com/ruboto/ruboto-core/master/dist/" + RUBOTO_APK;
// Called when the button is pressed.
public void getRubotoCore(View view) {
try {
startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("market://details?id=org.ruboto.core")));
} catch (android.content.ActivityNotFoundException anfe) {
try {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(RUBOTO_URL));
startActivity(intent);
} catch (Exception e) {}
}
}
protected void fireRubotoActivity() {
if(appStarted) return;
appStarted = true;
Log.i("Starting activity");
ScriptLoader.loadScript(this);
runOnUiThread(new Runnable() {
public void run() {
ScriptLoader.callOnCreate(EntryPointActivity.this, args[0]);
onStart();
onResume();
hideProgress();
}
});
}
private void showProgress() {
if (loadingDialog == null) {
if (splash > 0) {
Log.i("Showing splash");
requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
setContentView(splash);
} else {
Log.i("Showing progress");
loadingDialog = ProgressDialog.show(this, null, "Starting...", true, true);
loadingDialog.setCanceledOnTouchOutside(false);
loadingDialog.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface dialog) {
dialogCancelled = true;
finish();
}
});
}
}
}
private void hideProgress() {
if (loadingDialog != null) {
Log.d("Hide progress");
loadingDialog.dismiss();
loadingDialog = null;
}
}
private void registerPackageInstallReceiver() {
receiver = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent) {
Log.i("received broadcast: " + intent);
Log.i("URI: " + intent.getData());
if (intent.getData().toString().equals("package:org.ruboto.core")) {
Toast.makeText(context,"Ruboto Core is now installed.",Toast.LENGTH_SHORT).show();
if (receiver != null) {
unregisterReceiver(receiver);
receiver = null;
}
showProgress();
initJRuby(false);
}
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
filter.addDataScheme("package");
registerReceiver(receiver, filter);
}
}