main.go 2.9 KB

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