Skip to content

Commit 6beaebc

Browse files
xaionaro@dx.centerxaionaro@dx.center
authored andcommitted
feat: add persistablebundle overview example
1 parent dc501a2 commit 6beaebc

1 file changed

Lines changed: 134 additions & 0 deletions

File tree

  • examples/persistablebundle/overview
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// PersistableBundle API overview.
2+
//
3+
// Demonstrates the Android PersistableBundle NDK API, which provides a
4+
// type-safe key-value container that can be serialized to a Parcel for
5+
// IPC. PersistableBundles support boolean, int32, int64, float64, and
6+
// string values, as well as nested PersistableBundles.
7+
//
8+
// Available since Android API level 35 (Android 15).
9+
//
10+
// Operations demonstrated:
11+
//
12+
// - Create a new PersistableBundle
13+
// - Put and get scalar values (bool, int32, int64, float64, string)
14+
// - Query the bundle size
15+
// - Duplicate a bundle and compare with IsEqual
16+
// - Nest one bundle inside another
17+
// - Erase a key
18+
// - Clean up with Close
19+
package main
20+
21+
import (
22+
"fmt"
23+
"log"
24+
25+
"github.com/xaionaro-go/ndk/persistablebundle"
26+
)
27+
28+
func main() {
29+
// ---------------------------------------------------------------
30+
// Create a new PersistableBundle.
31+
// ---------------------------------------------------------------
32+
pb := persistablebundle.NewPersistableBundle()
33+
defer pb.Close()
34+
35+
// ---------------------------------------------------------------
36+
// Put scalar values of each supported type.
37+
// ---------------------------------------------------------------
38+
pb.PutBoolean("debug_enabled", true)
39+
pb.PutInt("retry_count", 3)
40+
pb.PutLong("session_id", 1234567890123)
41+
pb.PutDouble("threshold", 0.95)
42+
pb.PutString("app_name", "MyAndroidApp")
43+
44+
fmt.Printf("bundle size after inserting 5 keys: %d\n", pb.Size())
45+
46+
// ---------------------------------------------------------------
47+
// Get each value back.
48+
//
49+
// Get* methods return true if the key was found.
50+
// ---------------------------------------------------------------
51+
var boolVal bool
52+
if pb.GetBoolean("debug_enabled", &boolVal) {
53+
fmt.Printf(" debug_enabled = %v\n", boolVal)
54+
} else {
55+
log.Fatal("expected key 'debug_enabled' to exist")
56+
}
57+
58+
var intVal int32
59+
if pb.GetInt("retry_count", &intVal) {
60+
fmt.Printf(" retry_count = %d\n", intVal)
61+
} else {
62+
log.Fatal("expected key 'retry_count' to exist")
63+
}
64+
65+
var longVal int64
66+
if pb.GetLong("session_id", &longVal) {
67+
fmt.Printf(" session_id = %d\n", longVal)
68+
} else {
69+
log.Fatal("expected key 'session_id' to exist")
70+
}
71+
72+
var doubleVal float64
73+
if pb.GetDouble("threshold", &doubleVal) {
74+
fmt.Printf(" threshold = %.2f\n", doubleVal)
75+
} else {
76+
log.Fatal("expected key 'threshold' to exist")
77+
}
78+
79+
// Note: GetString requires a C string allocator callback,
80+
// so it is only available via the capi package.
81+
82+
// ---------------------------------------------------------------
83+
// Duplicate the bundle and verify equality.
84+
// ---------------------------------------------------------------
85+
pbCopy := pb.Dup()
86+
defer pbCopy.Close()
87+
88+
fmt.Printf("original == copy: %v\n", pb.IsEqual(pbCopy))
89+
90+
// ---------------------------------------------------------------
91+
// Nested bundles.
92+
//
93+
// PersistableBundles can contain other PersistableBundles,
94+
// enabling hierarchical configuration structures.
95+
// ---------------------------------------------------------------
96+
inner := persistablebundle.NewPersistableBundle()
97+
inner.PutInt("inner_value", 42)
98+
99+
pb.PutPersistableBundle("nested", inner)
100+
// The inner bundle has been copied into pb, so we can close ours.
101+
inner.Close()
102+
103+
fmt.Printf("bundle size after adding nested bundle: %d\n", pb.Size())
104+
105+
retrieved, ok := pb.GetPersistableBundle("nested")
106+
if !ok {
107+
log.Fatal("expected key 'nested' to exist")
108+
}
109+
defer retrieved.Close()
110+
111+
var innerVal int32
112+
if retrieved.GetInt("inner_value", &innerVal) {
113+
fmt.Printf(" nested.inner_value = %d\n", innerVal)
114+
} else {
115+
log.Fatal("expected key 'inner_value' in nested bundle")
116+
}
117+
118+
// ---------------------------------------------------------------
119+
// Erase a key.
120+
//
121+
// Erase returns the number of entries removed (0 or 1).
122+
// ---------------------------------------------------------------
123+
erased := pb.Erase("retry_count")
124+
fmt.Printf("erased 'retry_count': count=%d\n", erased)
125+
fmt.Printf("bundle size after erase: %d\n", pb.Size())
126+
127+
// Verify the key is gone.
128+
if pb.GetInt("retry_count", &intVal) {
129+
log.Fatal("expected key 'retry_count' to be erased")
130+
}
131+
fmt.Println("confirmed: 'retry_count' no longer exists")
132+
133+
fmt.Println("overview complete")
134+
}

0 commit comments

Comments
 (0)