Skip to content

Commit

Permalink
Merge pull request #3 from kaelaela/develop
Browse files Browse the repository at this point in the history
Add cache
  • Loading branch information
kaelaela authored Jul 31, 2016
2 parents 274adee + 1733a6b commit 84ec88c
Show file tree
Hide file tree
Showing 14 changed files with 311 additions and 104 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ogView.loadFrom(url);
- [x] Text ~~size, line, style,~~ color
- [x] Image position
- [ ] Rounded view
- [ ] Image cache
- [x] Image cache
- [ ] Enable load twitter
- [ ] More metadata

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0-beta3'
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
}
Expand Down
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
COMPILE_SDK_VERSION=23
BUILD_TOOLS_VERSION=23.0.2
MIN_SDK_VERSION=16
TARGET_SDK_VERSION=23
SUPPORT_APP_COMPAT_VERSION=23.2.1
TARGET_SDK_VERSION=24
SUPPORT_APP_COMPAT_VERSION=24.1.0

VERSION_NAME=1.0.2
VERSION_CODE=3
VERSION_NAME=1.0.3
VERSION_CODE=4

GROUP=me.kaelaela
ARTIFACT_ID=OpenGraphView
Expand Down
59 changes: 59 additions & 0 deletions library/src/main/java/me/kaelaela/opengraphview/OGCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package me.kaelaela.opengraphview;

import android.graphics.Bitmap;
import android.support.annotation.Nullable;
import android.support.v4.util.LruCache;
import android.text.TextUtils;
import me.kaelaela.opengraphview.network.model.OGData;

public class OGCache {

private static OGCache mOGCache;
private LruCache<String, OGData> mOGDataCache;
private LruCache<String, Bitmap> mImageCache;

public static OGCache getInstance() {
if (mOGCache == null) {
mOGCache = new OGCache();
}
return mOGCache;
}

private OGCache() {
final int max = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = max / 8;
mOGDataCache = new LruCache<>(cacheSize);
mImageCache = new LruCache<>(cacheSize);
}

public void add(String url, OGData ogData) {
if (get(url) == null) {
mOGDataCache.put(url, ogData);
}
}

@Nullable
public OGData get(String url) {
if (TextUtils.isEmpty(url)) {
return null;
}
return mOGDataCache.get(url);
}

public void addImage(String url, Bitmap bitmap) {
if (TextUtils.isEmpty(url) || bitmap == null) {
return;
}
if (getImage(url) == null) {
mImageCache.put(url, bitmap);
}
}

@Nullable
public Bitmap getImage(String url) {
if (TextUtils.isEmpty(url)) {
return null;
}
return mImageCache.get(url);
}
}
193 changes: 98 additions & 95 deletions library/src/main/java/me/kaelaela/opengraphview/OpenGraphView.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
Expand All @@ -25,8 +24,6 @@

