feat(android): update packages

use androidx exifinterface, use viewpager2,  update sdk , build tools, target sdk ,update fragment & transition versions
This commit is contained in:
triniwiz
2020-07-28 08:26:59 -04:00
parent 0dce8be03c
commit 08cc81616f
30 changed files with 64036 additions and 341 deletions

View File

@@ -70,17 +70,18 @@ export class ImageAsset extends ImageAssetBase {
function calculateAngleFromFile(filename: string) {
let rotationAngle = 0;
const ei = new android.media.ExifInterface(filename);
const orientation = ei.getAttributeInt(android.media.ExifInterface.TAG_ORIENTATION, android.media.ExifInterface.ORIENTATION_NORMAL);
const ExifInterface = (androidx as any).exifinterface.media.ExifInterface;
const ei = new ExifInterface(filename);
const orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case android.media.ExifInterface.ORIENTATION_ROTATE_90:
case ExifInterface.ORIENTATION_ROTATE_90:
rotationAngle = 90;
break;
case android.media.ExifInterface.ORIENTATION_ROTATE_180:
case ExifInterface.ORIENTATION_ROTATE_180:
rotationAngle = 180;
break;
case android.media.ExifInterface.ORIENTATION_ROTATE_270:
case ExifInterface.ORIENTATION_ROTATE_270:
rotationAngle = 270;
break;
}

View File

@@ -354,17 +354,18 @@ function getTargetFormat(format: 'png' | 'jpeg' | 'jpg'): android.graphics.Bitma
function getRotationAngleFromFile(filename: string): number {
let result = 0;
const ei = new android.media.ExifInterface(filename);
const orientation = ei.getAttributeInt(android.media.ExifInterface.TAG_ORIENTATION, android.media.ExifInterface.ORIENTATION_NORMAL);
const ExifInterface = (androidx as any).exifinterface.media.ExifInterface;
const ei = new ExifInterface(filename);
const orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case android.media.ExifInterface.ORIENTATION_ROTATE_90:
case ExifInterface.ORIENTATION_ROTATE_90:
result = 90;
break;
case android.media.ExifInterface.ORIENTATION_ROTATE_180:
case ExifInterface.ORIENTATION_ROTATE_180:
result = 180;
break;
case android.media.ExifInterface.ORIENTATION_ROTATE_270:
case ExifInterface.ORIENTATION_ROTATE_270:
result = 270;
break;
}

View File

@@ -24,6 +24,9 @@
"androidx.core*:*",
"androidx.viewpager.widget*:*",
"androidx.fragment*:*",
"androidx.transition*:*"
]
}
"androidx.transition*:*",
"androidx.viewpager*:*",
"androidx.recyclerview.widget:RecyclerView*",
"androidx.exifinterface*:*"
]
}

View File

Binary file not shown.

View File

Binary file not shown.

View File

