-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
86 lines (74 loc) · 2.37 KB
/
main.go
File metadata and controls
86 lines (74 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Query location provider availability for geofencing use cases.
// Note: RequestGeofence requires a PendingIntent which cannot be fully
// serialized from a non-app context. This example demonstrates the
// prerequisite checks: listing providers and verifying GPS availability.
//
// Build:
//
// GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o build/geofence ./examples/geofence/
// adb push build/geofence /data/local/tmp/ && adb shell /data/local/tmp/geofence
package main
import (
"context"
"fmt"
"os"
"github.com/AndroidGoLab/binder/android/location"
"github.com/AndroidGoLab/binder/binder"
"github.com/AndroidGoLab/binder/binder/versionaware"
"github.com/AndroidGoLab/binder/kernelbinder"
"github.com/AndroidGoLab/binder/servicemanager"
)
func main() {
ctx := context.Background()
drv, err := kernelbinder.Open(ctx, binder.WithMapSize(128*1024))
if err != nil {
fmt.Fprintf(os.Stderr, "open binder: %v\n", err)
os.Exit(1)
}
defer drv.Close(ctx)
transport, err := versionaware.NewTransport(ctx, drv, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "version-aware transport: %v\n", err)
os.Exit(1)
}
sm := servicemanager.New(transport)
lm, err := location.GetLocationManager(ctx, sm)
if err != nil {
fmt.Fprintf(os.Stderr, "get location manager: %v\n", err)
os.Exit(1)
}
providers, err := lm.GetAllProviders(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "GetAllProviders: %v\n", err)
os.Exit(1)
}
fmt.Printf("Available providers (%d):\n", len(providers))
for _, p := range providers {
fmt.Printf(" - %s\n", p)
}
// Check if GPS is available (required for geofencing).
hasGPS, err := lm.HasProvider(ctx, location.GpsProvider)
if err != nil {
fmt.Fprintf(os.Stderr, "HasProvider(gps): %v\n", err)
} else {
fmt.Printf("\nGPS provider available: %v\n", hasGPS)
}
hasFused, err := lm.HasProvider(ctx, location.FusedProvider)
if err != nil {
fmt.Fprintf(os.Stderr, "HasProvider(fused): %v\n", err)
} else {
fmt.Printf("Fused provider available: %v\n", hasFused)
}
// Check if location is enabled for the current user.
enabled, err := lm.IsLocationEnabledForUser(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "IsLocationEnabledForUser: %v\n", err)
} else {
fmt.Printf("Location enabled: %v\n", enabled)
}
if !hasGPS {
fmt.Println("\nGeofencing requires GPS. Provider not available.")
} else {
fmt.Println("\nGeofencing prerequisites met.")
}
}