mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
feat(android): support independent broadcast listeners (#10936)
This commit is contained in:
@@ -26,68 +26,6 @@ function getApplicationContext(): android.content.Context {
|
||||
return getNativeApp<android.app.Application>().getApplicationContext();
|
||||
}
|
||||
|
||||
export const androidRegisteredReceivers: { [key: string]: android.content.BroadcastReceiver } = {};
|
||||
export const androidPendingReceiverRegistrations = new Array<(context: android.content.Context) => void>();
|
||||
|
||||
declare class BroadcastReceiver extends android.content.BroadcastReceiver {
|
||||
constructor(onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void);
|
||||
}
|
||||
|
||||
let BroadcastReceiver_: typeof BroadcastReceiver;
|
||||
function initBroadcastReceiver() {
|
||||
if (BroadcastReceiver_) {
|
||||
return BroadcastReceiver_;
|
||||
}
|
||||
|
||||
@NativeClass
|
||||
class BroadcastReceiverImpl extends android.content.BroadcastReceiver {
|
||||
private _onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void;
|
||||
|
||||
constructor(onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
|
||||
super();
|
||||
this._onReceiveCallback = onReceiveCallback;
|
||||
|
||||
return global.__native(this);
|
||||
}
|
||||
|
||||
public onReceive(context: android.content.Context, intent: android.content.Intent) {
|
||||
if (this._onReceiveCallback) {
|
||||
this._onReceiveCallback(context, intent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BroadcastReceiver_ = BroadcastReceiverImpl;
|
||||
return BroadcastReceiver_;
|
||||
}
|
||||
|
||||
export function androidRegisterBroadcastReceiver(intentFilter: string, onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void, flags = 2): void {
|
||||
const registerFunc = (context: android.content.Context) => {
|
||||
const receiver: android.content.BroadcastReceiver = new (initBroadcastReceiver())(onReceiveCallback);
|
||||
if (SDK_VERSION >= 26) {
|
||||
context.registerReceiver(receiver, new android.content.IntentFilter(intentFilter), flags);
|
||||
} else {
|
||||
context.registerReceiver(receiver, new android.content.IntentFilter(intentFilter));
|
||||
}
|
||||
androidRegisteredReceivers[intentFilter] = receiver;
|
||||
};
|
||||
|
||||
if (getApplicationContext()) {
|
||||
registerFunc(getApplicationContext());
|
||||
} else {
|
||||
androidPendingReceiverRegistrations.push(registerFunc);
|
||||
}
|
||||
}
|
||||
|
||||
export function androidUnregisterBroadcastReceiver(intentFilter: string): void {
|
||||
const receiver = androidRegisteredReceivers[intentFilter];
|
||||
if (receiver) {
|
||||
getApplicationContext().unregisterReceiver(receiver);
|
||||
androidRegisteredReceivers[intentFilter] = undefined;
|
||||
delete androidRegisteredReceivers[intentFilter];
|
||||
}
|
||||
}
|
||||
|
||||
export function updateContentDescription(view: any /* View */, forceUpdate?: boolean): string | null {
|
||||
if (!view.nativeViewProtected) {
|
||||
return;
|
||||
@@ -204,6 +142,3 @@ export function setupAccessibleView(view: any /* any */): void {
|
||||
}
|
||||
|
||||
// stubs
|
||||
export const iosNotificationObservers: Array<any> = [];
|
||||
export function iosAddNotificationObserver(notificationName: string, onReceiveCallback: (notification: any) => void) {}
|
||||
export function iosRemoveNotificationObserver(observer: any, notificationName: string) {}
|
||||
|
||||
Reference in New Issue
Block a user