github.com/SkycoinProject/gomobile@v0.0.0-20190312151609-d3739f865fa6/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 windows 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/golang.org/x/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 golang.org/x/mobile/example/network 28 // $ gomobile build golang.org/x/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 golang.org/x/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 golang.org/x/mobile/example/network && network 40 package main 41 42 import ( 43 "net/http" 44 45 "golang.org/x/mobile/app" 46 "golang.org/x/mobile/event/lifecycle" 47 "golang.org/x/mobile/event/paint" 48 "golang.org/x/mobile/event/size" 49 "golang.org/x/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 glctx gl.Context 58 det, sz := determined, size.Event{} 59 for { 60 select { 61 case <-det: 62 a.Send(paint.Event{}) 63 det = nil 64 65 case e := <-a.Events(): 66 switch e := a.Filter(e).(type) { 67 case lifecycle.Event: 68 glctx, _ = e.DrawContext.(gl.Context) 69 case size.Event: 70 sz = e 71 case paint.Event: 72 if glctx == nil { 73 continue 74 } 75 onDraw(glctx, sz) 76 a.Publish() 77 } 78 } 79 } 80 }) 81 } 82 83 var ( 84 determined = make(chan struct{}) 85 ok = false 86 ) 87 88 func checkNetwork() { 89 defer close(determined) 90 91 _, err := http.Get("http://golang.org/") 92 if err != nil { 93 return 94 } 95 ok = true 96 } 97 98 func onDraw(glctx gl.Context, sz size.Event) { 99 select { 100 case <-determined: 101 if ok { 102 glctx.ClearColor(0, 1, 0, 1) 103 } else { 104 glctx.ClearColor(1, 0, 0, 1) 105 } 106 default: 107 glctx.ClearColor(0, 0, 0, 1) 108 } 109 glctx.Clear(gl.COLOR_BUFFER_BIT) 110 }