Improved Style

This commit is contained in:
Daniel Oeh 2012-05-30 18:36:10 +02:00
parent 2064796f91
commit fcf8881f8f
4 changed files with 130 additions and 110 deletions

View File

@ -174,7 +174,7 @@ public class PodDBAdapter {
if(image.getId() == 0) { if(image.getId() == 0) {
image.setId(db.insert(TABLE_NAME_FEED_IMAGES, null, values)); image.setId(db.insert(TABLE_NAME_FEED_IMAGES, null, values));
} else { } else {
db.update(TABLE_NAME_FEED_IMAGES, values, KEY_ID+"=?", new String[]{String.valueOf(image.getId())}); db.update(TABLE_NAME_FEED_IMAGES, values, KEY_ID + "=?", new String[]{String.valueOf(image.getId())});
} }
close(); close();
return image.getId(); return image.getId();
@ -198,7 +198,7 @@ public class PodDBAdapter {
if(media.getId() == 0) { if(media.getId() == 0) {
media.setId(db.insert(TABLE_NAME_FEED_MEDIA, null, values)); media.setId(db.insert(TABLE_NAME_FEED_MEDIA, null, values));
} else { } else {
db.update(TABLE_NAME_FEED_MEDIA, values, KEY_ID+"=?", new String[]{String.valueOf(media.getId())}); db.update(TABLE_NAME_FEED_MEDIA, values, KEY_ID + "=?", new String[]{String.valueOf(media.getId())});
} }
close(); close();
return media.getId(); return media.getId();
@ -214,70 +214,95 @@ public class PodDBAdapter {
values.put(KEY_LINK, item.getLink()); values.put(KEY_LINK, item.getLink());
values.put(KEY_DESCRIPTION, item.getDescription()); values.put(KEY_DESCRIPTION, item.getDescription());
values.put(KEY_PUBDATE, item.getPubDate()); values.put(KEY_PUBDATE, item.getPubDate());
if(item.getMedia() != null) { if (item.getMedia() != null) {
if(item.getMedia().getId() == 0) { if(item.getMedia().getId() == 0) {
setMedia(item.getMedia()); setMedia(item.getMedia());
} }
values.put(KEY_MEDIA, item.getMedia().getId()); values.put(KEY_MEDIA, item.getMedia().getId());
} }
if(item.getFeed().getId() == 0) { if (item.getFeed().getId() == 0) {
setFeed(item.getFeed()); setFeed(item.getFeed());
} }
values.put(KEY_FEED, item.getFeed().getId()); values.put(KEY_FEED, item.getFeed().getId());
values.put(KEY_READ, (item.isRead()) ? 1 : 0); values.put(KEY_READ, (item.isRead()) ? 1 : 0);
open(); open();
if(item.getId() == 0) { if (item.getId() == 0) {
item.setId(db.insert(TABLE_NAME_FEED_ITEMS, null, values)); item.setId(db.insert(TABLE_NAME_FEED_ITEMS, null, values));
} else { } else {
db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID+"=?", new String[]{String.valueOf(item.getId())}); db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID + "=?",
new String[]{String.valueOf(item.getId())});
} }
close(); close();
return item.getId(); return item.getId();
} }
public Cursor getAllCategoriesCursor() { /** Get all Categories from the Categories Table.
* @return The cursor of the query
* */
public final Cursor getAllCategoriesCursor() {
open(); open();
Cursor c = db.query(TABLE_NAME_FEED_CATEGORIES, null, null, null, null, null, null); Cursor c = db.query(TABLE_NAME_FEED_CATEGORIES, null, null, null, null, null, null);
return c; return c;
} }
public Cursor getAllFeedsCursor() { /** Get all Feeds from the Feed Table.
* @return The cursor of the query
* */
public final Cursor getAllFeedsCursor() {
open(); open();
Cursor c = db.query(TABLE_NAME_FEEDS, null, null, null, null, null, null); Cursor c = db.query(TABLE_NAME_FEEDS, null, null, null, null, null, null);
return c; return c;
} }
public Cursor getAllItemsOfFeedCursor(Feed feed) { /** Returns a cursor with all FeedItems of a Feed.
* @param feed The feed you want to get the FeedItems from.
* @return The cursor of the query
* */
public final Cursor getAllItemsOfFeedCursor(final Feed feed) {
open(); open();
Cursor c = db.query(TABLE_NAME_FEED_ITEMS, null, KEY_FEED + "=?", Cursor c = db.query(TABLE_NAME_FEED_ITEMS, null, KEY_FEED + "=?",
new String[]{String.valueOf(feed.getId())}, null, null, null); new String[]{String.valueOf(feed.getId())}, null, null, null);
return c; return c;
} }
public Cursor getFeedMediaOfItemCursor(FeedItem item) { /** Returns a cursor for a DB query in the FeedMedia table for a given ID.
* @param item The item you want to get the FeedMedia from
* @return The cursor of the query
* */
public final Cursor getFeedMediaOfItemCursor(final FeedItem item) {
open(); open();
Cursor c = db.query(TABLE_NAME_FEED_MEDIA, null, KEY_ID + "=?", Cursor c = db.query(TABLE_NAME_FEED_MEDIA, null, KEY_ID + "=?",
new String[]{String.valueOf(item.getMedia().getId())}, null, null, null); new String[]{String.valueOf(item.getMedia().getId())},
null, null, null);
return c; return c;
} }
public Cursor getImageOfFeedCursor(long id) { /** Returns a cursor for a DB query in the FeedImages table for a given ID.
* @param id ID of the FeedImage
* @return The cursor of the query
* */
public final Cursor getImageOfFeedCursor(final long id) {
open(); open();
Cursor c = db.query(TABLE_NAME_FEED_IMAGES, null, KEY_ID + "=?", Cursor c = db.query(TABLE_NAME_FEED_IMAGES, null, KEY_ID + "=?",
new String[]{String.valueOf(id)}, null, null, null); new String[]{String.valueOf(id)}, null, null, null);
return c; return c;
} }
public FeedMedia getFeedMedia(long row_index, FeedItem owner) throws SQLException{ /** Get a FeedMedia object from the Database.
* @param rowIndex DB Index of Media object
* @param owner FeedItem the Media object belongs to
* @return A newly created FeedMedia object
* */
public final FeedMedia getFeedMedia(final long rowIndex, final FeedItem owner)
throws SQLException {
open(); open();
Cursor cursor = db.query(TABLE_NAME_FEED_MEDIA, null, KEY_ID + "=?", Cursor cursor = db.query(TABLE_NAME_FEED_MEDIA, null, KEY_ID + "=?",
new String[]{String.valueOf(row_index)}, null, null, null); new String[]{String.valueOf(rowIndex)}, null, null, null);
if ((cursor.getCount() == 0) || !cursor.moveToFirst()) { if ((cursor.getCount() == 0) || !cursor.moveToFirst()) {
throw new SQLException("No FeedMedia found at index: " + row_index); throw new SQLException("No FeedMedia found at index: " + rowIndex);
} }
FeedMedia media = new FeedMedia(row_index, FeedMedia media = new FeedMedia(rowIndex,
owner, owner,
cursor.getLong(cursor.getColumnIndex(KEY_LENGTH)), cursor.getLong(cursor.getColumnIndex(KEY_LENGTH)),
cursor.getLong(cursor.getColumnIndex(KEY_POSITION)), cursor.getLong(cursor.getColumnIndex(KEY_POSITION)),
@ -287,14 +312,13 @@ public class PodDBAdapter {
cursor.getString(cursor.getColumnIndex(KEY_DOWNLOAD_URL))); cursor.getString(cursor.getColumnIndex(KEY_DOWNLOAD_URL)));
close(); close();
return media; return media;
} }
/** Searches the DB for a FeedImage of the given id. /** Searches the DB for a FeedImage of the given id.
* @param id The id of the object * @param id The id of the object
* @return The found object * @return The found object
* */ * */
public FeedImage getFeedImage(final long id) throws SQLException { public final FeedImage getFeedImage(final long id) throws SQLException {
open(); open();
Cursor cursor = this.getImageOfFeedCursor(id); Cursor cursor = this.getImageOfFeedCursor(id);
if ((cursor.getCount() == 0) || !cursor.moveToFirst()) { if ((cursor.getCount() == 0) || !cursor.moveToFirst()) {
@ -319,7 +343,6 @@ public class PodDBAdapter {
* @param name Name of the database * @param name Name of the database
* @param factory to use for creating cursor objects * @param factory to use for creating cursor objects
* @param version number of the database * @param version number of the database
*
* */ * */
public PodDBHelper(final Context context, public PodDBHelper(final Context context,
final String name, final CursorFactory factory, final String name, final CursorFactory factory,
@ -334,7 +357,6 @@ public class PodDBAdapter {
db.execSQL(CREATE_TABLE_FEED_CATEGORIES); db.execSQL(CREATE_TABLE_FEED_CATEGORIES);
db.execSQL(CREATE_TABLE_FEED_IMAGES); db.execSQL(CREATE_TABLE_FEED_IMAGES);
db.execSQL(CREATE_TABLE_FEED_MEDIA); db.execSQL(CREATE_TABLE_FEED_MEDIA);
} }
@Override @Override
@ -344,8 +366,6 @@ public class PodDBAdapter {
+ oldVersion + " to " + oldVersion + " to "
+ newVersion + "."); + newVersion + ".");
// TODO delete Database // TODO delete Database
} }
} }
} }

