From 305e076dbfafb1d516c076b0a39bb828fd1acb91 Mon Sep 17 00:00:00 2001 From: YuviPanda Date: Mon, 17 Jun 2013 16:59:31 +0000 Subject: [PATCH] Maintain max width / height info to speed up image downloading If we ever download an image's original, we'll save its width and height. This ensures that we don't try to fetch thumbnails that are larger than the image itself too often, reducing 500s and improving performance much. TODO: Save image's width / height during upload process itself GitHub: https://github.com/wikimedia/apps-android-commons/pull/8 Change-Id: Ice11cfabab96999b0d07b965d4515b47c1fff428 --- .../java/org/wikimedia/commons/Media.java | 18 +++++++++++++ .../wikimedia/commons/MediaWikiImageView.java | 22 ++++++++++++--- .../wikimedia/commons/category/Category.java | 8 ++++++ .../commons/contributions/Contribution.java | 27 +++++++++++++++++-- .../wikimedia/commons/data/DBOpenHelper.java | 2 +- 5 files changed, 70 insertions(+), 7 deletions(-) diff --git a/commons/src/main/java/org/wikimedia/commons/Media.java b/commons/src/main/java/org/wikimedia/commons/Media.java index cfe177cb3..9ff01268f 100644 --- a/commons/src/main/java/org/wikimedia/commons/Media.java +++ b/commons/src/main/java/org/wikimedia/commons/Media.java @@ -103,6 +103,22 @@ public class Media implements Parcelable { return Utils.makeThumbUrl(imageUrl, filename, width); } + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + protected Uri localUri; protected String imageUrl; protected String filename; @@ -110,6 +126,8 @@ public class Media implements Parcelable { protected long dataLength; protected Date dateCreated; protected Date dateUploaded; + protected int width; + protected int height; protected String creator; diff --git a/commons/src/main/java/org/wikimedia/commons/MediaWikiImageView.java b/commons/src/main/java/org/wikimedia/commons/MediaWikiImageView.java index afa95d869..550eb9ba7 100644 --- a/commons/src/main/java/org/wikimedia/commons/MediaWikiImageView.java +++ b/commons/src/main/java/org/wikimedia/commons/MediaWikiImageView.java @@ -27,6 +27,8 @@ import com.android.volley.VolleyError; import com.android.volley.toolbox.ImageLoader; import com.android.volley.toolbox.ImageLoader.ImageContainer; import com.android.volley.toolbox.ImageLoader.ImageListener; +import org.wikimedia.commons.contributions.Contribution; +import org.wikimedia.commons.contributions.ContributionsContentProvider; public class MediaWikiImageView extends ImageView { @@ -86,7 +88,15 @@ public class MediaWikiImageView extends ImageView { // Possible a similar size image has already been generated. // Reduces Server cache fragmentation, also increases chance of cache hit // If width is less than 320, we just use that directly, to avoid a case of the Maths - mUrl = mMedia.getThumbnailUrl(width <= 320 ? width : (width / 320) * 320); + int bucketedWidth = width <= 320 ? width: (width / 320) * 320; + if(mMedia.getWidth() != 0 && mMedia.getWidth() < bucketedWidth) { + // If we know that the width of the image is lesser than the required width + // We don't even try to load the thumbnai, go directly to the source + loadImageIfNecessary(isInLayoutPass, true); + return; + } else { + mUrl = mMedia.getThumbnailUrl(width <= 320 ? width : (width / 320) * 320); + } } // if the URL to be loaded in this view is empty, cancel any old requests and clear the @@ -151,11 +161,15 @@ public class MediaWikiImageView extends ImageView { return; } - - Log.d("Commons", "No-Error: For Url " + mUrl + " value is " + tryOriginal); - if (response.getBitmap() != null) { setImageBitmap(response.getBitmap()); + if(tryOriginal && mMedia instanceof Contribution && response.getBitmap().getWidth() > mMedia.getWidth() || response.getBitmap().getHeight() > mMedia.getHeight()) { + // If there is no width information for this image, save it. This speeds up image loading massively for smaller images + mMedia.setHeight(response.getBitmap().getHeight()); + mMedia.setWidth(response.getBitmap().getWidth()); + ((Contribution)mMedia).setContentProviderClient(MediaWikiImageView.this.getContext().getContentResolver().acquireContentProviderClient(ContributionsContentProvider.AUTHORITY)); + ((Contribution)mMedia).save(); + } if(loadingView != null) { loadingView.setVisibility(View.GONE); } diff --git a/commons/src/main/java/org/wikimedia/commons/category/Category.java b/commons/src/main/java/org/wikimedia/commons/category/Category.java index ae2045867..8aaf7e2d1 100644 --- a/commons/src/main/java/org/wikimedia/commons/category/Category.java +++ b/commons/src/main/java/org/wikimedia/commons/category/Category.java @@ -134,6 +134,14 @@ public class Category { if(from == 4) { // table added in version 5 onCreate(db); + from++; + onUpdate(db, from, to); + return; + } + if(from == 5) { + from++; + onUpdate(db, from, to); + return; } } } diff --git a/commons/src/main/java/org/wikimedia/commons/contributions/Contribution.java b/commons/src/main/java/org/wikimedia/commons/contributions/Contribution.java index c10727f3d..25e055402 100644 --- a/commons/src/main/java/org/wikimedia/commons/contributions/Contribution.java +++ b/commons/src/main/java/org/wikimedia/commons/contributions/Contribution.java @@ -189,6 +189,8 @@ public class Contribution extends Media { cv.put(Table.COLUMN_DESCRIPTION, description); cv.put(Table.COLUMN_CREATOR, creator); cv.put(Table.COLUMN_MULTIPLE, isMultiple ? 1 : 0); + cv.put(Table.COLUMN_WIDTH, width); + cv.put(Table.COLUMN_HEIGHT, height); return cv; } @@ -220,6 +222,9 @@ public class Contribution extends Media { c.description = cursor.getString(10); c.creator = cursor.getString(11); c.isMultiple = cursor.getInt(12) == 1; + c.width = cursor.getInt(13); + c.height = cursor.getInt(14); + return c; } @@ -252,6 +257,8 @@ public class Contribution extends Media { public static final String COLUMN_DESCRIPTION = "description"; public static final String COLUMN_CREATOR = "creator"; // Initial uploader public static final String COLUMN_MULTIPLE = "multiple"; + public static final String COLUMN_WIDTH = "width"; + public static final String COLUMN_HEIGHT = "height"; // NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES. public static final String[] ALL_FIELDS = { @@ -267,7 +274,9 @@ public class Contribution extends Media { COLUMN_SOURCE, COLUMN_DESCRIPTION, COLUMN_CREATOR, - COLUMN_MULTIPLE + COLUMN_MULTIPLE, + COLUMN_WIDTH, + COLUMN_HEIGHT }; @@ -284,7 +293,9 @@ public class Contribution extends Media { + "source STRING," + "description STRING," + "creator STRING," - + "multiple INTEGER" + + "multiple INTEGER," + + "width INTEGER," + + "height INTEGER" + ");"; @@ -313,11 +324,23 @@ public class Contribution extends Media { if(from == 3) { // Do nothing from++; + onUpdate(db, from, to); return; } if(from == 4) { // Do nothing -- added Category from++; + onUpdate(db, from, to); + return; + } + if(from == 5) { + // Added width and height fields + db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN width INTEGER;"); + db.execSQL("UPDATE " + TABLE_NAME + " SET width = 0"); + db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN height INTEGER;"); + db.execSQL("UPDATE " + TABLE_NAME + " SET height = 0"); + from++; + onUpdate(db, from, to); return; } } diff --git a/commons/src/main/java/org/wikimedia/commons/data/DBOpenHelper.java b/commons/src/main/java/org/wikimedia/commons/data/DBOpenHelper.java index 4430ac59a..af680650f 100644 --- a/commons/src/main/java/org/wikimedia/commons/data/DBOpenHelper.java +++ b/commons/src/main/java/org/wikimedia/commons/data/DBOpenHelper.java @@ -10,7 +10,7 @@ import org.wikimedia.commons.modifications.ModifierSequence; public class DBOpenHelper extends SQLiteOpenHelper{ private static final String DATABASE_NAME = "commons.db"; - private static final int DATABASE_VERSION = 5; + private static final int DATABASE_VERSION = 6; public DBOpenHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION);