feat(android): implement a 'activityNewIntent' event

This commit is contained in:
Eddy Verbruggen
2019-04-03 10:06:14 +02:00
committed by Martin Yankov
parent 2d63c9f276
commit 3efc06ed98
6 changed files with 77 additions and 18 deletions

View File

@@ -135,6 +135,10 @@ if (application.android) {
console.log("Event: " + args.eventName + ", Activity: " + args.activity); console.log("Event: " + args.eventName + ", Activity: " + args.activity);
// Set args.cancel = true to cancel back navigation and do something custom. // Set args.cancel = true to cancel back navigation and do something custom.
}); });
application.android.on(application.AndroidApplication.activityNewIntentEvent, function (args: application.AndroidActivityNewIntentEventData) {
console.log("Event: " + args.eventName + ", Activity: " + args.activity + ", Intent: " + args.intent);
});
} }
var time; var time;

View File

@@ -1,6 +1,6 @@
import { import {
AndroidActivityBundleEventData, AndroidActivityEventData, ApplicationEventData, OrientationChangedEventData, AndroidActivityBundleEventData, AndroidActivityEventData, ApplicationEventData, OrientationChangedEventData,
AndroidApplication as AndroidApplicationDefinition, AndroidApplication as AndroidApplicationDefinition, AndroidActivityNewIntentEventData,
AndroidActivityResultEventData, AndroidActivityBackPressedEventData, AndroidActivityRequestPermissionsEventData AndroidActivityResultEventData, AndroidActivityBackPressedEventData, AndroidActivityRequestPermissionsEventData
} from "."; } from ".";
@@ -25,6 +25,7 @@ const ActivityStopped = "activityStopped";
const SaveActivityState = "saveActivityState"; const SaveActivityState = "saveActivityState";
const ActivityResult = "activityResult"; const ActivityResult = "activityResult";
const ActivityBackPressed = "activityBackPressed"; const ActivityBackPressed = "activityBackPressed";
const ActivityNewIntent = "activityNewIntent";
const ActivityRequestPermissions = "activityRequestPermissions"; const ActivityRequestPermissions = "activityRequestPermissions";
export class AndroidApplication extends Observable implements AndroidApplicationDefinition { export class AndroidApplication extends Observable implements AndroidApplicationDefinition {
@@ -37,6 +38,7 @@ export class AndroidApplication extends Observable implements AndroidApplication
public static saveActivityStateEvent = SaveActivityState; public static saveActivityStateEvent = SaveActivityState;
public static activityResultEvent = ActivityResult; public static activityResultEvent = ActivityResult;
public static activityBackPressedEvent = ActivityBackPressed; public static activityBackPressedEvent = ActivityBackPressed;
public static activityNewIntentEvent = ActivityNewIntent;
public static activityRequestPermissionsEvent = ActivityRequestPermissions; public static activityRequestPermissionsEvent = ActivityRequestPermissions;
public paused: boolean; public paused: boolean;
@@ -119,6 +121,7 @@ export interface AndroidApplication {
on(event: "saveActivityState", callback: (args: AndroidActivityBundleEventData) => void, thisArg?: any); on(event: "saveActivityState", callback: (args: AndroidActivityBundleEventData) => void, thisArg?: any);
on(event: "activityResult", callback: (args: AndroidActivityResultEventData) => void, thisArg?: any); on(event: "activityResult", callback: (args: AndroidActivityResultEventData) => void, thisArg?: any);
on(event: "activityBackPressed", callback: (args: AndroidActivityBackPressedEventData) => void, thisArg?: any); on(event: "activityBackPressed", callback: (args: AndroidActivityBackPressedEventData) => void, thisArg?: any);
on(event: "activityNewIntent", callback: (args: AndroidActivityNewIntentEventData) => void, thisArg?: any);
on(event: "activityRequestPermissions", callback: (args: AndroidActivityRequestPermissionsEventData) => void, thisArg?: any); on(event: "activityRequestPermissions", callback: (args: AndroidActivityRequestPermissionsEventData) => void, thisArg?: any);
} }

View File

@@ -187,7 +187,7 @@ export function off(eventNames: string, callback?: any, thisArg?: any);
/** /**
* @deprecated use application.run() instead. * @deprecated use application.run() instead.
* *
* Call this method to start the application. Important: All code after this method call will not be executed! * Call this method to start the application. Important: All code after this method call will not be executed!
*/ */
export function start(entry?: NavigationEntry | string); export function start(entry?: NavigationEntry | string);
@@ -371,6 +371,16 @@ export interface AndroidActivityResultEventData extends AndroidActivityEventData
intent: any /* android.content.Intent */; intent: any /* android.content.Intent */;
} }
/**
* Data for the Android activity newIntent event.
*/
export interface AndroidActivityNewIntentEventData extends AndroidActivityEventData {
/**
* The intent.
*/
intent: any /* android.content.Intent */;
}
/** /**
* Data for the Android activity back pressed event. * Data for the Android activity back pressed event.
*/ */
@@ -480,6 +490,11 @@ export class AndroidApplication extends Observable {
*/ */
on(event: "activityBackPressed", callback: (args: AndroidActivityBackPressedEventData) => void, thisArg?: any); on(event: "activityBackPressed", callback: (args: AndroidActivityBackPressedEventData) => void, thisArg?: any);
/**
* This event is raised when the Android app was launched by an Intent with data.
*/
on(event: "activityNewIntent", callback: (args: AndroidActivityNewIntentEventData) => void, thisArg?: any);
/** /**
* This event is raised when the Android activity requests permissions. * This event is raised when the Android activity requests permissions.
*/ */
@@ -530,6 +545,11 @@ export class AndroidApplication extends Observable {
*/ */
public static activityBackPressedEvent: string; public static activityBackPressedEvent: string;
/**
* String value used when hooking to activityNewIntent event.
*/
public static activityNewIntentEvent: string;
/** /**
* String value used when hooking to requestPermissions event. * String value used when hooking to requestPermissions event.
*/ */

View File

@@ -26,12 +26,11 @@ class NativeScriptActivity extends android.support.v7.app.AppCompatActivity {
setActivityCallbacks(this); setActivityCallbacks(this);
} }
this._callbacks.onCreate(this, savedInstanceState, super.onCreate); this._callbacks.onCreate(this, savedInstanceState, this.getIntent(), super.onCreate);
} }
public onNewIntent(intent: android.content.Intent): void { public onNewIntent(intent: android.content.Intent): void {
super.onNewIntent(intent); this._callbacks.onNewIntent(this, intent, super.setIntent, super.onNewIntent);
super.setIntent(intent);
} }
public onSaveInstanceState(outState: android.os.Bundle): void { public onSaveInstanceState(outState: android.os.Bundle): void {

View File

@@ -944,11 +944,17 @@ class ActivityCallbacksImplementation implements AndroidActivityCallbacks {
} }
@profile @profile
public onCreate(activity: android.support.v7.app.AppCompatActivity, savedInstanceState: android.os.Bundle, superFunc: Function): void { public onCreate(activity: android.support.v7.app.AppCompatActivity, savedInstanceState: android.os.Bundle, intentOrSuperFunc: android.content.Intent | Function, superFunc?: Function): void {
if (traceEnabled()) { if (traceEnabled()) {
traceWrite(`Activity.onCreate(${savedInstanceState})`, traceCategories.NativeLifecycle); traceWrite(`Activity.onCreate(${savedInstanceState})`, traceCategories.NativeLifecycle);
} }
const intent: android.content.Intent = superFunc ? <android.content.Intent>intentOrSuperFunc : undefined;
if (!superFunc) {
superFunc = <Function>intentOrSuperFunc;
}
// If there is savedInstanceState this call will recreate all fragments that were previously in the navigation. // If there is savedInstanceState this call will recreate all fragments that were previously in the navigation.
// We take care of associating them with a Page from our backstack in the onAttachFragment callback. // We take care of associating them with a Page from our backstack in the onAttachFragment callback.
// If there is savedInstanceState and moduleLoaded is false we are restarted but process was killed. // If there is savedInstanceState and moduleLoaded is false we are restarted but process was killed.
@@ -966,6 +972,15 @@ class ActivityCallbacksImplementation implements AndroidActivityCallbacks {
} }
} }
if (intent && intent.getAction()) {
application.android.notify(<application.AndroidActivityNewIntentEventData>{
eventName: application.AndroidApplication.activityNewIntentEvent,
object: application.android,
activity,
intent
});
}
this.setActivityContent(activity, savedInstanceState, true); this.setActivityContent(activity, savedInstanceState, true);
moduleLoaded = true; moduleLoaded = true;
} }
@@ -982,6 +997,19 @@ class ActivityCallbacksImplementation implements AndroidActivityCallbacks {
outState.putInt(ROOT_VIEW_ID_EXTRA, rootView._domId); outState.putInt(ROOT_VIEW_ID_EXTRA, rootView._domId);
} }
@profile
public onNewIntent(activity: android.support.v7.app.AppCompatActivity, intent: android.content.Intent, superSetIntentFunc: Function, superFunc: Function): void {
superFunc.call(activity, intent);
superSetIntentFunc.call(activity, intent);
application.android.notify(<application.AndroidActivityNewIntentEventData>{
eventName: application.AndroidApplication.activityNewIntentEvent,
object: application.android,
activity,
intent
});
}
@profile @profile
public onStart(activity: any, superFunc: Function): void { public onStart(activity: any, superFunc: Function): void {
superFunc.call(activity); superFunc.call(activity);
@@ -1188,7 +1216,7 @@ class ActivityCallbacksImplementation implements AndroidActivityCallbacks {
if (shouldCreateRootFrame) { if (shouldCreateRootFrame) {
const extras = intent.getExtras(); const extras = intent.getExtras();
let frameId = -1; let frameId = -1;
// We have extras when we call - new Frame().navigate(); // We have extras when we call - new Frame().navigate();
// savedInstanceState is used when activity is recreated. // savedInstanceState is used when activity is recreated.
// NOTE: On API 23+ we get extras on first run. // NOTE: On API 23+ we get extras on first run.
@@ -1196,17 +1224,17 @@ class ActivityCallbacksImplementation implements AndroidActivityCallbacks {
if (extras) { if (extras) {
frameId = extras.getInt(INTENT_EXTRA, -1); frameId = extras.getInt(INTENT_EXTRA, -1);
} }
if (savedInstanceState && frameId < 0) { if (savedInstanceState && frameId < 0) {
frameId = savedInstanceState.getInt(INTENT_EXTRA, -1); frameId = savedInstanceState.getInt(INTENT_EXTRA, -1);
} }
if (!rootView) { if (!rootView) {
// If we have frameId from extras - we are starting a new activity from navigation (e.g. new Frame().navigate())) // If we have frameId from extras - we are starting a new activity from navigation (e.g. new Frame().navigate()))
// Then we check if we have frameId from savedInstanceState - this happens when Activity is destroyed but app was not (e.g. suspend) // Then we check if we have frameId from savedInstanceState - this happens when Activity is destroyed but app was not (e.g. suspend)
rootView = getFrameByNumberId(frameId) || new Frame(); rootView = getFrameByNumberId(frameId) || new Frame();
} }
if (rootView instanceof Frame) { if (rootView instanceof Frame) {
rootView.navigate(mainEntry); rootView.navigate(mainEntry);
} else { } else {

View File

@@ -16,7 +16,7 @@ export * from "../page";
export class Frame extends View { export class Frame extends View {
/** /**
* @deprecated * @deprecated
* *
* String value used when hooking to androidOptionSelected event (prefix `android` states that this event is available only in Android). * String value used when hooking to androidOptionSelected event (prefix `android` states that this event is available only in Android).
*/ */
public static androidOptionSelectedEvent: string; public static androidOptionSelectedEvent: string;
@@ -33,7 +33,7 @@ export class Frame extends View {
canGoBack(): boolean; canGoBack(): boolean;
/** /**
* Navigates to a Page instance as described by the module name. * Navigates to a Page instance as described by the module name.
* This method will require the module and will check for a Page property in the exports of the module. * This method will require the module and will check for a Page property in the exports of the module.
* @param pageModuleName The name of the module to require starting from the application root. * @param pageModuleName The name of the module to require starting from the application root.
* For example if you want to navigate to page called "myPage.js" in a folder called "subFolder" and your root folder is "app" you can call navigate method like this: * For example if you want to navigate to page called "myPage.js" in a folder called "subFolder" and your root folder is "app" you can call navigate method like this:
@@ -175,7 +175,7 @@ export class Frame extends View {
/** /**
* A basic method signature to hook an event listener (shortcut alias to the addEventListener method). * A basic method signature to hook an event listener (shortcut alias to the addEventListener method).
* @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change"). * @param eventNames - String corresponding to events (e.g. "propertyChange"). Optionally could be used more events separated by `,` (e.g. "propertyChange", "change").
* @param callback - Callback function which will be executed when event is raised. * @param callback - Callback function which will be executed when event is raised.
* @param thisArg - An optional parameter which will be used as `this` context for callback execution. * @param thisArg - An optional parameter which will be used as `this` context for callback execution.
*/ */
@@ -210,7 +210,7 @@ export function goBack();
/** /**
* @deprecated use getFrameById() if you want to retrieve a frame different than the topmost one * @deprecated use getFrameById() if you want to retrieve a frame different than the topmost one
* *
* Gets the frames stack. * Gets the frames stack.
*/ */
export function stack(): Array<Frame>; export function stack(): Array<Frame>;
@@ -264,7 +264,7 @@ export interface NavigationEntry extends ViewEntry {
transitionAndroid?: NavigationTransition; transitionAndroid?: NavigationTransition;
/** /**
* True to record the navigation in the backstack, false otherwise. * True to record the navigation in the backstack, false otherwise.
* If the parameter is set to false then the Page will be displayed but once navigated from it will not be able to be navigated back to. * If the parameter is set to false then the Page will be displayed but once navigated from it will not be able to be navigated back to.
*/ */
backstackVisible?: boolean; backstackVisible?: boolean;
@@ -350,7 +350,7 @@ export interface BackstackEntry {
} }
/** /**
* Represents the data passed to the androidOptionSelected event. * Represents the data passed to the androidOptionSelected event.
* This event is raised by the Android OS when an option in the Activity's action bar has been selected. * This event is raised by the Android OS when an option in the Activity's action bar has been selected.
*/ */
export interface AndroidOptionEventData extends EventData { export interface AndroidOptionEventData extends EventData {
@@ -398,7 +398,7 @@ export interface AndroidFrame extends Observable {
/** /**
* @deprecated this property is not used internally * @deprecated this property is not used internally
* *
* Gets or sets whether the page UI will be cached when navigating away from the page. * Gets or sets whether the page UI will be cached when navigating away from the page.
*/ */
cachePagesOnNavigate: boolean; cachePagesOnNavigate: boolean;
@@ -413,8 +413,12 @@ export interface AndroidFrame extends Observable {
export interface AndroidActivityCallbacks { export interface AndroidActivityCallbacks {
getRootView(): View; getRootView(): View;
resetActivityContent(activity: any): void; resetActivityContent(activity: any): void;
/**
* @deprecated use onCreate(activity, savedInstanceState, intent, superFunc) instead.
*/
onCreate(activity: any, savedInstanceState: any, superFunc: Function): void; onCreate(activity: any, savedInstanceState: any, superFunc: Function): void;
onCreate(activity: any, savedInstanceState: any, intent: any, superFunc: Function): void;
onSaveInstanceState(activity: any, outState: any, superFunc: Function): void; onSaveInstanceState(activity: any, outState: any, superFunc: Function): void;
onStart(activity: any, superFunc: Function): void; onStart(activity: any, superFunc: Function): void;
onStop(activity: any, superFunc: Function): void; onStop(activity: any, superFunc: Function): void;
@@ -423,6 +427,7 @@ export interface AndroidActivityCallbacks {
onBackPressed(activity: any, superFunc: Function): void; onBackPressed(activity: any, superFunc: Function): void;
onRequestPermissionsResult(activity: any, requestCode: number, permissions: Array<String>, grantResults: Array<number>, superFunc: Function): void; onRequestPermissionsResult(activity: any, requestCode: number, permissions: Array<String>, grantResults: Array<number>, superFunc: Function): void;
onActivityResult(activity: any, requestCode: number, resultCode: number, data: any, superFunc: Function); onActivityResult(activity: any, requestCode: number, resultCode: number, data: any, superFunc: Function);
onNewIntent(activity: any, intent: any, superSetIntentFunc: Function, superFunc: Function): void;
} }
export interface AndroidFragmentCallbacks { export interface AndroidFragmentCallbacks {