/
strings.go
334 lines (287 loc) · 8.43 KB
/
strings.go
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"bufio"
"bytes"
"fmt"
"strings"
"text/template"
)
const webapp = `
import (
"fmt"
"net/http"
"time"
"github.com/srinathh/mobilehtml5app/contextrouter"
"github.com/srinathh/mobilehtml5app/server"
"golang.org/x/net/context"
)
// App implements a web server backend for an android app
type App struct {
srv *server.Server
}
// NewApp returns an App
func NewApp() *App {
srv := server.NewServer()
srv.Router.HandleFunc(contextrouter.GET, "/", index)
srv.Router.HandleFunc(contextrouter.GET, "/:hellostring/:name", hello)
return &App{
srv: srv,
}
}
// Start is called by the native portion of the webapp to start the web server.
// It returns the server root URL (without the trailing slash) and any errors.
func (app *App) Start() (string, error) {
return app.srv.Start("127.0.0.1:0")
}
// Stop is called by the native portion of the webapp to stop the web server.
func (app *App) Stop() {
app.srv.Stop(time.Millisecond * 100)
}
// These two are autogenerated sample handlers for your webapp to get you started.
func index(_ context.Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte("<html><body><div><a href='/Namaste/Alice'>Alice</a></div><div><a href='/Hello/Bob'>Bob</a></div></body></html>"))
}
func hello(c context.Context, w http.ResponseWriter, r *http.Request) {
name := c.Value("name")
greetstring := c.Value("hellostring")
fmt.Fprintf(w, "<html><body><div>%s %s!</div><div><a href='/'>Back</a></div></body></html>", greetstring, name)
}
`
func buildDotGradle(b []byte) ([]byte, error) {
b = bytes.Replace(b, []byte("runProguard false"), []byte("minifyEnabled true"), 1)
if webview == "xwalk" {
return append(b, []byte(buildDotGradleTextXwalk)...), nil
}
return append(b, []byte(buildDotGradleTextSystem)...), nil
}
const buildDotGradleTextSystem = `
android {
defaultConfig {
minSdkVersion 19
}
}
repositories {
flatDir{
dirs 'libs'
}
}
dependencies {
compile(name:'backend', ext:'aar')
}
task genGoMobileAAR(type:Exec) {
workingDir '..'
commandLine 'gomobile', 'bind', '-o', 'androidapp/libs/backend.aar', '.'
}
preBuild.dependsOn(genGoMobileAAR)
`
const buildDotGradleTextXwalk = `
android {
defaultConfig {
minSdkVersion 15
}
}
repositories {
maven {
url 'https://download.01.org/crosswalk/releases/crosswalk/android/maven2'
}
flatDir{
dirs 'libs'
}
}
dependencies {
compile 'org.xwalk:xwalk_core_library:14.43.343.17'
compile(name:'backend', ext:'aar')
}
task genGoMobileAAR(type:Exec) {
workingDir '..'
commandLine 'gomobile', 'bind', '-o', 'androidapp/libs/backend.aar', '.'
}
preBuild.dependsOn(genGoMobileAAR)
`
func gradlewrapperDotProperties(b []byte) ([]byte, error) {
s := bufio.NewScanner(bytes.NewReader(b))
buf := bytes.Buffer{}
for s.Scan() {
txt := s.Text()
if strings.Index(txt, "distributionUrl") == 0 {
txt = fmt.Sprintf("distributionUrl=https\\://services.gradle.org/distributions/gradle-%s-all.zip", gradle)
}
buf.WriteString(txt + "\n")
}
return buf.Bytes(), s.Err()
}
func androidManifestDotXML(b []byte) ([]byte, error) {
i := bytes.Index(b, []byte("<application"))
if i < 0 {
return nil, fmt.Errorf("Could not find <application tag in AndroidManifest.xml")
}
buf := bytes.Buffer{}
buf.Write(b[:i])
if webview == "xwalk" {
buf.Write([]byte(androidManifestDotXMLTextXwalk))
} else {
buf.Write([]byte(androidManifestDotXMLTextSystem))
}
buf.Write(b[i:])
return buf.Bytes(), nil
}
const androidManifestDotXMLTextXwalk = `
<!-- Your app might need more permissions depending on functionality your app requires.
See https://crosswalk-project.org/documentation/embedding_crosswalk.html#Add-code-to-integrate-the-webview -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
`
const androidManifestDotXMLTextSystem = `
<uses-permission android:name="android.permission.INTERNET" />
`
func mainDotJava([]byte) ([]byte, error) {
var tmpl *template.Template
if webview == "xwalk" {
tmpl = template.Must(template.New("mainDotJavaText").Parse(mainDotJavaTextXwalk))
} else {
tmpl = template.Must(template.New("mainDotJavaText").Parse(mainDotJavaTextSystem))
}
params := struct {
PkgPath string
PkgName string
ClsName string
}{
PkgPath: pkgPath,
PkgName: pkgName,
ClsName: strings.ToUpper(pkgName[:1]) + pkgName[1:],
}
buf := bytes.Buffer{}
if err := tmpl.Execute(&buf, params); err != nil {
return nil, fmt.Errorf("error writing main.java :%s", err)
}
return buf.Bytes(), nil
}
const mainDotJavaTextSystem = `
package {{.PkgPath}};
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.webkit.WebView;
import go.{{.PkgName}}.{{.ClsName}};
public class Main extends Activity {
private WebView mWebView;
private {{.ClsName}}.App mSrv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebView = new WebView(this);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
setContentView(mWebView);
}
// We start the server on onResume
@Override
protected void onResume() {
super.onResume();
mSrv = {{.ClsName}}.NewApp();
try {
mWebView.loadUrl(mSrv.Start() + "/");
} catch (Exception e) {
Toast.makeText(this,"Error:"+e.toString(),Toast.LENGTH_LONG).show();
e.printStackTrace();
this.finish();
}
}
// Send a graceful shut down signal to the server. onPause is guaranteed
// to be called by Android while onStop or onDestroy may not be called.
@Override
protected void onPause() {
super.onPause();
mSrv.Stop();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
// We override back key press to close the app rather than pass it to the WebView
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
this.finish();
return true;
}
return super.dispatchKeyEvent(event);
}
}
`
const mainDotJavaTextXwalk = `
package {{.PkgPath}};
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;
import org.xwalk.core.XWalkView;
import go.{{.PkgName}}.{{.ClsName}};
public class Main extends Activity {
private XWalkView mWebView;
private {{.ClsName}}.App mSrv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebView = new XWalkView(this, this);
setContentView(mWebView);
}
// We start the server on onResume
@Override
protected void onResume() {
super.onResume();
if (mWebView != null) {
mWebView.resumeTimers();
mWebView.onShow();
}
mSrv = {{.ClsName}}.NewApp();
try {
mWebView.load(mSrv.Start() + "/", null);
} catch (Exception e) {
Toast.makeText(this,"Error:"+e.toString(),Toast.LENGTH_LONG).show();
e.printStackTrace();
this.finish();
}
}
// Send a graceful shut down signal to the server. onPause is guaranteed
// to be called by Android while onStop or onDestroy may not be called.
@Override
protected void onPause() {
super.onPause();
if (mWebView != null) {
mWebView.pauseTimers();
mWebView.onHide();
}
mSrv.Stop();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mWebView != null) {
mWebView.onDestroy();
}
}
// We override back key press to close the app rather than pass it to the XWalkView to give
// a consistent user experience with how apps behave on Android.
// Also see https://crosswalk-project.org/jira/browse/XWALK-4816
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
this.finish();
return true;
}
return super.dispatchKeyEvent(event);
}
}
`
const gitignore = `androidapp.iml
.gradle/
.idea/
local.properties
build/
`