mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Merge pull request #2885 from NativeScript/android-current-context
Application and ActivityEvents improvements
This commit is contained in:
@@ -9,7 +9,7 @@ import types = require("utils/types");
|
||||
global.moduleMerge(dialogsCommon, exports);
|
||||
|
||||
function createAlertDialog(options?: dialogs.DialogOptions): android.app.AlertDialog.Builder {
|
||||
var alert = new android.app.AlertDialog.Builder(appmodule.android.currentContext);
|
||||
const alert = new android.app.AlertDialog.Builder(appmodule.android.foregroundActivity);
|
||||
alert.setTitle(options && types.isString(options.title) ? options.title : "");
|
||||
alert.setMessage(options && types.isString(options.message) ? options.message : "");
|
||||
if (options && options.cancelable === false) {
|
||||
@@ -19,21 +19,21 @@ function createAlertDialog(options?: dialogs.DialogOptions): android.app.AlertDi
|
||||
}
|
||||
|
||||
function showDialog(builder: android.app.AlertDialog.Builder) {
|
||||
var dlg = builder.show();
|
||||
const dlg = builder.show();
|
||||
|
||||
var labelColor = dialogsCommon.getLabelColor();
|
||||
const labelColor = dialogsCommon.getLabelColor();
|
||||
if (labelColor) {
|
||||
var textViewId = dlg.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
|
||||
const textViewId = dlg.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
|
||||
if (textViewId) {
|
||||
var tv = <android.widget.TextView>dlg.findViewById(textViewId);
|
||||
const tv = <android.widget.TextView>dlg.findViewById(textViewId);
|
||||
if (tv) {
|
||||
tv.setTextColor(labelColor.android);
|
||||
}
|
||||
}
|
||||
|
||||
var messageTextViewId = dlg.getContext().getResources().getIdentifier("android:id/message", null, null);
|
||||
const messageTextViewId = dlg.getContext().getResources().getIdentifier("android:id/message", null, null);
|
||||
if (messageTextViewId) {
|
||||
var messageTextView = <android.widget.TextView>dlg.findViewById(messageTextViewId);
|
||||
const messageTextView = <android.widget.TextView>dlg.findViewById(messageTextViewId);
|
||||
if (messageTextView) {
|
||||
messageTextView.setTextColor(labelColor.android);
|
||||
}
|
||||
@@ -98,9 +98,9 @@ function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options
|
||||
export function alert(arg: any): Promise<void> {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
try {
|
||||
var options = !dialogsCommon.isDialogOptions(arg) ? { title: dialogsCommon.ALERT, okButtonText: dialogsCommon.OK, message: arg + "" } : arg;
|
||||
const options = !dialogsCommon.isDialogOptions(arg) ? { title: dialogsCommon.ALERT, okButtonText: dialogsCommon.OK, message: arg + "" } : arg;
|
||||
|
||||
var alert = createAlertDialog(options);
|
||||
const alert = createAlertDialog(options);
|
||||
|
||||
alert.setPositiveButton(options.okButtonText, new android.content.DialogInterface.OnClickListener({
|
||||
onClick: function (dialog: android.content.DialogInterface, id: number) {
|
||||
@@ -125,8 +125,8 @@ export function alert(arg: any): Promise<void> {
|
||||
export function confirm(arg: any): Promise<boolean> {
|
||||
return new Promise<boolean>((resolve, reject) => {
|
||||
try {
|
||||
var options = !dialogsCommon.isDialogOptions(arg) ? { title: dialogsCommon.CONFIRM, okButtonText: dialogsCommon.OK, cancelButtonText: dialogsCommon.CANCEL, message: arg + "" } : arg;
|
||||
var alert = createAlertDialog(options);
|
||||
const options = !dialogsCommon.isDialogOptions(arg) ? { title: dialogsCommon.CONFIRM, okButtonText: dialogsCommon.OK, cancelButtonText: dialogsCommon.CANCEL, message: arg + "" } : arg;
|
||||
const alert = createAlertDialog(options);
|
||||
|
||||
addButtonsToAlertDialog(alert, options, function (result) { resolve(result); });
|
||||
|
||||
@@ -139,9 +139,9 @@ export function confirm(arg: any): Promise<boolean> {
|
||||
}
|
||||
|
||||
export function prompt(arg: any): Promise<dialogs.PromptResult> {
|
||||
var options: dialogs.PromptOptions;
|
||||
let options: dialogs.PromptOptions;
|
||||
|
||||
var defaultOptions = {
|
||||
const defaultOptions = {
|
||||
title: dialogsCommon.PROMPT,
|
||||
okButtonText: dialogsCommon.OK,
|
||||
cancelButtonText: dialogsCommon.CANCEL,
|
||||
@@ -165,9 +165,9 @@ export function prompt(arg: any): Promise<dialogs.PromptResult> {
|
||||
|
||||
return new Promise<dialogs.PromptResult>((resolve, reject) => {
|
||||
try {
|
||||
var alert = createAlertDialog(options);
|
||||
const alert = createAlertDialog(options);
|
||||
|
||||
var input = new android.widget.EditText(appmodule.android.currentContext);
|
||||
const input = new android.widget.EditText(appmodule.android.foregroundActivity);
|
||||
|
||||
if (options && options.inputType === dialogsCommon.inputType.password) {
|
||||
input.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD);
|
||||
@@ -177,7 +177,7 @@ export function prompt(arg: any): Promise<dialogs.PromptResult> {
|
||||
|
||||
alert.setView(input);
|
||||
|
||||
var getText = function () { return input.getText().toString(); };
|
||||
const getText = function () { return input.getText().toString(); };
|
||||
|
||||
addButtonsToAlertDialog(alert, options, function (r) { resolve({ result: r, text: getText() }); });
|
||||
|
||||
@@ -191,9 +191,9 @@ export function prompt(arg: any): Promise<dialogs.PromptResult> {
|
||||
}
|
||||
|
||||
export function login(arg: any): Promise<dialogs.LoginResult> {
|
||||
var options: dialogs.LoginOptions;
|
||||
let options: dialogs.LoginOptions;
|
||||
|
||||
var defaultOptions = { title: dialogsCommon.LOGIN, okButtonText: dialogsCommon.OK, cancelButtonText: dialogsCommon.CANCEL };
|
||||
const defaultOptions = { title: dialogsCommon.LOGIN, okButtonText: dialogsCommon.OK, cancelButtonText: dialogsCommon.CANCEL };
|
||||
|
||||
if (arguments.length === 1) {
|
||||
if (types.isString(arguments[0])) {
|
||||
@@ -219,18 +219,18 @@ export function login(arg: any): Promise<dialogs.LoginResult> {
|
||||
|
||||
return new Promise<dialogs.LoginResult>((resolve, reject) => {
|
||||
try {
|
||||
var context = appmodule.android.currentContext;
|
||||
const context = appmodule.android.foregroundActivity;
|
||||
|
||||
var alert = createAlertDialog(options);
|
||||
const alert = createAlertDialog(options);
|
||||
|
||||
var userNameInput = new android.widget.EditText(context);
|
||||
const userNameInput = new android.widget.EditText(context);
|
||||
userNameInput.setText(options.userName ? options.userName : "");
|
||||
|
||||
var passwordInput = new android.widget.EditText(context);
|
||||
const passwordInput = new android.widget.EditText(context);
|
||||
passwordInput.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD);
|
||||
passwordInput.setText(options.password ? options.password : "");
|
||||
|
||||
var layout = new android.widget.LinearLayout(context);
|
||||
const layout = new android.widget.LinearLayout(context);
|
||||
layout.setOrientation(1);
|
||||
layout.addView(userNameInput);
|
||||
layout.addView(passwordInput);
|
||||
@@ -255,10 +255,9 @@ export function login(arg: any): Promise<dialogs.LoginResult> {
|
||||
}
|
||||
|
||||
export function action(arg: any): Promise<string> {
|
||||
let options: dialogs.ActionOptions;
|
||||
|
||||
var options: dialogs.ActionOptions;
|
||||
|
||||
var defaultOptions = { title: null, cancelButtonText: dialogsCommon.CANCEL };
|
||||
const defaultOptions = { title: null, cancelButtonText: dialogsCommon.CANCEL };
|
||||
|
||||
if (arguments.length === 1) {
|
||||
if (types.isString(arguments[0])) {
|
||||
@@ -284,10 +283,10 @@ export function action(arg: any): Promise<string> {
|
||||
|
||||
return new Promise<string>((resolve, reject) => {
|
||||
try {
|
||||
var activity = appmodule.android.foregroundActivity || appmodule.android.startActivity;
|
||||
var alert = new android.app.AlertDialog.Builder(activity);
|
||||
var message = options && types.isString(options.message) ? options.message : "";
|
||||
var title = options && types.isString(options.title) ? options.title : "";
|
||||
const activity = appmodule.android.foregroundActivity || appmodule.android.startActivity;
|
||||
const alert = new android.app.AlertDialog.Builder(activity);
|
||||
const message = options && types.isString(options.message) ? options.message : "";
|
||||
const title = options && types.isString(options.title) ? options.title : "";
|
||||
if (options && options.cancelable === false) {
|
||||
alert.setCancelable(false);
|
||||
}
|
||||
|
||||
@@ -43,4 +43,8 @@ class NativeScriptActivity extends android.app.Activity {
|
||||
protected onActivityResult(requestCode: number, resultCode: number, data: android.content.Intent): void {
|
||||
this._callbacks.onActivityResult(this, requestCode, resultCode, data, super.onActivityResult);
|
||||
}
|
||||
}
|
||||
|
||||
export function isNativeScriptActivity(activity: android.app.Activity): boolean {
|
||||
return activity instanceof NativeScriptActivity;
|
||||
}
|
||||
3
tns-core-modules/ui/frame/activity.d.ts
vendored
Normal file
3
tns-core-modules/ui/frame/activity.d.ts
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
declare module "ui/frame/activity" {
|
||||
export function isNativeScriptActivity(activity: any/* android.app.Activity */): boolean;
|
||||
}
|
||||
@@ -49,7 +49,7 @@ function onFragmentShown(fragment: android.app.Fragment) {
|
||||
}
|
||||
}
|
||||
|
||||
var isBack = currentNavigationContext ? currentNavigationContext.isBackNavigation : false;
|
||||
const isBack = currentNavigationContext ? currentNavigationContext.isBackNavigation : false;
|
||||
|
||||
frame._addView(page);
|
||||
|
||||
@@ -286,7 +286,7 @@ export class Frame extends frameCommon.Frame {
|
||||
this._currentEntry.isNavigation = true;
|
||||
}
|
||||
|
||||
var manager = this._android.activity.getFragmentManager();
|
||||
const manager = this._android.activity.getFragmentManager();
|
||||
if (manager.getBackStackEntryCount() > 0) {
|
||||
// pop all other fragments up until the named one
|
||||
// this handles cases where user may navigate to an inner page without adding it on the backstack
|
||||
@@ -337,12 +337,12 @@ export class Frame extends frameCommon.Frame {
|
||||
if (!this._android.activity) {
|
||||
return;
|
||||
}
|
||||
var manager = this._android.activity.getFragmentManager();
|
||||
var length = manager.getBackStackEntryCount();
|
||||
var i = length - 1;
|
||||
const manager = this._android.activity.getFragmentManager();
|
||||
const length = manager.getBackStackEntryCount();
|
||||
let i = length - 1;
|
||||
console.log(`Fragment Manager Back Stack: `);
|
||||
while (i >= 0) {
|
||||
var fragment = manager.findFragmentByTag(manager.getBackStackEntryAt(i--).getName());
|
||||
const fragment = manager.findFragmentByTag(manager.getBackStackEntryAt(i--).getName());
|
||||
console.log(`\t${fragment}`);
|
||||
}
|
||||
}
|
||||
@@ -375,7 +375,7 @@ export class Frame extends frameCommon.Frame {
|
||||
trace.write(`Frame _processNavigationContext: Drop For Activity GC-ed`, trace.categories.Navigation);
|
||||
}
|
||||
unsubscribe();
|
||||
return
|
||||
return;
|
||||
}
|
||||
if (isCurrent) {
|
||||
if (trace.enabled) {
|
||||
@@ -384,7 +384,7 @@ export class Frame extends frameCommon.Frame {
|
||||
super._processNavigationContext(navigationContext);
|
||||
unsubscribe();
|
||||
}
|
||||
}
|
||||
};
|
||||
let unsubscribe = () => {
|
||||
if (trace.enabled) {
|
||||
trace.write(`Frame _processNavigationContext: Unsubscribe from Activity.Resumed`, trace.categories.Navigation);
|
||||
@@ -392,7 +392,7 @@ export class Frame extends frameCommon.Frame {
|
||||
application.android.off(application.AndroidApplication.activityResumedEvent, resume);
|
||||
application.android.off(application.AndroidApplication.activityStoppedEvent, unsubscribe);
|
||||
application.android.off(application.AndroidApplication.activityDestroyedEvent, unsubscribe);
|
||||
}
|
||||
};
|
||||
|
||||
if (trace.enabled) {
|
||||
trace.write(`Frame._processNavigationContext: Subscribe for Activity.Resumed`, trace.categories.Navigation);
|
||||
@@ -563,7 +563,7 @@ function findPageForFragment(fragment: android.app.Fragment, frame: Frame) {
|
||||
}
|
||||
}
|
||||
else {
|
||||
var backStack = frame.backStack;
|
||||
const backStack = frame.backStack;
|
||||
for (let i = 0; i < backStack.length; i++) {
|
||||
if (backStack[i].fragmentTag === fragmentTag) {
|
||||
entry = backStack[i];
|
||||
@@ -590,7 +590,7 @@ function findPageForFragment(fragment: android.app.Fragment, frame: Frame) {
|
||||
|
||||
function startActivity(activity: android.app.Activity, frameId: number) {
|
||||
// TODO: Implicitly, we will open the same activity type as the current one
|
||||
var intent = new android.content.Intent(activity, activity.getClass());
|
||||
const intent = new android.content.Intent(activity, activity.getClass());
|
||||
intent.setAction(android.content.Intent.ACTION_DEFAULT);
|
||||
intent.putExtra(INTENT_EXTRA, frameId);
|
||||
|
||||
@@ -659,7 +659,7 @@ class FragmentCallbacksImplementation implements definition.AndroidFragmentCallb
|
||||
}
|
||||
|
||||
public onCreateAnimator(fragment: android.app.Fragment, transit: number, enter: boolean, nextAnim: number, superFunc: Function): android.animation.Animator {
|
||||
var nextAnimString: string;
|
||||
let nextAnimString: string;
|
||||
switch (nextAnim) {
|
||||
case -10: nextAnimString = "enter"; break;
|
||||
case -20: nextAnimString = "exit"; break;
|
||||
@@ -667,7 +667,7 @@ class FragmentCallbacksImplementation implements definition.AndroidFragmentCallb
|
||||
case -40: nextAnimString = "popExit"; break;
|
||||
}
|
||||
|
||||
var animator = transitionModule._onFragmentCreateAnimator(fragment, nextAnim);
|
||||
let animator = transitionModule._onFragmentCreateAnimator(fragment, nextAnim);
|
||||
|
||||
if (!animator) {
|
||||
animator = superFunc.call(fragment, transit, enter, nextAnim);
|
||||
@@ -706,8 +706,8 @@ class FragmentCallbacksImplementation implements definition.AndroidFragmentCallb
|
||||
if (trace.enabled) {
|
||||
trace.write(`${fragment}.onCreateView(inflater, container, ${savedInstanceState})`, trace.categories.NativeLifecycle);
|
||||
}
|
||||
var entry = this.entry;
|
||||
var page = entry.resolvedPage;
|
||||
const entry = this.entry;
|
||||
const page = entry.resolvedPage;
|
||||
if (savedInstanceState && savedInstanceState.getBoolean(HIDDEN, false)) {
|
||||
fragment.getFragmentManager().beginTransaction().hide(fragment).commit();
|
||||
page._onAttached(fragment.getActivity());
|
||||
@@ -780,7 +780,7 @@ class ActivityCallbacksImplementation implements definition.AndroidActivityCallb
|
||||
}
|
||||
|
||||
if (savedInstanceState && frameId < 0) {
|
||||
frameId = savedInstanceState.getInt(INTENT_EXTRA, -1)
|
||||
frameId = savedInstanceState.getInt(INTENT_EXTRA, -1);
|
||||
}
|
||||
|
||||
// If we have frameId from extras - we are starting a new activity from navigation (e.g. new Frame().navigate()))
|
||||
@@ -862,7 +862,7 @@ class ActivityCallbacksImplementation implements definition.AndroidActivityCallb
|
||||
}
|
||||
|
||||
public onDestroy(activity: any, superFunc: Function): void {
|
||||
let rootView = this._rootView
|
||||
let rootView = this._rootView;
|
||||
if (rootView && rootView._context) {
|
||||
rootView._onDetached(true);
|
||||
}
|
||||
@@ -872,6 +872,12 @@ class ActivityCallbacksImplementation implements definition.AndroidActivityCallb
|
||||
if (trace.enabled) {
|
||||
trace.write("NativeScriptActivity.onDestroy();", trace.categories.NativeLifecycle);
|
||||
}
|
||||
|
||||
const exitArgs = { eventName: application.exitEvent, object: application.android, android: activity };
|
||||
application.notify(exitArgs);
|
||||
if (application.onExit) {
|
||||
application.onExit();
|
||||
}
|
||||
}
|
||||
|
||||
public onBackPressed(activity: any, superFunc: Function): void {
|
||||
@@ -879,7 +885,7 @@ class ActivityCallbacksImplementation implements definition.AndroidActivityCallb
|
||||
trace.write("NativeScriptActivity.onBackPressed;", trace.categories.NativeLifecycle);
|
||||
}
|
||||
|
||||
var args = <application.AndroidActivityBackPressedEventData>{
|
||||
const args = <application.AndroidActivityBackPressedEventData>{
|
||||
eventName: "activityBackPressed",
|
||||
object: application.android,
|
||||
activity: activity,
|
||||
@@ -917,7 +923,7 @@ class ActivityCallbacksImplementation implements definition.AndroidActivityCallb
|
||||
trace.write(`NativeScriptActivity.onActivityResult(${requestCode}, ${resultCode}, ${data})`, trace.categories.NativeLifecycle);
|
||||
}
|
||||
|
||||
var result = application.android.onActivityResult;
|
||||
const result = application.android.onActivityResult;
|
||||
if (result) {
|
||||
result(requestCode, resultCode, data);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user