github.com/coming-chat/gomobile@v0.0.0-20220601074111-56995f7d7aba/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 irrelevant 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/golang.org/x/mobile/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 golang.org/x/mobile/example/network
    29  //	$ gomobile build golang.org/x/mobile/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 golang.org/x/mobile/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  //
    41  //	$ go install golang.org/x/mobile/example/network && network
    42  package main
    43  
    44  import (
    45  	"net/http"
    46  
    47  	"golang.org/x/mobile/app"
    48  	"golang.org/x/mobile/event/lifecycle"
    49  	"golang.org/x/mobile/event/paint"
    50  	"golang.org/x/mobile/event/size"
    51  	"golang.org/x/mobile/gl"
    52  )
    53  
    54  func main() {
    55  	// checkNetwork runs only once when the app first loads.
    56  	go checkNetwork()
    57  
    58  	app.Main(func(a app.App) {
    59  		var glctx gl.Context
    60  		det, sz := determined, size.Event{}
    61  		for {
    62  			select {
    63  			case <-det:
    64  				a.Send(paint.Event{})
    65  				det = nil
    66  
    67  			case e := <-a.Events():
    68  				switch e := a.Filter(e).(type) {
    69  				case lifecycle.Event:
    70  					glctx, _ = e.DrawContext.(gl.Context)
    71  				case size.Event:
    72  					sz = e
    73  				case paint.Event:
    74  					if glctx == nil {
    75  						continue
    76  					}
    77  					onDraw(glctx, sz)
    78  					a.Publish()
    79  				}
    80  			}
    81  		}
    82  	})
    83  }
    84  
    85  var (
    86  	determined = make(chan struct{})
    87  	ok         = false
    88  )
    89  
    90  func checkNetwork() {
    91  	defer close(determined)
    92  
    93  	_, err := http.Get("http://golang.org/")
    94  	if err != nil {
    95  		return
    96  	}
    97  	ok = true
    98  }
    99  
   100  func onDraw(glctx gl.Context, sz size.Event) {
   101  	select {
   102  	case <-determined:
   103  		if ok {
   104  			glctx.ClearColor(0, 1, 0, 1)
   105  		} else {
   106  			glctx.ClearColor(1, 0, 0, 1)
   107  		}
   108  	default:
   109  		glctx.ClearColor(0, 0, 0, 1)
   110  	}
   111  	glctx.Clear(gl.COLOR_BUFFER_BIT)
   112  }