Files
NativeScript/ui/image-cache/image-cache.android.ts
Hristo Deshev 77838ae9c6 Change from "classic" TS 1.6 imports to the default resolution scheme.
- Use relative imports in place of most of our absolute ones.
- Add "private" ambient modules for modules that we need to import using
an absolute path (e.g. when app/.../test-something.ts needs to import
ui/styling/style-scope)
2015-09-29 16:25:49 +03:00

66 lines
2.0 KiB
TypeScript

import common = require("./image-cache-common");
class LruBitmapCache extends android.util.LruCache<string, android.graphics.Bitmap> {
constructor(cacheSize: number) {
super(cacheSize);
return global.__native(this);
}
protected sizeOf(key: string, bitmap: android.graphics.Bitmap): number {
// The cache size will be measured in kilobytes rather than
// number of items.
var result = Math.round(bitmap.getByteCount() / 1024);
//console.log("sizeOf key: " + result);
return result;
}
//protected entryRemoved(evicted: boolean, key: string, oldValue: android.graphics.Bitmap, newValue: android.graphics.Bitmap): void {
// console.log("entryRemoved("+evicted+", "+key+", "+oldValue+", "+newValue+")");
//}
};
export class Cache extends common.Cache {
private _callback: any;
private _cache: LruBitmapCache;
constructor() {
super();
var maxMemory = java.lang.Runtime.getRuntime().maxMemory() / 1024;
var cacheSize = maxMemory / 8;
//console.log("cacheSize: " + cacheSize);
this._cache = new LruBitmapCache(cacheSize);
var that = new WeakRef(this);
this._callback = new (<any>com).tns.Async.CompleteCallback({
onComplete: function (result: any, context: any) {
var instance = that.get();
if (instance) {
instance._onDownloadCompleted(context, result)
}
}
});
}
public _downloadCore(request: common.DownloadRequest) {
(<any>com).tns.Async.DownloadImage(request.url, this._callback, request.key);
}
public get(key: string): any {
var result = this._cache.get(key);
return result;
}
public set(key: string, image: any): void {
this._cache.put(key, image);
}
public remove(key: string): void {
this._cache.remove(key);
}
public clear() {
this._cache.evictAll();
}
}