public class OpenGraphView extends RelativeLayout {

public static final String TAG = "OpenGraphView";

public static final int IMAGE_POS_LEFT = 0;
public static final int IMAGE_POS_RIGHT = 1;

Expand All @@ -40,10 +37,12 @@ public class OpenGraphView extends RelativeLayout {
private boolean mSeparate = false;
private View mSeparator;
private ImageView mImageView;
private ImageView mFavicon;
private String mUrl;
private OnLoadListener mOnLoadListener;
private Paint mFill = new Paint();
private Paint mStroke = new Paint();
private static OGCache mOGCache = OGCache.getInstance();

public OpenGraphView(Context context) {
this(context, null);
Expand Down Expand Up @@ -71,6 +70,7 @@ public OpenGraphView(Context context, AttributeSet attrs, int defStyleAttr) {
mStroke.setColor(ContextCompat.getColor(context, R.color.light_gray));

mImageView = (ImageView) findViewById(R.id.og_image);
mFavicon = (ImageView) findViewById(R.id.favicon);
setBgColor(array.getColor(R.styleable.OpenGraphView_bgColor,
ContextCompat.getColor(context, android.R.color.white)));
((TextView) findViewById(R.id.og_title)).setTextColor(
Expand Down Expand Up @@ -188,48 +188,49 @@ public void setOnLoadListener(OnLoadListener listener) {
mOnLoadListener = listener;
}

public void loadFrom(@Nullable String url) {
public void loadFrom(@Nullable final String url) {
setVisibility(TextUtils.isEmpty(url) ? GONE : VISIBLE);
if (TextUtils.isEmpty(url)) {
if (TextUtils.isEmpty(url) || mSeparator == null) {
return;
}
mUrl = url;
if (mSeparator == null) {
return;
}
mSeparator.setVisibility(GONE);
LoadOGDataTask task = new LoadOGDataTask(new LoadOGDataTask.OnLoadListener() {
@Override
public void onLoadStart() {
super.onLoadStart();
if (mOnLoadListener != null) {
mOnLoadListener.onLoadStart();
OGData ogData = mOGCache.get(url);
if (ogData == null) {
new LoadOGDataTask(new LoadOGDataTask.OnLoadListener() {
@Override
public void onLoadStart() {
super.onLoadStart();
if (mOnLoadListener != null) {
mOnLoadListener.onLoadStart();
}
}
loadFavicon(mUrl);
Log.d(TAG, "start loading");
}

@Override
public void onLoadSuccess(OGData ogData) {
super.onLoadSuccess(ogData);
Log.d(TAG, "success loading" + ogData.toString());
loadImage(ogData.getImage());
setOpenGraphData(ogData);
if (mOnLoadListener != null) {
mOnLoadListener.onLoadFinish();

@Override
public void onLoadSuccess(OGData ogData) {
super.onLoadSuccess(ogData);
mOGCache.add(url, ogData);
loadImage(ogData.getImage());
loadFavicon(mUrl);
setOpenGraphData(ogData);
if (mOnLoadListener != null) {
mOnLoadListener.onLoadFinish();
}
}
}

@Override
public void onLoadError() {
super.onLoadError();
if (mOnLoadListener != null) {
mOnLoadListener.onLoadError();
@Override
public void onLoadError() {
super.onLoadError();
if (mOnLoadListener != null) {
mOnLoadListener.onLoadError();
}
}
Log.d(TAG, "error loading");
}
});
task.execute(url);
}).execute(url);
} else {
loadImage(ogData.getImage());
loadFavicon(mUrl);
setOpenGraphData(ogData);
}
}

public void loadFrom(Uri uri) {
Expand All @@ -255,69 +256,74 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mViewHeight = h;
}

private void loadImage(String url) {
LoadImageTask task = new LoadImageTask(new LoadImageTask.OnLoadListener() {
@Override
public void onLoadStart() {
super.onLoadStart();
Log.d(TAG, "start image loading");
}

@Override
public void onLoadSuccess(Bitmap bitmap) {
super.onLoadSuccess(bitmap);
Log.d(TAG, "success image loading");
mImageView.setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.white));
mImageView.setImageBitmap(bitmap);
ImageAnimator.alphaAnimation(mImageView);
if (mSeparate) {
mSeparator.setVisibility(VISIBLE);
private void loadImage(final String url) {
Bitmap bitmap = mOGCache.getImage(url);
if (bitmap == null) {
new LoadImageTask(new LoadImageTask.OnLoadListener() {
@Override
public void onLoadStart() {
super.onLoadStart();
}
}

@Override
public void onLoadError() {
super.onLoadError();
Log.d(TAG, "error image loading");
}
});
task.execute(url);
if (mImageView != null) {
mImageView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.light_gray));

@Override
public void onLoadSuccess(Bitmap bitmap) {
super.onLoadSuccess(bitmap);
mOGCache.addImage(url, bitmap);
setImage(bitmap);
}

@Override
public void onLoadError() {
super.onLoadError();
}
}).execute(url);
} else {
setImage(bitmap);
}
}

private void setImage(Bitmap bitmap) {
mImageView.setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.white));
mImageView.setImageBitmap(bitmap);
ImageAnimator.alphaAnimation(mImageView);
if (mSeparate) {
mSeparator.setVisibility(VISIBLE);
}
}

private void loadFavicon(String url) {
Uri uri = Uri.parse(url);
String host = uri.getHost();
if (host != null) {
host = host.startsWith("www.") ? host.substring(4) : host;
final String host = uri.getHost().startsWith("www.") ? uri.getHost().substring(4) : uri.getHost();
Bitmap favicon = mOGCache.getImage(host);
if (favicon == null) {
new LoadFaviconTask(new LoadFaviconTask.OnLoadListener() {
@Override
public void onLoadStart() {
super.onLoadStart();
}

@Override
public void onLoadSuccess(Bitmap bitmap) {
super.onLoadSuccess(bitmap);
mOGCache.addImage(host, bitmap);
setFavicon(bitmap);
}

@Override
public void onLoadError() {
super.onLoadError();
mFavicon.setVisibility(GONE);
}
}).execute(host);
} else {
setFavicon(favicon);
}
final ImageView favicon = (ImageView) findViewById(R.id.favicon);
LoadFaviconTask task = new LoadFaviconTask(new LoadFaviconTask.OnLoadListener() {
@Override
public void onLoadStart() {
super.onLoadStart();
Log.d(TAG, "start favicon loading");
}

@Override
public void onLoadSuccess(Bitmap bitmap) {
super.onLoadSuccess(bitmap);
Log.d(TAG, "success favicon loading");
favicon.setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.white));
favicon.setImageBitmap(bitmap);
ImageAnimator.alphaAnimation(favicon);
}

@Override
public void onLoadError() {
super.onLoadError();
Log.d(TAG, "error favicon loading");
favicon.setVisibility(GONE);
}
});
task.execute(host);
}

private void setFavicon(Bitmap bitmap) {
mFavicon.setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.white));
mFavicon.setImageBitmap(bitmap);
ImageAnimator.alphaAnimation(mFavicon);
}

private void setOpenGraphData(OGData data) {
Expand All @@ -333,9 +339,6 @@ private void setOpenGraphData(OGData data) {
}

boolean isImageEmpty = TextUtils.isEmpty(data.getImage());
if (!isImageEmpty) {
mImageView.setImageURI(Uri.parse(data.getImage()));
}
mImageView.setVisibility(isImageEmpty ? GONE : VISIBLE);
if (TextUtils.isEmpty(data.getTitle()) && TextUtils.isEmpty(data.getDescription())) {
title.setText(mUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.text.TextUtils;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
Expand Down Expand Up @@ -30,6 +31,9 @@ public LoadImageTask(OnLoadListener listener) {
protected Bitmap doInBackground(String... urls) {
listener.onLoadStart();
String url = urls[0];
if (TextUtils.isEmpty(url)) {
return null;
}
Bitmap image = null;
try {
InputStream inputStream = new URL(url).openStream();
Expand Down
2 changes: 1 addition & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':library')
compile "com.android.support:appcompat-v7:$SUPPORT_APP_COMPAT_VERSION"
compile "com.android.support:design:$SUPPORT_APP_COMPAT_VERSION"
compile "com.android.support:customtabs:$SUPPORT_APP_COMPAT_VERSION"
}
Loading

0 comments on commit 84ec88c

Please sign in to comment.