github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/example/network/main.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build darwin linux 6 7 // An app that paints green if golang.org is reachable when the app first 8 // starts, or red otherwise. 9 // 10 // In order to access the network from the Android app, its AndroidManifest.xml 11 // file must include the permission to access the network. 12 // 13 // http://developer.android.com/guide/topics/manifest/manifest-intro.html#perms 14 // 15 // The gomobile tool auto-generates a default AndroidManifest file by default 16 // unless the package directory contains the AndroidManifest.xml. Users can 17 // customize app behavior, such as permissions and app name, by providing 18 // the AndroidManifest file. This is irrelevent to iOS. 19 // 20 // Note: This demo is an early preview of Go 1.5. In order to build this 21 // program as an Android APK using the gomobile tool. 22 // 23 // See http://godoc.org/github.com/c-darwin/mobile/cmd/gomobile to install gomobile. 24 // 25 // Get the network example and use gomobile to build or install it on your device. 26 // 27 // $ go get -d github.com/c-darwin/mobile/example/network 28 // $ gomobile build github.com/c-darwin/mobile/example/network # will build an APK 29 // 30 // # plug your Android device to your computer or start an Android emulator. 31 // # if you have adb installed on your machine, use gomobile install to 32 // # build and deploy the APK to an Android target. 33 // $ gomobile install github.com/c-darwin/mobile/example/network 34 // 35 // Switch to your device or emulator to start the network application from 36 // the launcher. 37 // You can also run the application on your desktop by running the command 38 // below. (Note: It currently doesn't work on Windows.) 39 // $ go install github.com/c-darwin/mobile/example/network && network 40 package main 41 42 import ( 43 "net/http" 44 45 "github.com/c-darwin/mobile/app" 46 "github.com/c-darwin/mobile/event/paint" 47 "github.com/c-darwin/mobile/event/size" 48 "github.com/c-darwin/mobile/exp/app/debug" 49 "github.com/c-darwin/mobile/gl" 50 ) 51 52 func main() { 53 // checkNetwork runs only once when the app first loads. 54 go checkNetwork() 55 56 app.Main(func(a app.App) { 57 var sz size.Event 58 for e := range a.Events() { 59 switch e := app.Filter(e).(type) { 60 case size.Event: 61 sz = e 62 case paint.Event: 63 onDraw(sz) 64 a.EndPaint(e) 65 } 66 } 67 }) 68 } 69 70 var ( 71 determined = make(chan struct{}) 72 ok = false 73 ) 74 75 func checkNetwork() { 76 defer close(determined) 77 78 _, err := http.Get("http://golang.org/") 79 if err != nil { 80 return 81 } 82 ok = true 83 } 84 85 func onDraw(sz size.Event) { 86 select { 87 case <-determined: 88 if ok { 89 gl.ClearColor(0, 1, 0, 1) 90 } else { 91 gl.ClearColor(1, 0, 0, 1) 92 } 93 default: 94 gl.ClearColor(0, 0, 0, 1) 95 } 96 gl.Clear(gl.COLOR_BUFFER_BIT) 97 98 debug.DrawFPS(sz) 99 }