2015-07-06 09:30:02 -04:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-07-28 12:29:19 -04:00
|
|
|
// +build darwin linux
|
|
|
|
|
2015-07-06 09:30:02 -04:00
|
|
|
// An app that paints green if golang.org is reachable when the app first
|
|
|
|
// starts, or red otherwise.
|
|
|
|
//
|
|
|
|
// In order to access the network from the Android app, its AndroidManifest.xml
|
|
|
|
// file must include the permission to access the network.
|
|
|
|
//
|
|
|
|
// http://developer.android.com/guide/topics/manifest/manifest-intro.html#perms
|
|
|
|
//
|
|
|
|
// The gomobile tool auto-generates a default AndroidManifest file by default
|
|
|
|
// unless the package directory contains the AndroidManifest.xml. Users can
|
|
|
|
// customize app behavior, such as permissions and app name, by providing
|
|
|
|
// the AndroidManifest file. This is irrelevent to iOS.
|
|
|
|
//
|
|
|
|
// Note: This demo is an early preview of Go 1.5. In order to build this
|
|
|
|
// program as an Android APK using the gomobile tool.
|
|
|
|
//
|
|
|
|
// See http://godoc.org/golang.org/x/mobile/cmd/gomobile to install gomobile.
|
|
|
|
//
|
|
|
|
// Get the network example and use gomobile to build or install it on your device.
|
|
|
|
//
|
|
|
|
// $ go get -d golang.org/x/mobile/example/network
|
|
|
|
// $ gomobile build golang.org/x/mobile/example/network # will build an APK
|
|
|
|
//
|
|
|
|
// # plug your Android device to your computer or start an Android emulator.
|
|
|
|
// # if you have adb installed on your machine, use gomobile install to
|
|
|
|
// # build and deploy the APK to an Android target.
|
|
|
|
// $ gomobile install golang.org/x/mobile/example/network
|
|
|
|
//
|
|
|
|
// Switch to your device or emulator to start the network application from
|
|
|
|
// the launcher.
|
|
|
|
// You can also run the application on your desktop by running the command
|
|
|
|
// below. (Note: It currently doesn't work on Windows.)
|
|
|
|
// $ go install golang.org/x/mobile/example/network && network
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"golang.org/x/mobile/app"
|
2015-08-08 10:18:15 -04:00
|
|
|
"golang.org/x/mobile/event/lifecycle"
|
2015-07-15 18:45:03 -04:00
|
|
|
"golang.org/x/mobile/event/paint"
|
2015-08-13 19:59:18 +10:00
|
|
|
"golang.org/x/mobile/event/size"
|
2015-07-06 09:30:02 -04:00
|
|
|
"golang.org/x/mobile/exp/app/debug"
|
2015-08-08 10:18:15 -04:00
|
|
|
"golang.org/x/mobile/exp/gl/glutil"
|
2015-07-06 09:30:02 -04:00
|
|
|
"golang.org/x/mobile/gl"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// checkNetwork runs only once when the app first loads.
|
|
|
|
go checkNetwork()
|
|
|
|
|
2015-07-15 18:45:03 -04:00
|
|
|
app.Main(func(a app.App) {
|
2015-08-08 10:18:15 -04:00
|
|
|
var glctx gl.Context
|
app: change EndPaint to Publish.
More than a name change, the painting model changes so that the app, not
the library, is responsible for driving painting. If the app is
animating and wants paint events at 60 Hz, it has to ask for that. If
the app is not animating and doesn't need to update its screen, it
shouldn't get any paint events.
Plenty of TODOs, and this CL doesn't get us to a perfect place, but it
is a checkpoint along the way.
The darwin_*.go code changes were minimal. I don't even have a Mac or
iOS device to test that this even builds. Even so, the TODOs about not
sending paint.Events unconditionally are important TODOs. That's the
whole point of switching to this model. I'll leave the actual
implementation to you (crawshaw).
Out of all the example apps, the change to example/network/main.go is
probably the most interesting.
It seems like there ought to be some way to reduce the copy/paste
between all of the example app code, but I'll leave that for future CLs.
Change-Id: I17e11c06174110c68e17f7183b2d8af19b6a170e
Reviewed-on: https://go-review.googlesource.com/14300
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-09-04 17:13:09 +10:00
|
|
|
det, sz := determined, size.Event{}
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-det:
|
|
|
|
a.Send(paint.Event{})
|
|
|
|
det = nil
|
|
|
|
|
|
|
|
case e := <-a.Events():
|
2015-09-22 13:19:05 -04:00
|
|
|
switch e := a.Filter(e).(type) {
|
2015-08-08 10:18:15 -04:00
|
|
|
case lifecycle.Event:
|
|
|
|
glctx, _ = e.DrawContext.(gl.Context)
|
|
|
|
if glctx != nil {
|
|
|
|
glctx = e.DrawContext.(gl.Context)
|
|
|
|
images = glutil.NewImages(glctx)
|
|
|
|
fps = debug.NewFPS(images)
|
|
|
|
}
|
app: change EndPaint to Publish.
More than a name change, the painting model changes so that the app, not
the library, is responsible for driving painting. If the app is
animating and wants paint events at 60 Hz, it has to ask for that. If
the app is not animating and doesn't need to update its screen, it
shouldn't get any paint events.
Plenty of TODOs, and this CL doesn't get us to a perfect place, but it
is a checkpoint along the way.
The darwin_*.go code changes were minimal. I don't even have a Mac or
iOS device to test that this even builds. Even so, the TODOs about not
sending paint.Events unconditionally are important TODOs. That's the
whole point of switching to this model. I'll leave the actual
implementation to you (crawshaw).
Out of all the example apps, the change to example/network/main.go is
probably the most interesting.
It seems like there ought to be some way to reduce the copy/paste
between all of the example app code, but I'll leave that for future CLs.
Change-Id: I17e11c06174110c68e17f7183b2d8af19b6a170e
Reviewed-on: https://go-review.googlesource.com/14300
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-09-04 17:13:09 +10:00
|
|
|
case size.Event:
|
|
|
|
sz = e
|
|
|
|
case paint.Event:
|
2015-08-08 10:18:15 -04:00
|
|
|
onDraw(glctx, sz)
|
app: change EndPaint to Publish.
More than a name change, the painting model changes so that the app, not
the library, is responsible for driving painting. If the app is
animating and wants paint events at 60 Hz, it has to ask for that. If
the app is not animating and doesn't need to update its screen, it
shouldn't get any paint events.
Plenty of TODOs, and this CL doesn't get us to a perfect place, but it
is a checkpoint along the way.
The darwin_*.go code changes were minimal. I don't even have a Mac or
iOS device to test that this even builds. Even so, the TODOs about not
sending paint.Events unconditionally are important TODOs. That's the
whole point of switching to this model. I'll leave the actual
implementation to you (crawshaw).
Out of all the example apps, the change to example/network/main.go is
probably the most interesting.
It seems like there ought to be some way to reduce the copy/paste
between all of the example app code, but I'll leave that for future CLs.
Change-Id: I17e11c06174110c68e17f7183b2d8af19b6a170e
Reviewed-on: https://go-review.googlesource.com/14300
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-09-04 17:13:09 +10:00
|
|
|
a.Publish()
|
|
|
|
}
|
2015-07-15 18:45:03 -04:00
|
|
|
}
|
|
|
|
}
|
2015-07-06 09:30:02 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2015-08-08 10:18:15 -04:00
|
|
|
images *glutil.Images
|
|
|
|
fps *debug.FPS
|
2015-07-06 09:30:02 -04:00
|
|
|
determined = make(chan struct{})
|
|
|
|
ok = false
|
|
|
|
)
|
|
|
|
|
|
|
|
func checkNetwork() {
|
|
|
|
defer close(determined)
|
|
|
|
|
|
|
|
_, err := http.Get("http://golang.org/")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
|
2015-08-08 10:18:15 -04:00
|
|
|
func onDraw(glctx gl.Context, sz size.Event) {
|
2015-07-06 09:30:02 -04:00
|
|
|
select {
|
|
|
|
case <-determined:
|
|
|
|
if ok {
|
2015-08-08 10:18:15 -04:00
|
|
|
glctx.ClearColor(0, 1, 0, 1)
|
2015-07-06 09:30:02 -04:00
|
|
|
} else {
|
2015-08-08 10:18:15 -04:00
|
|
|
glctx.ClearColor(1, 0, 0, 1)
|
2015-07-06 09:30:02 -04:00
|
|
|
}
|
|
|
|
default:
|
2015-08-08 10:18:15 -04:00
|
|
|
glctx.ClearColor(0, 0, 0, 1)
|
2015-07-06 09:30:02 -04:00
|
|
|
}
|
2015-08-08 10:18:15 -04:00
|
|
|
glctx.Clear(gl.COLOR_BUFFER_BIT)
|
2015-07-06 09:30:02 -04:00
|
|
|
|
2015-08-08 10:18:15 -04:00
|
|
|
fps.Draw(sz)
|
2015-07-06 09:30:02 -04:00
|
|
|
}
|