github.com/adwpc/xmobile@v0.0.0-20231212131043-3f9720cf0e99/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 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 irrelevant 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/adwpc/xmobile/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/adwpc/xmobile/example/network 28 // $ gomobile build github.com/adwpc/xmobile/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/adwpc/xmobile/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 // 40 // $ go install github.com/adwpc/xmobile/example/network && network 41 package main 42 43 import ( 44 "net/http" 45 46 "github.com/adwpc/xmobile/app" 47 "github.com/adwpc/xmobile/event/lifecycle" 48 "github.com/adwpc/xmobile/event/paint" 49 "github.com/adwpc/xmobile/event/size" 50 "github.com/adwpc/xmobile/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 }