Skip to content

Commit 7fd3b23

Browse files
committed
refactor for pr 67
1 parent ffb85ed commit 7fd3b23

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

app/GioForegroundService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public class GioForegroundService extends Service {
6262
startForeground(notificationID, builder.build());
6363
} catch (PackageManager.NameNotFoundException e) {
6464
throw new RuntimeException(e);
65+
} catch (java.lang.SecurityException e) {
66+
// XXX: notify the caller of Start that the service has failed
67+
throw new RuntimeException(e);
6568
}
6669
return START_NOT_STICKY;
6770
}

app/os_android.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,14 +1458,14 @@ func (_ ViewEvent) ImplementsEvent() {}
14581458
var foregroundService struct {
14591459
intent C.jobject
14601460
mu sync.Mutex
1461-
count int
1461+
stop map[*int]bool
14621462
}
14631463

14641464
// Start starts the foreground service
14651465
func Start(title, text string) (stop func(), err error) {
14661466
foregroundService.mu.Lock()
14671467
defer foregroundService.mu.Unlock()
1468-
if foregroundService.count == 0 {
1468+
if len(foregroundService.stop) == 0 {
14691469
runInJVM(javaVM(), func(env *C.JNIEnv) {
14701470
foregroundService.intent, err = callStaticObjectMethod(env, android.gioCls,
14711471
android.startForegroundService,
@@ -1476,23 +1476,31 @@ func Start(title, text string) (stop func(), err error) {
14761476
if err == nil {
14771477
// get a reference across JNI sessions to the returned intent
14781478
foregroundService.intent = C.jni_NewGlobalRef(env, foregroundService.intent)
1479+
1480+
} else {
1481+
panic(err)
14791482
}
14801483
})
14811484
}
14821485
if err != nil {
14831486
return nil, err
14841487
}
1485-
foregroundService.count++
1488+
ref := new(int)
1489+
foregroundService.stop[ref] = true
14861490
return func() {
14871491
foregroundService.mu.Lock()
14881492
defer foregroundService.mu.Unlock()
1489-
if foregroundService.count == 1 {
1493+
delete(foregroundService.stop, ref)
1494+
if len(foregroundService.stop) == 0 {
14901495
runInJVM(javaVM(), func(env *C.JNIEnv) {
14911496
defer C.jni_DeleteGlobalRef(env, foregroundService.intent)
14921497
callVoidMethod(env, android.appCtx, android.stopService, jvalue(foregroundService.intent))
14931498
})
14941499
}
1495-
foregroundService.count--
1496-
}, err
1500+
}, nil
1501+
1502+
}
14971503

1504+
func init() {
1505+
foregroundService.stop = make(map[*int]bool)
14981506
}

0 commit comments

Comments
 (0)