main.go 2.9 KB

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