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