Merge "Maintain max width / height info to speed up image downloading"

This commit is contained in:
Brion VIBBER 2013-06-17 17:41:49 +00:00 committed by Gerrit Code Review
commit 0d70ec6f80
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); 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 Uri localUri;
protected String imageUrl; protected String imageUrl;
protected String filename; protected String filename;
@ -110,6 +126,8 @@ public class Media implements Parcelable {
protected long dataLength; protected long dataLength;
protected Date dateCreated; protected Date dateCreated;
protected Date dateUploaded; protected Date dateUploaded;
protected int width;
protected int height;
protected String creator; 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;
import com.android.volley.toolbox.ImageLoader.ImageContainer; import com.android.volley.toolbox.ImageLoader.ImageContainer;
import com.android.volley.toolbox.ImageLoader.ImageListener; import com.android.volley.toolbox.ImageLoader.ImageListener;
import org.wikimedia.commons.contributions.Contribution;
import org.wikimedia.commons.contributions.ContributionsContentProvider;
public class MediaWikiImageView extends ImageView { public class MediaWikiImageView extends ImageView {
@ -90,7 +92,15 @@ public class MediaWikiImageView extends ImageView {
// Possible a similar size image has already been generated. // Possible a similar size image has already been generated.
// Reduces Server cache fragmentation, also increases chance of cache hit // 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 // 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 // if the URL to be loaded in this view is empty, cancel any old requests and clear the
@ -155,11 +165,15 @@ public class MediaWikiImageView extends ImageView {
return; return;
} }
Log.d("Commons", "No-Error: For Url " + mUrl + " value is " + tryOriginal);
if (response.getBitmap() != null) { if (response.getBitmap() != null) {
setImageBitmap(response.getBitmap()); 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) { if(loadingView != null) {
loadingView.setVisibility(View.GONE); loadingView.setVisibility(View.GONE);
} }

View file

@ -134,6 +134,14 @@ public class Category {
if(from == 4) { if(from == 4) {
// table added in version 5 // table added in version 5
onCreate(db); 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_DESCRIPTION, description);
cv.put(Table.COLUMN_CREATOR, creator); cv.put(Table.COLUMN_CREATOR, creator);
cv.put(Table.COLUMN_MULTIPLE, isMultiple ? 1 : 0); cv.put(Table.COLUMN_MULTIPLE, isMultiple ? 1 : 0);
cv.put(Table.COLUMN_WIDTH, width);
cv.put(Table.COLUMN_HEIGHT, height);
return cv; return cv;
} }
@ -220,6 +222,9 @@ public class Contribution extends Media {
c.description = cursor.getString(10); c.description = cursor.getString(10);
c.creator = cursor.getString(11); c.creator = cursor.getString(11);
c.isMultiple = cursor.getInt(12) == 1; c.isMultiple = cursor.getInt(12) == 1;
c.width = cursor.getInt(13);
c.height = cursor.getInt(14);
return c; return c;
} }
@ -252,6 +257,8 @@ public class Contribution extends Media {
public static final String COLUMN_DESCRIPTION = "description"; public static final String COLUMN_DESCRIPTION = "description";
public static final String COLUMN_CREATOR = "creator"; // Initial uploader public static final String COLUMN_CREATOR = "creator"; // Initial uploader
public static final String COLUMN_MULTIPLE = "multiple"; 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. // NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES.
public static final String[] ALL_FIELDS = { public static final String[] ALL_FIELDS = {
@ -267,7 +274,9 @@ public class Contribution extends Media {
COLUMN_SOURCE, COLUMN_SOURCE,
COLUMN_DESCRIPTION, COLUMN_DESCRIPTION,
COLUMN_CREATOR, COLUMN_CREATOR,
COLUMN_MULTIPLE COLUMN_MULTIPLE,
COLUMN_WIDTH,
COLUMN_HEIGHT
}; };
@ -284,7 +293,9 @@ public class Contribution extends Media {
+ "source STRING," + "source STRING,"
+ "description STRING," + "description STRING,"
+ "creator STRING," + "creator STRING,"
+ "multiple INTEGER" + "multiple INTEGER,"
+ "width INTEGER,"
+ "height INTEGER"
+ ");"; + ");";
@ -313,11 +324,23 @@ public class Contribution extends Media {
if(from == 3) { if(from == 3) {
// Do nothing // Do nothing
from++; from++;
onUpdate(db, from, to);
return; return;
} }
if(from == 4) { if(from == 4) {
// Do nothing -- added Category // Do nothing -- added Category
from++; 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; return;
} }
} }

View file

@ -10,7 +10,7 @@ import org.wikimedia.commons.modifications.ModifierSequence;
public class DBOpenHelper extends SQLiteOpenHelper{ public class DBOpenHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "commons.db"; 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) { public DBOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION); super(context, DATABASE_NAME, null, DATABASE_VERSION);