Back to project page photo-paper.
The source code is released under:
MIT License
If you think the Android project photo-paper listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.lukekorth.android_500px.models; /* w w w . ja v a 2s . c om*/ import android.content.Context; import com.activeandroid.Model; import com.activeandroid.annotation.Column; import com.activeandroid.annotation.Table; import com.activeandroid.query.From; import com.activeandroid.query.Select; import com.lukekorth.android_500px.helpers.Settings; import org.json.JSONException; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.List; @Table(name = "Photos") public class Photos extends Model { public static final String BASE_URL_500PX = "http://500px.com"; private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); @Column(name = "photo_id") public int photo_id; @Column(name = "name") public String name; @Column(name = "description") public String description; @Column(name = "user_name") public String userName; @Column(name = "created_at") public long createdAt; @Column(name = "feature") public String feature; @Column(name = "search") public String search; @Column(name = "category") public int category; @Column(name = "nsfw") public boolean nsfw; @Column(name = "image_url") public String imageUrl; @Column(name = "url_path") public String urlPath; @Column(name = "seen") public boolean seen; @Column(name = "seen_at") public long seenAt; @Column(name = "failed_count") public int failedCount; @Column(name = "added_at") public long addedAt; public Photos() { super(); } public static Photos create(JSONObject jsonPhoto, String feature, String search, int desiredHeight, int desiredWidth) { Logger logger = LoggerFactory.getLogger("Photos"); try { int actualHeight = jsonPhoto.getInt("height"); int actualWidth = jsonPhoto.getInt("width"); if (isAcceptableSize(desiredHeight, desiredWidth, actualHeight, actualWidth)) { int id = jsonPhoto.getInt("id"); if (!new Select().from(Photos.class).where("photo_id = ?", id).exists()) { Photos photo = new Photos(); photo.photo_id = id; photo.name = jsonPhoto.getString("name"); photo.description = jsonPhoto.getString("description"); photo.userName = jsonPhoto.getJSONObject("user").getString("fullname"); String createdAt = jsonPhoto.getString("created_at"); photo.createdAt = DATE_FORMAT.parse(createdAt.substring(0, createdAt.length() - 6)).getTime(); photo.feature = feature; photo.search = search; photo.category = jsonPhoto.getInt("category"); photo.nsfw = jsonPhoto.getBoolean("nsfw"); photo.imageUrl = jsonPhoto.getString("image_url"); photo.urlPath = jsonPhoto.getString("url"); photo.seen = false; photo.seenAt = 0; photo.addedAt = System.currentTimeMillis(); photo.save(); return photo; } else { logger.debug("Photo already exists"); } } else { logger.debug("Photo was not an acceptable size"); } } catch (JSONException e) { logger.error(e.getMessage()); } catch (ParseException e) { logger.error(e.getMessage()); } return null; } public static Photos getPhoto(int id) { return new Select().from(Photos.class) .where("photo_id = ?", id) .executeSingle(); } public static Photos getNextPhoto(Context context) { return getQuery(context).executeSingle(); } public static Photos getCurrentPhoto() { return getSeenQuery().executeSingle(); } public static List<Photos> getSeenPhotos() { return getSeenQuery().execute(); } public static int unseenPhotoCount(Context context) { return getQuery(context).count(); } private static From getQuery(Context context) { String feature = Settings.getFeature(context); From query = new Select().from(Photos.class) .where("feature = ?", feature); if (feature.equals("search")) { query.where("search = ?", Settings.getSearchQuery(context)); } else { int[] categories = Settings.getCategories(context); Object[] categoryArgs = new Object[categories.length]; String placeHolders = ""; for (int i = 0; i < categories.length; i++) { placeHolders += "?, "; categoryArgs[i] = categories[i]; } placeHolders = placeHolders.substring(0, placeHolders.length() - 2); query.where("category IN (" + placeHolders + ")", categoryArgs); } if (!Settings.allowNSFW(context)) { query.where("nsfw = ?", false); } return query.where("seen = ?", false) .orderBy("failed_count ASC, created_at ASC"); } private static From getSeenQuery() { return new Select().from(Photos.class) .where("seen = ?", true) .orderBy("seen_at DESC"); } private static boolean isAcceptableSize(int desiredHeight, int desiredWidth, int actualHeight, int actualWidth) { boolean scale; if (actualHeight >= desiredHeight && actualWidth >= desiredWidth) { scale = true; } else { double scaleHeight = 0; if (actualHeight < desiredHeight) { scaleHeight = (double) desiredHeight / actualHeight; } double scaleWidth = 0; if (actualWidth < desiredWidth) { scaleWidth = (double) desiredWidth / actualWidth; } scale = Math.max(scaleHeight, scaleWidth) <= 1.5; } double desiredAspectRatio = (double) desiredHeight / desiredWidth; double actualAspectRatio = (double) actualHeight / actualWidth; double percentDifference = (Math.abs(desiredAspectRatio - actualAspectRatio) / desiredAspectRatio) * 100; boolean aspectRatio = percentDifference < 30; return scale && aspectRatio; } }