View File

@ -4,53 +4,53 @@ import android.util.Log;
/** Provides methods for converting various units. */ /** Provides methods for converting various units. */
public final class Converter { public final class Converter {
/** Class shall not be instantiated. */ /** Class shall not be instantiated. */
private Converter() { private Converter() {
} }
/** Logging tag. */ /** Logging tag. */
private static final String TAG = "Converter"; private static final String TAG = "Converter";
/** Indicates that the value is in the Byte range.*/ /** Indicates that the value is in the Byte range.*/
private static final int B_RANGE = 0; private static final int B_RANGE = 0;
/** Indicates that the value is in the Kilobyte range.*/ /** Indicates that the value is in the Kilobyte range.*/
private static final int KB_RANGE = 1; private static final int KB_RANGE = 1;
/** Indicates that the value is in the Megabyte range.*/ /** Indicates that the value is in the Megabyte range.*/
private static final int MB_RANGE = 2; private static final int MB_RANGE = 2;
/** Indicates that the value is in the Gigabyte range.*/ /** Indicates that the value is in the Gigabyte range.*/
private static final int GB_RANGE = 3; private static final int GB_RANGE = 3;
/** Determines the length of the number for best readability.*/ /** Determines the length of the number for best readability.*/
private static final int NUM_LENGTH = 1000; private static final int NUM_LENGTH = 1000;
/** Takes a byte-value and converts it into a more readable /** Takes a byte-value and converts it into a more readable
* String. * String.
* @param input The value to convert * @param input The value to convert
* @return The converted String with a unit * @return The converted String with a unit
* */ * */
public static String byteToString(final long input) { public static String byteToString(final long input) {
int i = 0; int i = 0;
int result = 0; int result = 0;
for (i = 0; i < GB_RANGE + 1; i++) { for (i = 0; i < GB_RANGE + 1; i++) {
result = (int) (input / Math.pow(1024, i)); result = (int) (input / Math.pow(1024, i));
if (result < NUM_LENGTH) { if (result < NUM_LENGTH) {
break; break;
} }
} }
switch (i) { switch (i) {
case B_RANGE: case B_RANGE:
return result + " B"; return result + " B";
case KB_RANGE: case KB_RANGE:
return result + " KB"; return result + " KB";
case MB_RANGE: case MB_RANGE:
return result + " MB"; return result + " MB";
case GB_RANGE: case GB_RANGE:
return result + " GB"; return result + " GB";
default: default:
Log.e(TAG, "Error happened in byteToString"); Log.e(TAG, "Error happened in byteToString");
return "ERROR"; return "ERROR";
} }
} }
} }

