mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-18 05:18:39 +08:00
refactor: Application modules to classes
This commit is contained in:
@ -6,7 +6,7 @@ import * as httpModule from '../http';
|
||||
// Types.
|
||||
import { path as fsPath, knownFolders } from '../file-system';
|
||||
import { isFileOrResourcePath, RESOURCE_PREFIX, layout } from '../utils';
|
||||
import { getNativeApplication } from '../application';
|
||||
import { Application } from '../application';
|
||||
import { Font } from '../ui/styling/font';
|
||||
import { Color } from '../color';
|
||||
|
||||
@ -26,7 +26,7 @@ let resources: android.content.res.Resources;
|
||||
|
||||
function getApplication() {
|
||||
if (!application) {
|
||||
application = <android.app.Application>getNativeApplication();
|
||||
application = Application.android.getNativeApplication();
|
||||
}
|
||||
|
||||
return application;
|
||||
@ -96,10 +96,16 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
static fromResourceSync(name: string): ImageSource {
|
||||
const res = getResources();
|
||||
if (res) {
|
||||
const identifier: number = res.getIdentifier(name, 'drawable', getApplication().getPackageName());
|
||||
const identifier: number = res.getIdentifier(
|
||||
name,
|
||||
'drawable',
|
||||
getApplication().getPackageName()
|
||||
);
|
||||
if (0 < identifier) {
|
||||
// Load BitmapDrawable with getDrawable to make use of Android internal caching
|
||||
const bitmapDrawable = <android.graphics.drawable.BitmapDrawable>res.getDrawable(identifier);
|
||||
const bitmapDrawable = <android.graphics.drawable.BitmapDrawable>(
|
||||
res.getDrawable(identifier)
|
||||
);
|
||||
if (bitmapDrawable && bitmapDrawable.getBitmap) {
|
||||
return new ImageSource(bitmapDrawable.getBitmap());
|
||||
}
|
||||
@ -199,7 +205,11 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
const textWidth = textBounds.width();
|
||||
const textHeight = textBounds.height();
|
||||
if (textWidth > 0 && textHeight > 0) {
|
||||
const bitmap = android.graphics.Bitmap.createBitmap(textWidth, textHeight, android.graphics.Bitmap.Config.ARGB_8888);
|
||||
const bitmap = android.graphics.Bitmap.createBitmap(
|
||||
textWidth,
|
||||
textHeight,
|
||||
android.graphics.Bitmap.Config.ARGB_8888
|
||||
);
|
||||
|
||||
const canvas = new android.graphics.Canvas(bitmap);
|
||||
canvas.drawText(source, -textBounds.left, -textBounds.top, paint);
|
||||
@ -221,7 +231,9 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
}
|
||||
|
||||
public loadFromResource(name: string): boolean {
|
||||
console.log('fromResource() and loadFromResource() are deprecated. Use ImageSource.fromResource[Sync]() instead.');
|
||||
console.log(
|
||||
'fromResource() and loadFromResource() are deprecated. Use ImageSource.fromResource[Sync]() instead.'
|
||||
);
|
||||
|
||||
const imgSource = ImageSource.fromResourceSync(name);
|
||||
this.android = imgSource ? imgSource.android : null;
|
||||
@ -236,7 +248,9 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
}
|
||||
|
||||
public loadFromFile(path: string): boolean {
|
||||
console.log('fromFile() and loadFromFile() are deprecated. Use ImageSource.fromFile[Sync]() instead.');
|
||||
console.log(
|
||||
'fromFile() and loadFromFile() are deprecated. Use ImageSource.fromFile[Sync]() instead.'
|
||||
);
|
||||
|
||||
const imgSource = ImageSource.fromFileSync(path);
|
||||
this.android = imgSource ? imgSource.android : null;
|
||||
@ -251,7 +265,9 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
}
|
||||
|
||||
public loadFromData(data: any): boolean {
|
||||
console.log('fromData() and loadFromData() are deprecated. Use ImageSource.fromData[Sync]() instead.');
|
||||
console.log(
|
||||
'fromData() and loadFromData() are deprecated. Use ImageSource.fromData[Sync]() instead.'
|
||||
);
|
||||
|
||||
const imgSource = ImageSource.fromDataSync(data);
|
||||
this.android = imgSource ? imgSource.android : null;
|
||||
@ -266,7 +282,9 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
}
|
||||
|
||||
public loadFromBase64(source: string): boolean {
|
||||
console.log('fromBase64() and loadFromBase64() are deprecated. Use ImageSource.fromBase64[Sync]() instead.');
|
||||
console.log(
|
||||
'fromBase64() and loadFromBase64() are deprecated. Use ImageSource.fromBase64[Sync]() instead.'
|
||||
);
|
||||
|
||||
const imgSource = ImageSource.fromBase64Sync(source);
|
||||
this.android = imgSource ? imgSource.android : null;
|
||||
@ -281,7 +299,9 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
}
|
||||
|
||||
public loadFromFontIconCode(source: string, font: Font, color: Color): boolean {
|
||||
console.log('loadFromFontIconCode() is deprecated. Use ImageSource.fromFontIconCodeSync() instead.');
|
||||
console.log(
|
||||
'loadFromFontIconCode() is deprecated. Use ImageSource.fromFontIconCodeSync() instead.'
|
||||
);
|
||||
|
||||
const imgSource = ImageSource.fromFontIconCodeSync(source, font, color);
|
||||
this.android = imgSource ? imgSource.android : null;
|
||||
@ -295,13 +315,19 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
this.android = org.nativescript.widgets.Utils.getBitmapFromDrawable(source);
|
||||
return;
|
||||
}
|
||||
throw new Error('The method setNativeSource() expects an android.graphics.Bitmap or android.graphics.drawable.Drawable instance.');
|
||||
throw new Error(
|
||||
'The method setNativeSource() expects an android.graphics.Bitmap or android.graphics.drawable.Drawable instance.'
|
||||
);
|
||||
}
|
||||
|
||||
this.android = source;
|
||||
}
|
||||
|
||||
public saveToFile(path: string, format: 'png' | 'jpeg' | 'jpg', quality = 100): boolean {
|
||||
public saveToFile(
|
||||
path: string,
|
||||
format: 'png' | 'jpeg' | 'jpg',
|
||||
quality = 100
|
||||
): boolean {
|
||||
if (!this.android) {
|
||||
return false;
|
||||
}
|
||||
@ -309,7 +335,9 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
const targetFormat = getTargetFormat(format);
|
||||
|
||||
// TODO add exception handling
|
||||
const outputStream = new java.io.BufferedOutputStream(new java.io.FileOutputStream(path));
|
||||
const outputStream = new java.io.BufferedOutputStream(
|
||||
new java.io.FileOutputStream(path)
|
||||
);
|
||||
|
||||
const res = this.android.compress(targetFormat, quality, outputStream);
|
||||
outputStream.close();
|
||||
@ -317,7 +345,11 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
return res;
|
||||
}
|
||||
|
||||
public saveToFileAsync(path: string, format: 'png' | 'jpeg' | 'jpg', quality = 100): Promise<boolean> {
|
||||
public saveToFileAsync(
|
||||
path: string,
|
||||
format: 'png' | 'jpeg' | 'jpg',
|
||||
quality = 100
|
||||
): Promise<boolean> {
|
||||
return new Promise((resolve, reject) => {
|
||||
org.nativescript.widgets.Utils.saveToFileAsync(
|
||||
this.android,
|
||||
@ -348,7 +380,10 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
const targetFormat = getTargetFormat(format);
|
||||
|
||||
const outputStream = new java.io.ByteArrayOutputStream();
|
||||
const base64Stream = new android.util.Base64OutputStream(outputStream, android.util.Base64.NO_WRAP);
|
||||
const base64Stream = new android.util.Base64OutputStream(
|
||||
outputStream,
|
||||
android.util.Base64.NO_WRAP
|
||||
);
|
||||
|
||||
this.android.compress(targetFormat, quality, base64Stream);
|
||||
|
||||
@ -358,7 +393,10 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
return outputStream.toString();
|
||||
}
|
||||
|
||||
public toBase64StringAsync(format: 'png' | 'jpeg' | 'jpg', quality = 100): Promise<string> {
|
||||
public toBase64StringAsync(
|
||||
format: 'png' | 'jpeg' | 'jpg',
|
||||
quality = 100
|
||||
): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
org.nativescript.widgets.Utils.toBase64StringAsync(
|
||||
this.android,
|
||||
@ -381,8 +419,17 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
}
|
||||
|
||||
public resize(maxSize: number, options?: any): ImageSource {
|
||||
const dim = getScaledDimensions(this.android.getWidth(), this.android.getHeight(), maxSize);
|
||||
const bm: android.graphics.Bitmap = android.graphics.Bitmap.createScaledBitmap(this.android, dim.width, dim.height, options && options.filter);
|
||||
const dim = getScaledDimensions(
|
||||
this.android.getWidth(),
|
||||
this.android.getHeight(),
|
||||
maxSize
|
||||
);
|
||||
const bm: android.graphics.Bitmap = android.graphics.Bitmap.createScaledBitmap(
|
||||
this.android,
|
||||
dim.width,
|
||||
dim.height,
|
||||
options && options.filter
|
||||
);
|
||||
|
||||
return new ImageSource(bm);
|
||||
}
|
||||
@ -410,7 +457,9 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
}
|
||||
}
|
||||
|
||||
function getTargetFormat(format: 'png' | 'jpeg' | 'jpg'): android.graphics.Bitmap.CompressFormat {
|
||||
function getTargetFormat(
|
||||
format: 'png' | 'jpeg' | 'jpg'
|
||||
): android.graphics.Bitmap.CompressFormat {
|
||||
switch (format) {
|
||||
case 'jpeg':
|
||||
case 'jpg':
|
||||
@ -423,7 +472,10 @@ 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 orientation = ei.getAttributeInt(
|
||||
android.media.ExifInterface.TAG_ORIENTATION,
|
||||
android.media.ExifInterface.ORIENTATION_NORMAL
|
||||
);
|
||||
|
||||
switch (orientation) {
|
||||
case android.media.ExifInterface.ORIENTATION_ROTATE_90:
|
||||
@ -447,7 +499,9 @@ export function fromAsset(asset: ImageAsset): Promise<ImageSource> {
|
||||
}
|
||||
|
||||
export function fromResource(name: string): ImageSource {
|
||||
console.log('fromResource() is deprecated. Use ImageSource.fromResourceSync() instead.');
|
||||
console.log(
|
||||
'fromResource() is deprecated. Use ImageSource.fromResourceSync() instead.'
|
||||
);
|
||||
|
||||
return ImageSource.fromResourceSync(name);
|
||||
}
|
||||
@ -465,7 +519,9 @@ export function fromData(data: any): ImageSource {
|
||||
}
|
||||
|
||||
export function fromFontIconCode(source: string, font: Font, color: Color): ImageSource {
|
||||
console.log('fromFontIconCode() is deprecated. Use ImageSource.fromFontIconCodeSync() instead.');
|
||||
console.log(
|
||||
'fromFontIconCode() is deprecated. Use ImageSource.fromFontIconCodeSync() instead.'
|
||||
);
|
||||
|
||||
return ImageSource.fromFontIconCodeSync(source, font, color);
|
||||
}
|
||||
@ -489,7 +545,9 @@ export function fromUrl(url: string): Promise<ImageSourceDefinition> {
|
||||
}
|
||||
|
||||
export function fromFileOrResource(path: string): ImageSource {
|
||||
console.log('fromFileOrResource() is deprecated. Use ImageSource.fromFileOrResourceSync() instead.');
|
||||
console.log(
|
||||
'fromFileOrResource() is deprecated. Use ImageSource.fromFileOrResourceSync() instead.'
|
||||
);
|
||||
|
||||
return ImageSource.fromFileOrResourceSync(path);
|
||||
}
|
||||
|
Reference in New Issue
Block a user