Android Open Source - AspectJ-Android-Example Caching Aspect






From Project

Back to project page AspectJ-Android-Example.

License

The source code is released under:

Apache License

If you think the Android project AspectJ-Android-Example listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package android.mobile.peakgames.net.aspectjandroid.aspect;
/*from   ww  w . ja  v a 2 s  . co  m*/
import android.graphics.Bitmap;
import android.util.Log;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import java.util.HashMap;

@Aspect
public class CachingAspect {
    private static final String TAG = CachingAspect.class.getName();

    private HashMap<String, Bitmap> cache = new HashMap<String, Bitmap>(21);

    @Pointcut("execution(* android.mobile.peakgames.net.aspectjandroid.AspectActivity.fetchImage(..))")
    public void loadImageEntryPoint() {
    }

    @Around("loadImageEntryPoint()")
    public Object loadImageMethod(ProceedingJoinPoint joinPoint) {
        String imageUri = (String) joinPoint.getArgs()[0];

        Object object = null;
        if (cache.containsKey(imageUri)) {
            Log.d(TAG, "Image " + imageUri + " found in cache");

            Bitmap bitmap = cache.get(imageUri);
            return bitmap;
        }
        else {
            try {
                object = joinPoint.proceed();
                cache.put(imageUri, (Bitmap)object);
                Log.d(TAG, "Cached " + imageUri);
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }

        return object;
    }
}




Java Source Code List

android.mobile.peakgames.net.aspectjandroid.ApplicationTest.java
android.mobile.peakgames.net.aspectjandroid.AspectActivity.java
android.mobile.peakgames.net.aspectjandroid.SecureMethod.java
android.mobile.peakgames.net.aspectjandroid.Session.java
android.mobile.peakgames.net.aspectjandroid.aspect.AuthenticationAspect.java
android.mobile.peakgames.net.aspectjandroid.aspect.CachingAspect.java
android.mobile.peakgames.net.aspectjandroid.aspect.ExceptionHandlingAspect.java
android.mobile.peakgames.net.aspectjandroid.aspect.LoggingAspect.java
android.mobile.peakgames.net.aspectjandroid.aspect.ProfilingAspect.java
android.mobile.peakgames.net.aspectjandroid.exception.AuthenticationException.java