@@ -10,6 +10,7 @@ import { fontSizeProperty, fontInternalProperty } from '../styling/style-propert
import { RESOURCE_PREFIX, ad, layout } from '../../utils';
import { Frame } from '../frame';
import * as application from '../../application';
import { Tabs } from '../tabs/index.android';
export * from './tab-view-common';
@@ -18,14 +19,14 @@ const PRIMARY_COLOR = 'colorPrimary';
const DEFAULT_ELEVATION = 4;
interface PagerAdapter {
new (owner: TabView): androidx.viewpager.widget.PagerAdapter;
new (owner: TabView): any;
}
const TABID = '_tabId';
const INDEX = '_index';
let PagerAdapter: PagerAdapter;
let appResources: android.content.res.Resources;
let PagerChangeCallback: any;
function makeFragmentName(viewId: number, id: number): string {
return 'android:viewpager:' + viewId + ':' + id;
}
@@ -127,23 +128,20 @@ function initializeNativeClasses() {
}
}
const POSITION_UNCHANGED = -1;
const POSITION_NONE = -2;
class FragmentPagerAdapter extends androidx.viewpager.widget.PagerAdapter {
@NativeClass
class FragmentPagerAdapter extends androidx.viewpager2.adapter.FragmentStateAdapter {
public items: Array<TabViewItemDefinition>;
private mCurTransaction: androidx.fragment.app.FragmentTransaction;
private mCurrentPrimaryItem: androidx.fragment.app.Fragment;
constructor(public owner: TabView) {
super();
super(owner._getFragmentManager(), (application.android.foregroundActivity || application.android.startActivity).getLifecycle());
return global.__native(this);
}
getCount() {
getItemCount() {
const items = this.items;
return items ? items.length : 0;
}
@@ -153,128 +151,63 @@ function initializeNativeClasses() {
return '';
}
return items[index].title;
return ''; // items[index].title;
}
startUpdate(container: android.view.ViewGroup): void {
if (container.getId() === android.view.View.NO_ID) {
throw new Error(`ViewPager with adapter ${this} requires a view containerId`);
}
}
instantiateItem(container: android.view.ViewGroup, position: number): java.lang.Object {
const fragmentManager = this.owner._getFragmentManager();
if (!this.mCurTransaction) {
this.mCurTransaction = fragmentManager.beginTransaction();
}
const itemId = this.getItemId(position);
const name = makeFragmentName(container.getId(), itemId);
let fragment: androidx.fragment.app.Fragment = fragmentManager.findFragmentByTag(name);
if (fragment != null) {
this.mCurTransaction.attach(fragment);
} else {
fragment = TabFragmentImplementation.newInstance(this.owner._domId, position);
this.mCurTransaction.add(container.getId(), fragment, name);
}
if (fragment !== this.mCurrentPrimaryItem) {
fragment.setMenuVisibility(false);
fragment.setUserVisibleHint(false);
}
public createFragment(position: number): androidx.fragment.app.Fragment {
const tabItems = this.owner.items;
const tabItem = tabItems ? tabItems[position] : null;
if (tabItem) {
tabItem.canBeLoaded = true;
}
return fragment;
return TabFragmentImplementation.newInstance(this.owner._domId, position);
}
getItemPosition(object: java.lang.Object): number {
return this.items ? POSITION_UNCHANGED : POSITION_NONE;
}
public onBindViewHolder(holder, position) {
super.onBindViewHolder(holder, position);
this.owner.selectedIndex = position;
destroyItem(container: android.view.ViewGroup, position: number, object: java.lang.Object): void {
if (!this.mCurTransaction) {
const fragmentManager = this.owner._getFragmentManager();
this.mCurTransaction = fragmentManager.beginTransaction();
}
const tab = this.owner;
const tabItems = tab.items;
const newTabItem = tabItems ? tabItems[position] : null;
const fragment: androidx.fragment.app.Fragment = <androidx.fragment.app.Fragment>object;
this.mCurTransaction.detach(fragment);
if (this.mCurrentPrimaryItem === fragment) {
this.mCurrentPrimaryItem = null;
}
const tabItems = this.owner.items;
const tabItem = tabItems ? tabItems[position] : null;
if (tabItem) {
tabItem.canBeLoaded = false;
if (newTabItem) {
tab._loadUnloadTabItems(tab.selectedIndex);
}
}
setPrimaryItem(container: android.view.ViewGroup, position: number, object: java.lang.Object): void {
const fragment = <androidx.fragment.app.Fragment>object;
if (fragment !== this.mCurrentPrimaryItem) {
if (this.mCurrentPrimaryItem != null) {
this.mCurrentPrimaryItem.setMenuVisibility(false);
this.mCurrentPrimaryItem.setUserVisibleHint(false);
}
if (fragment != null) {
fragment.setMenuVisibility(true);
fragment.setUserVisibleHint(true);
}
this.mCurrentPrimaryItem = fragment;
this.owner.selectedIndex = position;
const tab = this.owner;
const tabItems = tab.items;
const newTabItem = tabItems ? tabItems[position] : null;
if (newTabItem) {
tab._loadUnloadTabItems(tab.selectedIndex);
public onViewRecycled(holder: androidx.viewpager2.adapter.FragmentViewHolder): void {
super.onViewRecycled(holder);
if (holder.getLayoutPosition) {
const position = holder.getLayoutPosition();
const tabItems = this.owner.items;
const tabItem = tabItems ? tabItems[position] : null;
if (tabItem) {
tabItem.canBeLoaded = false;
}
}
}
finishUpdate(container: android.view.ViewGroup): void {
this._commitCurrentTransaction();
}
isViewFromObject(view: android.view.View, object: java.lang.Object): boolean {
return (<androidx.fragment.app.Fragment>object).getView() === view;
}
saveState(): android.os.Parcelable {
// Commit the current transaction on save to prevent "No view found for id 0xa" exception on restore.
// Related to: https://github.com/NativeScript/NativeScript/issues/6466
this._commitCurrentTransaction();
return null;
}
restoreState(state: android.os.Parcelable, loader: java.lang.ClassLoader): void {
//
}
getItemId(position: number): number {
return position;
}
}
private _commitCurrentTransaction() {
if (this.mCurTransaction != null) {
this.mCurTransaction.commitNowAllowingStateLoss();
this.mCurTransaction = null;
}
@NativeClass
class PageChangeCallbackImpl extends androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback {
constructor(public owner: Tabs) {
super();
return global.__native(this);
}
onPageSelected(position: number) {
super.onPageSelected(position);
this.owner.selectedIndex = position;
}
}
PagerChangeCallback = PageChangeCallbackImpl;
PagerAdapter = FragmentPagerAdapter;
appResources = application.android.context.getResources();
}
@@ -442,7 +375,7 @@ export class TabView extends TabViewBase {
private _pagerAdapter: androidx.viewpager.widget.PagerAdapter;
private _androidViewId: number = -1;
public _originalBackground: any;
private _pageChangeCallback: any;
constructor() {
super();
tabs.push(new WeakRef(this));
@@ -473,6 +406,7 @@ export class TabView extends TabViewBase {
const context: android.content.Context = this._context;
const nativeView = new org.nativescript.widgets.GridLayout(context);
const viewPager = new org.nativescript.widgets.TabViewPager(context);
this._pageChangeCallback = new PagerChangeCallback(this);
const tabLayout = new org.nativescript.widgets.TabLayout(context);
const lp = new org.nativescript.widgets.CommonLayoutParams();
const primaryColor = ad.resources.getPaletteColor(PRIMARY_COLOR, context);
@@ -505,7 +439,7 @@ export class TabView extends TabViewBase {
const adapter = new PagerAdapter(this);
viewPager.setAdapter(adapter);
(<any>viewPager).adapter = adapter;
(<any>viewPager).registerOnPageChangeCallback(this._pageChangeCallback);
nativeView.addView(tabLayout);
(<any>nativeView).tabLayout = tabLayout;

View File

@@ -25,13 +25,13 @@ const TABID = '_tabId';
const INDEX = '_index';
interface PagerAdapter {
new (owner: Tabs): androidx.viewpager.widget.PagerAdapter;
new (owner: Tabs): any;
}
let PagerAdapter: PagerAdapter;
let TabsBar: any;
let appResources: android.content.res.Resources;
let PagerChangeCallback: any;
function makeFragmentName(viewId: number, id: number): string {
return 'android:viewpager:' + viewId + ':' + id;
}
@@ -133,23 +133,19 @@ function initializeNativeClasses() {
}
}
const POSITION_UNCHANGED = -1;
const POSITION_NONE = -2;
class FragmentPagerAdapter extends androidx.viewpager.widget.PagerAdapter {
@NativeClass
class FragmentPagerAdapter extends androidx.viewpager2.adapter.FragmentStateAdapter {
public items: Array<TabContentItem>;
private mCurTransaction: androidx.fragment.app.FragmentTransaction;
private mCurrentPrimaryItem: androidx.fragment.app.Fragment;
constructor(public owner: Tabs) {
super();
super(owner._getFragmentManager(), (application.android.foregroundActivity || application.android.startActivity).getLifecycle());
return global.__native(this);
}
getCount() {
getItemCount() {
const items = this.items;
return items ? items.length : 0;
}
@@ -162,123 +158,42 @@ function initializeNativeClasses() {
return ''; // items[index].title;
}
startUpdate(container: android.view.ViewGroup): void {
if (container.getId() === android.view.View.NO_ID) {
throw new Error(`ViewPager with adapter ${this} requires a view containerId`);
}
}
instantiateItem(container: android.view.ViewGroup, position: number): java.lang.Object {
const fragmentManager = this.owner._getFragmentManager();
if (!this.mCurTransaction) {
this.mCurTransaction = fragmentManager.beginTransaction();
}
const itemId = this.getItemId(position);
const name = makeFragmentName(container.getId(), itemId);
let fragment: androidx.fragment.app.Fragment = fragmentManager.findFragmentByTag(name);
if (fragment != null) {
this.mCurTransaction.attach(fragment);
} else {
fragment = TabFragmentImplementation.newInstance(this.owner._domId, position);
this.mCurTransaction.add(container.getId(), fragment, name);
}
if (fragment !== this.mCurrentPrimaryItem) {
fragment.setMenuVisibility(false);
fragment.setUserVisibleHint(false);
}
public createFragment(position: number): androidx.fragment.app.Fragment {
const tabItems = this.owner.items;
const tabItem = tabItems ? tabItems[position] : null;
if (tabItem) {
tabItem.canBeLoaded = true;
}
return fragment;
return TabFragmentImplementation.newInstance(this.owner._domId, position);
}
getItemPosition(object: java.lang.Object): number {
return this.items ? POSITION_UNCHANGED : POSITION_NONE;
}
public onBindViewHolder(holder, position) {
super.onBindViewHolder(holder, position);
const tab = this.owner;
const tabItems = tab.items;
const newTabItem = tabItems ? tabItems[position] : null;
destroyItem(container: android.view.ViewGroup, position: number, object: java.lang.Object): void {
if (!this.mCurTransaction) {
const fragmentManager = this.owner._getFragmentManager();
this.mCurTransaction = fragmentManager.beginTransaction();
}
const fragment: androidx.fragment.app.Fragment = <androidx.fragment.app.Fragment>object;
this.mCurTransaction.detach(fragment);
if (this.mCurrentPrimaryItem === fragment) {
this.mCurrentPrimaryItem = null;
}
const tabItems = this.owner.items;
const tabItem = tabItems ? tabItems[position] : null;
if (tabItem) {
tabItem.canBeLoaded = false;
if (newTabItem) {
tab._loadUnloadTabItems(tab.selectedIndex);
}
}
setPrimaryItem(container: android.view.ViewGroup, position: number, object: java.lang.Object): void {
const fragment = <androidx.fragment.app.Fragment>object;
if (fragment !== this.mCurrentPrimaryItem) {
if (this.mCurrentPrimaryItem != null) {
this.mCurrentPrimaryItem.setMenuVisibility(false);
this.mCurrentPrimaryItem.setUserVisibleHint(false);
}
if (fragment != null) {
fragment.setMenuVisibility(true);
fragment.setUserVisibleHint(true);
}
this.mCurrentPrimaryItem = fragment;
this.owner.selectedIndex = position;
const tab = this.owner;
const tabItems = tab.items;
const newTabItem = tabItems ? tabItems[position] : null;
if (newTabItem) {
tab._loadUnloadTabItems(tab.selectedIndex);
public onViewRecycled(holder: androidx.viewpager2.adapter.FragmentViewHolder): void {
super.onViewRecycled(holder);
if (holder.getLayoutPosition) {
const position = holder.getLayoutPosition();
const tabItems = this.owner.items;
const tabItem = tabItems ? tabItems[position] : null;
if (tabItem) {
tabItem.canBeLoaded = false;
}
}
}
finishUpdate(container: android.view.ViewGroup): void {
this._commitCurrentTransaction();
}
isViewFromObject(view: android.view.View, object: java.lang.Object): boolean {
return (<androidx.fragment.app.Fragment>object).getView() === view;
}
saveState(): android.os.Parcelable {
// Commit the current transaction on save to prevent "No view found for id 0xa" exception on restore.
// Related to: https://github.com/NativeScript/NativeScript/issues/6466
this._commitCurrentTransaction();
return null;
}
restoreState(state: android.os.Parcelable, loader: java.lang.ClassLoader): void {
//
}
getItemId(position: number): number {
return position;
}
private _commitCurrentTransaction() {
if (this.mCurTransaction != null) {
this.mCurTransaction.commitNowAllowingStateLoss();
this.mCurTransaction = null;
}
}
}
@NativeClass
@@ -334,8 +249,22 @@ function initializeNativeClasses() {
}
}
@NativeClass
class PageChangeCallbackImpl extends androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback {
constructor(public owner: Tabs) {
super();
return global.__native(this);
}
onPageSelected(position: number) {
super.onPageSelected(position);
this.owner.selectedIndex = position;
}
}
PagerAdapter = FragmentPagerAdapter;
TabsBar = TabsBarImplementation;
PagerChangeCallback = PageChangeCallbackImpl;
appResources = application.android.context.getResources();
}
@@ -374,15 +303,15 @@ function iterateIndexRange(index: number, eps: number, lastIndex: number, callba
export class Tabs extends TabsBase {
private _tabsBar: org.nativescript.widgets.TabsBar;
private _viewPager: androidx.viewpager.widget.ViewPager;
private _pagerAdapter: androidx.viewpager.widget.PagerAdapter;
private _viewPager;
private _pagerAdapter: androidx.viewpager2.adapter.FragmentStateAdapter;
private _androidViewId: number = -1;
public _originalBackground: any;
private _textTransform: TextTransform = 'uppercase';
private _selectedItemColor: Color;
private _unSelectedItemColor: Color;
public animationEnabled: boolean;
private _pageChangeCallback: any;
constructor() {
super();
tabs.push(new WeakRef(this));
@@ -414,6 +343,7 @@ export class Tabs extends TabsBase {
const context: android.content.Context = this._context;
const nativeView = new org.nativescript.widgets.GridLayout(context);
const viewPager = new org.nativescript.widgets.TabViewPager(context);
this._pageChangeCallback = new PagerChangeCallback(this);
const tabsBar = new TabsBar(context, this);
const lp = new org.nativescript.widgets.CommonLayoutParams();
const primaryColor = ad.resources.getPaletteColor(PRIMARY_COLOR, context);
@@ -439,7 +369,7 @@ export class Tabs extends TabsBase {
const adapter = new PagerAdapter(this);
viewPager.setAdapter(adapter);
(<any>viewPager).adapter = adapter;
(<any>viewPager).registerOnPageChangeCallback(this._pageChangeCallback);
nativeView.addView(tabsBar);
(<any>nativeView).tabsBar = tabsBar;