Fix Android Test Application and some android issues

This commit is contained in:
Nathanael Anderson
2020-08-14 01:28:49 -05:00
parent a351695d29
commit 315f0e0f73
8 changed files with 41 additions and 65 deletions

View File

@@ -1,17 +1,17 @@
/* tslint:disable:no-unused-variable */
import { Application, isAndroid } from '@nativescript/core';
import { Application } from '@nativescript/core';
import * as TKUnit from '../tk-unit';
export * from './application-tests-common';
// >> application-app-android
var androidApp = Application.android;
let androidApp = Application.android;
// << application-app-android
// >> application-app-android-context
var context = Application.android.context;
let context = Application.android.context;
//// get the Files (Documents) folder (directory)
var dir = context.getFilesDir();
let dir = context.getFilesDir();
// << application-app-android-context
// >> application-app-android-current
@@ -22,16 +22,16 @@ if (androidApp.foregroundActivity === androidApp.startActivity) {
// >> application-app-android-broadcast
//// Register the broadcast receiver
if (isAndroid) {
if (androidApp) {
Application.android.registerBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED, function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
var level = intent.getIntExtra(android.os.BatteryManager.EXTRA_LEVEL, -1);
var scale = intent.getIntExtra(android.os.BatteryManager.EXTRA_SCALE, -1);
var percent = (level / scale) * 100.0;
const level = intent.getIntExtra(android.os.BatteryManager.EXTRA_LEVEL, -1);
const scale = intent.getIntExtra(android.os.BatteryManager.EXTRA_SCALE, -1);
const percent = (level / scale) * 100.0;
////console.log("Battery: " + percent + "%");
});
}
//// When no longer needed, unregister the broadcast receiver
if (isAndroid) {
if (androidApp) {
Application.android.unregisterBroadcastReceiver(android.content.Intent.ACTION_BATTERY_CHANGED);
}
// << application-app-android-broadcast

View File

@@ -1,9 +1,10 @@
import { Frame } from '@nativescript/core/ui/frame';
import * as TKUnit from '../../tk-unit';
import { unsetValue, PercentLength } from '@nativescript/core/ui/core/view';
import { unsetValue } from '@nativescript/core/ui/core/view';
import { PercentLength } from '@nativescript/core/ui/styling/style-properties';
export * from './frame-tests-common';
export function test_percent_width_and_height_set_to_page_support() {
let topFrame = Frame.topmost();
let currentPage = topFrame.currentPage;

View File

@@ -1,12 +1,14 @@
import * as TKUnit from '../../tk-unit';
import * as helper from '../../ui-helper';
import { View, isIOS, unsetValue } from '@nativescript/core/ui/core/view';
import { View, unsetValue } from '@nativescript/core/ui/core/view';
import { Button } from '@nativescript/core/ui/button';
import * as types from '@nativescript/core/utils/types';
import { StackLayout } from '@nativescript/core/ui/layouts/stack-layout';
import { Label } from '@nativescript/core/ui/label';
import { Trace } from '@nativescript/core';
import { Color } from '@nativescript/core/color';
import { isIOS } from '@nativescript/core';
// enable the trace, it is disabled by default
Trace.enable();

View File

@@ -49,7 +49,7 @@ export class AndroidApplication extends Observable implements AndroidApplication
// we are using these property to store the callbacks to avoid early GC collection which would trigger MarkReachableObjects
private callbacks: any = {};
public init(nativeApp: android.app.Application) {
public init(nativeApp: android.app.Application): void {
if (this.nativeApp === nativeApp) {
return;
}
@@ -107,13 +107,12 @@ export class AndroidApplication extends Observable implements AndroidApplication
this._systemAppearance = value;
}
public registerBroadcastReceiver(intentFilter: string, onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
public registerBroadcastReceiver(intentFilter: string, onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void): void {
ensureBroadCastReceiverClass();
const that = this;
const registerFunc = function (context: android.content.Context) {
const registerFunc = (context: android.content.Context) => {
const receiver: android.content.BroadcastReceiver = new BroadcastReceiverClass(onReceiveCallback);
context.registerReceiver(receiver, new android.content.IntentFilter(intentFilter));
that._registeredReceivers[intentFilter] = receiver;
this._registeredReceivers[intentFilter] = receiver;
};
if (this.context) {
@@ -123,7 +122,7 @@ export class AndroidApplication extends Observable implements AndroidApplication
}
}
public unregisterBroadcastReceiver(intentFilter: string) {
public unregisterBroadcastReceiver(intentFilter: string): void {
const receiver = this._registeredReceivers[intentFilter];
if (receiver) {
this.context.unregisterReceiver(receiver);
@@ -188,7 +187,7 @@ export function addCss(cssText: string, attributeScoped?: boolean): void {
const CALLBACKS = '_callbacks';
export function _resetRootView(entry?: NavigationEntry | string) {
export function _resetRootView(entry?: NavigationEntry | string): void {
const activity = androidApp.foregroundActivity;
if (!activity) {
throw new Error('Cannot find android activity.');
@@ -322,7 +321,7 @@ function initLifecycleCallbacks() {
object: androidApp,
activity,
});
let viewTreeObserver = rootView.getViewTreeObserver();
const viewTreeObserver = rootView.getViewTreeObserver();
viewTreeObserver.removeOnGlobalLayoutListener(global.onGlobalLayoutListener);
},
});
@@ -419,7 +418,7 @@ function initLifecycleCallbacks() {
}
function initComponentCallbacks() {
let componentCallbacks = new android.content.ComponentCallbacks2({
const componentCallbacks = new android.content.ComponentCallbacks2({
onLowMemory: <any>profile('onLowMemory', function () {
gc();
java.lang.System.gc();
@@ -477,51 +476,24 @@ function ensureBroadCastReceiverClass() {
return;
}
const BroadcastReceiver = (<any>android.content.BroadcastReceiver).extend({
init(onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
// super();
this._onReceiveCallback = onReceiveCallback;
@NativeClass
class BroadcastReceiver extends android.content.BroadcastReceiver {
private _onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void;
// return global.__native(this);
},
_onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
if (this._onReceiveCallback) {
this._onReceiveCallback(context, intent);
}
},
});
constructor(onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
super();
this._onReceiveCallback = onReceiveCallback;
// @NativeClass
// class BroadcastReceiver extends android.content.BroadcastReceiver {
// private _onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void;
return global.__native(this);
}
// constructor(onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) {
// super();
// this._onReceiveCallback = onReceiveCallback;
public onReceive(context: android.content.Context, intent: android.content.Intent) {
if (this._onReceiveCallback) {
this._onReceiveCallback(context, intent);
}
}
}
// return global.__native(this);
// }
// public onReceive(context: android.content.Context, intent: android.content.Intent) {
// if (this._onReceiveCallback) {
// this._onReceiveCallback(context, intent);
// }
// }
// }
// var BroadcastReceiver = (function (_super) {
// __extends(BroadcastReceiver, _super);
// function BroadcastReceiver(onReceiveCallback) {
// var _this = _super.call(this) || this;
// _this._onReceiveCallback = onReceiveCallback;
// return global.__native(_this);
// }
// BroadcastReceiver.prototype.onReceive = function (context, intent) {
// if (this._onReceiveCallback) {
// this._onReceiveCallback(context, intent);
// }
// };
// return BroadcastReceiver;
// })(android.content.BroadcastReceiver);
BroadcastReceiverClass = BroadcastReceiver;
}

View File

@@ -577,7 +577,7 @@ export module knownFolders {
export module ios {
function _checkPlatform(knownFolderName: string) {
if (!global.isIOS) {
console.log(`The "${knownFolderName}" known folder is available on iOS only!`);
throw new Error(`The "${knownFolderName}" known folder is available on iOS only!`);
}
}

View File

@@ -10,7 +10,7 @@ import { EventData } from '../../../data/observable';
import { Trace } from '../../../trace';
import { ViewHelper } from './view-helper';
import { HorizontalAlignment, VerticalAlignment, Visibility, Length, PercentLength } from '../../styling/style-properties';
import { HorizontalAlignment, VerticalAlignment, Visibility, Length, PercentLength, BackgroundRepeat } from '../../styling/style-properties';
import { observe as gestureObserve, GesturesObserver, GestureTypes, GestureEventData, fromString as gestureFromString } from '../../gestures';
@@ -19,7 +19,6 @@ import { Builder } from '../../builder';
import { sanitizeModuleName } from '../../builder/module-name-sanitizer';
import { StyleScope } from '../../styling/style-scope';
import { LinearGradient } from '../../styling/linear-gradient';
import { BackgroundRepeat } from '../../styling/style-properties';
import { TextTransform } from '../../text-base';
import * as am from '../../animation';

View File

@@ -13,6 +13,8 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:name="com.tns.NativeScriptApplication"