View File

@ -5,18 +5,18 @@ import android.util.Log;
/**Utility class for creating large random numbers.*/ /**Utility class for creating large random numbers.*/
public final class NumberGenerator { public final class NumberGenerator {
/** Class shall not be instantiated.*/ /** Class shall not be instantiated.*/
private NumberGenerator() { private NumberGenerator() {
} }
/**Logging tag.*/ /**Logging tag.*/
private static final String TAG = "NumberGenerator"; private static final String TAG = "NumberGenerator";
/** Takes a string and generates a random value out of /** Takes a string and generates a random value out of
* the hash-value of that string. * the hash-value of that string.
* @param strSeed The string to take for the return value * @param strSeed The string to take for the return value
* @return The generated random value * @return The generated random value
* */ * */
public static long generateLong(final String strSeed) { public static long generateLong(final String strSeed) {
long seed = (long) strSeed.hashCode(); long seed = (long) strSeed.hashCode();
Log.d(TAG, "Taking " + seed + " as seed."); Log.d(TAG, "Taking " + seed + " as seed.");

View File

@ -1,39 +1,39 @@
package de.podfetcher.util; package de.podfetcher.util;
import android.util.Log; import android.util.Log;
/** Provides methods for checking and editing a URL.*/ /** Provides methods for checking and editing a URL.*/
public final class URLChecker { public final class URLChecker {
/**Class shall not be instantiated.*/ /**Class shall not be instantiated.*/
private URLChecker() { private URLChecker() {
} }
/**Logging tag.*/ /**Logging tag.*/
private static final String TAG = "URLChecker"; private static final String TAG = "URLChecker";
/**Indicator for URLs made by Feedburner.*/ /**Indicator for URLs made by Feedburner.*/
private static final String FEEDBURNER_URL = "feeds.feedburner.com"; private static final String FEEDBURNER_URL = "feeds.feedburner.com";
/**Prefix that is appended to URLs by Feedburner.*/ /**Prefix that is appended to URLs by Feedburner.*/
private static final String FEEDBURNER_PREFIX = "?format=xml"; private static final String FEEDBURNER_PREFIX = "?format=xml";
/** Checks if URL is valid and modifies it if necessary. /** Checks if URL is valid and modifies it if necessary.
* @param url The url which is going to be prepared * @param url The url which is going to be prepared
* @return The prepared url * @return The prepared url
* */ * */
public static String prepareURL(final String url) { public static String prepareURL(final String url) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
if (!url.startsWith("http")) { if (!url.startsWith("http")) {
builder.append("http://"); builder.append("http://");
Log.d(TAG, "Missing http; appending"); Log.d(TAG, "Missing http; appending");
} }
builder.append(url); builder.append(url);
if (url.contains(FEEDBURNER_URL)) { if (url.contains(FEEDBURNER_URL)) {
Log.d(TAG, Log.d(TAG,
"URL seems to be Feedburner URL; appending prefix"); "URL seems to be Feedburner URL; appending prefix");
builder.append(FEEDBURNER_PREFIX); builder.append(FEEDBURNER_PREFIX);
} }
return builder.toString(); return builder.toString();
} }
} }