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
This commit is contained in:
YuviPanda 2013-06-17 16:59:31 +00:00 committed by SuchABot
parent 65a01c4459
commit 305e076dbf
5 changed files with 70 additions and 7 deletions

View file

@ -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;

View file

@ -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,8 +88,16 @@ 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
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
// currently loaded image.
@ -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);
}

View file

@ -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;
}
}
}

View file

@ -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;
}
}

View file

@ -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);