mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 11:01:21 +08:00

Switch Activity / Fragment / FragmentManager implementation from native framework to support library APIs BREAKING CHANGE: NativeScript core framework now extends support library APIs versus native framework classes as per Google's latest guidelines: - NativeScript activities now extend `android.support.v7.app.AppCompatActivity` (vs android.app.Activity) - NativeScript fragments now extend `android.support.v4.app.Fragment` (vs android.app.Fragment) - NativeScript now works internally with `android.support.v4.app.FragmentManager` (vs android.app.FragmentManager) The implications of these changes should be mostly transparent to the developer except for the fact that the support library Fragment / FragmentManager work with Animation APIs versus Animator APIs. For Android API Levels lower than 28 the new Fragment API uses a different fragment enter animation by default. You can customise the transition per navigation entry or globally via the [navigation transitions API](https://docs.nativescript.org/core-concepts/navigation#navigation-transitions) Before: Default fragment enter animation was fade animation After: Default fragment enter animation for API levels lower than 28 is now a fast "push fade" animation; default fragment enter animation for API levels equal to or greater than 28 remains fade animation Before: AndroidFragmentCallbacks interface exposed the following `onCreateAnimator(...)` method ``` ts export interface AndroidFragmentCallbacks { onCreateAnimator(fragment: any, transit: number, enter: boolean, nextAnim: number, superFunc: Function): any; // ... } ``` After: AndroidFragmentCallbacks interface now exposes the following `onCreateAnimation(...)` method instead (and `onCreateAnimator(...)` is now removed) ``` ts export interface AndroidFragmentCallbacks { onCreateAnimation(fragment: any, transit: number, enter: boolean, nextAnim: number, superFunc: Function): any; // ... } ``` Before: Transition class exposed the following abstract `createAndroidAnimator(...)` method ``` ts export class Transition { public createAndroidAnimator(transitionType: string): any; // ... } ``` After: Transition class now exposes the following abstract `createAndroidAnimation(...)` method instead (and `createAndroidAnimation(...) is now removed) ``` ts export class Transition { public createAndroidAnimation(transitionType: string): any; // ... } ``` To migrate the code of your custom transitions follow the example below: Before: ``` ts import * as transition from "tns-core-modules/ui/transition"; export class CustomTransition extends transition.Transition { constructor(duration: number, curve: any) { super(duration, curve); } public createAndroidAnimator(transitionType: string): android.animation.Animator { var scaleValues = Array.create("float", 2); switch (transitionType) { case transition.AndroidTransitionType.enter: case transition.AndroidTransitionType.popEnter: scaleValues[0] = 0; scaleValues[1] = 1; break; case transition.AndroidTransitionType.exit: case transition.AndroidTransitionType.popExit: scaleValues[0] = 1; scaleValues[1] = 0; break; } var objectAnimators = Array.create(android.animation.Animator, 2); objectAnimators[0] = android.animation.ObjectAnimator.ofFloat(null, "scaleX", scaleValues); objectAnimators[1] = android.animation.ObjectAnimator.ofFloat(null, "scaleY", scaleValues); var animatorSet = new android.animation.AnimatorSet(); animatorSet.playTogether(objectAnimators); var duration = this.getDuration(); if (duration !== undefined) { animatorSet.setDuration(duration); } animatorSet.setInterpolator(this.getCurve()); return animatorSet; } } ``` After: ``` ts import * as transition from "tns-core-modules/ui/transition"; export class CustomTransition extends transition.Transition { constructor(duration: number, curve: any) { super(duration, curve); } public createAndroidAnimation(transitionType: string): android.view.animation.Animation { const scaleValues = []; switch (transitionType) { case transition.AndroidTransitionType.enter: case transition.AndroidTransitionType.popEnter: scaleValues[0] = 0; scaleValues[1] = 1; break; case transition.AndroidTransitionType.exit: case transition.AndroidTransitionType.popExit: scaleValues[0] = 1; scaleValues[1] = 0; break; } const animationSet = new android.view.animation.AnimationSet(false); const duration = this.getDuration(); if (duration !== undefined) { animationSet.setDuration(duration); } animationSet.setInterpolator(this.getCurve()); animationSet.addAnimation( new android.view.animation.ScaleAnimation( scaleValues[0], scaleValues[1], scaleValues[0], scaleValues[1] )); return animationSet; } } ```
223 lines
7.5 KiB
TypeScript
223 lines
7.5 KiB
TypeScript
import {
|
|
write as traceWrite, categories as traceCategories, messageType as traceMessageType
|
|
} from "../trace";
|
|
|
|
export * from "./utils-common";
|
|
|
|
import { getNativeApplication, android as androidApp } from "../application";
|
|
|
|
export module layout {
|
|
let density: number;
|
|
|
|
// cache the MeasureSpec constants here, to prevent extensive marshaling calls to and from Java
|
|
// TODO: While this boosts the performance it is error-prone in case Google changes these constants
|
|
const MODE_SHIFT = 30;
|
|
const MODE_MASK = 0x3 << MODE_SHIFT;
|
|
let sdkVersion: number;
|
|
let useOldMeasureSpec = false;
|
|
|
|
export function makeMeasureSpec(size: number, mode: number): number {
|
|
if (sdkVersion === undefined) {
|
|
// check whether the old layout is needed
|
|
sdkVersion = ad.getApplicationContext().getApplicationInfo().targetSdkVersion;
|
|
useOldMeasureSpec = sdkVersion <= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
|
|
}
|
|
|
|
if (useOldMeasureSpec) {
|
|
return size + mode;
|
|
}
|
|
|
|
return (size & ~MODE_MASK) | (mode & MODE_MASK);
|
|
}
|
|
|
|
export function getDisplayDensity(): number {
|
|
if (density === undefined) {
|
|
density = ad.getResources().getDisplayMetrics().density;
|
|
}
|
|
|
|
return density;
|
|
}
|
|
|
|
export function toDevicePixels(value: number): number {
|
|
return value * getDisplayDensity();
|
|
}
|
|
|
|
export function toDeviceIndependentPixels(value: number): number {
|
|
return value / getDisplayDensity();
|
|
}
|
|
|
|
export function measureNativeView(nativeView: any /* android.view.View */, width: number, widthMode: number, height: number, heightMode: number): { width: number, height: number } {
|
|
const view = <android.view.View>nativeView;
|
|
view.measure(makeMeasureSpec(width, widthMode), makeMeasureSpec(height, heightMode));
|
|
return {
|
|
width: view.getMeasuredWidth(),
|
|
height: view.getMeasuredHeight()
|
|
};
|
|
}
|
|
}
|
|
|
|
// We are using "ad" here to avoid namespace collision with the global android object
|
|
export module ad {
|
|
|
|
let application: android.app.Application;
|
|
let applicationContext: android.content.Context;
|
|
let contextResources: android.content.res.Resources;
|
|
let packageName: string;
|
|
export function getApplicationContext() {
|
|
if (!applicationContext) {
|
|
applicationContext = getApplication().getApplicationContext();
|
|
}
|
|
|
|
return applicationContext;
|
|
}
|
|
export function getApplication() {
|
|
if (!application) {
|
|
application = (<android.app.Application>getNativeApplication());
|
|
}
|
|
|
|
return application;
|
|
}
|
|
export function getResources() {
|
|
if (!contextResources) {
|
|
contextResources = getApplication().getResources();
|
|
}
|
|
|
|
return contextResources;
|
|
}
|
|
function getPackageName() {
|
|
if (!packageName) {
|
|
packageName = getApplicationContext().getPackageName();
|
|
}
|
|
|
|
return packageName;
|
|
}
|
|
|
|
let inputMethodManager: android.view.inputmethod.InputMethodManager;
|
|
export function getInputMethodManager(): android.view.inputmethod.InputMethodManager {
|
|
if (!inputMethodManager) {
|
|
inputMethodManager = <android.view.inputmethod.InputMethodManager>getApplicationContext().getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
|
|
}
|
|
return inputMethodManager;
|
|
}
|
|
|
|
export function showSoftInput(nativeView: android.view.View): void {
|
|
const inputManager = getInputMethodManager();
|
|
if (inputManager && nativeView instanceof android.view.View) {
|
|
inputManager.showSoftInput(nativeView, android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT);
|
|
}
|
|
}
|
|
|
|
export function dismissSoftInput(nativeView?: android.view.View): void {
|
|
const inputManager = getInputMethodManager();
|
|
let windowToken: android.os.IBinder;
|
|
|
|
if (nativeView instanceof android.view.View) {
|
|
windowToken = nativeView.getWindowToken()
|
|
} else if (androidApp.foregroundActivity instanceof android.support.v7.app.AppCompatActivity) {
|
|
const decorView = androidApp.foregroundActivity.getWindow().getDecorView();
|
|
windowToken = decorView ? decorView.getWindowToken() : null;
|
|
}
|
|
|
|
if (inputManager && windowToken) {
|
|
inputManager.hideSoftInputFromWindow(windowToken, 0);
|
|
}
|
|
}
|
|
|
|
export module collections {
|
|
export function stringArrayToStringSet(str: string[]): java.util.HashSet<string> {
|
|
var hashSet = new java.util.HashSet<string>();
|
|
if (str !== undefined) {
|
|
for (var element in str) {
|
|
hashSet.add("" + str[element]);
|
|
}
|
|
}
|
|
return hashSet;
|
|
}
|
|
|
|
export function stringSetToStringArray(stringSet: any): string[] {
|
|
var arr = [];
|
|
if (stringSet !== undefined) {
|
|
var it = stringSet.iterator();
|
|
while (it.hasNext()) {
|
|
var element = "" + it.next();
|
|
arr.push(element);
|
|
}
|
|
}
|
|
|
|
return arr;
|
|
}
|
|
}
|
|
|
|
export module resources {
|
|
var attr;
|
|
var attrCache = new Map<string, number>();
|
|
|
|
export function getDrawableId(name) {
|
|
return getId(":drawable/" + name);
|
|
}
|
|
|
|
export function getStringId(name) {
|
|
return getId(":string/" + name);
|
|
}
|
|
|
|
export function getId(name: string): number {
|
|
var resources = getResources();
|
|
var packageName = getPackageName();
|
|
var uri = packageName + name;
|
|
return resources.getIdentifier(uri, null, null);
|
|
}
|
|
export function getPalleteColor(name: string, context: android.content.Context): number {
|
|
return getPaletteColor(name, context);
|
|
}
|
|
export function getPaletteColor(name: string, context: android.content.Context): number {
|
|
if (attrCache.has(name)) {
|
|
return attrCache.get(name);
|
|
}
|
|
|
|
var result = 0;
|
|
try {
|
|
if (!attr) {
|
|
attr = java.lang.Class.forName("android.support.v7.appcompat.R$attr")
|
|
}
|
|
|
|
let colorID = 0;
|
|
let field = attr.getField(name);
|
|
if (field) {
|
|
colorID = field.getInt(null);
|
|
}
|
|
|
|
if (colorID) {
|
|
let typedValue = new android.util.TypedValue();
|
|
context.getTheme().resolveAttribute(colorID, typedValue, true);
|
|
result = typedValue.data;
|
|
}
|
|
}
|
|
catch (ex) {
|
|
traceWrite("Cannot get pallete color: " + name, traceCategories.Error, traceMessageType.error);
|
|
}
|
|
|
|
attrCache.set(name, result);
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
|
|
export function GC() {
|
|
gc();
|
|
}
|
|
|
|
export function openUrl(location: string): boolean {
|
|
const context = ad.getApplicationContext();
|
|
try {
|
|
var intent = new android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(location.trim()));
|
|
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
context.startActivity(intent);
|
|
} catch (e) {
|
|
// We Don't do anything with an error. We just output it
|
|
traceWrite("Error in OpenURL", traceCategories.Error, traceMessageType.error);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|