1 | import { LinearGradient } from './linear-gradient';
|
2 | import { isDataURI, isFileOrResourcePath, RESOURCE_PREFIX, FILE_PREFIX } from '../../utils';
|
3 | import { parse } from '../../css-value';
|
4 | import { path, knownFolders } from '../../file-system';
|
5 | import { Application } from '../../application';
|
6 | export * from './background-common';
|
7 | function fromBase64(source) {
|
8 | const bytes = android.util.Base64.decode(source, android.util.Base64.DEFAULT);
|
9 | return android.graphics.BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
|
10 | }
|
11 | function fromGradient(gradient) {
|
12 | const colors = Array.create('int', gradient.colorStops.length);
|
13 | const stops = Array.create('float', gradient.colorStops.length);
|
14 | let hasStops = false;
|
15 | gradient.colorStops.forEach((stop, index) => {
|
16 | colors[index] = stop.color.android;
|
17 | if (stop.offset) {
|
18 | stops[index] = stop.offset.value;
|
19 | hasStops = true;
|
20 | }
|
21 | });
|
22 | const alpha = gradient.angle / (Math.PI * 2);
|
23 | const startX = Math.pow(Math.sin(Math.PI * (alpha + 0.75)), 2);
|
24 | const startY = Math.pow(Math.sin(Math.PI * (alpha + 0.5)), 2);
|
25 | const endX = Math.pow(Math.sin(Math.PI * (alpha + 0.25)), 2);
|
26 | const endY = Math.pow(Math.sin(Math.PI * alpha), 2);
|
27 | return new org.nativescript.widgets.LinearGradientDefinition(startX, startY, endX, endY, colors, hasStops ? stops : null);
|
28 | }
|
29 | const pattern = /url\(('|")(.*?)\1\)/;
|
30 | export function refreshBorderDrawable(view, borderDrawable) {
|
31 | const nativeView = view.nativeViewProtected;
|
32 | const context = nativeView.getContext();
|
33 | const background = view.style.backgroundInternal;
|
34 | if (background) {
|
35 | const backgroundPositionParsedCSSValues = createNativeCSSValueArray(background.position);
|
36 | const backgroundSizeParsedCSSValues = createNativeCSSValueArray(background.size);
|
37 | const blackColor = -16777216;
|
38 | let imageUri;
|
39 | if (background.image && typeof background.image === 'string' && background.image !== 'none') {
|
40 | imageUri = background.image;
|
41 | const match = imageUri.match(pattern);
|
42 | if (match && match[2]) {
|
43 | imageUri = match[2];
|
44 | }
|
45 | }
|
46 | let bitmap = null;
|
47 | if (isDataURI(imageUri)) {
|
48 | const base64Data = imageUri.split(',')[1];
|
49 | if (base64Data !== undefined) {
|
50 | bitmap = fromBase64(base64Data);
|
51 | imageUri = null;
|
52 | }
|
53 | }
|
54 | else if (isFileOrResourcePath(imageUri)) {
|
55 | if (imageUri.indexOf(RESOURCE_PREFIX) !== 0) {
|
56 | let fileName = imageUri;
|
57 | if (fileName.indexOf('~/') === 0) {
|
58 | fileName = path.join(knownFolders.currentApp().path, fileName.replace('~/', ''));
|
59 | }
|
60 | imageUri = FILE_PREFIX + fileName;
|
61 | }
|
62 | }
|
63 | let gradient = null;
|
64 | if (background.image && background.image instanceof LinearGradient) {
|
65 | gradient = fromGradient(background.image);
|
66 | }
|
67 | borderDrawable.refresh(background.borderTopColor ? background.borderTopColor.android : blackColor, background.borderRightColor ? background.borderRightColor.android : blackColor, background.borderBottomColor ? background.borderBottomColor.android : blackColor, background.borderLeftColor ? background.borderLeftColor.android : blackColor, background.borderTopWidth, background.borderRightWidth, background.borderBottomWidth, background.borderLeftWidth, background.borderTopLeftRadius, background.borderTopRightRadius, background.borderBottomRightRadius, background.borderBottomLeftRadius, background.clipPath, background.color ? background.color.android : 0, imageUri, bitmap, gradient, context, background.repeat, background.position, backgroundPositionParsedCSSValues, background.size, backgroundSizeParsedCSSValues);
|
68 |
|
69 | }
|
70 | }
|
71 | function createNativeCSSValueArray(css) {
|
72 | if (!css) {
|
73 | return null;
|
74 | }
|
75 | const cssValues = parse(css);
|
76 | const nativeArray = Array.create(org.nativescript.widgets.CSSValue, cssValues.length);
|
77 | for (let i = 0, length = cssValues.length; i < length; i++) {
|
78 | nativeArray[i] = new org.nativescript.widgets.CSSValue(cssValues[i].type, cssValues[i].string, cssValues[i].unit, cssValues[i].value);
|
79 | }
|
80 | return nativeArray;
|
81 | }
|
82 | export var CacheMode;
|
83 | (function (CacheMode) {
|
84 | CacheMode[CacheMode["none"] = 0] = "none";
|
85 | CacheMode[CacheMode["memory"] = 1] = "memory";
|
86 | CacheMode[CacheMode["diskAndMemory"] = 2] = "diskAndMemory";
|
87 | })(CacheMode || (CacheMode = {}));
|
88 | let currentCacheMode;
|
89 | let imageFetcher;
|
90 | export function initImageCache(context, mode = CacheMode.diskAndMemory, memoryCacheSize = 0.25, diskCacheSize = 10 * 1024 * 1024) {
|
91 | if (currentCacheMode === mode) {
|
92 | return;
|
93 | }
|
94 | currentCacheMode = mode;
|
95 | if (!imageFetcher) {
|
96 | imageFetcher = org.nativescript.widgets.image.Fetcher.getInstance(context);
|
97 | }
|
98 | else {
|
99 | imageFetcher.clearCache();
|
100 | }
|
101 | const params = new org.nativescript.widgets.image.Cache.CacheParams();
|
102 | params.memoryCacheEnabled = mode !== CacheMode.none;
|
103 | params.setMemCacheSizePercent(memoryCacheSize);
|
104 | params.diskCacheEnabled = mode === CacheMode.diskAndMemory;
|
105 | params.diskCacheSize = diskCacheSize;
|
106 | const imageCache = org.nativescript.widgets.image.Cache.getInstance(params);
|
107 | imageFetcher.addImageCache(imageCache);
|
108 | imageFetcher.initCache();
|
109 | }
|
110 | function onLiveSync(args) {
|
111 | if (imageFetcher) {
|
112 | imageFetcher.clearCache();
|
113 | }
|
114 | }
|
115 | global.NativeScriptGlobals.events.on('livesync', onLiveSync);
|
116 | global.NativeScriptGlobals.addEventWiring(() => {
|
117 | Application.android.on('activityStarted', (args) => {
|
118 | if (!imageFetcher) {
|
119 | initImageCache(args.activity);
|
120 | }
|
121 | else {
|
122 | imageFetcher.initCache();
|
123 | }
|
124 | });
|
125 | });
|
126 | global.NativeScriptGlobals.addEventWiring(() => {
|
127 | Application.android.on('activityStopped', (args) => {
|
128 | if (imageFetcher) {
|
129 | imageFetcher.closeCache();
|
130 | }
|
131 | });
|
132 | });
|
133 |
|
\ | No newline at end of file |