Repository navigation
Expand file tree
/
Copy pathCatalogDemos.swift
More file actions
270 lines (246 loc) · 9.69 KB
/
Copy pathCatalogDemos.swift
File metadata and controls
270 lines (246 loc) · 9.69 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//
// CatalogDemos.swift
// SwiftAndroidApp
//
// Per-library demo implementations, exercising the PureSwift Android SDK
// wrappers. Internal only — jextract never sees this file's declarations.
//
import AndroidKit
import AndroidApp
import AndroidGraphics
import AndroidLocation
import AndroidMedia
import AndroidNet
import AndroidNFC
import AndroidUtil
import AndroidView
enum CatalogDemos {
// MARK: - AndroidOS
static func androidOS(_ out: (String) -> Void) {
do {
let build = try JavaClass<AndroidOS.Build>()
out("Manufacturer: \(build.MANUFACTURER)")
out("Model: \(build.MODEL)")
out("Device: \(build.DEVICE)")
let version = try JavaClass<AndroidOS.Build.VERSION>()
out("Android version: \(version.RELEASE) (API \(version.SDK_INT))")
let clock = try JavaClass<AndroidOS.SystemClock>()
out("Uptime: \(clock.uptimeMillis() / 1000)s")
out("Elapsed realtime: \(clock.elapsedRealtime() / 1000)s")
if let context = Catalog.context {
let contextClass = try JavaClass<AndroidContent.Context>()
if let battery = context.getSystemService(contextClass.BATTERY_SERVICE)?
.as(AndroidOS.BatteryManager.self) {
let capacity = try JavaClass<AndroidOS.BatteryManager>().BATTERY_PROPERTY_CAPACITY
out("Battery level: \(battery.getIntProperty(capacity))%")
out("Charging: \(battery.isCharging())")
}
if let power = context.getSystemService(contextClass.POWER_SERVICE)?
.as(AndroidOS.PowerManager.self) {
out("Screen interactive: \(power.isInteractive())")
}
}
} catch {
out("Error: \(error)")
}
}
// MARK: - AndroidApp
static func androidApp(_ out: (String) -> Void) {
guard let context = Catalog.context else {
out("No Android Context available")
return
}
do {
let serviceName = try JavaClass<AndroidContent.Context>().ACTIVITY_SERVICE
guard let manager = context.getSystemService(serviceName)?
.as(AndroidApp.ActivityManager.self) else {
out("ActivityManager unavailable")
return
}
out("Memory class: \(manager.getMemoryClass()) MB")
out("Low RAM device: \(manager.isLowRamDevice())")
let info = AndroidApp.ActivityManager.MemoryInfo()
manager.getMemoryInfo(info)
let mb: Int64 = 1024 * 1024
out("Available memory: \(info.availMem / mb) MB")
out("Total memory: \(info.totalMem / mb) MB")
out("Low memory: \(info.lowMemory)")
} catch {
out("Error: \(error)")
}
}
// MARK: - AndroidGraphics
static func androidGraphics(_ out: (String) -> Void) {
do {
let config = AndroidGraphics.Bitmap.Config(.ARGB_8888)
guard let bitmap = try JavaClass<AndroidGraphics.Bitmap>()
.createBitmap(64, 64, config) else {
out("Unable to create bitmap")
return
}
out("Bitmap: \(bitmap.getWidth())×\(bitmap.getHeight()) ARGB_8888")
out("Byte count: \(bitmap.getByteCount())")
bitmap.recycle()
let colorClass = try JavaClass<AndroidGraphics.Color>()
let magenta = colorClass.rgb(Int32(255), Int32(0), Int32(128))
out("Color.rgb(255, 0, 128) = #\(String(UInt32(bitPattern: magenta), radix: 16, uppercase: true))")
} catch {
out("Error: \(error)")
}
}
// MARK: - AndroidView
static func androidView(_ out: (String) -> Void) {
guard let context = Catalog.context else {
out("No Android Context available")
return
}
do {
let configClass = try JavaClass<AndroidView.ViewConfiguration>()
if let configuration = configClass.get(context) {
out("Scaled touch slop: \(configuration.getScaledTouchSlop()) px")
}
out("Long press timeout: \(configClass.getLongPressTimeout()) ms")
out("Double tap timeout: \(configClass.getDoubleTapTimeout()) ms")
} catch {
out("Error: \(error)")
}
}
// MARK: - AndroidUtil
static func androidUtil(_ out: (String) -> Void) {
guard let context = Catalog.context else {
out("No Android Context available")
return
}
guard let metrics = context.getResources()?.getDisplayMetrics() else {
out("DisplayMetrics unavailable")
return
}
out("Display: \(metrics.widthPixels)×\(metrics.heightPixels) px")
out("Density: \(metrics.density) (\(metrics.densityDpi) dpi)")
}
// MARK: - AndroidContent
static func androidContent(_ out: (String) -> Void) {
guard let context = Catalog.context else {
out("No Android Context available")
return
}
out("Package: \(context.getPackageName())")
if let info = context.getApplicationInfo() {
out("Target SDK: \(info.targetSdkVersion)")
out("Min SDK: \(info.minSdkVersion)")
out("Data dir: \(info.dataDir)")
}
}
// MARK: - AndroidLocation
static func androidLocation(_ out: (String) -> Void) {
guard let context = Catalog.context else {
out("No Android Context available")
return
}
do {
let serviceName = try JavaClass<AndroidContent.Context>().LOCATION_SERVICE
guard let manager = context.getSystemService(serviceName)?
.as(AndroidLocation.LocationManager.self) else {
out("LocationManager unavailable")
return
}
guard let providers = manager.getAllProviders() else {
out("No providers")
return
}
out("Providers: \(providers.size())")
for i in 0..<providers.size() {
guard let provider = providers.get(i) else { continue }
let name = String(provider.toString())
let enabled = manager.isProviderEnabled(name)
out("• \(name): \(enabled ? "enabled" : "disabled")")
}
} catch {
out("Error: \(error)")
}
}
// MARK: - AndroidMedia
static func androidMedia(_ out: (String) -> Void) {
guard let context = Catalog.context else {
out("No Android Context available")
return
}
do {
let serviceName = try JavaClass<AndroidContent.Context>().AUDIO_SERVICE
guard let audio = context.getSystemService(serviceName)?
.as(AndroidMedia.AudioManager.self) else {
out("AudioManager unavailable")
return
}
let audioClass = try JavaClass<AndroidMedia.AudioManager>()
let streams: [(String, Int32)] = [
("Music", audioClass.STREAM_MUSIC),
("Ring", audioClass.STREAM_RING),
("Alarm", audioClass.STREAM_ALARM),
("Notification", audioClass.STREAM_NOTIFICATION),
]
for (label, stream) in streams {
let volume = audio.getStreamVolume(stream)
let max = audio.getStreamMaxVolume(stream)
out("\(label) volume: \(volume)/\(max)")
}
let ringer = audio.getRingerMode()
let ringerName: String
switch ringer {
case audioClass.RINGER_MODE_SILENT: ringerName = "silent"
case audioClass.RINGER_MODE_VIBRATE: ringerName = "vibrate"
default: ringerName = "normal"
}
out("Ringer mode: \(ringerName)")
} catch {
out("Error: \(error)")
}
}
// MARK: - AndroidNet
static func androidNet(_ out: (String) -> Void) {
guard let context = Catalog.context else {
out("No Android Context available")
return
}
do {
let contextClass = try JavaClass<AndroidContent.Context>()
if let connectivity = context.getSystemService(contextClass.CONNECTIVITY_SERVICE)?
.as(AndroidNet.ConnectivityManager.self) {
if let info = connectivity.getActiveNetworkInfo() {
out("Active network: \(info.getTypeName())")
out("Connected: \(info.isConnected())")
} else {
out("No active network")
}
} else {
out("ConnectivityManager unavailable")
}
if let wifi = context.getSystemService(contextClass.WIFI_SERVICE)?
.as(AndroidNet.WifiManager.self) {
out("Wi-Fi enabled: \(wifi.isWifiEnabled())")
} else {
out("WifiManager unavailable")
}
} catch {
out("Error: \(error)")
}
}
// MARK: - AndroidNFC
static func androidNFC(_ out: (String) -> Void) {
guard let context = Catalog.context else {
out("No Android Context available")
return
}
do {
let adapterClass = try JavaClass<AndroidNFC.NfcAdapter>()
if let adapter = adapterClass.getDefaultAdapter(context) {
out("NFC adapter present")
out("Enabled: \(adapter.isEnabled())")
} else {
out("No NFC adapter on this device")
}
} catch {
out("Error: \(error)")
}
}
}