Debian Codegraph @

example/network/main.go

raw symbols SCIP
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/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 //
40 // $ go install golang.org/x/mobile/example/network && network
41 package mainscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/example/network`/
42
43 import (
44 "net/httpscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/"
45
46 "golang.org/x/mobile/appscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/"
47 "golang.org/x/mobile/event/lifecyclescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/lifecycle`/"
48 "golang.org/x/mobile/event/paintscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/paint`/"
49 "golang.org/x/mobile/event/sizescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/size`/"
50 "golang.org/x/mobile/glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/"
51 )
52
53 func mainscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/example/network`/main().() {
54 // checkNetwork runs only once when the app first loads.
55 go checkNetworkscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/example/network`/checkNetwork().()
56
57 appscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/.Mainscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/Main().(func(a appscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/.Appscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/App#) {
58 var glctx glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.Contextscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#
59 det, sz := determinedscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/example/network`/determined., sizescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/size`/.Eventscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/size`/Event#{}
60 for {
61 select {
62 case <-det:
63 a.Sendscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/App#Send.(paintscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/paint`/.Eventscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/paint`/Event#{})
64 det = nil
65
66 case e := <-a.Eventsscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/App#Events.():
67 switch e := a.Filterscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/App#Filter.(e).(type) {
68 case lifecyclescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/lifecycle`/.Eventscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/lifecycle`/Event#:
69 glctx, _ = e.DrawContextscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/lifecycle`/Event#DrawContext..(glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.Contextscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#)
70 case sizescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/size`/.Eventscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/size`/Event#:
71 sz = e
72 case paintscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/paint`/.Eventscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/paint`/Event#:
73 if glctx == nil {
74 continue
75 }
76 onDrawscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/example/network`/onDraw().(glctx, sz)
77 a.Publishscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/app`/App#Publish.()
78 }
79 }
80 }
81 })
82 }
83
84 var (
85 determinedscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/example/network`/determined. = make(chan struct{})
86 okscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/example/network`/ok. = false
87 )
88
89 func checkNetworkscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/example/network`/checkNetwork().() {
90 defer close(determinedscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/example/network`/determined.)
91
92 _, err := httpscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/.Getscip-go gomod github.com/golang/go/src go1.24.0 `net/http`/Get().("http://golang.org/")
93 if err != nil {
94 return
95 }
96 okscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/example/network`/ok. = true
97 }
98
99 func onDrawscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/example/network`/onDraw().(glctx glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.Contextscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#, sz sizescip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/size`/.Eventscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/event/size`/Event#) {
100 select {
101 case <-determinedscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/example/network`/determined.:
102 if okscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/example/network`/ok. {
103 glctx.ClearColorscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#ClearColor.(0, 1, 0, 1)
104 } else {
105 glctx.ClearColorscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#ClearColor.(1, 0, 0, 1)
106 }
107 default:
108 glctx.ClearColorscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#ClearColor.(0, 0, 0, 1)
109 }
110 glctx.Clearscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/Context#Clear.(glscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/.COLOR_BUFFER_BITscip-go gomod golang.org/x/mobile . `golang.org/x/mobile/gl`/COLOR_BUFFER_BIT.)
111 }