From f06f790b48914dff0ec91c268834e50dd9121d54 Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Sun, 22 Jul 2012 09:37:28 -0400 Subject: [PATCH] Cleaned up code --- LocationRinger/AndroidManifest.xml | 4 +- .../EnableScrollingListener.java | 5 +- .../FeatureRemovedListener.java | 6 +- .../OnContentChangedListener.java | 13 +- .../SearchRequestedListener.java | 7 +- .../LocationRinger/db/DatabaseListener.java | 10 +- .../LocationRinger/db/RingerDatabase.java | 795 +++++------ .../android/LocationRinger/debug/Debug.java | 33 +- .../receivers/GetLocationWidget.java | 154 ++- .../receivers/LocationChangedReceiver.java | 18 +- .../PassiveLocationChangedReceiver.java | 3 +- .../receivers/SystemReceiver.java | 47 +- .../services/LocationService.java | 122 +- .../services/RingerProcessingService.java | 241 ++-- .../ui/CheckLocationShortcut.java | 172 +-- .../LocationRinger/ui/FirstBootDialog.java | 52 +- .../LocationRinger/ui/ListActivity.java | 526 ++++---- .../ui/RingerInformationActivity.java | 224 ++-- .../LocationRinger/ui/RingerListAdapter.java | 153 +-- .../LocationRinger/ui/SearchDialog.java | 144 +- .../LocationRinger/ui/SettingsActivity.java | 118 +- .../LocationRinger/ui/TextViewPreference.java | 42 +- .../android/LocationRinger/ui/ViewPager.java | 29 +- .../ui/fragments/AboutRingerFragment.java | 249 ++-- .../ui/fragments/BaseFeatureFragment.java | 49 +- .../fragments/BaseFragmentListFragment.java | 83 +- .../ui/fragments/FeatureListFragment.java | 309 ++--- .../fragments/LocationInfomationFragment.java | 241 ++-- .../ui/fragments/MapFragment.java | 3 +- .../ui/fragments/RingtoneFragment.java | 196 +-- .../ui/fragments/ToggleButtonFragment.java | 71 +- .../ui/fragments/VolumeFragment.java | 183 +-- .../viewpagerindicator/FragmentAdapter.java | 26 +- .../viewpagerindicator/PageIndicator.java | 60 +- .../TitlePageIndicator.java | 1158 ++++++++--------- .../viewpagerindicator/TitleProvider.java | 13 +- .../TitledFragmentAdapter.java | 22 +- 37 files changed, 2889 insertions(+), 2692 deletions(-) diff --git a/LocationRinger/AndroidManifest.xml b/LocationRinger/AndroidManifest.xml index 8587c10..f8bf737 100644 --- a/LocationRinger/AndroidManifest.xml +++ b/LocationRinger/AndroidManifest.xml @@ -2,8 +2,8 @@ + android:versionCode="67" + android:versionName="19b5c13" > diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/EnableScrollingListener.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/EnableScrollingListener.java index 9477004..8bc2ac3 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/EnableScrollingListener.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/EnableScrollingListener.java @@ -8,10 +8,11 @@ package com.TwentyCodes.android.LocationRinger; /** * A simple listener to allow fragments to set scrolling enabled + * * @author ricky */ public interface EnableScrollingListener { - + public void setScrollEnabled(boolean enabled); - + } \ No newline at end of file diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/FeatureRemovedListener.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/FeatureRemovedListener.java index 48e9b0a..c510a92 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/FeatureRemovedListener.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/FeatureRemovedListener.java @@ -9,13 +9,15 @@ package com.TwentyCodes.android.LocationRinger; import android.support.v4.app.Fragment; /** - * This interface will be used to notify + * This interface will be used to notify + * * @author ricky barrette */ public interface FeatureRemovedListener { /** - * Called when a feature is removed from the list + * Called when a feature is removed from the list + * * @author ricky barrette */ public void onFeatureRemoved(Fragment fragment); diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/OnContentChangedListener.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/OnContentChangedListener.java index 1fc4b26..073d22a 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/OnContentChangedListener.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/OnContentChangedListener.java @@ -8,16 +8,17 @@ package com.TwentyCodes.android.LocationRinger; import android.content.ContentValues; - /** - * This interface will be used to pass the content updated in fragments down to the main FragmentActivity + * This interface will be used to pass the content updated in fragments down to + * the main FragmentActivity + * * @author ricky */ public interface OnContentChangedListener { - void onRingerContentChanged(ContentValues ringer); - void onInfoContentChanged(ContentValues info); - - void onInfoContentRemoved(String... keys); + + void onInfoContentRemoved(String... keys); + + void onRingerContentChanged(ContentValues ringer); } \ No newline at end of file diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/SearchRequestedListener.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/SearchRequestedListener.java index 4191ac6..53b87fd 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/SearchRequestedListener.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/SearchRequestedListener.java @@ -7,13 +7,16 @@ package com.TwentyCodes.android.LocationRinger; /** - * A simple interface to allow a compent other than an activity to handle a seach event + * A simple interface to allow a compent other than an activity to handle a + * seach event + * * @author ricky barrette */ public interface SearchRequestedListener { - + /** * Called when the a seach is request via seach button + * * @return * @author ricky barrette */ diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/db/DatabaseListener.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/db/DatabaseListener.java index 79cc961..c8a9634 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/db/DatabaseListener.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/db/DatabaseListener.java @@ -7,17 +7,19 @@ package com.TwentyCodes.android.LocationRinger.db; /** - * This interface will be used to listen to see when the database events are complete + * This interface will be used to listen to see when the database events are + * complete + * * @author ricky barrette */ public interface DatabaseListener { - public void onDatabaseUpgradeComplete(); + public void onDatabaseUpgrade(); - public void onRingerDeletionComplete(); + public void onDatabaseUpgradeComplete(); public void onRestoreComplete(); - public void onDatabaseUpgrade(); + public void onRingerDeletionComplete(); } \ No newline at end of file diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/db/RingerDatabase.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/db/RingerDatabase.java index bd95cb2..857b687 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/db/RingerDatabase.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/db/RingerDatabase.java @@ -32,11 +32,179 @@ import com.TwentyCodes.android.LocationRinger.R; import com.TwentyCodes.android.LocationRinger.debug.Debug; /** - * This class will be the main interface between location ringer and it's database + * This class will be the main interface between location ringer and it's + * database + * * @author ricky barrette */ public class RingerDatabase { + /** + * A helper class to manage database creation and version management. + * + * @author ricky barrette + */ + private class OpenHelper extends SQLiteOpenHelper { + + /** + * Creates a new OpenHelper + * + * @param context + * @author ricky barrette + */ + public OpenHelper(final Context context) { + super(context, DATABASE_NAME, null, DATABASE_VERSION); + } + + /** + * Converts the database from version 2 to 3 + * + * @param db + * @author ricky barrette + */ + private void convert2to3(final SQLiteDatabase db) { + // get all the ringer information from the old table + final Cursor cursor = db.query("two", new String[] { KEY_RINGER_NAME, KEY_RINGTONE, KEY_NOTIFICATION_RINGTONE, KEY_RINGTONE_IS_SILENT, + KEY_NOTIFICATION_IS_SILENT, KEY_IS_ENABLED, KEY_RADIUS, KEY_LOCATION_LAT, KEY_LOCATION_LON, KEY_RINGTONE_URI, KEY_NOTIFICATION_RINGTONE_URI, + KEY_RINGTONE_VOLUME, KEY_NOTIFICATION_RINGTONE_VOLUME, KEY_WIFI, KEY_BT, KEY_MUSIC_VOLUME, KEY_ALARM_VOLUME }, null, null, null, null, null); + + /* + * iterate through the database moving data over to the version 3 + * tables + */ + final int count = cursor.getColumnCount(); + if (cursor.moveToFirst()) + do { + final ContentValues ringer = new ContentValues(); + if (Debug.DEBUG) + Log.v(TAG, "Converting: " + cursor.getString(0)); + for (int i = 0; i < count; i++) { + if (Debug.DEBUG) + Log.v(TAG, i + " = " + cursor.getColumnName(i) + " ~ " + cursor.getString(i)); + switch (i) { + case 0: // ringer name + ringer.put(cursor.getColumnName(i), cursor.getString(0)); + break; + case 5: // is enabled + ringer.put(KEY_IS_ENABLED, cursor.getString(i)); + break; + case 6: // radius + ringer.put(KEY_RADIUS, cursor.getString(i)); + break; + case 7: // lat + ringer.put(KEY_LOCATION_LAT, cursor.getString(i)); + break; + case 8: // lon + ringer.put(KEY_LOCATION_LON, cursor.getString(i)); + break; + default: + final ContentValues values = new ContentValues(); + values.put(KEY_RINGER_NAME, cursor.getString(0)); + values.put(KEY, cursor.getColumnName(i)); + values.put(KEY_VALUE, cursor.getString(i)); + db.insert(RINGER_INFO_TABLE, null, values); + } + } + db.insert(RINGER_TABLE, null, ringer); + } while (cursor.moveToNext()); + if (cursor != null && !cursor.isClosed()) + cursor.close(); + } + + /** + * Creates the initial database structure + * + * @param db + * @author ricky barrette + */ + private void createDatabase(final SQLiteDatabase db) { + db.execSQL("CREATE TABLE " + RINGER_TABLE + "(id INTEGER PRIMARY KEY, " + KEY_RINGER_NAME + " TEXT, " + KEY_IS_ENABLED + " TEXT)"); + db.execSQL("CREATE TABLE " + RINGER_INFO_TABLE + "(id INTEGER PRIMARY KEY, " + KEY_RINGER_NAME + " TEXT, " + KEY + " TEXT, " + KEY_VALUE + " TEXT)"); + } + + /** + * called when the database is created for the first time. this will + * create our Ringer database (non-Javadoc) + * + * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase) + * @author ricky barrette + */ + @Override + public void onCreate(final SQLiteDatabase db) { + if (Debug.DROP_TABLE_EVERY_TIME) + db.execSQL("DROP TABLE IF EXISTS " + RINGER_TABLE); + createDatabase(db); + // insert the default ringer into this table + db.execSQL("insert into " + RINGER_TABLE + "(" + KEY_RINGER_NAME + ") values ('" + mContext.getString(R.string.default_ringer) + "')"); + db.execSQL("insert into " + RINGER_INFO_TABLE + "(" + KEY_RINGER_NAME + ", " + KEY + ", " + KEY_VALUE + ") values ('" + + mContext.getString(R.string.default_ringer) + "', '" + KEY_RINGER_DESCRIPTION + "', '" + mContext.getString(R.string.about_default_ringer) + "')"); + } + + /** + * called when the database needs to be updated (non-Javadoc) + * + * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, + * int, int) + * @author ricky barrette + */ + @Override + public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) { + Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion); + + if (mListener != null) + mListener.onDatabaseUpgrade(); + + isUpgrading = true; + + // upgrade thread + new Thread(new Runnable() { + @Override + public void run() { + Looper.prepare(); + switch (oldVersion) { + case 1: + db.execSQL("ALTER TABLE " + RINGER_TABLE + " ADD " + KEY_MUSIC_VOLUME + " INTEGER"); + db.execSQL("ALTER TABLE " + RINGER_TABLE + " ADD " + KEY_ALARM_VOLUME + " INTEGER"); + case 2: + // rename the old ringer table + db.execSQL("ALTER TABLE " + RINGER_TABLE + " RENAME TO two"); + // create a new ringer table + createDatabase(db); + // convert database to the new version + convert2to3(db); + // remove old tables + db.execSQL("DROP TABLE IF EXISTS two"); + case 3: + final Cursor c = db.query(RINGER_TABLE, new String[] { "id", KEY_RINGER_NAME, KEY_LOCATION_LAT, KEY_LOCATION_LON, KEY_RADIUS }, null, null, null, + null, null); + ; + c.moveToFirst(); + if (c.moveToFirst()) + do { + if (Debug.DEBUG) + Log.d(TAG, "Moving: " + c.getInt(0) + " " + c.getString(1) + " " + c.getInt(2) + ", " + c.getInt(3) + " @ " + c.getInt(4) + "m"); + final ContentValues ringer = new ContentValues(); + final ContentValues info = new ContentValues(); + ringer.put(KEY_RINGER_NAME, c.getString(1)); + info.put(KEY_LOCATION_LAT, c.getInt(2)); + info.put(KEY_LOCATION_LON, c.getInt(3)); + info.put(KEY_RADIUS, c.getInt(4)); + updateRinger(c.getInt(0), ringer, info); + } while (c.moveToNext()); + // drop old location trigger information + db.execSQL("CREATE TABLE ringers_new (" + "id INTEGER PRIMARY KEY, " + KEY_RINGER_NAME + " TEXT, " + KEY_IS_ENABLED + " TEXT)"); + db.execSQL("INSERT INTO ringers_new SELECT id, " + KEY_RINGER_NAME + ", " + KEY_IS_ENABLED + " FROM " + RINGER_TABLE); + db.execSQL("DROP TABLE " + RINGER_TABLE); + db.execSQL("ALTER TABLE ringers_new RENAME TO " + RINGER_TABLE); + + } + mHandler.sendEmptyMessage(DATABASEUPGRADECOMPLETE); + isUpgrading = false; + } + }).start(); + } + } + private static final String TAG = "RingerDatabase"; private final Context mContext; private SQLiteDatabase mDb; @@ -44,39 +212,39 @@ public class RingerDatabase { public boolean isUpgrading = false; private final static Handler mHandler; protected static final int DELETIONCOMPLETE = 0; + protected static final int DATABASEUPGRADECOMPLETE = 1; - static{ - mHandler = new Handler(){ + static { + mHandler = new Handler() { @Override - public void handleMessage(Message msg) { - if(mListener != null) - switch(msg.what){ - case DELETIONCOMPLETE: - mListener.onRingerDeletionComplete(); - break; - case DATABASEUPGRADECOMPLETE: - mListener.onDatabaseUpgradeComplete(); + public void handleMessage(final Message msg) { + if (mListener != null) + switch (msg.what) { + case DELETIONCOMPLETE: + mListener.onRingerDeletionComplete(); + break; + case DATABASEUPGRADECOMPLETE: + mListener.onDatabaseUpgradeComplete(); break; } - } + } }; } - + /* * database information values */ - private final int DATABASE_VERSION = 4; - + private final int DATABASE_VERSION = 4; /* - * the following is for the table that holds the other table names + * the following is for the table that holds the other table names */ private final String DATABASE_NAME = "ringers.db"; private final String RINGER_TABLE = "ringers"; - private static final String RINGER_INFO_TABLE = "ringer_info"; + private static final String RINGER_INFO_TABLE = "ringer_info"; /* - * Database keys + * Database keys */ @Deprecated public final static String KEY_LOCATION_LAT = "location_lat"; @@ -88,9 +256,9 @@ public class RingerDatabase { public final static String KEY_NOTIFICATION_RINGTONE = "notification_ringtone"; @Deprecated public final static String KEY_RINGTONE_IS_SILENT = "ringtone_is_silent"; + @Deprecated public final static String KEY_NOTIFICATION_IS_SILENT = "notification_is_silent"; - public final static String KEY_IS_ENABLED = "is_enabled"; public final static String KEY_RADIUS = "radius"; public final static String KEY_RINGER_NAME = "ringer_name"; @@ -101,7 +269,7 @@ public class RingerDatabase { public final static String KEY_NOTIFICATION_RINGTONE_VOLUME = "notification_ringtone_volume"; public final static String KEY_WIFI = "wifi"; public final static String KEY_BT = "bt"; - public final static String KEY_MUSIC_VOLUME = "music_volume"; + public final static String KEY_MUSIC_VOLUME = "music_volume"; public final static String KEY_ALARM_VOLUME = "alarm_volume"; public static final String KEY_VALUE = "value"; public static final String KEY = "key"; @@ -110,455 +278,297 @@ public class RingerDatabase { public static final String KEY_DTMF_VOLUME = "dtmf_volume"; public static final String KEY_SYSTEM_VOLUME = "system_volume"; public static final String KEY_CALL_VOLUME = "call_volume"; - public static final String KEY_RINGER_DESCRIPTION = "ringer_description"; - - - /** - * A helper class to manage database creation and version management. - * @author ricky barrette - */ - private class OpenHelper extends SQLiteOpenHelper { - /** - * Creates a new OpenHelper - * @param context - * @author ricky barrette - */ - public OpenHelper(Context context) { - super(context, DATABASE_NAME, null, DATABASE_VERSION); - } - - /** - * Converts the database from version 2 to 3 - * @param db - * @author ricky barrette - */ - private void convert2to3(SQLiteDatabase db){ - //get all the ringer information from the old table - final Cursor cursor = db.query("two", new String[] { KEY_RINGER_NAME, KEY_RINGTONE, - KEY_NOTIFICATION_RINGTONE, KEY_RINGTONE_IS_SILENT, - KEY_NOTIFICATION_IS_SILENT, KEY_IS_ENABLED, - KEY_RADIUS, KEY_LOCATION_LAT, KEY_LOCATION_LON, - KEY_RINGTONE_URI, KEY_NOTIFICATION_RINGTONE_URI, - KEY_RINGTONE_VOLUME, KEY_NOTIFICATION_RINGTONE_VOLUME, - KEY_WIFI, KEY_BT, KEY_MUSIC_VOLUME, KEY_ALARM_VOLUME - }, null, null, null, null, null); - - /* - * iterate through the database moving data over to the version 3 tables - */ - int count = cursor.getColumnCount(); - if (cursor.moveToFirst()) { - do { - final ContentValues ringer = new ContentValues(); - if(Debug.DEBUG) - Log.v(TAG, "Converting: " + cursor.getString(0)); - for(int i = 0; i < count; i++){ - if(Debug.DEBUG) - Log.v(TAG, i + " = "+ cursor.getColumnName(i) +" ~ " + cursor.getString(i)); - switch(i){ - case 0: //ringer name - ringer.put(cursor.getColumnName(i), cursor.getString(0)); - break; - case 5: //is enabled - ringer.put(KEY_IS_ENABLED, cursor.getString(i)); - break; - case 6: //radius - ringer.put(KEY_RADIUS, cursor.getString(i)); - break; - case 7: // lat - ringer.put(KEY_LOCATION_LAT, cursor.getString(i)); - break; - case 8: // lon - ringer.put(KEY_LOCATION_LON, cursor.getString(i)); - break; - default: - ContentValues values = new ContentValues(); - values.put(KEY_RINGER_NAME, cursor.getString(0)); - values.put(KEY, cursor.getColumnName(i)); - values.put(KEY_VALUE, cursor.getString(i)); - db.insert(RINGER_INFO_TABLE, null, values); - } - } - db.insert(RINGER_TABLE, null, ringer); - } while (cursor.moveToNext()); - } - if (cursor != null && !cursor.isClosed()) { - cursor.close(); - } - } - - /** - * Creates the initial database structure - * @param db - * @author ricky barrette - */ - private void createDatabase(SQLiteDatabase db){ - db.execSQL("CREATE TABLE " + RINGER_TABLE + - "(id INTEGER PRIMARY KEY, " + - KEY_RINGER_NAME+" TEXT, " + - KEY_IS_ENABLED+" TEXT)"); - db.execSQL("CREATE TABLE " + RINGER_INFO_TABLE + - "(id INTEGER PRIMARY KEY, " + - KEY_RINGER_NAME+" TEXT, " + - KEY+" TEXT, " + - KEY_VALUE+" TEXT)"); - } - - /** - * called when the database is created for the first time. this will create our Ringer database - * (non-Javadoc) - * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase) - * @author ricky barrette - */ - @Override - public void onCreate(SQLiteDatabase db) { - if(Debug.DROP_TABLE_EVERY_TIME) - db.execSQL("DROP TABLE IF EXISTS " + RINGER_TABLE); - createDatabase(db); - //insert the default ringer into this table - db.execSQL("insert into " + RINGER_TABLE + "(" + KEY_RINGER_NAME + ") values ('"+RingerDatabase.this.mContext.getString(R.string.default_ringer)+"')"); - db.execSQL("insert into " + RINGER_INFO_TABLE + "(" + KEY_RINGER_NAME + ", " + KEY + ", " + KEY_VALUE + - ") values ('"+RingerDatabase.this.mContext.getString(R.string.default_ringer) - + "', '" + KEY_RINGER_DESCRIPTION - + "', '" + RingerDatabase.this.mContext.getString(R.string.about_default_ringer)+"')"); - } - - /** - * called when the database needs to be updated - * (non-Javadoc) - * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int) - * @author ricky barrette - */ - @Override - public void onUpgrade(final SQLiteDatabase db, final int oldVersion, int newVersion) { - Log.w(TAG, "Upgrading database from version "+oldVersion+" to "+newVersion); - - if(mListener != null) - mListener.onDatabaseUpgrade(); - - RingerDatabase.this.isUpgrading = true; - - //upgrade thread - new Thread( new Runnable(){ - @Override - public void run(){ - Looper.prepare(); - switch(oldVersion){ - case 1: - db.execSQL("ALTER TABLE " + RINGER_TABLE + " ADD "+ KEY_MUSIC_VOLUME+" INTEGER"); - db.execSQL("ALTER TABLE " + RINGER_TABLE + " ADD "+ KEY_ALARM_VOLUME+" INTEGER"); - case 2: - //rename the old ringer table - db.execSQL("ALTER TABLE " + RINGER_TABLE + " RENAME TO two"); - //create a new ringer table - createDatabase(db); - //convert database to the new version - convert2to3(db); - //remove old tables - db.execSQL("DROP TABLE IF EXISTS two"); - case 3: - Cursor c = db.query(RINGER_TABLE, new String[] { "id", KEY_RINGER_NAME, KEY_LOCATION_LAT, KEY_LOCATION_LON, KEY_RADIUS }, null, null, null, null, null);; - c.moveToFirst(); - if (c.moveToFirst()) { - do { - if(Debug.DEBUG) - Log.d(TAG, "Moving: "+c.getInt(0)+" "+c.getString(1)+" "+c.getInt(2)+", "+c.getInt(3)+" @ "+ c.getInt(4) +"m"); - ContentValues ringer = new ContentValues(); - ContentValues info = new ContentValues(); - ringer.put(KEY_RINGER_NAME, c.getString(1)); - info.put(KEY_LOCATION_LAT, c.getInt(2)); - info.put(KEY_LOCATION_LON, c.getInt(3)); - info.put(KEY_RADIUS, c.getInt(4)); - updateRinger(c.getInt(0), ringer, info); - } while (c.moveToNext()); - } - //drop old location trigger information - db.execSQL("CREATE TABLE ringers_new (" + - "id INTEGER PRIMARY KEY, " + - KEY_RINGER_NAME+" TEXT, " + - KEY_IS_ENABLED+" TEXT)"); - db.execSQL("INSERT INTO ringers_new SELECT id, "+ KEY_RINGER_NAME +", "+ KEY_IS_ENABLED +" FROM "+RINGER_TABLE); - db.execSQL("DROP TABLE "+ RINGER_TABLE); - db.execSQL("ALTER TABLE ringers_new RENAME TO "+ RINGER_TABLE); - - } - mHandler.sendEmptyMessage(DATABASEUPGRADECOMPLETE); - RingerDatabase.this.isUpgrading = false; - } - }).start(); - } - } - - /** - * Creates a new RingerDatabase - * @param context - * @author ricky barrette - */ - public RingerDatabase(Context context){ - this(context, null); - } - - /** - * Creates a new RingerDatabase - * @param context - * @param listener - * @author ricky barrette - */ - public RingerDatabase(Context context, DatabaseListener listener){ - mListener = listener; - this.mContext = context; - this.mDb = new OpenHelper(this.mContext).getWritableDatabase(); - } + public static final String KEY_RINGER_DESCRIPTION = "ringer_description"; /** * Parses a string boolean from the database + * * @param bool * @return true or false * @author ricky barrette */ - public static boolean parseBoolean(String bool){ + public static boolean parseBoolean(final String bool) { try { return bool == null ? false : Integer.parseInt(bool) == 1 ? true : false; - } catch (NumberFormatException e) { + } catch (final NumberFormatException e) { return false; } } - + + /** + * Creates a new RingerDatabase + * + * @param context + * @author ricky barrette + */ + public RingerDatabase(final Context context) { + this(context, null); + } + + /** + * Creates a new RingerDatabase + * + * @param context + * @param listener + * @author ricky barrette + */ + public RingerDatabase(final Context context, final DatabaseListener listener) { + mListener = listener; + mContext = context; + mDb = new OpenHelper(mContext).getWritableDatabase(); + } + /** * Backs up the database + * * @return true if successful * @author ricky barrette */ - public boolean backup(){ - final File dbFile = new File(Environment.getDataDirectory() + "/data/"+mContext.getPackageName()+"/databases/"+DATABASE_NAME); + public boolean backup() { + final File dbFile = new File(Environment.getDataDirectory() + "/data/" + mContext.getPackageName() + "/databases/" + DATABASE_NAME); - final File exportDir = new File(Environment.getExternalStorageDirectory(), "/"+this.mContext.getString(R.string.app_name)); - if (!exportDir.exists()) { + final File exportDir = new File(Environment.getExternalStorageDirectory(), "/" + mContext.getString(R.string.app_name)); + if (!exportDir.exists()) exportDir.mkdirs(); - } - + final File file = new File(exportDir, dbFile.getName()); try { file.createNewFile(); - this.copyFile(dbFile, file); + copyFile(dbFile, file); return true; - } catch (IOException e) { + } catch (final IOException e) { e.printStackTrace(); return false; } } - + /** * Checks to see if this ringer name is original, if not it renames it + * * @param name * @return */ - private String checkRingerName(final String name){ - - final List names = this.getAllRingerTitles(); + private String checkRingerName(final String name) { + + final List names = getAllRingerTitles(); String ringerName = name; int count = 1; - - for(int index = 0; index < names.size(); index++ ){ - if(ringerName.equals(names.get(index))){ - ringerName = name + count+++""; - index = 0; - } - } + + for (int index = 0; index < names.size(); index++) + if (ringerName.equals(names.get(index))) { + ringerName = name + count++ + ""; + index = 0; + } return ringerName; } - + /** * Copies a file - * @param src file - * @param dst file + * + * @param src + * file + * @param dst + * file * @throws IOException * @author ricky barrette */ private void copyFile(final File src, final File dst) throws IOException { - final FileChannel inChannel = new FileInputStream(src).getChannel(); - final FileChannel outChannel = new FileOutputStream(dst).getChannel(); - try { - inChannel.transferTo(0, inChannel.size(), outChannel); - } finally { - if (inChannel != null) - inChannel.close(); - if (outChannel != null) - outChannel.close(); - } - } + final FileInputStream in = new FileInputStream(src); + final FileOutputStream out = new FileOutputStream(dst); + final FileChannel inChannel = in.getChannel(); + final FileChannel outChannel = out.getChannel(); + try { + inChannel.transferTo(0, inChannel.size(), outChannel); + } finally { + if (inChannel != null) + inChannel.close(); + if (outChannel != null) + outChannel.close(); + if (in != null) + in.close(); + if (out != null) + out.close(); + + } + } /** * deletes a note by its row id + * * @param id * @author ricky barrette */ public void deleteRinger(final long id) { - + final ProgressDialog progress = ProgressDialog.show(RingerDatabase.this.mContext, "", RingerDatabase.this.mContext.getText(R.string.deleteing), true, true); - - //ringer deleting thread - new Thread( new Runnable(){ - @Override - public void run(){ - Looper.prepare(); - + + // ringer deleting thread + new Thread(new Runnable() { + @Override + public void run() { + Looper.prepare(); + /* - * get the ringer name from the id, and then delete all its information from the ringer information table + * get the ringer name from the id, and then delete all its + * information from the ringer information table */ - RingerDatabase.this.mDb.delete(RINGER_INFO_TABLE, KEY_RINGER_NAME +" = "+ DatabaseUtils.sqlEscapeString(RingerDatabase.this.getRingerName(id)), null); - + mDb.delete(RINGER_INFO_TABLE, KEY_RINGER_NAME + " = " + DatabaseUtils.sqlEscapeString(RingerDatabase.this.getRingerName(id)), null); + /* * finally delete the ringer from the ringer table */ - RingerDatabase.this.mDb.delete(RINGER_TABLE, "id = "+ id, null); - updateRowIds(id +1); + mDb.delete(RINGER_TABLE, "id = " + id, null); + updateRowIds(id + 1); mHandler.sendEmptyMessage(DELETIONCOMPLETE); progress.dismiss(); - } - }).start(); + } + }).start(); } /** - * @return a cursor containing all ringers - * @author ricky barrette - */ - public Cursor getAllRingers(){ - return this.mDb.query(RINGER_TABLE, new String[] { KEY_RINGER_NAME, KEY_IS_ENABLED }, null, null, null, null, null); - } - - /** - * returns all ringer names in the database, where or not if they are enabled - * @return list of all strings in the database table - * @author ricky barrette - */ - public List getAllRingerTitles() { - final List list = new ArrayList(); - final Cursor cursor = this.mDb.query(RINGER_TABLE, new String[] { KEY_RINGER_NAME }, null, null, null, null, null); - if (cursor.moveToFirst()) { - do { - list.add(cursor.getString(0)); - } while (cursor.moveToNext()); - } - if (cursor != null && !cursor.isClosed()) { - cursor.close(); - } - return list; - } - - /** - * returns all ringer descriptions in the database, where or not if they are enabled + * returns all ringer descriptions in the database, where or not if they are + * enabled + * * @return list of all strings in the database table * @author ricky barrette */ public List getAllRingerDescriptions() { final List list = new ArrayList(); final List ringers = getAllRingerTitles(); - for(String ringer: ringers){ + for (final String ringer : ringers) list.add(getRingerInfo(ringer).getAsString(KEY_RINGER_DESCRIPTION)); - } return list; } - + + /** + * @return a cursor containing all ringers + * @author ricky barrette + */ + public Cursor getAllRingers() { + return mDb.query(RINGER_TABLE, new String[] { KEY_RINGER_NAME, KEY_IS_ENABLED }, null, null, null, null, null); + } + + /** + * returns all ringer names in the database, where or not if they are + * enabled + * + * @return list of all strings in the database table + * @author ricky barrette + */ + public List getAllRingerTitles() { + final List list = new ArrayList(); + final Cursor cursor = mDb.query(RINGER_TABLE, new String[] { KEY_RINGER_NAME }, null, null, null, null, null); + if (cursor.moveToFirst()) + do + list.add(cursor.getString(0)); + while (cursor.moveToNext()); + if (cursor != null && !cursor.isClosed()) + cursor.close(); + return list; + } + /** * gets a ringer from a row id; + * * @param id * @return cursor containing the note * @author ricky barrette */ - public Cursor getRingerFromId(long id) { - return this.mDb.query(RINGER_TABLE, new String[]{ KEY_RINGER_NAME, KEY_IS_ENABLED }, "id = "+id, null, null, null, null); + public Cursor getRingerFromId(final long id) { + return mDb.query(RINGER_TABLE, new String[] { KEY_RINGER_NAME, KEY_IS_ENABLED }, "id = " + id, null, null, null, null); } /** * gets a ringer's info from the supplied ringer name + * * @param ringerName * @return * @author ricky barrette */ - public ContentValues getRingerInfo(final String ringerName){ + public ContentValues getRingerInfo(final String ringerName) { final ContentValues values = new ContentValues(); - final Cursor info = this.mDb.query(RINGER_INFO_TABLE, new String[]{ KEY, KEY_VALUE }, KEY_RINGER_NAME +" = "+ DatabaseUtils.sqlEscapeString(ringerName), null, null, null, null); - if (info.moveToFirst()) { - do { + final Cursor info = mDb.query(RINGER_INFO_TABLE, new String[] { KEY, KEY_VALUE }, KEY_RINGER_NAME + " = " + DatabaseUtils.sqlEscapeString(ringerName), null, + null, null, null); + if (info.moveToFirst()) + do values.put(info.getString(0), info.getString(1)); - } while (info.moveToNext()); - } - if (info != null && !info.isClosed()) { + while (info.moveToNext()); + if (info != null && !info.isClosed()) info.close(); - } return values; } - + /** * Retrieves the ringer's name form the ringer table + * * @param id * @return ringer's name * @author ricky barrette */ public String getRingerName(final long id) { - String name = null; - final Cursor cursor = this.mDb.query(RINGER_TABLE, new String[]{ KEY_RINGER_NAME }, "id = "+id, null, null, null, null);; - if (cursor.moveToFirst()) { + String name = null; + final Cursor cursor = mDb.query(RINGER_TABLE, new String[] { KEY_RINGER_NAME }, "id = " + id, null, null, null, null); + ; + if (cursor.moveToFirst()) name = cursor.getString(0); - } - if (cursor != null && !cursor.isClosed()) { + if (cursor != null && !cursor.isClosed()) cursor.close(); - } return name; } - + /** * Inserts a new ringer into the database - * @param ringer values - * @param ringerInfo values + * + * @param ringer + * values + * @param ringerInfo + * values * @author ricky barrette */ - public void insertRinger(final ContentValues ringer, final ContentValues ringerInfo){ + public void insertRinger(final ContentValues ringer, final ContentValues ringerInfo) { ringer.put(RingerDatabase.KEY_RINGER_NAME, checkRingerName(ringer.getAsString(RingerDatabase.KEY_RINGER_NAME))); mDb.insert(RINGER_TABLE, null, ringer); final String ringerName = ringer.getAsString(RingerDatabase.KEY_RINGER_NAME); - - //insert the information values - for(final Entry item : ringerInfo.valueSet()){ + + // insert the information values + for (final Entry item : ringerInfo.valueSet()) { final ContentValues values = new ContentValues(); values.put(KEY_RINGER_NAME, ringerName); values.put(KEY, item.getKey()); /* - * Try get the value. - * If there is a class cast exception, try casting to the next object type. + * Try get the value. If there is a class cast exception, try + * casting to the next object type. * - * The following types are tried: - * String - * Integer - * Boolean + * The following types are tried: String Integer Boolean */ try { values.put(KEY_VALUE, (String) item.getValue()); - } catch (ClassCastException e) { + } catch (final ClassCastException e) { try { values.put(KEY_VALUE, (Boolean) item.getValue() ? 1 : 0); - } catch (ClassCastException e1) { + } catch (final ClassCastException e1) { values.put(KEY_VALUE, (Integer) item.getValue()); } } mDb.insert(RINGER_INFO_TABLE, null, values); } } - + /** * Checks to see if a ringer is enabled - * @param row id + * + * @param row + * id * @return true if the ringer is enabled * @author ricky barrette */ public boolean isRingerEnabled(final long id) { - final Cursor cursor = this.mDb.query(RINGER_TABLE, new String[] { KEY_IS_ENABLED }, "id = "+id, null, null, null, null); + final Cursor cursor = mDb.query(RINGER_TABLE, new String[] { KEY_IS_ENABLED }, "id = " + id, null, null, null, null); if (cursor.moveToFirst()) { - if(Debug.DEBUG) - Log.d(TAG, "isRingerEnabled("+id+") = "+ cursor.getString(0)); + if (Debug.DEBUG) + Log.d(TAG, "isRingerEnabled(" + id + ") = " + cursor.getString(0)); return parseBoolean(cursor.getString(0)); } return false; @@ -566,128 +576,131 @@ public class RingerDatabase { /** * Restores the database from the sdcard + * * @return true if successful * @author ricky barrette */ - public void restore(){ - final File dbFile = new File(Environment.getDataDirectory() + "/data/"+mContext.getPackageName()+"/databases/"+DATABASE_NAME); + public void restore() { + final File dbFile = new File(Environment.getDataDirectory() + "/data/" + mContext.getPackageName() + "/databases/" + DATABASE_NAME); - final File exportDir = new File(Environment.getExternalStorageDirectory(), "/"+this.mContext.getString(R.string.app_name)); - if (!exportDir.exists()) { + final File exportDir = new File(Environment.getExternalStorageDirectory(), "/" + mContext.getString(R.string.app_name)); + if (!exportDir.exists()) exportDir.mkdirs(); - } - + final File file = new File(exportDir, dbFile.getName()); try { file.createNewFile(); - this.copyFile(file, dbFile); - } catch (IOException e) { + copyFile(file, dbFile); + } catch (final IOException e) { e.printStackTrace(); } /* * close and reopen the database to upgrade it. */ - this.mDb.close(); - this.mDb = new OpenHelper(this.mContext).getWritableDatabase(); - if(this.mDb.isOpen() && ! this.isUpgrading) - if(mListener != null) + mDb.close(); + mDb = new OpenHelper(mContext).getWritableDatabase(); + if (mDb.isOpen() && !isUpgrading) + if (mListener != null) mListener.onRestoreComplete(); } public int setRingerEnabled(final long id, final boolean enabled) { - if(Debug.DEBUG) - Log.d(TAG, "setRingerEnabled("+id+") = "+ enabled); + if (Debug.DEBUG) + Log.d(TAG, "setRingerEnabled(" + id + ") = " + enabled); final ContentValues values = new ContentValues(); values.put(KEY_IS_ENABLED, enabled); - return mDb.update(RINGER_TABLE, values, "id" + "= "+ id, null); + return mDb.update(RINGER_TABLE, values, "id" + "= " + id, null); } /** * updates a ringer by it's id + * * @param id - * @param ringer values - * @param info values + * @param ringer + * values + * @param info + * values * @author ricky barrette */ - public void updateRinger(final long id, final ContentValues ringer, final ContentValues info) throws NullPointerException{ - - if(ringer == null || info == null) + public void updateRinger(final long id, final ContentValues ringer, final ContentValues info) throws NullPointerException { + + if (ringer == null || info == null) throw new NullPointerException("ringer content was null"); - + final String ringer_name = getRingerName(id); - + /* - * here we retrive the old values. - * we will compare the old value against the new values. - * we will delete all values that are NOT included in the new values. + * here we retrive the old values. we will compare the old value against + * the new values. we will delete all values that are NOT included in + * the new values. */ final ContentValues old = getRingerInfo(ringer_name); - for(final Entry item : info.valueSet()){ - if(old.containsKey(item.getKey())) + for (final Entry item : info.valueSet()) + if (old.containsKey(item.getKey())) old.remove(item.getKey()); - } - for(final Entry item : old.valueSet()){ - RingerDatabase.this.mDb.delete(RINGER_INFO_TABLE, KEY +" = "+ DatabaseUtils.sqlEscapeString(item.getKey()) +" and "+ KEY_RINGER_NAME +" = "+DatabaseUtils.sqlEscapeString(ringer_name), null); - } - + for (final Entry item : old.valueSet()) + RingerDatabase.this.mDb.delete(RINGER_INFO_TABLE, KEY + " = " + DatabaseUtils.sqlEscapeString(item.getKey()) + " and " + KEY_RINGER_NAME + " = " + + DatabaseUtils.sqlEscapeString(ringer_name), null); + /* * here we want to update the ringer name if needed */ - if(!ringer_name.equals(ringer.getAsString(RingerDatabase.KEY_RINGER_NAME))) + if (!ringer_name.equals(ringer.getAsString(RingerDatabase.KEY_RINGER_NAME))) ringer.put(RingerDatabase.KEY_RINGER_NAME, checkRingerName(ringer.getAsString(RingerDatabase.KEY_RINGER_NAME))); - + /* * here we wanr to update the information values in the info table */ - for(final Entry item : info.valueSet()){ + for (final Entry item : info.valueSet()) { final ContentValues values = new ContentValues(); values.put(KEY_RINGER_NAME, ringer.getAsString(KEY_RINGER_NAME)); values.put(KEY, item.getKey()); try { values.put(KEY_VALUE, (String) item.getValue()); - } catch (ClassCastException e) { + } catch (final ClassCastException e) { try { values.put(KEY_VALUE, (Boolean) item.getValue() ? 1 : 0); - } catch (ClassCastException e1) { + } catch (final ClassCastException e1) { values.put(KEY_VALUE, (Integer) item.getValue()); } } - + /* - * here we are going to try to update a row, - * if that fails we will insert a row insead + * here we are going to try to update a row, if that fails we will + * insert a row insead */ - if(!(mDb.update(RINGER_INFO_TABLE, values, KEY_RINGER_NAME + "="+ DatabaseUtils.sqlEscapeString(ringer_name) +" AND " + KEY +"='"+ item.getKey()+"'", null) > 0)) + if (!(mDb.update(RINGER_INFO_TABLE, values, KEY_RINGER_NAME + "=" + DatabaseUtils.sqlEscapeString(ringer_name) + " AND " + KEY + "='" + item.getKey() + "'", + null) > 0)) mDb.insert(RINGER_INFO_TABLE, null, values); } - - //update the ringer table - mDb.update(RINGER_TABLE, ringer, "id" + "= "+ id, null); + + // update the ringer table + mDb.update(RINGER_TABLE, ringer, "id" + "= " + id, null); } /** * Updates the row ids after a row is deleted - * @param id of the row to start with + * + * @param id + * of the row to start with * @author ricky barrette */ private void updateRowIds(long id) { long currentRow; final ContentValues values = new ContentValues(); - final Cursor cursor = this.mDb.query(RINGER_TABLE, new String[] { "id" },null, null, null, null, null); - if (cursor.moveToFirst()) { + final Cursor cursor = mDb.query(RINGER_TABLE, new String[] { "id" }, null, null, null, null, null); + if (cursor.moveToFirst()) do { currentRow = cursor.getLong(0); - if(currentRow == id){ + if (currentRow == id) { id++; values.clear(); - values.put("id", currentRow -1); - mDb.update(RINGER_TABLE, values, "id" + "= "+ currentRow, null); + values.put("id", currentRow - 1); + mDb.update(RINGER_TABLE, values, "id" + "= " + currentRow, null); } } while (cursor.moveToNext()); - } - if (cursor != null && !cursor.isClosed()) { + if (cursor != null && !cursor.isClosed()) cursor.close(); - } } } \ No newline at end of file diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/debug/Debug.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/debug/Debug.java index b8afbea..55d0d0e 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/debug/Debug.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/debug/Debug.java @@ -11,60 +11,63 @@ import android.content.Context; /** * A convince class containing debugging variables - * @author ricky barrette */ + * + * @author ricky barrette + */ public class Debug { - + public static final boolean SUPPORTS_FROYO; public static final boolean SUPPORTS_GINGERBREAD; public static final boolean SUPPORTS_HONEYCOMB; - + public static final int SHARED_PREFS_MODE; - /** * Sets the logging output of this application */ public static final boolean DEBUG = true; /** - * The amount of intersecting that is needed between a users accuracy radius and a ringers location radius + * The amount of intersecting that is needed between a users accuracy radius + * and a ringers location radius */ public static final float FUDGE_FACTOR = .002f; - + /** * Drops the ringer database table every time the database is created */ public static boolean DROP_TABLE_EVERY_TIME = false; - + /** * Max radius that can be set by a ringer */ public static final int MAX_RADIUS_IN_METERS = 600; - + /** * the update interval in ms */ public static final long UPDATE_INTERVAL = AlarmManager.INTERVAL_FIFTEEN_MINUTES; - + /** * minum accracy required to report in meters */ public static final int ACCURACY = 100; - + /** - * all lolcations with an accuracy greater then this will be ignored. in meters + * all lolcations with an accuracy greater then this will be ignored. in + * meters */ public static final int IGNORE = 500; - static{ + static { SUPPORTS_FROYO = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO; - + SUPPORTS_GINGERBREAD = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD; - + SUPPORTS_HONEYCOMB = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB; - + SHARED_PREFS_MODE = SUPPORTS_HONEYCOMB ? Context.MODE_MULTI_PROCESS : Context.MODE_PRIVATE; } } \ No newline at end of file diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/receivers/GetLocationWidget.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/receivers/GetLocationWidget.java index d777072..1422565 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/receivers/GetLocationWidget.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/receivers/GetLocationWidget.java @@ -21,7 +21,9 @@ import com.TwentyCodes.android.LocationRinger.services.LocationService; import com.TwentyCodes.android.LocationRinger.ui.SettingsActivity; /** - * This widget will be used to force a Location update from the users home screen + * This widget will be used to force a Location update from the users home + * screen + * * @author ricky barrette */ public class GetLocationWidget extends AppWidgetProvider { @@ -29,87 +31,101 @@ public class GetLocationWidget extends AppWidgetProvider { public final String TAG = "GetLocationWidget"; public static final String ACTION_UPDATE = "action_update"; - + /** - * Called in response to the ACTION_APPWIDGET_UPDATE broadcast when this AppWidget provider is being asked to provide RemoteViews for a set of AppWidgets. - * Override this method to implement your own AppWidget functionality. - * @see android.appwidget.AppWidgetProvider#onUpdate(android.content.Context, android.appwidget.AppWidgetManager, int[]) + * Called in response to the ACTION_APPWIDGET_DELETED broadcast when one or + * more AppWidget instances have been deleted. Override this method to + * implement your own AppWidget functionality. (non-Javadoc) + * + * @see android.appwidget.AppWidgetProvider#onDeleted(android.content.Context, + * int[]) + * @param context + * @param appWidgetIds + * @author ricky barrette + */ + @Override + public void onDeleted(final Context context, final int[] appWidgetIds) { + if (Debug.DEBUG) + Log.v(TAG, "onDelete()"); + super.onDeleted(context, appWidgetIds); + } + + /** + * Implements onReceive(Context, Intent) to dispatch calls to the various + * other methods on AppWidgetProvider. (non-Javadoc) + * + * @see android.appwidget.AppWidgetProvider#onReceive(android.content.Context, + * android.content.Intent) + * @param context + * @param intent + * received + * @author ricky barrette + */ + @Override + public void onReceive(final Context context, final Intent intent) { + if (Debug.DEBUG) + Log.v(TAG, "onReceive"); + // v1.5 fix that doesn't call onDelete Action + final String action = intent.getAction(); + + if (action.equals(ACTION_UPDATE)) { + final AppWidgetManager mgr = AppWidgetManager.getInstance(context); + onUpdate(context, mgr, mgr.getAppWidgetIds(new ComponentName(context, GetLocationWidget.class))); + } + + if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) { + final int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); + if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) + onDeleted(context, new int[] { appWidgetId }); + } + super.onReceive(context, intent); + } + + /** + * Called in response to the ACTION_APPWIDGET_UPDATE broadcast when this + * AppWidget provider is being asked to provide RemoteViews for a set of + * AppWidgets. Override this method to implement your own AppWidget + * functionality. + * + * @see android.appwidget.AppWidgetProvider#onUpdate(android.content.Context, + * android.appwidget.AppWidgetManager, int[]) * @param context * @param appWidgetManager * @param appWidgetIds * @author ricky barrette */ @Override - public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) { if (Debug.DEBUG) Log.v(TAG, "onUpdate()"); final int N = appWidgetIds.length; - // Perform this loop procedure for each App Widget that belongs to this provider - for (int i=0; i item : ringer.valueSet()) + + if (Debug.DEBUG) + for (final Entry item : ringer.valueSet()) Log.d(TAG, item.getKey()); - + applyRinger(ringer); - - if(Debug.DEBUG) + + if (Debug.DEBUG) Log.d(TAG, "Finished processing ringers"); - - //store is default - this.mSettings.edit().putBoolean(SettingsActivity.IS_DEFAULT, isDeafult).commit(); - + + // store is default + mSettings.edit().putBoolean(SettingsActivity.IS_DEFAULT, isDeafult).commit(); + this.stopSelf(mStartId); } /** * set the volume of a particular stream + * * @param volume * @param stream * @author ricky barrette */ - private void setStreamVolume(int volume, int stream) { + private void setStreamVolume(final int volume, final int stream) { /* - * if the seek bar is set to a value that is higher than what the the stream value is set for - * then subtract the seek bar's value from the current volume of the stream, and then - * raise the stream by that many times + * if the seek bar is set to a value that is higher than what the the + * stream value is set for then subtract the seek bar's value from the + * current volume of the stream, and then raise the stream by that many + * times */ if (volume > mAudioManager.getStreamVolume(stream)) { - int adjust = volume - mAudioManager.getStreamVolume(stream); - for (int i = 0; i < adjust; i++) { + final int adjust = volume - mAudioManager.getStreamVolume(stream); + for (int i = 0; i < adjust; i++) mAudioManager.adjustSuggestedStreamVolume(AudioManager.ADJUST_RAISE, stream, 0); - } } - + /* - * if the seek bar is set to a value that is lower than what the the stream value is set for - * then subtract the current volume of the stream from the seek bar's value, and then - * lower the stream by that many times + * if the seek bar is set to a value that is lower than what the the + * stream value is set for then subtract the current volume of the + * stream from the seek bar's value, and then lower the stream by that + * many times */ if (volume < mAudioManager.getStreamVolume(stream)) { - int adjust = mAudioManager.getStreamVolume(stream) - volume; - for (int i = 0; i < adjust; i++) { + final int adjust = mAudioManager.getStreamVolume(stream) - volume; + for (int i = 0; i < adjust; i++) mAudioManager.adjustSuggestedStreamVolume(AudioManager.ADJUST_LOWER, stream, 0); - } - } + } } } \ No newline at end of file diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/CheckLocationShortcut.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/CheckLocationShortcut.java index 44f70b9..a45e525 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/CheckLocationShortcut.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/CheckLocationShortcut.java @@ -29,103 +29,113 @@ import com.TwentyCodes.android.LocationRinger.R; import com.TwentyCodes.android.LocationRinger.services.LocationService; /** - * This Activity actually handles two stages of a launcher shortcut's life cycle. + * This Activity actually handles two stages of a launcher shortcut's life + * cycle. + * + * 1. Your application offers to provide shortcuts to the launcher. When the + * user installs a shortcut, an activity within your application generates the + * actual shortcut and returns it to the launcher, where it is shown to the user + * as an icon. * - * 1. Your application offers to provide shortcuts to the launcher. When - * the user installs a shortcut, an activity within your application - * generates the actual shortcut and returns it to the launcher, where it - * is shown to the user as an icon. - * * 2. Any time the user clicks on an installed shortcut, an intent is sent. - * Typically this would then be handled as necessary by an activity within - * your application. - * - * We handle stage 1 (creating a shortcut) by simply sending back the information (in the form - * of an {@link android.content.Intent} that the launcher will use to create the shortcut. + * Typically this would then be handled as necessary by an activity within your + * application. * - * You can also implement this in an interactive way, by having your activity actually present - * UI for the user to select the specific nature of the shortcut, such as a contact, picture, URL, - * media item, or action. + * We handle stage 1 (creating a shortcut) by simply sending back the + * information (in the form of an {@link android.content.Intent} that the + * launcher will use to create the shortcut. * - * We handle stage 2 (responding to a shortcut) in this sample by simply displaying the contents - * of the incoming {@link android.content.Intent}. + * You can also implement this in an interactive way, by having your activity + * actually present UI for the user to select the specific nature of the + * shortcut, such as a contact, picture, URL, media item, or action. * - * In a real application, you would probably use the shortcut intent to display specific content - * or start a particular operation. + * We handle stage 2 (responding to a shortcut) in this sample by simply + * displaying the contents of the incoming {@link android.content.Intent}. + * + * In a real application, you would probably use the shortcut intent to display + * specific content or start a particular operation. */ public class CheckLocationShortcut extends Activity { - @Override - public void onCreate(Bundle icicle) { - super.onCreate(icicle); + @Override + public void onCreate(final Bundle icicle) { + super.onCreate(icicle); - // Resolve the intent + // Resolve the intent - final Intent intent = getIntent(); - final String action = intent.getAction(); + final Intent intent = getIntent(); + final String action = intent.getAction(); - // If the intent is a request to create a shortcut, we'll do that and exit + // If the intent is a request to create a shortcut, we'll do that and + // exit - if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) { - setupShortcut(); - finish(); - return; - } - - /* - * start the location service in single shot mode - */ - LocationService.startSingleShotService(this); - - this.finish(); - } + if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) { + setupShortcut(); + finish(); + return; + } - /** - * This function creates a shortcut and returns it to the caller. There are actually two - * intents that you will send back. - * - * The first intent serves as a container for the shortcut and is returned to the launcher by - * setResult(). This intent must contain three fields: - * - *
    - *
  • {@link android.content.Intent#EXTRA_SHORTCUT_INTENT} The shortcut intent.
  • - *
  • {@link android.content.Intent#EXTRA_SHORTCUT_NAME} The text that will be displayed with - * the shortcut.
  • - *
  • {@link android.content.Intent#EXTRA_SHORTCUT_ICON} The shortcut's icon, if provided as a - * bitmap, or {@link android.content.Intent#EXTRA_SHORTCUT_ICON_RESOURCE} if provided as - * a drawable resource.
  • - *
- * - * If you use a simple drawable resource, note that you must wrapper it using - * {@link android.content.Intent.ShortcutIconResource}, as shown below. This is required so - * that the launcher can access resources that are stored in your application's .apk file. If - * you return a bitmap, such as a thumbnail, you can simply put the bitmap into the extras - * bundle using {@link android.content.Intent#EXTRA_SHORTCUT_ICON}. - * - * The shortcut intent can be any intent that you wish the launcher to send, when the user - * clicks on the shortcut. Typically this will be {@link android.content.Intent#ACTION_VIEW} - * with an appropriate Uri for your content, but any Intent will work here as long as it - * triggers the desired action within your Activity. - */ - private void setupShortcut() { - // First, set up the shortcut intent. For this example, we simply create an intent that - // will bring us directly back to this activity. A more typical implementation would use a - // data Uri in order to display a more specific result, or a custom action in order to - // launch a specific operation. + /* + * start the location service in single shot mode + */ + LocationService.startSingleShotService(this); - Intent shortcutIntent = new Intent(Intent.ACTION_MAIN); - shortcutIntent.setClassName(this, this.getClass().getName()); + finish(); + } - // Then, set up the container intent (the response to the caller) + /** + * This function creates a shortcut and returns it to the caller. There are + * actually two intents that you will send back. + * + * The first intent serves as a container for the shortcut and is returned + * to the launcher by setResult(). This intent must contain three fields: + * + *
    + *
  • {@link android.content.Intent#EXTRA_SHORTCUT_INTENT} The shortcut + * intent.
  • + *
  • {@link android.content.Intent#EXTRA_SHORTCUT_NAME} The text that will + * be displayed with the shortcut.
  • + *
  • {@link android.content.Intent#EXTRA_SHORTCUT_ICON} The shortcut's + * icon, if provided as a bitmap, or + * {@link android.content.Intent#EXTRA_SHORTCUT_ICON_RESOURCE} if provided + * as a drawable resource.
  • + *
+ * + * If you use a simple drawable resource, note that you must wrapper it + * using {@link android.content.Intent.ShortcutIconResource}, as shown + * below. This is required so that the launcher can access resources that + * are stored in your application's .apk file. If you return a bitmap, such + * as a thumbnail, you can simply put the bitmap into the extras bundle + * using {@link android.content.Intent#EXTRA_SHORTCUT_ICON}. + * + * The shortcut intent can be any intent that you wish the launcher to send, + * when the user clicks on the shortcut. Typically this will be + * {@link android.content.Intent#ACTION_VIEW} with an appropriate Uri for + * your content, but any Intent will work here as long as it triggers the + * desired action within your Activity. + */ + private void setupShortcut() { + // First, set up the shortcut intent. For this example, we simply create + // an intent that + // will bring us directly back to this activity. A more typical + // implementation would use a + // data Uri in order to display a more specific result, or a custom + // action in order to + // launch a specific operation. - Intent intent = new Intent(); - intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); - intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.start_location_service)); - Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon); - intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); + final Intent shortcutIntent = new Intent(Intent.ACTION_MAIN); + shortcutIntent.setClassName(this, this.getClass().getName()); - // Now, return the result to the launcher + // Then, set up the container intent (the response to the caller) - setResult(RESULT_OK, intent); - } + final Intent intent = new Intent(); + intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); + intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.start_location_service)); + final Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon); + intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); + + // Now, return the result to the launcher + + setResult(RESULT_OK, intent); + } } \ No newline at end of file diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/FirstBootDialog.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/FirstBootDialog.java index 4385d2c..c2c4291 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/FirstBootDialog.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/FirstBootDialog.java @@ -6,74 +6,78 @@ */ package com.TwentyCodes.android.LocationRinger.ui; -import com.TwentyCodes.android.LocationRinger.R; -import com.TwentyCodes.android.LocationRinger.debug.Debug; - import android.app.Dialog; import android.content.Context; import android.view.View; import android.view.Window; +import com.TwentyCodes.android.LocationRinger.R; +import com.TwentyCodes.android.LocationRinger.debug.Debug; + /** * This class will be used to display the first boot dialog + * * @author ricky barrette */ public class FirstBootDialog extends Dialog implements android.view.View.OnClickListener { /** * Creates a new FirstBootDialog + * * @param context * @author ricky barrette */ - public FirstBootDialog(Context context) { + public FirstBootDialog(final Context context) { super(context); build(context); } - - /** - * Creates a new FirstBootDialog - * @param context - * @param theme - * @author ricky barrette - */ - public FirstBootDialog(Context context, int theme) { - super(context, theme); - build(context); - } - /** * Creates a new FirstBootDialog + * * @param context * @param cancelable * @param cancelListener * @author ricky barrette */ - public FirstBootDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { + public FirstBootDialog(final Context context, final boolean cancelable, final OnCancelListener cancelListener) { super(context, cancelable, cancelListener); build(context); } + /** + * Creates a new FirstBootDialog + * + * @param context + * @param theme + * @author ricky barrette + */ + public FirstBootDialog(final Context context, final int theme) { + super(context, theme); + build(context); + } + /** * Builds the dialog + * * @param context * @author ricky barrette */ - private void build(Context context) { - this.requestWindowFeature(Window.FEATURE_LEFT_ICON); + private void build(final Context context) { + requestWindowFeature(Window.FEATURE_LEFT_ICON); this.setContentView(R.layout.first_boot_dialog); - this.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.icon); + setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.icon); this.setTitle(R.string.welcome); - this.findViewById(R.id.ok_button).setOnClickListener(this); + findViewById(R.id.ok_button).setOnClickListener(this); } /** * called when the ok button is clicked */ @Override - public void onClick(View arg0) { - this.getContext().getSharedPreferences(SettingsActivity.SETTINGS, Debug.SHARED_PREFS_MODE).edit().putBoolean(SettingsActivity.IS_FIRST_BOOT, false).commit(); - this.dismiss(); + public void onClick(final View arg0) { + getContext().getSharedPreferences(SettingsActivity.SETTINGS, Debug.SHARED_PREFS_MODE).edit().putBoolean(SettingsActivity.IS_FIRST_BOOT, false).commit(); + dismiss(); } } \ No newline at end of file diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/ListActivity.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/ListActivity.java index 0107bc5..f972e10 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/ListActivity.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/ListActivity.java @@ -45,13 +45,13 @@ import com.skyhookwireless.wps.WPSReturnCode; @SuppressLint("Registered") public class ListActivity extends Activity implements OnItemClickListener, OnClickListener, DatabaseListener, RegistrationCallback { - - private RingerDatabase mDb; + + private RingerDatabase mDb; private ListView mListView; private SharedPreferences mSettings; private ProgressDialog mProgress; private Dialog mSplashDialog; - + public static final String NO_SPLASH = "no splash"; public static final String KEY_RINGER = "key_ringer"; public static final String KEY_INFO = "key_info"; @@ -60,58 +60,60 @@ public class ListActivity extends Activity implements OnItemClickListener, OnCli private static final int ACTIVITY_EDIT = 4; private static final String KEY_ROWID = "key_row_id"; public static final String ACTION_NEW_RINGER = "action_new_ringer"; - + @Override public void done() { } @Override - public WPSContinuation handleError(WPSReturnCode arg0) { + public WPSContinuation handleError(final WPSReturnCode arg0) { Toast.makeText(this, R.string.skyhook_error_registration, Toast.LENGTH_SHORT).show(); return WPSContinuation.WPS_CONTINUE; } - - @Override + + @Override public void handleSuccess() { Toast.makeText(this, R.string.registered, Toast.LENGTH_SHORT).show(); - this.mSettings.edit().putBoolean(SettingsActivity.IS_REGISTERED, true).commit(); + mSettings.edit().putBoolean(SettingsActivity.IS_REGISTERED, true).commit(); } - - /** - * called when the note edit activity finishes - * (non-Javadoc) - * @see android.app.Activity#onActivityResult(int, int, android.content.Intent) + + /** + * called when the note edit activity finishes (non-Javadoc) + * + * @see android.app.Activity#onActivityResult(int, int, + * android.content.Intent) * @author ricky barrette */ @Override public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) { super.onActivityResult(requestCode, resultCode, intent); - if (resultCode == RESULT_OK){ - - final ProgressDialog progress = ProgressDialog.show(this, "", this.getText(R.string.saving), true, true); - + if (resultCode == RESULT_OK) { + + final ProgressDialog progress = ProgressDialog.show(this, "", getText(R.string.saving), true, true); + new Thread(new Runnable() { @Override public void run() { switch (requestCode) { case ACTIVITY_CREATE: - ContentValues ringer = (ContentValues) intent.getParcelableExtra(KEY_RINGER); + final ContentValues ringer = (ContentValues) intent.getParcelableExtra(KEY_RINGER); mDb.insertRinger(ringer, (ContentValues) intent.getParcelableExtra(KEY_INFO)); break; case ACTIVITY_EDIT: - mDb.updateRinger(intent.getLongExtra(KEY_ROWID, 1), (ContentValues) intent.getParcelableExtra(KEY_RINGER), (ContentValues) intent.getParcelableExtra(KEY_INFO)); + mDb.updateRinger(intent.getLongExtra(KEY_ROWID, 1), (ContentValues) intent.getParcelableExtra(KEY_RINGER), + (ContentValues) intent.getParcelableExtra(KEY_INFO)); break; } - + final String action = ListActivity.this.getIntent().getAction(); - if(action != null) - if(action.equals(ACTION_NEW_RINGER)) + if (action != null) + if (action.equals(ACTION_NEW_RINGER)) finish(); else - ListActivity.this.mListView.post(new Runnable(){ + mListView.post(new Runnable() { @Override - public void run(){ + public void run() { progress.dismiss(); populate(); } @@ -120,131 +122,140 @@ public class ListActivity extends Activity implements OnItemClickListener, OnCli }).start(); } else { final String action = ListActivity.this.getIntent().getAction(); - if(action != null) - if(action.equals(ACTION_NEW_RINGER)) + if (action != null) + if (action.equals(ACTION_NEW_RINGER)) finish(); } } @Override - public void onClick(View v) { - Intent i = new Intent(this, RingerInformationActivity.class); - startActivityForResult(i, ACTIVITY_CREATE); + public void onClick(final View v) { + final Intent i = new Intent(this, RingerInformationActivity.class); + startActivityForResult(i, ACTIVITY_CREATE); } /** - * called when the context menu item has been selected - * (non-Javadoc) - * @see android.app.Activity#onContextItemSelected(android.view.MenuItem) - * @author ricky barrette - */ - public boolean onContextItemSelected(MenuItem item) { - switch(item.getItemId()) { - case R.id.delete: - final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); - if(info.id == 0) - Toast.makeText(this, this.getString(R.string.cant_delete_default), Toast.LENGTH_SHORT).show(); - else - mDb.deleteRinger(info.id +1); - return true; - } - return super.onContextItemSelected(item); - } + * called when the context menu item has been selected (non-Javadoc) + * + * @see android.app.Activity#onContextItemSelected(android.view.MenuItem) + * @author ricky barrette + */ + @Override + public boolean onContextItemSelected(final MenuItem item) { + switch (item.getItemId()) { + case R.id.delete: + final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); + if (info.id == 0) + Toast.makeText(this, this.getString(R.string.cant_delete_default), Toast.LENGTH_SHORT).show(); + else + mDb.deleteRinger(info.id + 1); + return true; + } + return super.onContextItemSelected(item); + } - /** - * Called when the activity is first created. - */ - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - final Intent intent = getIntent(); + /** + * Called when the activity is first created. + */ + @Override + public void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + final Intent intent = getIntent(); final String action = intent.getAction(); - - // If the intent is a request to create a shortcut, we'll do that and exit - + + // If the intent is a request to create a shortcut, we'll do that and + // exit + if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) { setupShortcut(); finish(); return; } - - setContentView(R.layout.ringer_list); - this.setTitle(R.string.app_name); - this.mDb = new RingerDatabase(this, this); - this.mListView = (ListView) findViewById(R.id.ringer_list); - this.mListView.setOnItemClickListener(this); - this.mListView.setOnCreateContextMenuListener(this); - this.mListView.setEmptyView(findViewById(android.R.id.empty)); - findViewById(R.id.add_ringer_button).setOnClickListener(this); - populate(); - this.mSettings = this.getSharedPreferences(SettingsActivity.SETTINGS, Debug.SHARED_PREFS_MODE); - - if(this.mSettings.getBoolean(SettingsActivity.IS_FIRST_BOOT, true)) - new FirstBootDialog(this).show(); - - if(! this.mSettings.getBoolean(SettingsActivity.IS_REGISTERED, false)){ - new SkyHookRegistration(this).registerNewUser(this); - } - -// if(!this.getIntent().hasExtra(NO_SPLASH)) -// showSplashScreen(); - - if(action != null) - if(action.equals(ACTION_NEW_RINGER)) - startActivityForResult(new Intent(this, RingerInformationActivity.class), ACTIVITY_CREATE); - } - - /** - * called when the activity is first created, creates a context menu - * @param menu - * @param v - * @param menuInfo - * @return - * @author ricky barrette - */ - public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { - super.onCreateContextMenu(menu, v, menuInfo); - MenuInflater inflater = getMenuInflater(); - inflater.inflate(R.menu.ringer_list_context_menu, menu); - } + + setContentView(R.layout.ringer_list); + this.setTitle(R.string.app_name); + mDb = new RingerDatabase(this, this); + mListView = (ListView) findViewById(R.id.ringer_list); + mListView.setOnItemClickListener(this); + mListView.setOnCreateContextMenuListener(this); + mListView.setEmptyView(findViewById(android.R.id.empty)); + findViewById(R.id.add_ringer_button).setOnClickListener(this); + populate(); + mSettings = getSharedPreferences(SettingsActivity.SETTINGS, Debug.SHARED_PREFS_MODE); + + if (mSettings.getBoolean(SettingsActivity.IS_FIRST_BOOT, true)) + new FirstBootDialog(this).show(); + + if (!mSettings.getBoolean(SettingsActivity.IS_REGISTERED, false)) + new SkyHookRegistration(this).registerNewUser(this); + + // if(!this.getIntent().hasExtra(NO_SPLASH)) + // showSplashScreen(); + + if (action != null) + if (action.equals(ACTION_NEW_RINGER)) + startActivityForResult(new Intent(this, RingerInformationActivity.class), ACTIVITY_CREATE); + } /** - * called when the activity is first created, creates options menu - * (non-Javadoc) - * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu) - * @author ricky barrette - */ - @Override - public boolean onCreateOptionsMenu (Menu menu) { - final MenuInflater inflater = getMenuInflater(); - inflater.inflate(R.menu.ringer_list_menu, menu); - return super.onCreateOptionsMenu(menu); - - } - - /** - * Called when a database is being upgraded - * @author ricky barrette - */ - @Override - public void onDatabaseUpgrade() { - this.mProgress = ProgressDialog.show(this, "", this.getText(R.string.upgrading), true, true); + * called when the activity is first created, creates a context menu + * + * @param menu + * @param v + * @param menuInfo + * @return + * @author ricky barrette + */ + @Override + public void onCreateContextMenu(final ContextMenu menu, final View v, final ContextMenuInfo menuInfo) { + super.onCreateContextMenu(menu, v, menuInfo); + final MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.ringer_list_context_menu, menu); } - + /** - * called when a database upgrade is finished - * @author ricky barrette - */ + * called when the activity is first created, creates options menu + * (non-Javadoc) + * + * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu) + * @author ricky barrette + */ + @Override + public boolean onCreateOptionsMenu(final Menu menu) { + final MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.ringer_list_menu, menu); + return super.onCreateOptionsMenu(menu); + + } + + /** + * Called when a database is being upgraded + * + * @author ricky barrette + */ + @Override + public void onDatabaseUpgrade() { + mProgress = ProgressDialog.show(this, "", getText(R.string.upgrading), true, true); + } + + /** + * called when a database upgrade is finished + * + * @author ricky barrette + */ @Override public void onDatabaseUpgradeComplete() { populate(); - if(this.mProgress != null) - this.mProgress.dismiss(); + if (mProgress != null) + mProgress.dismiss(); } - - /* (non-Javadoc) + + /* + * (non-Javadoc) + * * @see android.app.Activity#onDestroy() + * * @author ricky barrette */ @Override @@ -253,87 +264,88 @@ public class ListActivity extends Activity implements OnItemClickListener, OnCli PassiveLocationListener.requestPassiveLocationUpdates(this, new Intent(this, PassiveLocationChangedReceiver.class)); super.onDestroy(); } - - /** - * called when an item in the list view has been clicked, - * this will open the note edit dialog for the selected note - * (non-Javadoc) - * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long) - * @author ricky barrette - */ - @Override - public void onItemClick(AdapterView arg0, View v, int postion, final long id) { - - final ProgressDialog progress = ProgressDialog.show(this, "", this.getText(R.string.loading), true, true); - - //post to social sites in a new thread to prevent ANRs - new Thread( new Runnable(){ - @Override - public void run(){ - Looper.prepare(); - - final Intent i = new Intent(ListActivity.this, RingerInformationActivity.class).putExtra(KEY_ROWID, id+1); - - /* - * get the ringer - */ - final Cursor ringer = mDb.getRingerFromId(id+1); - if (ringer.moveToFirst()){ - ContentValues r = new ContentValues(); - r.put(RingerDatabase.KEY_RINGER_NAME, ringer.getString(0)); - r.put(RingerDatabase.KEY_IS_ENABLED, RingerDatabase.parseBoolean(ringer.getString(1))); - i.putExtra(KEY_RINGER, r); - - if (ringer != null && !ringer.isClosed()) { + + /** + * called when an item in the list view has been clicked, this will open the + * note edit dialog for the selected note (non-Javadoc) + * + * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, + * android.view.View, int, long) + * @author ricky barrette + */ + @Override + public void onItemClick(final AdapterView arg0, final View v, final int postion, final long id) { + + final ProgressDialog progress = ProgressDialog.show(this, "", getText(R.string.loading), true, true); + + // post to social sites in a new thread to prevent ANRs + new Thread(new Runnable() { + @Override + public void run() { + Looper.prepare(); + + final Intent i = new Intent(ListActivity.this, RingerInformationActivity.class).putExtra(KEY_ROWID, id + 1); + + /* + * get the ringer + */ + final Cursor ringer = mDb.getRingerFromId(id + 1); + if (ringer.moveToFirst()) { + final ContentValues r = new ContentValues(); + r.put(RingerDatabase.KEY_RINGER_NAME, ringer.getString(0)); + r.put(RingerDatabase.KEY_IS_ENABLED, RingerDatabase.parseBoolean(ringer.getString(1))); + i.putExtra(KEY_RINGER, r); + + if (ringer != null && !ringer.isClosed()) ringer.close(); - } - - if(id == 0) + + if (id == 0) i.putExtra(KEY_IS_DEFAULT, true); - - /* - * get the ringer's info, and parse it into content values - */ + + /* + * get the ringer's info, and parse it into content values + */ i.putExtra(KEY_INFO, mDb.getRingerInfo(r.getAsString(RingerDatabase.KEY_RINGER_NAME))); - } - + } + progress.dismiss(); - - //start the ringer info activity in editor mode - startActivityForResult(i, ACTIVITY_EDIT); - } - }).start(); + // start the ringer info activity in editor mode + startActivityForResult(i, ACTIVITY_EDIT); + + } + }).start(); } - - /** - * called when an option is selected form the menu - * (non-Javadoc) - * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem) - * @author ricky barrette - */ - @Override - public boolean onOptionsItemSelected (MenuItem item) { - switch (item.getItemId()){ - case R.id.settings: - startActivity(new Intent(this, SettingsActivity.class)); - return true; - - case R.id.backup: - mDb.backup(); - SettingsActivity.backup(this); - break; - - case R.id.restore: - mDb.restore(); - SettingsActivity.restore(this); - break; - } - return super.onOptionsItemSelected(item); - } - /** + /** + * called when an option is selected form the menu (non-Javadoc) + * + * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem) + * @author ricky barrette + */ + @Override + public boolean onOptionsItemSelected(final MenuItem item) { + switch (item.getItemId()) { + case R.id.settings: + startActivity(new Intent(this, SettingsActivity.class)); + return true; + + case R.id.backup: + mDb.backup(); + SettingsActivity.backup(this); + break; + + case R.id.restore: + mDb.restore(); + SettingsActivity.restore(this); + break; + } + return super.onOptionsItemSelected(item); + } + + /** * (non-Javadoc) + * * @see android.app.Activity#onPause() */ @Override @@ -343,11 +355,11 @@ public class ListActivity extends Activity implements OnItemClickListener, OnCli } /** - * Called when the database is restored - */ + * Called when the database is restored + */ @Override public void onRestoreComplete() { - populate(); + populate(); } /** @@ -355,85 +367,89 @@ public class ListActivity extends Activity implements OnItemClickListener, OnCli */ @Override public void onRingerDeletionComplete() { - populate(); + populate(); } /** * populates the list view from the data base + * * @author ricky barrette */ private void populate() { - findViewById(R.id.add_ringer_button_hint).setVisibility(this.mDb.getAllRingerTitles().size() > 1 ? View.GONE : View.VISIBLE); + findViewById(R.id.add_ringer_button_hint).setVisibility(mDb.getAllRingerTitles().size() > 1 ? View.GONE : View.VISIBLE); mListView.setAdapter(new RingerListAdapter(this, mDb)); } - /** + /** * Removes the Dialog that displays the splash screen */ protected void removeSplashScreen() { - if (mSplashDialog != null) { - mSplashDialog.dismiss(); - mSplashDialog = null; - } + if (mSplashDialog != null) { + mSplashDialog.dismiss(); + mSplashDialog = null; + } } /** - * Restarts the service if its not already running. - * @author ricky barrette - */ - private void restartService() { - final SharedPreferences sharedPrefs = this.getSharedPreferences(SettingsActivity.SETTINGS, Debug.SHARED_PREFS_MODE); - if(! sharedPrefs.getBoolean(SettingsActivity.IS_SERVICE_STARTED, false)){ + * Restarts the service if its not already running. + * + * @author ricky barrette + */ + private void restartService() { + final SharedPreferences sharedPrefs = getSharedPreferences(SettingsActivity.SETTINGS, Debug.SHARED_PREFS_MODE); + if (!sharedPrefs.getBoolean(SettingsActivity.IS_SERVICE_STARTED, false)) { // cancel the previous service - LocationService.stopService(this).run(); - //start the new service + com.TwentyCodes.android.location.LocationService.stopService(this).run(); + // start the new service LocationService.startMultiShotService(this); - } + } } /** - * Creates a shortcut for the launcher + * Creates a shortcut for the launcher + * * @author ricky barrette */ private void setupShortcut() { - Intent shortcutIntent = new Intent(this, this.getClass()); - shortcutIntent.setAction(ACTION_NEW_RINGER); + final Intent shortcutIntent = new Intent(this, this.getClass()); + shortcutIntent.setAction(ACTION_NEW_RINGER); - //set up the container intent and return to the launcher - Intent intent = new Intent(); - intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); - intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.new_ringer)); - Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon); - intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); - setResult(RESULT_OK, intent); + // set up the container intent and return to the launcher + final Intent intent = new Intent(); + intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); + intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.new_ringer)); + final Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon); + intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); + setResult(RESULT_OK, intent); } -// /** -// * Shows the splash screen over the full Activity -// */ -// protected void showSplashScreen() { -//// mMap.setGPSDialogEnabled(false); -// mSplashDialog = new Dialog(this, android.R.style.Theme_Translucent); -// mSplashDialog.setContentView(R.layout.powered_by_skyhook); -// mSplashDialog.setCancelable(false); -// mSplashDialog.show(); -// -// // Set Runnable to remove splash screen just in case -// final Handler handler = new Handler(); -// handler.postDelayed(new Runnable() { -// @Override -// public void run() { -// removeSplashScreen(); -// -// /* -// * uncomment the following to display the eula -// */ -//// //loads first boot dialog if this is the first boot -//// if (! mSettings.getBoolean(Settings.ACCEPTED, false) || Debug.FORCE_FIRSTBOOT_DIALOG) -//// eulaAlert(); -//// else -//// update(); -// } -// }, 2000); -// } + // /** + // * Shows the splash screen over the full Activity + // */ + // protected void showSplashScreen() { + // // mMap.setGPSDialogEnabled(false); + // mSplashDialog = new Dialog(this, android.R.style.Theme_Translucent); + // mSplashDialog.setContentView(R.layout.powered_by_skyhook); + // mSplashDialog.setCancelable(false); + // mSplashDialog.show(); + // + // // Set Runnable to remove splash screen just in case + // final Handler handler = new Handler(); + // handler.postDelayed(new Runnable() { + // @Override + // public void run() { + // removeSplashScreen(); + // + // /* + // * uncomment the following to display the eula + // */ + // // //loads first boot dialog if this is the first boot + // // if (! mSettings.getBoolean(Settings.ACCEPTED, false) || + // Debug.FORCE_FIRSTBOOT_DIALOG) + // // eulaAlert(); + // // else + // // update(); + // } + // }, 2000); + // } } \ No newline at end of file diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/RingerInformationActivity.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/RingerInformationActivity.java index 90cc2e2..72f98a2 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/RingerInformationActivity.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/RingerInformationActivity.java @@ -40,9 +40,10 @@ import com.jakewharton.android.viewpagerindicator.TitledFragmentAdapter; /** * This activity will handle displaying ringer options + * * @author ricky */ -public class RingerInformationActivity extends FragmentActivity implements OnContentChangedListener, EnableScrollingListener, OnPageChangeListener{ +public class RingerInformationActivity extends FragmentActivity implements OnContentChangedListener, EnableScrollingListener, OnPageChangeListener { private static final String TAG = "RingerInformationActivity"; private ContentValues mRinger; @@ -53,217 +54,228 @@ public class RingerInformationActivity extends FragmentActivity implements OnCon /** * Logs the content values + * * @param values * @author ricky barrette */ - private void logContentValues(ContentValues values) { - for(Entry item : values.valueSet()) - Log.d(TAG, item.getKey() +" = "+ item.getValue()); + private void logContentValues(final ContentValues values) { + for (final Entry item : values.valueSet()) + Log.d(TAG, item.getKey() + " = " + item.getValue()); } + /** - * Called when the activity is first created - * (non-Javadoc) + * Called when the activity is first created (non-Javadoc) + * * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle) */ @SuppressLint("NewApi") @Override - protected void onCreate(Bundle arg0) { + protected void onCreate(final Bundle arg0) { super.onCreate(arg0); Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this)); - + final Intent intent = getIntent(); setContentView(R.layout.ringer_information_activity); - + /* * Set up the action bar if required */ - if(Debug.SUPPORTS_HONEYCOMB) + if (Debug.SUPPORTS_HONEYCOMB) getActionBar().setDisplayHomeAsUpEnabled(true); - - this.mData = new Intent().putExtras(intent); - - this.mRinger = this.mData.getParcelableExtra(ListActivity.KEY_RINGER); - this.mInfo = this.mData.getParcelableExtra(ListActivity.KEY_INFO); - if(this.mRinger == null) - this.mRinger = new ContentValues(); - if(this.mInfo == null) - this.mInfo = new ContentValues(); + mData = new Intent().putExtras(intent); + + mRinger = mData.getParcelableExtra(ListActivity.KEY_RINGER); + mInfo = mData.getParcelableExtra(ListActivity.KEY_INFO); + + if (mRinger == null) + mRinger = new ContentValues(); + if (mInfo == null) + mInfo = new ContentValues(); /* * set the title */ - this.setTitle(this.mRinger.containsKey(RingerDatabase.KEY_RINGER_NAME) ?this.getString(R.string.editing)+" "+this.mRinger.getAsString(RingerDatabase.KEY_RINGER_NAME) + this.setTitle(mRinger.containsKey(RingerDatabase.KEY_RINGER_NAME) ? this.getString(R.string.editing) + " " + mRinger.getAsString(RingerDatabase.KEY_RINGER_NAME) : getString(R.string.new_ringer)); - - boolean isDefault = getString(R.string.default_ringer).equals(this.mRinger.getAsString(RingerDatabase.KEY_RINGER_NAME)); - + + final boolean isDefault = getString(R.string.default_ringer).equals(mRinger.getAsString(RingerDatabase.KEY_RINGER_NAME)); + /* * Page titles */ - String[] titles = this.getResources().getStringArray(isDefault ? R.array.ringer_info_titles_default : R.array.ringer_info_titles); - - ArrayList fragments = new ArrayList(); - + final String[] titles = getResources().getStringArray(isDefault ? R.array.ringer_info_titles_default : R.array.ringer_info_titles); + + final ArrayList fragments = new ArrayList(); + /* * about page */ - if(!isDefault) - fragments.add(new AboutRingerFragment(this.mRinger, this.mInfo, this)); - + if (!isDefault) + fragments.add(new AboutRingerFragment(mRinger, mInfo, this)); + /* * Location page */ - if(!isDefault){ - this.mLocationInfomationFragment = new LocationInfomationFragment(this.mInfo, this, this); - fragments.add(this.mLocationInfomationFragment); + if (!isDefault) { + mLocationInfomationFragment = new LocationInfomationFragment(mInfo, this, this); + fragments.add(mLocationInfomationFragment); } - - fragments.add(new FeatureListFragment(this.mInfo, this)); - - //Populate the pager - this.mPager = (ViewPager)findViewById(R.id.pager); - if(this.mPager != null) - this.mPager.setAdapter(new TitledFragmentAdapter(this.getSupportFragmentManager(), fragments, titles)); - - //populate the pager's indicator - TitlePageIndicator indicator = (TitlePageIndicator)findViewById(R.id.indicator); - if(indicator != null) - indicator.setViewPager(this.mPager); - + + fragments.add(new FeatureListFragment(mInfo, this)); + + // Populate the pager + mPager = (ViewPager) findViewById(R.id.pager); + if (mPager != null) + mPager.setAdapter(new TitledFragmentAdapter(getSupportFragmentManager(), fragments, titles)); + + // populate the pager's indicator + final TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator); + if (indicator != null) + indicator.setViewPager(mPager); + indicator.setOnPageChangeListener(this); } - + /** * Creates the main menu that is displayed when the menu button is clicked + * * @author ricky barrette */ - public boolean onCreateOptionsMenu(Menu menu) { - MenuInflater inflater = getMenuInflater(); - inflater.inflate(R.menu.ringer_info_menu, menu); + @Override + public boolean onCreateOptionsMenu(final Menu menu) { + final MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.ringer_info_menu, menu); return super.onCreateOptionsMenu(menu); } /** - * Called when the ringer info has changed - * (non-Javadoc) + * Called when the ringer info has changed (non-Javadoc) + * * @see com.TwentyCodes.android.LocationRinger.OnContentChangedListener#onInfoContentChanged(android.content.ContentValues) */ @Override - public void onInfoContentChanged(ContentValues values) { - if(Debug.DEBUG){ - Log.v(TAG,"onInfoContentChanged()"); + public void onInfoContentChanged(final ContentValues values) { + if (Debug.DEBUG) { + Log.v(TAG, "onInfoContentChanged()"); logContentValues(values); } - this.mInfo.putAll(values); + mInfo.putAll(values); } - + /** - * Called when a feature is removed - * (non-Javadoc) + * Called when a feature is removed (non-Javadoc) + * * @see com.TwentyCodes.android.LocationRinger.OnContentChangedListener#onInfoContentRemoved(java.lang.String[]) */ @Override - public void onInfoContentRemoved(String... keys) { - for(String key : keys) - if(this.mInfo.containsKey(key)) - this.mInfo.remove(key); + public void onInfoContentRemoved(final String... keys) { + for (final String key : keys) + if (mInfo.containsKey(key)) + mInfo.remove(key); } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem) + * * @author ricky barrette */ @Override public boolean onOptionsItemSelected(final MenuItem item) { - switch(item.getItemId()){ - case R.id.save: - save(); - break; - case android.R.id.home: - final Intent intent = new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra(ListActivity.NO_SPLASH, ListActivity.NO_SPLASH); - startActivity(intent); - return true; + switch (item.getItemId()) { + case R.id.save: + save(); + break; + case android.R.id.home: + final Intent intent = new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra(ListActivity.NO_SPLASH, ListActivity.NO_SPLASH); + startActivity(intent); + return true; } return super.onOptionsItemSelected(item); } - @Override - public void onPageScrolled(int arg0, float arg1, int arg2) { + public void onPageScrolled(final int arg0, final float arg1, final int arg2) { // TODO Auto-generated method stub } - + /** - * called when the pager's page is changed - * we use this to dismiss the soft keyboard - * (non-Javadoc) + * called when the pager's page is changed we use this to dismiss the soft + * keyboard (non-Javadoc) + * * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrollStateChanged(int) */ @Override - public void onPageScrollStateChanged(int arg0) { - InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); + public void onPageScrollStateChanged(final int arg0) { + final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(mPager.getWindowToken(), 0); } @Override - public void onPageSelected(int arg0) { + public void onPageSelected(final int arg0) { } /** - * Called when the ringer content has been changed - * (non-Javadoc) + * Called when the ringer content has been changed (non-Javadoc) + * * @see com.TwentyCodes.android.LocationRinger.OnContentChangedListener#onRingerContentChanged(android.content.ContentValues) */ @Override - public void onRingerContentChanged(ContentValues values) { - if(Debug.DEBUG){ - Log.v(TAG,"onRingerContentChanged()"); + public void onRingerContentChanged(final ContentValues values) { + if (Debug.DEBUG) { + Log.v(TAG, "onRingerContentChanged()"); logContentValues(values); } - this.mRinger.putAll(values); + mRinger.putAll(values); } /** * (non-Javadoc) + * * @see android.app.Activity#onSearchRequested() */ @Override public boolean onSearchRequested() { - if(this.mLocationInfomationFragment != null && this.mPager.getCurrentItem() == 1) - return this.mLocationInfomationFragment.onSearchRequested(); + if (mLocationInfomationFragment != null && mPager.getCurrentItem() == 1) + return mLocationInfomationFragment.onSearchRequested(); return super.onSearchRequested(); } - + /** - * Prepares a bundle containing all the information that needs to be saved, and returns it to the starting activity + * Prepares a bundle containing all the information that needs to be saved, + * and returns it to the starting activity + * * @author ricky barrette */ private void save() { - final ProgressDialog progress = ProgressDialog.show(this, "", this.getText(R.string.saving), true, true); - - //Generate the intent in a thread to prevent anr's and allow for progress dialog - new Thread( new Runnable(){ - @Override - public void run(){ - Looper.prepare(); - RingerInformationActivity.this.mData.putExtra(ListActivity.KEY_RINGER, RingerInformationActivity.this.mRinger).putExtra(ListActivity.KEY_INFO, RingerInformationActivity.this.mInfo); - RingerInformationActivity.this.setResult(Activity.RESULT_OK, RingerInformationActivity.this.mData); - progress.dismiss(); - RingerInformationActivity.this.finish(); - } - }).start(); + final ProgressDialog progress = ProgressDialog.show(this, "", getText(R.string.saving), true, true); + + // Generate the intent in a thread to prevent anr's and allow for + // progress dialog + new Thread(new Runnable() { + @Override + public void run() { + Looper.prepare(); + mData.putExtra(ListActivity.KEY_RINGER, mRinger).putExtra(ListActivity.KEY_INFO, mInfo); + RingerInformationActivity.this.setResult(Activity.RESULT_OK, mData); + progress.dismiss(); + RingerInformationActivity.this.finish(); + } + }).start(); } /** * Called when the scrolling state of the view pager is changed * (non-Javadoc) + * * @see com.TwentyCodes.android.LocationRinger.EnableScrollingListener#setScrollEnabled(boolean) */ @Override - public void setScrollEnabled(boolean enabled) { - this.mPager.setScrollEnabled(enabled); + public void setScrollEnabled(final boolean enabled) { + mPager.setScrollEnabled(enabled); } } \ No newline at end of file diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/RingerListAdapter.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/RingerListAdapter.java index edd5b96..a7abbf0 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/RingerListAdapter.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/RingerListAdapter.java @@ -24,29 +24,38 @@ import com.TwentyCodes.android.LocationRinger.db.RingerDatabase; import com.TwentyCodes.android.LocationRinger.debug.Debug; /** - * This adapter will be used to populate the list view with all the ringers names, and manage enabling/disabling of ringers based on their check box. + * This adapter will be used to populate the list view with all the ringers + * names, and manage enabling/disabling of ringers based on their check box. + * * @author ricky barrette */ public class RingerListAdapter extends BaseAdapter { - private static final String TAG = "RingerListAdapter"; + class ViewHolder { + TextView title; + TextView description; + CheckBox checkbox; + } - private RingerDatabase mDb; - private List mTitles; - private LayoutInflater mInflater; - private List mDescriptions; + private static final String TAG = "RingerListAdapter"; + private final RingerDatabase mDb; + private final List mTitles; + private final LayoutInflater mInflater; + + private final List mDescriptions; /** * Creates a new RingerListAdapter + * * @param context * @param listener * @param db * @author ricky barrette */ - public RingerListAdapter(Context context, RingerDatabase db) { + public RingerListAdapter(final Context context, final RingerDatabase db) { super(); // Cache the LayoutInflate to avoid asking for a new one each time. - mInflater = LayoutInflater.from(context); + mInflater = LayoutInflater.from(context); mDb = db; mTitles = db.getAllRingerTitles(); mDescriptions = db.getAllRingerDescriptions(); @@ -58,88 +67,84 @@ public class RingerListAdapter extends BaseAdapter { } @Override - public String getItem(int position) { + public String getItem(final int position) { return mTitles.get(position); } @Override - public long getItemId(int position) { + public long getItemId(final int position) { return position; } @SuppressWarnings("unused") @Override - public View getView(final int position, View convertView, ViewGroup parent) { - - // A ViewHolder keeps references to children views to avoid unnecessary calls to findViewById() on each row. - ViewHolder holder; + public View getView(final int position, View convertView, final ViewGroup parent) { - // When convertView is not null, we can reuse it directly, there is no need - // to reinflate it. We only inflate a new View when the convertView supplied - // by ListView is null. - if (convertView == null) { - convertView = mInflater.inflate(R.layout.list_item, null); + // A ViewHolder keeps references to children views to avoid unnecessary + // calls to findViewById() on each row. + ViewHolder holder; - // Creates a ViewHolder and store references to the two children views - // we want to bind data to. - holder = new ViewHolder(); - holder.title = (TextView) convertView.findViewById(android.R.id.text1); - holder.description = (TextView) convertView.findViewById(android.R.id.text2); - holder.checkbox = (CheckBox) convertView.findViewById(R.id.ringer_enabled_checkbox); + // When convertView is not null, we can reuse it directly, there is no + // need + // to reinflate it. We only inflate a new View when the convertView + // supplied + // by ListView is null. + if (convertView == null) { + convertView = mInflater.inflate(R.layout.list_item, null); - convertView.setTag(holder); - } else { - // Get the ViewHolder back to get fast access to the TextView - // and the ImageView. - holder = (ViewHolder) convertView.getTag(); - } - - if(Debug.DEBUG){ - Log.d(TAG, "postion = "+position); + // Creates a ViewHolder and store references to the two children + // views + // we want to bind data to. + holder = new ViewHolder(); + holder.title = (TextView) convertView.findViewById(android.R.id.text1); + holder.description = (TextView) convertView.findViewById(android.R.id.text2); + holder.checkbox = (CheckBox) convertView.findViewById(R.id.ringer_enabled_checkbox); - if(convertView == null) - Log.e(TAG,"convertview is null!!!"); - - if(holder == null) - Log.e(TAG,"holder is null!!!"); - - if(holder.title == null) - Log.e(TAG,"holder.text is null!!!"); - - if(holder.checkbox == null) - Log.e(TAG,"holder.checkbox is null!!!"); - } - - /* - * Bind the data efficiently with the holder. - * Remember that you should always call setChecked() after calling setOnCheckedChangedListener. - * This will prevent the list from changing the values on you. - */ - holder.title.setText(getItem(position)); - holder.description.setText(mDescriptions.get(position)); - holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){ + convertView.setTag(holder); + } else + // Get the ViewHolder back to get fast access to the TextView + // and the ImageView. + holder = (ViewHolder) convertView.getTag(); + + if (Debug.DEBUG) { + Log.d(TAG, "postion = " + position); + + if (convertView == null) + Log.e(TAG, "convertview is null!!!"); + + if (holder == null) + Log.e(TAG, "holder is null!!!"); + + if (holder.title == null) + Log.e(TAG, "holder.text is null!!!"); + + if (holder.checkbox == null) + Log.e(TAG, "holder.checkbox is null!!!"); + } + + /* + * Bind the data efficiently with the holder. Remember that you should + * always call setChecked() after calling setOnCheckedChangedListener. + * This will prevent the list from changing the values on you. + */ + holder.title.setText(getItem(position)); + holder.description.setText(mDescriptions.get(position)); + holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - mDb.setRingerEnabled(position +1, isChecked); + public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) { + mDb.setRingerEnabled(position + 1, isChecked); } }); - holder.checkbox.setChecked(mDb.isRingerEnabled(position +1)); - - //Remove the checkbox for the default ringer - if(position == 0) { - holder.checkbox.setVisibility(View.INVISIBLE); - if(holder.description.getText().toString() == null) - holder.description.setText(R.string.about_default_ringer); - } - else - holder.checkbox.setVisibility(View.VISIBLE); - return convertView; + holder.checkbox.setChecked(mDb.isRingerEnabled(position + 1)); + + // Remove the checkbox for the default ringer + if (position == 0) { + holder.checkbox.setVisibility(View.INVISIBLE); + if (holder.description.getText().toString() == null) + holder.description.setText(R.string.about_default_ringer); + } else + holder.checkbox.setVisibility(View.VISIBLE); + return convertView; } - - class ViewHolder { - TextView title; - TextView description; - CheckBox checkbox; - } } \ No newline at end of file diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/SearchDialog.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/SearchDialog.java index 173593d..e951a3f 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/SearchDialog.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/SearchDialog.java @@ -36,26 +36,29 @@ import com.TwentyCodes.android.location.ReverseGeocoder; import com.google.android.maps.GeoPoint; /** - * This dialog will be used to get users input for the address that they want to search for. A GeoPoint location will be returned via LocationSelectedListener + * This dialog will be used to get users input for the address that they want to + * search for. A GeoPoint location will be returned via LocationSelectedListener + * * @author ricky barrette */ -public class SearchDialog extends Dialog implements android.view.View.OnClickListener, OnItemClickListener, OnEditorActionListener{ +public class SearchDialog extends Dialog implements android.view.View.OnClickListener, OnItemClickListener, OnEditorActionListener { protected static final String TAG = "SearchDialog"; - private ListView mAddressList; - private EditText mAddress; + private final ListView mAddressList; + private final EditText mAddress; private JSONArray mResults; - private ProgressBar mProgress; - private Handler mHandler; - private Context mContext; - private OnLocationSelectedListener mListener; + private final ProgressBar mProgress; + private final Handler mHandler; + private final Context mContext; + private final OnLocationSelectedListener mListener; /** * Creates a new search dialog + * * @param context * @author ricky barrette */ - public SearchDialog(Context context, OnLocationSelectedListener listener) { + public SearchDialog(final Context context, final OnLocationSelectedListener listener) { super(context); this.setTitle(R.string.search); this.setContentView(R.layout.address_dialog); @@ -64,28 +67,27 @@ public class SearchDialog extends Dialog implements android.view.View.OnClickLis mAddressList.setOnItemClickListener(this); mAddress = (EditText) findViewById(R.id.address); mAddress.setOnEditorActionListener(this); - this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); + getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); mProgress = (ProgressBar) findViewById(R.id.search_progress); mHandler = new Handler(); mContext = context; mListener = listener; } - /** * Retrieves all the strings from the JSON Array + * * @return list of addresses * @author ricky barrette */ private ArrayList getAddress() { - if(Debug.DEBUG) - Log.d(TAG,"getAddress()"); - ArrayList list = new ArrayList(); + if (Debug.DEBUG) + Log.d(TAG, "getAddress()"); + final ArrayList list = new ArrayList(); try { - for(int i = 0; i < mResults.length(); i++){ + for (int i = 0; i < mResults.length(); i++) list.add(mResults.getJSONObject(i).getString("address")); - } - } catch (JSONException e) { + } catch (final JSONException e) { e.printStackTrace(); return null; } @@ -94,111 +96,115 @@ public class SearchDialog extends Dialog implements android.view.View.OnClickLis /** * Retrieves the GeoPoint from the JSON Array for the given index - * @param index for the place + * + * @param index + * for the place * @return GeoPoint of the place * @author ricky barrette */ - private GeoPoint getCoords(int index){ - if(Debug.DEBUG) - Log.d(TAG,"getCoords()"); + private GeoPoint getCoords(final int index) { + if (Debug.DEBUG) + Log.d(TAG, "getCoords()"); try { - JSONArray coords = mResults.getJSONObject(index).getJSONObject("Point").getJSONArray("coordinates"); - if(Debug.DEBUG) - Log.d(TAG,"creating geopoint: "+ new GeoPoint((int) (coords.getDouble(1) *1E6), (int) (coords.getDouble(0)*1E6)).toString()); - return new GeoPoint((int) (coords.getDouble(1) *1E6), (int) (coords.getDouble(0)*1E6)); - } catch (JSONException e) { + final JSONArray coords = mResults.getJSONObject(index).getJSONObject("Point").getJSONArray("coordinates"); + if (Debug.DEBUG) + Log.d(TAG, "creating geopoint: " + new GeoPoint((int) (coords.getDouble(1) * 1E6), (int) (coords.getDouble(0) * 1E6)).toString()); + return new GeoPoint((int) (coords.getDouble(1) * 1E6), (int) (coords.getDouble(0) * 1E6)); + } catch (final JSONException e) { e.printStackTrace(); } return null; } - + /** - * Called when the search button is clicked - * (non-Javadoc) + * Called when the search button is clicked (non-Javadoc) + * * @see android.view.View.OnClickListener#onClick(android.view.View) */ @Override public void onClick(final View v) { - switch(v.getId()){ - case R.id.ok: - search(); - break; + switch (v.getId()) { + case R.id.ok: + search(); + break; } } - + /** * Called when the seach button on the soft keyboard is pressed * (non-Javadoc) - * @see android.widget.TextView.OnEditorActionListener#onEditorAction(android.widget.TextView, int, android.view.KeyEvent) + * + * @see android.widget.TextView.OnEditorActionListener#onEditorAction(android.widget.TextView, + * int, android.view.KeyEvent) */ @Override - public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { + public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) { search(); return false; } /** - * Called when an Item from the list is selected - * (non-Javadoc) - * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long) + * Called when an Item from the list is selected (non-Javadoc) + * + * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, + * android.view.View, int, long) */ @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - if(Debug.DEBUG) - Log.d(TAG,"slected "+ (int) id); + public void onItemClick(final AdapterView parent, final View view, final int position, final long id) { + if (Debug.DEBUG) + Log.d(TAG, "slected " + (int) id); mListener.onLocationSelected(getCoords((int) id)); - this.dismiss(); + dismiss(); } private void search() { - final InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE); - imm.hideSoftInputFromWindow(this.mAddress.getWindowToken(), 0); - final View v = this.findViewById(R.id.ok); + final InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(mAddress.getWindowToken(), 0); + final View v = findViewById(R.id.ok); v.setEnabled(false); mProgress.setVisibility(View.VISIBLE); mProgress.setIndeterminate(true); - new Thread( new Runnable(){ + new Thread(new Runnable() { @Override - public void run(){ - if(Debug.DEBUG) - Log.d(TAG,"strarting search and parsing") ; + public void run() { + if (Debug.DEBUG) + Log.d(TAG, "strarting search and parsing"); try { mResults = ReverseGeocoder.addressSearch(mAddress.getText().toString()); - } catch (IOException e) { + } catch (final IOException e) { e.printStackTrace(); - } catch (JSONException e) { + } catch (final JSONException e) { e.printStackTrace(); } - if(mResults != null){ - if(Debug.DEBUG) - Log.d(TAG,"finished searching and parsing"); - //update UI - mHandler.post(new Runnable(){ + if (mResults != null) { + if (Debug.DEBUG) + Log.d(TAG, "finished searching and parsing"); + // update UI + mHandler.post(new Runnable() { @Override - public void run(){ - if(Debug.DEBUG) - Log.d(TAG,"populating list"); + public void run() { + if (Debug.DEBUG) + Log.d(TAG, "populating list"); mAddressList.setAdapter(new ArrayAdapter(mContext, android.R.layout.simple_list_item_1, getAddress())); v.setEnabled(true); mProgress.setVisibility(View.INVISIBLE); mProgress.setIndeterminate(false); - if(Debug.DEBUG) - Log.d(TAG,"finished"); + if (Debug.DEBUG) + Log.d(TAG, "finished"); } }); - } else { - //update the UI - mHandler.post(new Runnable(){ + } else + // update the UI + mHandler.post(new Runnable() { @Override - public void run(){ + public void run() { v.setEnabled(true); mProgress.setVisibility(View.INVISIBLE); mProgress.setIndeterminate(false); - if(Debug.DEBUG) - Log.d(TAG,"failed"); + if (Debug.DEBUG) + Log.d(TAG, "failed"); } }); - } } }).start(); } diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/SettingsActivity.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/SettingsActivity.java index e8407e1..dd7b388 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/SettingsActivity.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/SettingsActivity.java @@ -31,10 +31,11 @@ import com.TwentyCodes.android.LocationRinger.debug.Debug; /** * This is the settings activity for location ringer + * * @author ricky barrette */ public class SettingsActivity extends PreferenceActivity implements OnPreferenceClickListener { - + public static final String SETTINGS = "settings"; public static final String EMAIL = "email"; public static final String START_ON_BOOT = "start_on_boot"; @@ -46,26 +47,26 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference public static final String RESTORE = "restore"; public static final String BACKUP = "backup"; public static final String CURRENT = "current"; - + /** * Backs up the database + * * @return true if successful * @author ricky barrette */ - public static boolean backup(final Context context){ - final File dbFile = new File(Environment.getDataDirectory() + "/data/"+context.getPackageName()+"/shared_prefs/"+SETTINGS+".xml"); + public static boolean backup(final Context context) { + final File dbFile = new File(Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/shared_prefs/" + SETTINGS + ".xml"); - final File exportDir = new File(Environment.getExternalStorageDirectory(), "/"+context.getString(R.string.app_name)); - if (!exportDir.exists()) { + final File exportDir = new File(Environment.getExternalStorageDirectory(), "/" + context.getString(R.string.app_name)); + if (!exportDir.exists()) exportDir.mkdirs(); - } final File file = new File(exportDir, dbFile.getName()); try { file.createNewFile(); copyFile(dbFile, file); return true; - } catch (IOException e) { + } catch (final IOException e) { e.printStackTrace(); return false; } @@ -73,57 +74,61 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference /** * Copies a file - * @param src file - * @param dst file + * + * @param src + * file + * @param dst + * file * @throws IOException * @author ricky barrette */ private static void copyFile(final File src, final File dst) throws IOException { final FileInputStream in = new FileInputStream(src); final FileOutputStream out = new FileOutputStream(dst); - final FileChannel inChannel = in.getChannel(); - final FileChannel outChannel = out.getChannel(); - try { - inChannel.transferTo(0, inChannel.size(), outChannel); - } finally { - if (inChannel != null) - inChannel.close(); - if (outChannel != null) - outChannel.close(); - if(in != null) - in.close(); - if(out != null) - out.close(); - - } - } - + final FileChannel inChannel = in.getChannel(); + final FileChannel outChannel = out.getChannel(); + try { + inChannel.transferTo(0, inChannel.size(), outChannel); + } finally { + if (inChannel != null) + inChannel.close(); + if (outChannel != null) + outChannel.close(); + if (in != null) + in.close(); + if (out != null) + out.close(); + + } + } + /** * Restores the database from the sdcard + * * @return true if successful * @author ricky barrette */ - public static void restore(final Context context){ - final File dbFile = new File(Environment.getDataDirectory() + "/data/"+context.getPackageName()+"/shared_prefs/"+SETTINGS+".xml"); + public static void restore(final Context context) { + final File dbFile = new File(Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/shared_prefs/" + SETTINGS + ".xml"); - final File exportDir = new File(Environment.getExternalStorageDirectory(), "/"+context.getString(R.string.app_name)); - if (!exportDir.exists()) { + final File exportDir = new File(Environment.getExternalStorageDirectory(), "/" + context.getString(R.string.app_name)); + if (!exportDir.exists()) exportDir.mkdirs(); - } final File file = new File(exportDir, dbFile.getName()); try { file.createNewFile(); copyFile(file, dbFile); - } catch (IOException e) { + } catch (final IOException e) { e.printStackTrace(); } context.getSharedPreferences(SETTINGS, Debug.SHARED_PREFS_MODE).edit().remove(IS_FIRST_RINGER_PROCESSING).remove(IS_DEFAULT).remove(IS_SERVICE_STARTED).commit(); } - + /** * generates the exception repost email intent + * * @param report * @return intent to start users email client * @author ricky barrette @@ -132,65 +137,66 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference /* * get the build information, and build the string */ - final PackageManager pm = this.getPackageManager(); + final PackageManager pm = getPackageManager(); PackageInfo pi; try { - pi = pm.getPackageInfo(this.getPackageName(), 0); - } catch (NameNotFoundException eNnf) { - //doubt this will ever run since we want info about our own package + pi = pm.getPackageInfo(getPackageName(), 0); + } catch (final NameNotFoundException eNnf) { + // doubt this will ever run since we want info about our own package pi = new PackageInfo(); pi.versionName = "unknown"; pi.versionCode = 1; } - + final Intent intent = new Intent(Intent.ACTION_SEND); final String theSubject = this.getString(R.string.app_name); - final String theBody = "\n\n\n"+ Build.FINGERPRINT +"\n"+ this.getString(R.string.app_name)+" "+pi.versionName+" bulid "+pi.versionCode; - intent.putExtra(Intent.EXTRA_EMAIL,new String[] {this.getString(R.string.email)}); + final String theBody = "\n\n\n" + Build.FINGERPRINT + "\n" + this.getString(R.string.app_name) + " " + pi.versionName + " bulid " + pi.versionCode; + intent.putExtra(Intent.EXTRA_EMAIL, new String[] { this.getString(R.string.email) }); intent.putExtra(Intent.EXTRA_TEXT, theBody); intent.putExtra(Intent.EXTRA_SUBJECT, theSubject); intent.setType("message/rfc822"); return intent; } - + @SuppressWarnings("deprecation") @SuppressLint("NewApi") @Override - public void onCreate(final Bundle savedInstanceState){ + public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); - this.getPreferenceManager().setSharedPreferencesMode(Debug.SHARED_PREFS_MODE); - this.getPreferenceManager().setSharedPreferencesName(SETTINGS); + getPreferenceManager().setSharedPreferencesMode(Debug.SHARED_PREFS_MODE); + getPreferenceManager().setSharedPreferencesName(SETTINGS); addPreferencesFromResource(R.xml.setings); - this.findPreference(EMAIL).setOnPreferenceClickListener(this); - + findPreference(EMAIL).setOnPreferenceClickListener(this); + /* * Set up the action bar if required */ - if(Debug.SUPPORTS_HONEYCOMB) + if (Debug.SUPPORTS_HONEYCOMB) getActionBar().setDisplayHomeAsUpEnabled(true); } - + /** * (non-Javadoc) + * * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem) * @author ricky barrette */ @Override public boolean onOptionsItemSelected(final MenuItem item) { - switch(item.getItemId()){ - case android.R.id.home: - final Intent intent = new Intent(this, ListActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra(ListActivity.NO_SPLASH, ListActivity.NO_SPLASH); - startActivity(intent); - return true; + switch (item.getItemId()) { + case android.R.id.home: + final Intent intent = new Intent(this, ListActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra(ListActivity.NO_SPLASH, ListActivity.NO_SPLASH); + startActivity(intent); + return true; } return super.onOptionsItemSelected(item); } - + /** * called when the email preference button is clicked */ @Override - public boolean onPreferenceClick(Preference preference) { + public boolean onPreferenceClick(final Preference preference) { this.startActivity(generateEmailIntent()); return false; } diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/TextViewPreference.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/TextViewPreference.java index 1e623c5..9ce150f 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/TextViewPreference.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/TextViewPreference.java @@ -11,71 +11,77 @@ import android.util.AttributeSet; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; +import android.view.ViewGroup.LayoutParams; import android.widget.LinearLayout; import android.widget.TextView; /** - * this class will be a simple TextView to be used in a preference activity. you set the text using the set title tag + * this class will be a simple TextView to be used in a preference activity. you + * set the text using the set title tag + * * @author ricky barrette */ public class TextViewPreference extends Preference { /** - * creates a preference that is nothing but a text view + * creates a preference that is nothing but a text view + * * @param context */ - public TextViewPreference(Context context) { + public TextViewPreference(final Context context) { super(context); // TODO Auto-generated constructor stub } - + /** * creates a preference that is nothing but a text view + * * @param context * @param attrs */ - public TextViewPreference(Context context, AttributeSet attrs) { + public TextViewPreference(final Context context, final AttributeSet attrs) { super(context, attrs); } /** * creates a preference that is nothing but a text view + * * @param context * @param attrs * @param defStyle */ - public TextViewPreference(Context context, AttributeSet attrs, int defStyle) { + public TextViewPreference(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); } - /** - * creates a linear layout the contains only a textview. - * (non-Javadoc) + * creates a linear layout the contains only a textview. (non-Javadoc) + * * @see android.preference.Preference#onCreateView(android.view.ViewGroup) * @param parent * @return * @author ricky barrette */ @Override - protected View onCreateView(ViewGroup parent){ - + protected View onCreateView(final ViewGroup parent) { + /* - * create a vertical linear layout that width and height that wraps content + * create a vertical linear layout that width and height that wraps + * content */ - LinearLayout layout = new LinearLayout(getContext()); - LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); + final LinearLayout layout = new LinearLayout(getContext()); + final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.gravity = Gravity.CENTER; layout.setPadding(15, 5, 10, 5); layout.setOrientation(LinearLayout.VERTICAL); layout.removeAllViews(); - + /* - * create a textview that will be used to display the title provided in xml - * and add it to the lay out + * create a textview that will be used to display the title provided in + * xml and add it to the lay out */ - TextView title = new TextView(getContext()); + final TextView title = new TextView(getContext()); title.setText(getTitle()); title.setTextSize(16); title.setTypeface(Typeface.SANS_SERIF); diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/ViewPager.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/ViewPager.java index e15a96f..d0642c7 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/ViewPager.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/ViewPager.java @@ -14,7 +14,7 @@ import android.view.MotionEvent; * @author ricky */ public class ViewPager extends android.support.v4.view.ViewPager { - + private boolean isEnabled = true; /** @@ -22,7 +22,7 @@ public class ViewPager extends android.support.v4.view.ViewPager { * @param apiKey * @author ricky barrette */ - public ViewPager(Context context) { + public ViewPager(final Context context) { super(context); } @@ -31,27 +31,28 @@ public class ViewPager extends android.support.v4.view.ViewPager { * @param attrs * @author ricky barrette */ - public ViewPager(Context context, AttributeSet attrs) { + public ViewPager(final Context context, final AttributeSet attrs) { super(context, attrs); } - - /** - * Enables or disabled the scrollview's ability to scroll - * @param enabled - * @author ricky barrette - */ - public void setScrollEnabled(boolean enabled){ - isEnabled = enabled; - } @Override - public boolean onInterceptTouchEvent(MotionEvent ev) { + public boolean onInterceptTouchEvent(final MotionEvent ev) { return isEnabled ? super.onInterceptTouchEvent(ev) : false; } @Override - public boolean onTouchEvent(MotionEvent ev) { + public boolean onTouchEvent(final MotionEvent ev) { return isEnabled ? super.onTouchEvent(ev) : false; } + /** + * Enables or disabled the scrollview's ability to scroll + * + * @param enabled + * @author ricky barrette + */ + public void setScrollEnabled(final boolean enabled) { + isEnabled = enabled; + } + } \ No newline at end of file diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/AboutRingerFragment.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/AboutRingerFragment.java index 8d5380b..c9c5374 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/AboutRingerFragment.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/AboutRingerFragment.java @@ -8,11 +8,6 @@ package com.TwentyCodes.android.LocationRinger.ui.fragments; import java.util.Map.Entry; -import com.TwentyCodes.android.LocationRinger.R; -import com.TwentyCodes.android.LocationRinger.OnContentChangedListener; -import com.TwentyCodes.android.LocationRinger.db.RingerDatabase; -import com.TwentyCodes.android.LocationRinger.debug.Debug; - import android.annotation.SuppressLint; import android.content.ContentValues; import android.content.Context; @@ -30,157 +25,171 @@ import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.EditText; import android.widget.ToggleButton; +import com.TwentyCodes.android.LocationRinger.OnContentChangedListener; +import com.TwentyCodes.android.LocationRinger.R; +import com.TwentyCodes.android.LocationRinger.db.RingerDatabase; +import com.TwentyCodes.android.LocationRinger.debug.Debug; + /** * This fragment will used to allow the user to enter/edit ringer information + * * @author ricky */ @SuppressLint({ "ValidFragment", "ValidFragment" }) public class AboutRingerFragment extends Fragment implements OnCheckedChangeListener { + /** + * This Edit text class is used in place of a standard edit text. It will + * update the pass the updated information though a listener + * + * @author ricky barrette + */ + public static class ListeningEditText extends EditText { + private String mKey; + private OnContentChangedListener mListener; + private final ContentValues mTemp; + + /** + * Creates a new ListeningEditText + * + * @param context + * @author ricky barrette + */ + public ListeningEditText(final Context context) { + super(context); + mTemp = new ContentValues(); + } + + /** + * Creates a new ListeningEditText + * + * @param context + * @param attrs + * @author ricky barrette + */ + public ListeningEditText(final Context context, final AttributeSet attrs) { + super(context, attrs); + mTemp = new ContentValues(); + } + + /** + * Creates a new ListeningEditText + * + * @param context + * @param attrs + * @param defStyle + * @author ricky barrette + */ + public ListeningEditText(final Context context, final AttributeSet attrs, final int defStyle) { + super(context, attrs, defStyle); + mTemp = new ContentValues(); + } + + /** + * Called when the edit text is drawn + * + * @author ricky barrette + */ + @Override + public void onDraw(final Canvas canvas) { + super.onDraw(canvas); + if (mListener != null) { + mTemp.put(mKey, this.getText().toString()); + if (mKey.equals(RingerDatabase.KEY_RINGER_NAME)) + mListener.onRingerContentChanged(mTemp); + else + mListener.onInfoContentChanged(mTemp); + } + } + + /** + * Sets the key for this ListeningEditText + * + * @param key + * @ author ricky barrette + */ + public void setKey(final String key) { + mKey = key; + } + + /** + * Sets the listener of this ListeningEditText + * + * @param listener + * @ author ricky barrette + */ + public void setListener(final OnContentChangedListener listener) { + mListener = listener; + } + + } + private static final String TAG = "AboutRingerFragment"; private ListeningEditText mRingerName; private ListeningEditText mRingerDescription; private ToggleButton mRingerEnabled; private final OnContentChangedListener mListener; private final ContentValues mInfo; + private final ContentValues mRinger; - - public AboutRingerFragment(final ContentValues ringer, final ContentValues info, final OnContentChangedListener listener){ + + public AboutRingerFragment(final ContentValues ringer, final ContentValues info, final OnContentChangedListener listener) { super(); - this.mInfo = info; - this.mRinger = ringer; - this.mListener = listener; + mInfo = info; + mRinger = ringer; + mListener = listener; } - + @Override public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) { - if(this.mListener != null){ + if (mListener != null) { final ContentValues info = new ContentValues(); info.put(RingerDatabase.KEY_IS_ENABLED, isChecked); - this.mListener.onRingerContentChanged(info); + mListener.onRingerContentChanged(info); } } @Override public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { - - this.getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); - + + getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); + final View view = inflater.inflate(R.layout.ringer_about_fragment, container, false); - - if(Debug.DEBUG){ - for(Entry item : this.mInfo.valueSet()) - Log.d(TAG, item.getKey() +" = "+ item.getValue()); - - for(Entry item : this.mRinger.valueSet()) - Log.d(TAG, item.getKey() +" = "+ item.getValue()); + + if (Debug.DEBUG) { + for (final Entry item : mInfo.valueSet()) + Log.d(TAG, item.getKey() + " = " + item.getValue()); + + for (final Entry item : mRinger.valueSet()) + Log.d(TAG, item.getKey() + " = " + item.getValue()); } - + /* * ringer name */ - this.mRingerName = (ListeningEditText) view.findViewById(R.id.ringer_name); - if(this.mRinger.containsKey(RingerDatabase.KEY_RINGER_NAME)) - this.mRingerName.setText(this.mRinger.getAsString(RingerDatabase.KEY_RINGER_NAME)); - this.mRingerName.setKey(RingerDatabase.KEY_RINGER_NAME); - this.mRingerName.setListener(this.mListener); - + mRingerName = (ListeningEditText) view.findViewById(R.id.ringer_name); + if (mRinger.containsKey(RingerDatabase.KEY_RINGER_NAME)) + mRingerName.setText(mRinger.getAsString(RingerDatabase.KEY_RINGER_NAME)); + mRingerName.setKey(RingerDatabase.KEY_RINGER_NAME); + mRingerName.setListener(mListener); + /* * ringer description */ - this.mRingerDescription = (ListeningEditText) view.findViewById(R.id.ringer_description); - if(this.mInfo.containsKey(RingerDatabase.KEY_RINGER_DESCRIPTION)) - this.mRingerDescription.setText(this.mInfo.getAsString(RingerDatabase.KEY_RINGER_DESCRIPTION)); - this.mRingerDescription.setKey(RingerDatabase.KEY_RINGER_DESCRIPTION); - this.mRingerDescription.setListener(this.mListener); - + mRingerDescription = (ListeningEditText) view.findViewById(R.id.ringer_description); + if (mInfo.containsKey(RingerDatabase.KEY_RINGER_DESCRIPTION)) + mRingerDescription.setText(mInfo.getAsString(RingerDatabase.KEY_RINGER_DESCRIPTION)); + mRingerDescription.setKey(RingerDatabase.KEY_RINGER_DESCRIPTION); + mRingerDescription.setListener(mListener); + /* * ringer enabled */ - this.mRingerEnabled = (ToggleButton) view.findViewById(R.id.ringer_enabled); - if(this.mRinger.containsKey(RingerDatabase.KEY_IS_ENABLED)) - this.mRingerEnabled.setChecked(this.mRinger.getAsBoolean(RingerDatabase.KEY_IS_ENABLED)); - this.mRingerEnabled.setOnCheckedChangeListener(this); - + mRingerEnabled = (ToggleButton) view.findViewById(R.id.ringer_enabled); + if (mRinger.containsKey(RingerDatabase.KEY_IS_ENABLED)) + mRingerEnabled.setChecked(mRinger.getAsBoolean(RingerDatabase.KEY_IS_ENABLED)); + mRingerEnabled.setOnCheckedChangeListener(this); + return view; } - - /** - * This Edit text class is used in place of a standard edit text. - * It will update the pass the updated information though a listener - * @author ricky barrette - */ - public static class ListeningEditText extends EditText{ - private String mKey; - private OnContentChangedListener mListener; - private final ContentValues mTemp; - - /** - * Creates a new ListeningEditText - * @param context - * @author ricky barrette - */ - public ListeningEditText(Context context) { - super(context); - this.mTemp = new ContentValues(); - } - - /** - * Creates a new ListeningEditText - * @param context - * @param attrs - * @author ricky barrette - */ - public ListeningEditText(Context context, AttributeSet attrs) { - super(context, attrs); - this.mTemp = new ContentValues(); - } - - /** - * Creates a new ListeningEditText - * @param context - * @param attrs - * @param defStyle - * @author ricky barrette - */ - public ListeningEditText(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - this.mTemp = new ContentValues(); - } - - /** - * Called when the edit text is drawn - * @author ricky barrette - */ - @Override - public void onDraw(Canvas canvas){ - super.onDraw(canvas); - if(mListener != null){ - mTemp.put(this.mKey, this.getText().toString()); - if(this.mKey.equals(RingerDatabase.KEY_RINGER_NAME)) - this.mListener.onRingerContentChanged(mTemp); - else - this.mListener.onInfoContentChanged(mTemp); - } - } - - /** - * Sets the key for this ListeningEditText - * @param key - * @ author ricky barrette - */ - public void setKey(String key){ - this.mKey = key; - } - - /** - * Sets the listener of this ListeningEditText - * @param listener - * @ author ricky barrette - */ - public void setListener(OnContentChangedListener listener){ - this.mListener = listener; - } - - } } \ No newline at end of file diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/BaseFeatureFragment.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/BaseFeatureFragment.java index c43d15d..d7ac093 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/BaseFeatureFragment.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/BaseFeatureFragment.java @@ -6,9 +6,6 @@ */ package com.TwentyCodes.android.LocationRinger.ui.fragments; -import com.TwentyCodes.android.LocationRinger.FeatureRemovedListener; -import com.TwentyCodes.android.LocationRinger.R; - import android.annotation.SuppressLint; import android.os.Bundle; import android.support.v4.app.Fragment; @@ -18,41 +15,47 @@ import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ImageView; +import com.TwentyCodes.android.LocationRinger.FeatureRemovedListener; +import com.TwentyCodes.android.LocationRinger.R; + /** * This is a simple extention of a fragment that will allow for storage of an id + * * @author ricky barrette */ @SuppressLint("ValidFragment") -public class BaseFeatureFragment extends Fragment implements OnClickListener{ +public class BaseFeatureFragment extends Fragment implements OnClickListener { private final int mId; private final FeatureRemovedListener mRemovedListener; private final int mIconRes; private final int mLayout; private ImageView mIcon; - + /** * Creates a new Feature Fragment + * * @param id * @param layout * @param listener * @author ricky barrette */ - public BaseFeatureFragment(int id, int layout, FeatureRemovedListener listener){ + public BaseFeatureFragment(final int id, final int layout, final FeatureRemovedListener listener) { this(id, layout, -1, listener); } - + /** * Creates a new FeatureFragment + * * @param id * @param layout * @param icon * @param listener * @author ricky barrette */ - public BaseFeatureFragment(int id, int layout, int icon, FeatureRemovedListener listener){ + public BaseFeatureFragment(final int id, final int layout, final int icon, final FeatureRemovedListener listener) { super(); - if ( listener == null ) + if (listener == null) throw new NullPointerException(); mRemovedListener = listener; mId = id; @@ -69,36 +72,40 @@ public class BaseFeatureFragment extends Fragment implements OnClickListener{ /** * Called when the user clicks the remove button + * * @param v * @author ricky barrette */ @Override - public void onClick(View v) { - if(v.getId() == R.id.close) - if(this.mRemovedListener != null) - this.mRemovedListener.onFeatureRemoved(this); + public void onClick(final View v) { + if (v.getId() == R.id.close) + if (mRemovedListener != null) + mRemovedListener.onFeatureRemoved(this); } - + /** * (non-Javadoc) - * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) + * + * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, + * android.view.ViewGroup, android.os.Bundle) */ @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { final View view = inflater.inflate(mLayout, container, false); mIcon = (ImageView) view.findViewById(R.id.icon); view.findViewById(R.id.close).setOnClickListener(this); - if(this.mIconRes != -1) - setIcon(this.mIconRes); + if (mIconRes != -1) + setIcon(mIconRes); return view; } - + /** * Sets the icon of this feature fragment + * * @param icon * @author ricky barrette */ - public void setIcon(int icon){ - mIcon.setImageDrawable(this.getActivity().getResources().getDrawable(icon)); + public void setIcon(final int icon) { + mIcon.setImageDrawable(getActivity().getResources().getDrawable(icon)); } } \ No newline at end of file diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/BaseFragmentListFragment.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/BaseFragmentListFragment.java index 5a018d0..b955221 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/BaseFragmentListFragment.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/BaseFragmentListFragment.java @@ -22,6 +22,7 @@ import com.TwentyCodes.android.LocationRinger.debug.Debug; /** * This fragment will be used to display a list of fragments + * * @author ricky barrette */ public abstract class BaseFragmentListFragment extends Fragment { @@ -33,9 +34,10 @@ public abstract class BaseFragmentListFragment extends Fragment { /** * Creates a new Populated BaseFragmentListFragment + * * @author ricky barrette */ - public BaseFragmentListFragment(ArrayList fragments, int layout, int container) { + public BaseFragmentListFragment(final ArrayList fragments, final int layout, final int container) { super(); mFragments = fragments; mLayout = layout; @@ -44,11 +46,12 @@ public abstract class BaseFragmentListFragment extends Fragment { /** * Creates a new Empty Base Fragment List + * * @param layout * @param container * @author ricky barrette */ - public BaseFragmentListFragment(int layout, int container) { + public BaseFragmentListFragment(final int layout, final int container) { mLayout = layout; mContainer = container; mFragments = new ArrayList(); @@ -56,58 +59,64 @@ public abstract class BaseFragmentListFragment extends Fragment { /** * Adds the fragment to the list + * * @param fragment * @author ricky barrette */ - public void add(final Fragment fragment){ - this.mFragments.add(fragment); - final FragmentTransaction transaction = this.getFragmentManager().beginTransaction(); + public void add(final Fragment fragment) { + mFragments.add(fragment); + final FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.add(mContainer, fragment, fragment.getTag()); transaction.commit(); } - + /** * Adds a collection ofs fragments to the list + * * @param fragments * @author ricky barrette */ - public void addAll(final ArrayList fragments){ - final FragmentTransaction transaction = this.getFragmentManager().beginTransaction(); - for(Fragment f : fragments){ - this.mFragments.add(f); + public void addAll(final ArrayList fragments) { + final FragmentTransaction transaction = getFragmentManager().beginTransaction(); + for (final Fragment f : fragments) { + mFragments.add(f); transaction.add(mContainer, f, f.getTag()); } transaction.commit(); } - + /** - * Adds a collection ofs fragments to the list, but doesn't preform any transactions + * Adds a collection ofs fragments to the list, but doesn't preform any + * transactions + * * @param fragment * @author ricky barrette */ - protected void addAllInit(final ArrayList fragments){ - for(Fragment f : fragments){ - this.mFragments.add(f); - } + protected void addAllInit(final ArrayList fragments) { + for (final Fragment f : fragments) + mFragments.add(f); } - + /** * Loads all the fragments + * * @author ricky barrette */ private void loadFragments() { - final FragmentTransaction transaction = this.getFragmentManager().beginTransaction(); - for(Fragment fragment : this.mFragments) + final FragmentTransaction transaction = getFragmentManager().beginTransaction(); + for (final Fragment fragment : mFragments) transaction.add(mContainer, fragment, fragment.getTag()); transaction.commit(); } /** * (non-Javadoc) - * @see android.support.v4.app.Fragment#onActivityResult(int, int, android.content.Intent) + * + * @see android.support.v4.app.Fragment#onActivityResult(int, int, + * android.content.Intent) */ @Override - public void onActivityResult(int arg0, int arg1, Intent arg2) { + public void onActivityResult(final int arg0, final int arg1, final Intent arg2) { removeFragments(); loadFragments(); super.onActivityResult(arg0, arg1, arg2); @@ -115,32 +124,35 @@ public abstract class BaseFragmentListFragment extends Fragment { /** * (non-Javadoc) + * * @see android.support.v4.app.ListFragment#onCreateView(android.view.LayoutInflater, * android.view.ViewGroup, android.os.Bundle) */ @Override - public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle bundle) { + public View onCreateView(final LayoutInflater inflator, final ViewGroup container, final Bundle bundle) { return inflator.inflate(mLayout, null); } - + /** * (non-Javadoc) + * * @see android.support.v4.app.Fragment#onPause() */ @Override public void onPause() { - try{ + try { removeFragments(); - } catch(IllegalStateException e){ + } catch (final IllegalStateException e) { e.printStackTrace(); - //do nothing + // do nothing } - Collections.reverse(this.mFragments); + Collections.reverse(mFragments); super.onPause(); } - + /** * (non-Javadoc) + * * @see android.support.v4.app.Fragment#onResume() */ @Override @@ -153,26 +165,27 @@ public abstract class BaseFragmentListFragment extends Fragment { /** * Removes a fragment from the list + * * @param fragment * @author ricky barrette */ - public void remove(final Fragment fragment){ - this.mFragments.remove(fragment); - final FragmentTransaction transaction = this.getFragmentManager().beginTransaction(); + public void remove(final Fragment fragment) { + mFragments.remove(fragment); + final FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.remove(fragment); transaction.commit(); } /** * Removes all fragments from the the view + * * @throws IllegalStateException * @author ricky barrette */ private void removeFragments() throws IllegalStateException { - final FragmentTransaction transaction = this.getFragmentManager().beginTransaction(); - for(Fragment fragment : this.mFragments){ - transaction.remove(fragment); - } + final FragmentTransaction transaction = getFragmentManager().beginTransaction(); + for (final Fragment fragment : mFragments) + transaction.remove(fragment); transaction.commit(); } } diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java index 810df61..be3f6e5 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/FeatureListFragment.java @@ -30,11 +30,12 @@ import com.TwentyCodes.android.LocationRinger.db.RingerDatabase; /** * This fragment will be used to display a list of features + * * @author ricky */ @SuppressLint("ValidFragment") public class FeatureListFragment extends BaseFragmentListFragment implements OnClickListener, android.content.DialogInterface.OnClickListener, FeatureRemovedListener { - + private static final int KEY_ADDED_RINGTONE = 0; private static final int KEY_ADDED_NOTIFICATIONTONE = 1; private static final int KEY_ADDED_ALARM_VOLUME = 2; @@ -45,234 +46,236 @@ public class FeatureListFragment extends BaseFragmentListFragment implements OnC private final OnContentChangedListener mListener; private final ArrayList mAdded; + /** + * Creates a new empty feature list fragment + * + * @param info + * @param listener + * @author ricky barrette + */ + public FeatureListFragment(final ContentValues info, final OnContentChangedListener listener) { + this(info, listener, new ArrayList(), new ArrayList()); + } + /** * Creates a new populated FeatureListFragment + * * @param info * @param listener * @param fragments * @author ricky barrette */ - public FeatureListFragment(ContentValues info, OnContentChangedListener listener, ArrayList fragments, ArrayList added) { + public FeatureListFragment(final ContentValues info, final OnContentChangedListener listener, final ArrayList fragments, final ArrayList added) { super(R.layout.fragment_list_contianer, R.id.fragment_list_contianer); - - if ( info == null ) + + if (info == null) throw new NullPointerException(); - if ( listener == null ) + if (listener == null) throw new NullPointerException(); - if ( fragments == null ) + if (fragments == null) throw new NullPointerException(); - if ( added == null ) + if (added == null) throw new NullPointerException(); - + mInfo = info; mListener = listener; mAdded = added; } - - /** - * Creates a new empty feature list fragment - * @param info - * @param listener - * @author ricky barrette - */ - public FeatureListFragment(ContentValues info, OnContentChangedListener listener){ - this(info, listener, new ArrayList(), new ArrayList()); - } /** * Initializes a feature fragment + * * @param fragmentCode * @return * @author ricky barrette */ - public Fragment initFeatureFragment(int fragmentCode){ + public Fragment initFeatureFragment(final int fragmentCode) { Fragment f = null; - switch(fragmentCode){ - case KEY_ADDED_RINGTONE: - f= new RingtoneFragment(this.mInfo, this.mListener, this, AudioManager.STREAM_RING, KEY_ADDED_RINGTONE); - mAdded.add(KEY_ADDED_RINGTONE); - break; - case KEY_ADDED_NOTIFICATIONTONE: - f = new RingtoneFragment(this.mInfo, this.mListener, this, AudioManager.STREAM_NOTIFICATION, KEY_ADDED_NOTIFICATIONTONE); - mAdded.add(KEY_ADDED_NOTIFICATIONTONE); - break; - case KEY_ADDED_ALARM_VOLUME: - f = new VolumeFragment(this.mInfo, this.getActivity(), this.mListener, this, AudioManager.STREAM_ALARM, KEY_ADDED_ALARM_VOLUME); - mAdded.add(KEY_ADDED_ALARM_VOLUME); - break; - case KEY_ADDED_MUSIC_VOLUME: - f = new VolumeFragment(this.mInfo, this.getActivity(), this.mListener, this, AudioManager.STREAM_MUSIC, KEY_ADDED_MUSIC_VOLUME); - mAdded.add(KEY_ADDED_MUSIC_VOLUME); - break; - case KEY_ADDED_BT: - f = new ToggleButtonFragment(R.drawable.ic_action_bluetooth, this.getString(R.string.bluetooth), RingerDatabase.KEY_BT, this.mInfo, this.mListener, this, KEY_ADDED_BT); - mAdded.add(KEY_ADDED_BT); - break; - case KEY_ADDED_WIFI: - f = new ToggleButtonFragment(R.drawable.ic_action_wifi, this.getString(R.string.wifi), RingerDatabase.KEY_WIFI, this.mInfo, this.mListener, this, KEY_ADDED_WIFI); - mAdded.add(KEY_ADDED_WIFI); - break; - + switch (fragmentCode) { + case KEY_ADDED_RINGTONE: + f = new RingtoneFragment(mInfo, mListener, this, AudioManager.STREAM_RING, KEY_ADDED_RINGTONE); + mAdded.add(KEY_ADDED_RINGTONE); + break; + case KEY_ADDED_NOTIFICATIONTONE: + f = new RingtoneFragment(mInfo, mListener, this, AudioManager.STREAM_NOTIFICATION, KEY_ADDED_NOTIFICATIONTONE); + mAdded.add(KEY_ADDED_NOTIFICATIONTONE); + break; + case KEY_ADDED_ALARM_VOLUME: + f = new VolumeFragment(mInfo, getActivity(), mListener, this, AudioManager.STREAM_ALARM, KEY_ADDED_ALARM_VOLUME); + mAdded.add(KEY_ADDED_ALARM_VOLUME); + break; + case KEY_ADDED_MUSIC_VOLUME: + f = new VolumeFragment(mInfo, getActivity(), mListener, this, AudioManager.STREAM_MUSIC, KEY_ADDED_MUSIC_VOLUME); + mAdded.add(KEY_ADDED_MUSIC_VOLUME); + break; + case KEY_ADDED_BT: + f = new ToggleButtonFragment(R.drawable.ic_action_bluetooth, this.getString(R.string.bluetooth), RingerDatabase.KEY_BT, mInfo, mListener, this, KEY_ADDED_BT); + mAdded.add(KEY_ADDED_BT); + break; + case KEY_ADDED_WIFI: + f = new ToggleButtonFragment(R.drawable.ic_action_wifi, this.getString(R.string.wifi), RingerDatabase.KEY_WIFI, mInfo, mListener, this, KEY_ADDED_WIFI); + mAdded.add(KEY_ADDED_WIFI); + break; + } return f; } /** - * Initializes feature fragments based upon current records + * Initializes feature fragments based upon current records + * * @author ricky barrette */ private ArrayList initList() { - ArrayList what = new ArrayList(); - - if(this.mInfo.containsKey(RingerDatabase.KEY_RINGTONE_VOLUME)){ + final ArrayList what = new ArrayList(); + + if (mInfo.containsKey(RingerDatabase.KEY_RINGTONE_VOLUME)) what.add(initFeatureFragment(KEY_ADDED_RINGTONE)); - } - - if(this.mInfo.containsKey(RingerDatabase.KEY_NOTIFICATION_RINGTONE_VOLUME)){ + + if (mInfo.containsKey(RingerDatabase.KEY_NOTIFICATION_RINGTONE_VOLUME)) what.add(initFeatureFragment(KEY_ADDED_NOTIFICATIONTONE)); - } - - if(this.mInfo.containsKey(RingerDatabase.KEY_ALARM_VOLUME)){ + + if (mInfo.containsKey(RingerDatabase.KEY_ALARM_VOLUME)) what.add(initFeatureFragment(KEY_ADDED_ALARM_VOLUME)); - } - - if(this.mInfo.containsKey(RingerDatabase.KEY_MUSIC_VOLUME)){ + + if (mInfo.containsKey(RingerDatabase.KEY_MUSIC_VOLUME)) what.add(initFeatureFragment(KEY_ADDED_MUSIC_VOLUME)); - } - - if(this.mInfo.containsKey(RingerDatabase.KEY_BT)){ + + if (mInfo.containsKey(RingerDatabase.KEY_BT)) what.add(initFeatureFragment(KEY_ADDED_BT)); - } - - if(this.mInfo.containsKey(RingerDatabase.KEY_WIFI)){ + + if (mInfo.containsKey(RingerDatabase.KEY_WIFI)) what.add(initFeatureFragment(KEY_ADDED_WIFI)); - } return what; } - + /** - * Called when an item is picked from the add featue list - * (non-Javadoc) - * @see android.content.DialogInterface.OnClickListener#onClick(android.content.DialogInterface, int) + * Called when an item is picked from the add featue list (non-Javadoc) + * + * @see android.content.DialogInterface.OnClickListener#onClick(android.content.DialogInterface, + * int) */ @Override - public void onClick(DialogInterface dialog, int which) { + public void onClick(final DialogInterface dialog, final int which) { final Fragment f = initFeatureFragment(which); - if(f != null) + if (f != null) add(f); } /** - * Called when the add feature button is clicked - * (non-Javadoc) + * Called when the add feature button is clicked (non-Javadoc) + * * @see android.view.View.OnClickListener#onClick(android.view.View) */ @Override - public void onClick(View v) { - new AlertDialog.Builder(this.getActivity()) - .setTitle(R.string.add_feature) - .setAdapter( - new ArrayAdapter(this.getActivity(), android.R.layout.simple_list_item_1, this.getResources().getStringArray(R.array.features)){ - - /** - * we override this, because we want to filter which items are enabled - * (non-Javadoc) - * @see android.widget.BaseAdapter#areAllItemsEnabled() - */ - @Override - public boolean areAllItemsEnabled(){ - return false; + public void onClick(final View v) { + new AlertDialog.Builder(getActivity()).setTitle(R.string.add_feature) + .setAdapter(new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.features)) { + + /** + * we override this, because we want to filter which items + * are enabled (non-Javadoc) + * + * @see android.widget.BaseAdapter#areAllItemsEnabled() + */ + @Override + public boolean areAllItemsEnabled() { + return false; + } + + /** + * here we want to grey out disabled items in the list + * (non-Javadoc) + * + * @see android.widget.ArrayAdapter#getView(int, + * android.view.View, android.view.ViewGroup) + */ + @Override + public View getView(final int position, final View convertView, final ViewGroup parent) { + final View v = super.getView(position, convertView, parent); + v.setEnabled(isEnabled(position)); + + if (Integer.valueOf(android.os.Build.VERSION.SDK_INT) < 11) { + final TextView t = (TextView) v.findViewById(android.R.id.text1); + t.setTextColor(isEnabled(position) ? Color.BLACK : Color.GRAY); } - - /** - * here we want to grey out disabled items in the list - * (non-Javadoc) - * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup) - */ - @Override - public View getView(int position, View convertView, ViewGroup parent){ - final View v = super.getView(position, convertView, parent); - v.setEnabled(isEnabled(position)); - - if(Integer.valueOf(android.os.Build.VERSION.SDK_INT) < 11){ - final TextView t = (TextView) v.findViewById(android.R.id.text1); - t.setTextColor(isEnabled(position) ? Color.BLACK : Color.GRAY); - } - - return v; - } - - /** - * here we can notify the adaptor if an item should be enabled or not - * (non-Javadoc) - * @see android.widget.BaseAdapter#isEnabled(int) - */ - @Override - public boolean isEnabled(int position){ - return ! mAdded.contains(position); - } - }, this) - .show(); + + return v; + } + + /** + * here we can notify the adaptor if an item should be + * enabled or not (non-Javadoc) + * + * @see android.widget.BaseAdapter#isEnabled(int) + */ + @Override + public boolean isEnabled(final int position) { + return !mAdded.contains(position); + } + }, this).show(); } /** - * Called when the activity is first created - * (non-Javadoc) + * Called when the activity is first created (non-Javadoc) + * * @see android.support.v4.app.Fragment#onCreate(android.os.Bundle) */ @Override - public void onCreate(Bundle arg0) { + public void onCreate(final Bundle arg0) { addAllInit(initList()); super.onCreate(arg0); } /** - * Called when the view needs to be created - * (non-Javadoc) - * @see com.TwentyCodes.android.LocationRinger.ui.fragments.BaseFragmentListFragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) + * Called when the view needs to be created (non-Javadoc) + * + * @see com.TwentyCodes.android.LocationRinger.ui.fragments.BaseFragmentListFragment#onCreateView(android.view.LayoutInflater, + * android.view.ViewGroup, android.os.Bundle) */ @Override - public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle bundle) { - final View v = super.onCreateView(inflator, container, bundle); + public View onCreateView(final LayoutInflater inflator, final ViewGroup container, final Bundle bundle) { + final View v = super.onCreateView(inflator, container, bundle); v.findViewById(R.id.add_feature_button).setOnClickListener(this); return v; } /** - * Called when a fragment needs to be removed - * (non-Javadoc) + * Called when a fragment needs to be removed (non-Javadoc) + * * @see com.TwentyCodes.android.LocationRinger.FeatureRemovedListener#onFeatureRemoved(android.support.v4.app.Fragment) */ @Override - public void onFeatureRemoved(Fragment f) { - this.remove(f); - - if(f instanceof BaseFeatureFragment){ - final int id = ((BaseFeatureFragment) f).getFragmentId(); + public void onFeatureRemoved(final Fragment f) { + remove(f); + + if (f instanceof BaseFeatureFragment) { + final int id = ((BaseFeatureFragment) f).getFragmentId(); mAdded.remove(Integer.valueOf(id)); - + /* - * we need to notify our parent activity that the feature have been removed. + * we need to notify our parent activity that the feature have been + * removed. */ - switch(id){ - case KEY_ADDED_RINGTONE: - this.mListener.onInfoContentRemoved(RingerDatabase.KEY_RINGTONE_URI, RingerDatabase.KEY_RINGTONE_VOLUME); - break; - case KEY_ADDED_NOTIFICATIONTONE: - this.mListener.onInfoContentRemoved(RingerDatabase.KEY_NOTIFICATION_RINGTONE_URI, RingerDatabase.KEY_NOTIFICATION_RINGTONE_VOLUME); - break; - case KEY_ADDED_ALARM_VOLUME: - this.mListener.onInfoContentRemoved(RingerDatabase.KEY_ALARM_VOLUME); - break; - case KEY_ADDED_MUSIC_VOLUME: - this.mListener.onInfoContentRemoved(RingerDatabase.KEY_MUSIC_VOLUME); - break; - case KEY_ADDED_BT: - this.mListener.onInfoContentRemoved(RingerDatabase.KEY_BT); - break; - case KEY_ADDED_WIFI: - this.mListener.onInfoContentRemoved(RingerDatabase.KEY_WIFI); - break; + switch (id) { + case KEY_ADDED_RINGTONE: + mListener.onInfoContentRemoved(RingerDatabase.KEY_RINGTONE_URI, RingerDatabase.KEY_RINGTONE_VOLUME); + break; + case KEY_ADDED_NOTIFICATIONTONE: + mListener.onInfoContentRemoved(RingerDatabase.KEY_NOTIFICATION_RINGTONE_URI, RingerDatabase.KEY_NOTIFICATION_RINGTONE_VOLUME); + break; + case KEY_ADDED_ALARM_VOLUME: + mListener.onInfoContentRemoved(RingerDatabase.KEY_ALARM_VOLUME); + break; + case KEY_ADDED_MUSIC_VOLUME: + mListener.onInfoContentRemoved(RingerDatabase.KEY_MUSIC_VOLUME); + break; + case KEY_ADDED_BT: + mListener.onInfoContentRemoved(RingerDatabase.KEY_BT); + break; + case KEY_ADDED_WIFI: + mListener.onInfoContentRemoved(RingerDatabase.KEY_WIFI); + break; } } } diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/LocationInfomationFragment.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/LocationInfomationFragment.java index 61ae5dd..9cb9e0d 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/LocationInfomationFragment.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/LocationInfomationFragment.java @@ -36,11 +36,14 @@ import com.TwentyCodes.android.overlays.RadiusOverlay; import com.google.android.maps.GeoPoint; /** - * This fragment will be used to display and allow the user to edit the ringers location trigger + * This fragment will be used to display and allow the user to edit the ringers + * location trigger + * * @author ricky */ @SuppressLint("ValidFragment") -public class LocationInfomationFragment extends Fragment implements GeoPointLocationListener, OnClickListener, OnCheckedChangeListener, OnSeekBarChangeListener, OnLocationSelectedListener, SearchRequestedListener { +public class LocationInfomationFragment extends Fragment implements GeoPointLocationListener, OnClickListener, OnCheckedChangeListener, OnSeekBarChangeListener, + OnLocationSelectedListener, SearchRequestedListener { private static final String TAG = "RingerInformationHowActivity"; private final ContentValues mInfo; @@ -53,177 +56,177 @@ public class LocationInfomationFragment extends Fragment implements GeoPointLoca private GeoPoint mPoint; private SkyHook mSkyHook; private View view; - + /** * Creates a new MapFragment + * * @author ricky barrette - * @param ringerInformationActivity + * @param ringerInformationActivity */ public LocationInfomationFragment(final ContentValues info, final OnContentChangedListener listener, final EnableScrollingListener enabledListener) { - this.mInfo = info; - this.mListener = listener; - this.mEnableScrollingListener = enabledListener; + mInfo = info; + mListener = listener; + mEnableScrollingListener = enabledListener; } /** * Called when a toggle button's state is changed + * * @author ricky barrette */ @Override public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) { - view.findViewById(R.id.buttons).setVisibility(isChecked ? View.VISIBLE : View.GONE); - - if(mEnableScrollingListener != null) - mEnableScrollingListener.setScrollEnabled(!isChecked); - - if(isChecked) - this.mSkyHook.getUpdates(); - else - this.mSkyHook.removeUpdates(); - - this.mMap.setDoubleTapZoonEnabled(isChecked); - //buttons - this.mMap.setBuiltInZoomControls(isChecked); - this.mMap.setClickable(isChecked); - this.mRadius.setEnabled(isChecked); - Toast.makeText(this.getActivity(), isChecked ? getString(R.string.map_editing_enabled) : getString(R.string.map_editiing_disabled), Toast.LENGTH_SHORT).show(); + view.findViewById(R.id.buttons).setVisibility(isChecked ? View.VISIBLE : View.GONE); + + if (mEnableScrollingListener != null) + mEnableScrollingListener.setScrollEnabled(!isChecked); + + if (isChecked) + mSkyHook.getUpdates(); + else + mSkyHook.removeUpdates(); + + mMap.setDoubleTapZoonEnabled(isChecked); + // buttons + mMap.setBuiltInZoomControls(isChecked); + mMap.setClickable(isChecked); + mRadius.setEnabled(isChecked); + Toast.makeText(getActivity(), isChecked ? getString(R.string.map_editing_enabled) : getString(R.string.map_editiing_disabled), Toast.LENGTH_SHORT).show(); } /** * Called when a view is clicked + * * @author ricky barrette */ @Override public void onClick(final View v) { - switch (v.getId()){ - case R.id.mark_my_location: - if(this.mPoint != null) - onLocationSelected(mPoint); - break; - case R.id.my_location: - if(this.mPoint != null) - this.mMap.setMapCenter(mPoint); - break; - case R.id.map_mode: - this.mMap.setSatellite(mMap.isSatellite() ? false : true); - break; - case R.id.search: - new SearchDialog(this.getActivity(), this).show(); - break; + switch (v.getId()) { + case R.id.mark_my_location: + if (mPoint != null) + onLocationSelected(mPoint); + break; + case R.id.my_location: + if (mPoint != null) + mMap.setMapCenter(mPoint); + break; + case R.id.map_mode: + mMap.setSatellite(mMap.isSatellite() ? false : true); + break; + case R.id.search: + new SearchDialog(getActivity(), this).show(); + break; } } - - @Override public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { view = inflater.inflate(R.layout.map_info_fragment, container, false); - - this.mSkyHook = new SkyHook(this.getActivity()); - this.mSkyHook.setLocationListener(this); - - this.mMap = (MapFragment) this.getFragmentManager().findFragmentById(R.id.mapview); - this.mRadius = (SeekBar) view.findViewById(R.id.radius); - this.mRadius.setMax(Debug.MAX_RADIUS_IN_METERS); - this.mMap.setClickable(false); - this.mMapEditToggle = (ToggleButton) view.findViewById(R.id.map_edit_toggle); - this.mMapEditToggle.setChecked(false); - this.mMapEditToggle.setOnCheckedChangeListener(this); - this.mRadiusOverlay = new RadiusOverlay(); - this.mRadiusOverlay.setLocationSelectedListener(this); - this.mRadius.setOnSeekBarChangeListener(this); - this.mMap.addOverlay(mRadiusOverlay); - this.mRadius.setEnabled(false); - - if (this.mInfo.get(RingerDatabase.KEY_LOCATION) != null){ - final String[] point = this.mInfo.getAsString(RingerDatabase.KEY_LOCATION).split(","); - this.mRadiusOverlay.setLocation(new GeoPoint(Integer.parseInt(point[0]), Integer.parseInt(point[1]))); + + mSkyHook = new SkyHook(getActivity()); + mSkyHook.setLocationListener(this); + + mMap = (MapFragment) getFragmentManager().findFragmentById(R.id.mapview); + mRadius = (SeekBar) view.findViewById(R.id.radius); + mRadius.setMax(Debug.MAX_RADIUS_IN_METERS); + mMap.setClickable(false); + mMapEditToggle = (ToggleButton) view.findViewById(R.id.map_edit_toggle); + mMapEditToggle.setChecked(false); + mMapEditToggle.setOnCheckedChangeListener(this); + mRadiusOverlay = new RadiusOverlay(); + mRadiusOverlay.setLocationSelectedListener(this); + mRadius.setOnSeekBarChangeListener(this); + mMap.addOverlay(mRadiusOverlay); + mRadius.setEnabled(false); + + if (mInfo.get(RingerDatabase.KEY_LOCATION) != null) { + final String[] point = mInfo.getAsString(RingerDatabase.KEY_LOCATION).split(","); + mRadiusOverlay.setLocation(new GeoPoint(Integer.parseInt(point[0]), Integer.parseInt(point[1]))); } - - if (this.mInfo.get(RingerDatabase.KEY_RADIUS) != null){ - this.mRadius.setProgress(this.mInfo.getAsInteger(RingerDatabase.KEY_RADIUS)); + + if (mInfo.get(RingerDatabase.KEY_RADIUS) != null) + mRadius.setProgress(mInfo.getAsInteger(RingerDatabase.KEY_RADIUS)); + + if (mRadiusOverlay.getLocation() != null) { + mMap.setMapCenter(mRadiusOverlay.getLocation()); + mMap.setZoom(16); } - - if(this.mRadiusOverlay.getLocation() != null){ - this.mMap.setMapCenter(this.mRadiusOverlay.getLocation()); - this.mMap.setZoom(16); - } - - this.mMap.setDoubleTapZoonEnabled(false); - + + mMap.setDoubleTapZoonEnabled(false); + view.findViewById(R.id.my_location).setOnClickListener(this); view.findViewById(R.id.mark_my_location).setOnClickListener(this); view.findViewById(R.id.search).setOnClickListener(this); view.findViewById(R.id.map_mode).setOnClickListener(this); - + return view; } /** - * Called when the location is a first fix - * (non-Javadoc) + * Called when the location is a first fix (non-Javadoc) + * * @see com.TwentyCodes.android.location.GeoPointLocationListener#onFirstFix(boolean) */ @Override - public void onFirstFix(final boolean isFirstFix) { + public void onFirstFix(final boolean isFirstFix) { if (isFirstFix) mMap.enableGPSProgess(); else mMap.disableGPSProgess(); - - if(mPoint != null){ - + + if (mPoint != null) /* - * if this is the first fix and the radius overlay does not have a point specified - * then pan the map, and zoom in to the users current location + * if this is the first fix and the radius overlay does not have a + * point specified then pan the map, and zoom in to the users + * current location */ - if(isFirstFix) - if(this.mRadiusOverlay.getLocation() == null){ - if(this.mMap != null){ - this.mMap.setMapCenter(mPoint); - this.mMap.setZoom((this.mMap.getMap().getMaxZoomLevel() - 5)); + if (isFirstFix) + if (mRadiusOverlay.getLocation() == null) + if (mMap != null) { + mMap.setMapCenter(mPoint); + mMap.setZoom(mMap.getMap().getMaxZoomLevel() - 5); } - } - } } /** * Called when skyhook has a location to report + * * @author ricky barrette */ @Override public void onLocationChanged(final GeoPoint point, final int accuracy) { - this.mPoint = point; + mPoint = point; } /** - * Called when a location has been selected - * (non-Javadoc) + * Called when a location has been selected (non-Javadoc) + * * @see com.TwentyCodes.android.location.OnLocationSelectedListener#onLocationSelected(com.google.android.maps.GeoPoint) */ @Override public void onLocationSelected(final GeoPoint point) { - if(point != null){ - if(Debug.DEBUG) - Log.d(TAG, "onLocationSelected() "+ point.toString()); + if (point != null) { + if (Debug.DEBUG) + Log.d(TAG, "onLocationSelected() " + point.toString()); - if(this.mRadiusOverlay != null) - this.mRadiusOverlay.setLocation(point); - - if(this.mMap != null) - this.mMap.setMapCenter(point); - - if(this.mListener != null){ + if (mRadiusOverlay != null) + mRadiusOverlay.setLocation(point); + + if (mMap != null) + mMap.setMapCenter(point); + + if (mListener != null) { final ContentValues info = new ContentValues(); info.put(RingerDatabase.KEY_LOCATION, point.toString()); - this.mListener.onInfoContentChanged(info); + mListener.onInfoContentChanged(info); } - } else if(Debug.DEBUG) + } else if (Debug.DEBUG) Log.d(TAG, "onLocationSelected() Location was null"); } /** * (non-Javadoc) + * * @see android.support.v4.app.Fragment#onPause() */ @Override @@ -234,41 +237,44 @@ public class LocationInfomationFragment extends Fragment implements GeoPointLoca /** * Called when a seekbar is has its progress changed + * * @author ricky barrette */ @Override public void onProgressChanged(final SeekBar seekBar, final int progress, final boolean fromUser) { - switch (seekBar.getId()){ - case R.id.radius: - this.mRadiusOverlay.setRadius(progress); - this.mMap.invalidate(); - if(this.mListener != null){ - final ContentValues info = new ContentValues(); - info.put(RingerDatabase.KEY_RADIUS, progress); - this.mListener.onInfoContentChanged(info); - } - break; + switch (seekBar.getId()) { + case R.id.radius: + mRadiusOverlay.setRadius(progress); + mMap.invalidate(); + if (mListener != null) { + final ContentValues info = new ContentValues(); + info.put(RingerDatabase.KEY_RADIUS, progress); + mListener.onInfoContentChanged(info); + } + break; } } /** * (non-Javadoc) + * * @see android.support.v4.app.Fragment#onResume() */ @Override public void onResume() { - if(mMapEditToggle.isChecked()) + if (mMapEditToggle.isChecked()) mSkyHook.getUpdates(); super.onResume(); } /** * (non-Javadoc) + * * @see com.TwentyCodes.android.LocationRinger.SearchRequestedListener#onSearchRequested() */ @Override public boolean onSearchRequested() { - new SearchDialog(this.getActivity(), this).show(); + new SearchDialog(getActivity(), this).show(); return true; } @@ -276,13 +282,16 @@ public class LocationInfomationFragment extends Fragment implements GeoPointLoca public void onStartTrackingTouch(final SeekBar seekBar) { } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see com.google.android.maps.MapActivity#onDestroy() + * * @author ricky barrette */ @Override public void onStop() { - this.mSkyHook.removeUpdates(); + mSkyHook.removeUpdates(); super.onDestroy(); } diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/MapFragment.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/MapFragment.java index 4f46f05..51aea45 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/MapFragment.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/MapFragment.java @@ -24,10 +24,11 @@ public class MapFragment extends BaseMapFragment { /** * (non-Javadoc) + * * @see com.TwentyCodes.android.fragments.BaseMapFragment#onMapViewCreate(com.TwentyCodes.android.location.MapView) */ @Override - public void onMapViewCreate(MapView map) { + public void onMapViewCreate(final MapView map) { // TODO Auto-generated method stub } diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/RingtoneFragment.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/RingtoneFragment.java index 1a42940..9dec0bc 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/RingtoneFragment.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/RingtoneFragment.java @@ -36,11 +36,12 @@ import com.TwentyCodes.android.LocationRinger.debug.Debug; /** * This fragment will be for ringtone settings + * * @author ricky */ @SuppressLint("ValidFragment") public class RingtoneFragment extends BaseFeatureFragment implements OnClickListener, OnSeekBarChangeListener { - + private static final String TAG = "RingtoneFragment"; private final int mStream; private final int mType; @@ -52,77 +53,85 @@ public class RingtoneFragment extends BaseFeatureFragment implements OnClickList private EditText mRingtone; private Uri mRingtoneURI; private SeekBar mVolume; - - public RingtoneFragment(ContentValues info, OnContentChangedListener changedListener, FeatureRemovedListener removedListener, int stream, int id){ + + public RingtoneFragment(final ContentValues info, final OnContentChangedListener changedListener, final FeatureRemovedListener removedListener, final int stream, + final int id) { super(id, R.layout.ringtone_fragment, removedListener); - - if ( info == null ) + + if (info == null) throw new NullPointerException(); - if ( changedListener == null ) + if (changedListener == null) throw new NullPointerException(); - - this.mChangedListener = changedListener; - this.mStream = stream; - this.mInfo = info; - - switch(stream){ - case AudioManager.STREAM_NOTIFICATION: - mKeyUri = RingerDatabase.KEY_NOTIFICATION_RINGTONE_URI; - mKeyVolume = RingerDatabase.KEY_NOTIFICATION_RINGTONE_VOLUME; - mLabel = R.string.notification_ringtone; - mType = RingtoneManager.TYPE_NOTIFICATION; - break; - - case AudioManager.STREAM_RING: - default: - mKeyUri = RingerDatabase.KEY_RINGTONE_URI; - mKeyVolume = RingerDatabase.KEY_RINGTONE_VOLUME; - mLabel = R.string.ringtone; - mType = RingtoneManager.TYPE_RINGTONE; - break; + + mChangedListener = changedListener; + mStream = stream; + mInfo = info; + + switch (stream) { + case AudioManager.STREAM_NOTIFICATION: + mKeyUri = RingerDatabase.KEY_NOTIFICATION_RINGTONE_URI; + mKeyVolume = RingerDatabase.KEY_NOTIFICATION_RINGTONE_VOLUME; + mLabel = R.string.notification_ringtone; + mType = RingtoneManager.TYPE_NOTIFICATION; + break; + + case AudioManager.STREAM_RING: + default: + mKeyUri = RingerDatabase.KEY_RINGTONE_URI; + mKeyVolume = RingerDatabase.KEY_RINGTONE_VOLUME; + mLabel = R.string.ringtone; + mType = RingtoneManager.TYPE_RINGTONE; + break; } } /** - * starts the ringtone picker - * @param ringtoneCode RingtoneManager.TYPE_? - * @param uri of current tone + * starts the ringtone picker + * + * @param ringtoneCode + * RingtoneManager.TYPE_? + * @param uri + * of current tone * @author ricky barrette */ - private void getRingtoneURI(final int ringtoneCode, final Uri uri){ - final Intent intent = new Intent( RingtoneManager.ACTION_RINGTONE_PICKER); - intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, ringtoneCode); - intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, R.string.select_tone); - intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true); - intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, false); - intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, uri); - startActivityForResult( intent, ringtoneCode); + private void getRingtoneURI(final int ringtoneCode, final Uri uri) { + final Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); + intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, ringtoneCode); + intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, R.string.select_tone); + intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true); + intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, false); + intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, uri); + startActivityForResult(intent, ringtoneCode); } - + /** - * Notifys the listener that the ringtone has changedRingtoneManager.getActualDefaultRingtoneUri(this.getActivity(), mType) + * Notifys the listener that the ringtone has + * changedRingtoneManager.getActualDefaultRingtoneUri(this.getActivity(), + * mType) + * * @param tone * @author ricky barrette */ - private void notifyRingtoneChanged(Uri tone) { - if(this.mChangedListener != null){ - ContentValues info = new ContentValues(); - info.put(this.mKeyUri, tone != null ? tone.toString() : null); - this.mChangedListener.onInfoContentChanged(info); + private void notifyRingtoneChanged(final Uri tone) { + if (mChangedListener != null) { + final ContentValues info = new ContentValues(); + info.put(mKeyUri, tone != null ? tone.toString() : null); + mChangedListener.onInfoContentChanged(info); } } - + /** * Notifys the listener that the volume has changed + * * @param progress * @author ricky barrette */ - private void notifyVolumeChanged(int progress) { + private void notifyVolumeChanged(final int progress) { setIcon(progress == 0 ? R.drawable.ic_action_silent : R.drawable.ic_action_volume); - if(this.mChangedListener != null){ + if (mChangedListener != null) { final ContentValues info = new ContentValues(); - info.put(this.mKeyVolume, progress); - this.mChangedListener.onInfoContentChanged(info); + info.put(mKeyVolume, progress); + mChangedListener.onInfoContentChanged(info); } } @@ -130,34 +139,35 @@ public class RingtoneFragment extends BaseFeatureFragment implements OnClickList * Called when the ringtone picker activity returns it's result */ @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { + public void onActivityResult(final int requestCode, final int resultCode, final Intent data) { if (resultCode == Activity.RESULT_OK) { final Uri tone = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); - if(tone == null){ - this.mRingtone.setText(R.string.silent); + if (tone == null) { + mRingtone.setText(R.string.silent); mVolume.setEnabled(false); mVolume.setProgress(0); notifyVolumeChanged(0); } else { mVolume.setEnabled(true); - Ringtone ringtone = RingtoneManager.getRingtone(this.getActivity(), Uri.parse(tone.toString())); - this.mRingtone.setText(ringtone.getTitle(this.getActivity())); + final Ringtone ringtone = RingtoneManager.getRingtone(getActivity(), Uri.parse(tone.toString())); + mRingtone.setText(ringtone.getTitle(getActivity())); } - + notifyRingtoneChanged(tone); } super.onActivityResult(requestCode, resultCode, data); } - + /** * Called when a view is clicked + * * @author ricky barrette */ @Override - public void onClick(View v) { - switch(v.getId()){ + public void onClick(final View v) { + switch (v.getId()) { case R.id.ringtone: - getRingtoneURI(this.mType, mRingtoneURI); + getRingtoneURI(mType, mRingtoneURI); break; default: super.onClick(v); @@ -166,81 +176,81 @@ public class RingtoneFragment extends BaseFeatureFragment implements OnClickList } @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { final View view = super.onCreateView(inflater, container, savedInstanceState); - final AudioManager audioManager = (AudioManager) this.getActivity().getSystemService(Context.AUDIO_SERVICE); - - if(Debug.DEBUG) - for(Entry item : this.mInfo.valueSet()) - Log.d(TAG, item.getKey() +" = "+ item.getValue()); - + final AudioManager audioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE); + + if (Debug.DEBUG) + for (final Entry item : mInfo.valueSet()) + Log.d(TAG, item.getKey() + " = " + item.getValue()); + /* * initialize the views */ final TextView label = (TextView) view.findViewById(R.id.title); label.setText(mLabel); - + setIcon(R.drawable.ic_action_volume); - - this.mRingtone = (EditText) view.findViewById(R.id.ringtone); + + mRingtone = (EditText) view.findViewById(R.id.ringtone); mVolume = (SeekBar) view.findViewById(R.id.ringtone_volume); - - this.mRingtone.setOnClickListener(this); + + mRingtone.setOnClickListener(this); mVolume.setMax(audioManager.getStreamMaxVolume(mStream)); - + view.findViewById(R.id.close).setOnClickListener(this); - + /* * volume */ - if(this.mInfo.containsKey(this.mKeyVolume)) - mVolume.setProgress(Integer.parseInt(this.mInfo.getAsString(this.mKeyVolume))); + if (mInfo.containsKey(mKeyVolume)) + mVolume.setProgress(Integer.parseInt(mInfo.getAsString(mKeyVolume))); else { mVolume.setProgress(audioManager.getStreamVolume(mStream)); notifyVolumeChanged(audioManager.getStreamVolume(mStream)); } - + /* * ringtone & uri */ - if(this.mInfo.containsKey(this.mKeyUri)){ - try{ - this.mRingtoneURI = Uri.parse(this.mInfo.getAsString(this.mKeyUri)); - } catch (NullPointerException e){ - this.mRingtoneURI = null; + if (mInfo.containsKey(mKeyUri)) { + try { + mRingtoneURI = Uri.parse(mInfo.getAsString(mKeyUri)); + } catch (final NullPointerException e) { + mRingtoneURI = null; } - this.mVolume.setEnabled(this.mInfo.getAsString(this.mKeyUri) != null); + mVolume.setEnabled(mInfo.getAsString(mKeyUri) != null); } else { - this.mRingtoneURI = RingtoneManager.getActualDefaultRingtoneUri(this.getActivity(), mType); - notifyRingtoneChanged(this.mRingtoneURI); + mRingtoneURI = RingtoneManager.getActualDefaultRingtoneUri(getActivity(), mType); + notifyRingtoneChanged(mRingtoneURI); } try { - this.mRingtone.setText(RingtoneManager.getRingtone(this.getActivity(), mRingtoneURI).getTitle(this.getActivity())); - } catch (NullPointerException e) { + mRingtone.setText(RingtoneManager.getRingtone(getActivity(), mRingtoneURI).getTitle(getActivity())); + } catch (final NullPointerException e) { mVolume.setEnabled(false); mRingtone.setText(R.string.silent); mVolume.setProgress(0); } - + setIcon(mVolume.getProgress() == 0 ? R.drawable.ic_action_silent : R.drawable.ic_action_volume); - + mVolume.setOnSeekBarChangeListener(this); return view; } @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - if(fromUser) + public void onProgressChanged(final SeekBar seekBar, final int progress, final boolean fromUser) { + if (fromUser) notifyVolumeChanged(progress); } @Override - public void onStartTrackingTouch(SeekBar seekBar) { + public void onStartTrackingTouch(final SeekBar seekBar) { } @Override - public void onStopTrackingTouch(SeekBar seekBar) { + public void onStopTrackingTouch(final SeekBar seekBar) { } - + } \ No newline at end of file diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/ToggleButtonFragment.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/ToggleButtonFragment.java index dd3ab28..49d6837 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/ToggleButtonFragment.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/ToggleButtonFragment.java @@ -24,6 +24,7 @@ import com.TwentyCodes.android.LocationRinger.db.RingerDatabase; /** * A simple fragment that displays a toggle button and a title label + * * @author ricky */ @SuppressLint("ValidFragment") @@ -36,55 +37,59 @@ public class ToggleButtonFragment extends BaseFeatureFragment implements OnCheck /** * Creates a new ToggleButtonFtagment + * * @author ricky barrette */ - public ToggleButtonFragment(int icon, String title, String key, ContentValues info, OnContentChangedListener changedListener, FeatureRemovedListener removedListener, int id) { + public ToggleButtonFragment(final int icon, final String title, final String key, final ContentValues info, final OnContentChangedListener changedListener, + final FeatureRemovedListener removedListener, final int id) { super(id, R.layout.toggle_button_fragment, icon, removedListener); - - if ( info == null ) + + if (info == null) throw new NullPointerException(); - if (title == null ) + if (title == null) throw new NullPointerException(); - if ( key == null ) + if (key == null) throw new NullPointerException(); - if ( changedListener == null ) + if (changedListener == null) throw new NullPointerException(); - - this.mTitle = title; - this.mKey = key; - this.mInfo = info; - this.mChangedListener = changedListener; + + mTitle = title; + mKey = key; + mInfo = info; + mChangedListener = changedListener; } /** - * Called when the fragment's view needs to be created - * (non-Javadoc) - * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) + * Called when the toggle button is clicked (non-Javadoc) + * + * @see android.widget.CompoundButton.OnCheckedChangeListener#onCheckedChanged(android.widget.CompoundButton, + * boolean) */ @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) { + if (mChangedListener != null) { + final ContentValues info = new ContentValues(); + info.put(mKey, isChecked); + mChangedListener.onInfoContentChanged(info); + } + } + + /** + * Called when the fragment's view needs to be created (non-Javadoc) + * + * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, + * android.view.ViewGroup, android.os.Bundle) + */ + @Override + public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { final View view = super.onCreateView(inflater, container, savedInstanceState); final TextView t = (TextView) view.findViewById(R.id.title); - t.setText(this.mTitle); - + t.setText(mTitle); + final ToggleButton b = (ToggleButton) view.findViewById(R.id.toggle); - if(this.mInfo.containsKey(this.mKey)) - b.setChecked(RingerDatabase.parseBoolean(this.mInfo.getAsString(this.mKey))); + if (mInfo.containsKey(mKey)) + b.setChecked(RingerDatabase.parseBoolean(mInfo.getAsString(mKey))); b.setOnCheckedChangeListener(this); return view; } - - /** - * Called when the toggle button is clicked - * (non-Javadoc) - * @see android.widget.CompoundButton.OnCheckedChangeListener#onCheckedChanged(android.widget.CompoundButton, boolean) - */ - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - if(this.mChangedListener != null){ - ContentValues info = new ContentValues(); - info.put(this.mKey, isChecked); - this.mChangedListener.onInfoContentChanged(info); - } - } } \ No newline at end of file diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/VolumeFragment.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/VolumeFragment.java index d1661c1..0d090f4 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/VolumeFragment.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/fragments/VolumeFragment.java @@ -29,6 +29,7 @@ import com.TwentyCodes.android.LocationRinger.debug.Debug; /** * This fragment will represent the volume fragments + * * @author ricky */ @SuppressLint("ValidFragment") @@ -41,122 +42,126 @@ public class VolumeFragment extends BaseFeatureFragment implements OnSeekBarChan private final String mKey; private final ContentValues mInfo; private final int mLabel; - + /** * Creates a new Volume Fragment + * * @param info * @param context * @param changedListener * @param stream * @author ricky barrette */ - public VolumeFragment(ContentValues info, Context context, OnContentChangedListener changedListener, FeatureRemovedListener removedListener, int stream, int id){ + public VolumeFragment(final ContentValues info, final Context context, final OnContentChangedListener changedListener, final FeatureRemovedListener removedListener, + final int stream, final int id) { super(id, R.layout.volume_fragment, removedListener); - - if ( info == null ) - throw new NullPointerException(); - if ( context == null ) - throw new NullPointerException(); - if ( changedListener == null ) - throw new NullPointerException(); - - this.mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); - this.mStream = stream; - this.mChangedListener = changedListener; - this.mInfo = info; - - switch(this.mStream){ - case AudioManager.STREAM_ALARM: - this.mLabel = R.string.alarm_volume; - this.mKey = RingerDatabase.KEY_ALARM_VOLUME; - break; - case AudioManager.STREAM_DTMF: - this.mLabel = R.string.dtmf_volume; - this.mKey = RingerDatabase.KEY_DTMF_VOLUME; - break; - case AudioManager.STREAM_MUSIC: - this.mLabel = R.string.music_volume; - this.mKey = RingerDatabase.KEY_MUSIC_VOLUME; - break; - case AudioManager.STREAM_NOTIFICATION: - this.mLabel = R.string.notification_volume; - this.mKey = RingerDatabase.KEY_NOTIFICATION_RINGTONE_VOLUME; - break; - case AudioManager.STREAM_RING: - this.mLabel = R.string.ringtone_volume; - this.mKey = RingerDatabase.KEY_RINGTONE_VOLUME; - break; - case AudioManager.STREAM_SYSTEM: - this.mLabel = R.string.system_volume; - this.mKey = RingerDatabase.KEY_SYSTEM_VOLUME; - break; - case AudioManager.STREAM_VOICE_CALL: - this.mLabel = R.string.call_volume; - this.mKey = RingerDatabase.KEY_CALL_VOLUME; - break; - default: - this.mLabel = R.string.volume; - this.mKey = RingerDatabase.KEY_RINGTONE_VOLUME; - break; - } - } - /** - * Called when the fragment's view needs to be created - * (non-Javadoc) - * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) - */ - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - - if(Debug.DEBUG) - for(Entry item : this.mInfo.valueSet()) - Log.d(TAG, item.getKey() +" = "+ item.getValue()); - - final View view = super.onCreateView(inflater, container, savedInstanceState); - final TextView label = (TextView) view.findViewById(R.id.title); - final SeekBar volume = (SeekBar) view.findViewById(R.id.volume); - volume.setMax(this.mAudioManager.getStreamMaxVolume(mStream)); - volume.setProgress(this.mAudioManager.getStreamVolume(mStream)); - volume.setOnSeekBarChangeListener(this); - - label.setText(mLabel); - - if(this.mInfo.containsKey(this.mKey)) - volume.setProgress(Integer.parseInt(this.mInfo.getAsString(this.mKey))); - else - notifyListener(this.mAudioManager.getStreamVolume(mStream)); - - setIcon(volume.getProgress() == 0 ? R.drawable.ic_action_silent : R.drawable.ic_action_volume); - return view; - } + if (info == null) + throw new NullPointerException(); + if (context == null) + throw new NullPointerException(); + if (changedListener == null) + throw new NullPointerException(); - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - if(fromUser){ - notifyListener(progress); - setIcon(progress == 0 ? R.drawable.ic_action_silent : R.drawable.ic_action_volume); + mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + mStream = stream; + mChangedListener = changedListener; + mInfo = info; + + switch (mStream) { + case AudioManager.STREAM_ALARM: + mLabel = R.string.alarm_volume; + mKey = RingerDatabase.KEY_ALARM_VOLUME; + break; + case AudioManager.STREAM_DTMF: + mLabel = R.string.dtmf_volume; + mKey = RingerDatabase.KEY_DTMF_VOLUME; + break; + case AudioManager.STREAM_MUSIC: + mLabel = R.string.music_volume; + mKey = RingerDatabase.KEY_MUSIC_VOLUME; + break; + case AudioManager.STREAM_NOTIFICATION: + mLabel = R.string.notification_volume; + mKey = RingerDatabase.KEY_NOTIFICATION_RINGTONE_VOLUME; + break; + case AudioManager.STREAM_RING: + mLabel = R.string.ringtone_volume; + mKey = RingerDatabase.KEY_RINGTONE_VOLUME; + break; + case AudioManager.STREAM_SYSTEM: + mLabel = R.string.system_volume; + mKey = RingerDatabase.KEY_SYSTEM_VOLUME; + break; + case AudioManager.STREAM_VOICE_CALL: + mLabel = R.string.call_volume; + mKey = RingerDatabase.KEY_CALL_VOLUME; + break; + default: + mLabel = R.string.volume; + mKey = RingerDatabase.KEY_RINGTONE_VOLUME; + break; } } /** * Notifys the listener of changes made to the volume + * * @param progress * @author ricky barrette */ private void notifyListener(final int progress) { - if(this.mChangedListener != null){ + if (mChangedListener != null) { final ContentValues info = new ContentValues(); - info.put(this.mKey, progress); - this.mChangedListener.onInfoContentChanged(info); + info.put(mKey, progress); + mChangedListener.onInfoContentChanged(info); + } + } + + /** + * Called when the fragment's view needs to be created (non-Javadoc) + * + * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, + * android.view.ViewGroup, android.os.Bundle) + */ + @Override + public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { + + if (Debug.DEBUG) + for (final Entry item : mInfo.valueSet()) + Log.d(TAG, item.getKey() + " = " + item.getValue()); + + final View view = super.onCreateView(inflater, container, savedInstanceState); + final TextView label = (TextView) view.findViewById(R.id.title); + final SeekBar volume = (SeekBar) view.findViewById(R.id.volume); + volume.setMax(mAudioManager.getStreamMaxVolume(mStream)); + volume.setProgress(mAudioManager.getStreamVolume(mStream)); + volume.setOnSeekBarChangeListener(this); + + label.setText(mLabel); + + if (mInfo.containsKey(mKey)) + volume.setProgress(Integer.parseInt(mInfo.getAsString(mKey))); + else + notifyListener(mAudioManager.getStreamVolume(mStream)); + + setIcon(volume.getProgress() == 0 ? R.drawable.ic_action_silent : R.drawable.ic_action_volume); + return view; + } + + @Override + public void onProgressChanged(final SeekBar seekBar, final int progress, final boolean fromUser) { + if (fromUser) { + notifyListener(progress); + setIcon(progress == 0 ? R.drawable.ic_action_silent : R.drawable.ic_action_volume); } } @Override - public void onStartTrackingTouch(SeekBar seekBar) { + public void onStartTrackingTouch(final SeekBar seekBar) { } @Override - public void onStopTrackingTouch(SeekBar seekBar) { + public void onStopTrackingTouch(final SeekBar seekBar) { } } \ No newline at end of file diff --git a/LocationRinger/src/com/jakewharton/android/viewpagerindicator/FragmentAdapter.java b/LocationRinger/src/com/jakewharton/android/viewpagerindicator/FragmentAdapter.java index 451924b..b8fc6ba 100644 --- a/LocationRinger/src/com/jakewharton/android/viewpagerindicator/FragmentAdapter.java +++ b/LocationRinger/src/com/jakewharton/android/viewpagerindicator/FragmentAdapter.java @@ -14,32 +14,34 @@ import android.support.v4.app.FragmentPagerAdapter; /** * This adaptor maintains the How and What fragments + * * @author ricky */ class FragmentAdapter extends FragmentPagerAdapter { - private ArrayList mFragments; + private final ArrayList mFragments; /** * Creates a new FragmentAdaptor + * * @param fm - * @param fragments to be displayed + * @param fragments + * to be displayed * @author ricky barrette */ - public FragmentAdapter(FragmentManager fm, ArrayList fragments) { + public FragmentAdapter(final FragmentManager fm, final ArrayList fragments) { super(fm); - this.mFragments = fragments; - } - - @Override - public Fragment getItem(int position) { - return this.mFragments.get(position); + mFragments = fragments; } @Override public int getCount() { - return this.mFragments.size(); + return mFragments.size(); } - - + + @Override + public Fragment getItem(final int position) { + return mFragments.get(position); + } + } \ No newline at end of file diff --git a/LocationRinger/src/com/jakewharton/android/viewpagerindicator/PageIndicator.java b/LocationRinger/src/com/jakewharton/android/viewpagerindicator/PageIndicator.java index f965233..cf3e0eb 100644 --- a/LocationRinger/src/com/jakewharton/android/viewpagerindicator/PageIndicator.java +++ b/LocationRinger/src/com/jakewharton/android/viewpagerindicator/PageIndicator.java @@ -24,35 +24,39 @@ import android.support.v4.view.ViewPager; * number and the current visible view. */ public interface PageIndicator extends ViewPager.OnPageChangeListener { - /** - * Bind the indicator to a ViewPager. - * - * @param view - */ - public void setViewPager(ViewPager view); + /** + *

+ * Set the current page of both the ViewPager and indicator. + *

+ * + *

+ * This must be used if you need to set the page before the + * views are drawn on screen (e.g., default start page). + *

+ * + * @param item + */ + public void setCurrentItem(int item); - /** - * Bind the indicator to a ViewPager. - * - * @param view - * @param initialPosition - */ - public void setViewPager(ViewPager view, int initialPosition); + /** + * Set a page change listener which will receive forwarded events. + * + * @param listener + */ + public void setOnPageChangeListener(ViewPager.OnPageChangeListener listener); - /** - *

Set the current page of both the ViewPager and indicator.

- * - *

This must be used if you need to set the page before - * the views are drawn on screen (e.g., default start page).

- * - * @param item - */ - public void setCurrentItem(int item); + /** + * Bind the indicator to a ViewPager. + * + * @param view + */ + public void setViewPager(ViewPager view); - /** - * Set a page change listener which will receive forwarded events. - * - * @param listener - */ - public void setOnPageChangeListener(ViewPager.OnPageChangeListener listener); + /** + * Bind the indicator to a ViewPager. + * + * @param view + * @param initialPosition + */ + public void setViewPager(ViewPager view, int initialPosition); } diff --git a/LocationRinger/src/com/jakewharton/android/viewpagerindicator/TitlePageIndicator.java b/LocationRinger/src/com/jakewharton/android/viewpagerindicator/TitlePageIndicator.java index b0657be..63338e7 100644 --- a/LocationRinger/src/com/jakewharton/android/viewpagerindicator/TitlePageIndicator.java +++ b/LocationRinger/src/com/jakewharton/android/viewpagerindicator/TitlePageIndicator.java @@ -39,594 +39,576 @@ import com.TwentyCodes.android.LocationRinger.R; /** * A TitlePageIndicator is a PageIndicator which displays the title of left view * (if exist), the title of the current select view (centered) and the title of - * the right view (if exist). When the user scrolls the ViewPager then titles are - * also scrolled. + * the right view (if exist). When the user scrolls the ViewPager then titles + * are also scrolled. */ public class TitlePageIndicator extends TextView implements PageIndicator, View.OnTouchListener { - private static final float UNDERLINE_FADE_PERCENTAGE = 0.25f; - - public enum IndicatorStyle { - None(0), Triangle(1), Underline(2); - - public final int value; - - private IndicatorStyle(int value) { - this.value = value; - } - - public static IndicatorStyle fromValue(int value) { - for (IndicatorStyle style : IndicatorStyle.values()) { - if (style.value == value) { - return style; - } - } - return null; - } - } - - private ViewPager mViewPager; - private ViewPager.OnPageChangeListener mListener; - private TitleProvider mTitleProvider; - private int mCurrentPage; - private int mCurrentOffset; - private final Paint mPaintText; - private final Paint mPaintSelected; - private final Path mPath; - private final Paint mPaintFooterLine; - private IndicatorStyle mFooterIndicatorStyle; - private final Paint mPaintFooterIndicator; - private float mFooterIndicatorHeight; - private float mFooterIndicatorPadding; - private float mFooterIndicatorUnderlinePadding; - private float mTitlePadding; - /** Left and right side padding for not active view titles. */ - private float mClipPadding; - private float mFooterLineHeight; - - - public TitlePageIndicator(Context context) { - this(context, null); - } - - public TitlePageIndicator(Context context, AttributeSet attrs) { - this(context, attrs, R.attr.titlePageIndicatorStyle); - } - - public TitlePageIndicator(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - super.setOnTouchListener(this); - mPath = new Path(); - - //Load defaults from resources - final Resources res = getResources(); - final int defaultFooterColor = res.getColor(R.color.default_title_indicator_footer_color); - final float defaultFooterLineHeight = res.getDimension(R.dimen.default_title_indicator_footer_line_height); - final int defaultFooterIndicatorStyle = res.getInteger(R.integer.default_title_indicator_footer_indicator_style); - final float defaultFooterIndicatorHeight = res.getDimension(R.dimen.default_title_indicator_footer_indicator_height); - final float defaultFooterIndicatorPadding = res.getDimension(R.dimen.default_title_indicator_footer_indicator_padding); - final float defaultFooterIndicatorUnderlinePadding = res.getDimension(R.dimen.default_title_indicator_footer_indicator_underline_padding); - final int defaultSelectedColor = res.getColor(R.color.default_title_indicator_selected_color); - final boolean defaultSelectedBold = res.getBoolean(R.bool.default_title_indicator_selected_bold); - final int defaultTextColor = res.getColor(R.color.default_title_indicator_text_color); - final float defaultTextSize = res.getDimension(R.dimen.default_title_indicator_text_size); - final float defaultTitlePadding = res.getDimension(R.dimen.default_title_indicator_title_padding); - final float defaultClipPadding = res.getDimension(R.dimen.default_title_indicator_clip_padding); - - //Retrieve styles attributes - TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TitlePageIndicator, defStyle, R.style.Widget_TitlePageIndicator); - - //Retrieve the colors to be used for this view and apply them. - mFooterLineHeight = a.getDimension(R.styleable.TitlePageIndicator_footerLineHeight, defaultFooterLineHeight); - mFooterIndicatorStyle = IndicatorStyle.fromValue(a.getInteger(R.styleable.TitlePageIndicator_footerIndicatorStyle, defaultFooterIndicatorStyle)); - mFooterIndicatorHeight = a.getDimension(R.styleable.TitlePageIndicator_footerIndicatorHeight, defaultFooterIndicatorHeight); - mFooterIndicatorPadding = a.getDimension(R.styleable.TitlePageIndicator_footerIndicatorPadding, defaultFooterIndicatorPadding); - mFooterIndicatorUnderlinePadding = a.getDimension(R.styleable.TitlePageIndicator_footerIndicatorUnderlinePadding, defaultFooterIndicatorUnderlinePadding); - mTitlePadding = a.getDimension(R.styleable.TitlePageIndicator_titlePadding, defaultTitlePadding); - mClipPadding = a.getDimension(R.styleable.TitlePageIndicator_clipPadding, defaultClipPadding); - - final float textSize = a.getDimension(R.styleable.TitlePageIndicator_textSize, defaultTextSize); - final int footerColor = a.getColor(R.styleable.TitlePageIndicator_footerColor, defaultFooterColor); - mPaintText = new Paint(); - mPaintText.setColor(a.getColor(R.styleable.TitlePageIndicator_textColor, defaultTextColor)); - mPaintText.setTextSize(textSize); - mPaintText.setAntiAlias(true); - mPaintSelected = new Paint(); - mPaintSelected.setColor(a.getColor(R.styleable.TitlePageIndicator_selectedColor, defaultSelectedColor)); - mPaintSelected.setTextSize(textSize); - mPaintSelected.setFakeBoldText(a.getBoolean(R.styleable.TitlePageIndicator_selectedBold, defaultSelectedBold)); - mPaintSelected.setAntiAlias(true); - mPaintFooterLine = new Paint(); - mPaintFooterLine.setStyle(Paint.Style.FILL_AND_STROKE); - mPaintFooterLine.setStrokeWidth(mFooterLineHeight); - mPaintFooterLine.setColor(footerColor); - mPaintFooterIndicator = new Paint(); - mPaintFooterIndicator.setStyle(Paint.Style.FILL_AND_STROKE); - mPaintFooterIndicator.setColor(footerColor); - - a.recycle(); - } - - - public int getFooterColor() { - return mPaintFooterLine.getColor(); - } - - public void setFooterColor(int footerColor) { - mPaintFooterLine.setColor(footerColor); - invalidate(); - } - - public float getFooterLineHeight() { - return mFooterLineHeight; - } - - public void setFooterLineHeight(float footerLineHeight) { - mFooterLineHeight = footerLineHeight; - invalidate(); - } - - public float getFooterIndicatorHeight() { - return mFooterIndicatorHeight; - } - - public void setFooterIndicatorHeight(float footerTriangleHeight) { - mFooterIndicatorHeight = footerTriangleHeight; - invalidate(); - } - - public IndicatorStyle getFooterIndicatorStyle() { - return mFooterIndicatorStyle; - } - - public void setFooterIndicatorStyle(IndicatorStyle indicatorStyle) { - mFooterIndicatorStyle = indicatorStyle; - invalidate(); - } - - public int getSelectedColor() { - return mPaintSelected.getColor(); - } - - public void setSelectedColor(int selectedColor) { - mPaintSelected.setColor(selectedColor); - invalidate(); - } - - public boolean isSelectedBold() { - return mPaintSelected.isFakeBoldText(); - } - - public void setSelectedBold(boolean selectedBold) { - mPaintSelected.setFakeBoldText(selectedBold); - invalidate(); - } - - public int getTextColor() { - return mPaintText.getColor(); - } - - public void setTextColor(int textColor) { - mPaintText.setColor(textColor); - invalidate(); - } - - public float getTextSize() { - return mPaintText.getTextSize(); - } - - public void setTextSize(float textSize) { - mPaintText.setTextSize(textSize); - invalidate(); - } - - public float getTitlePadding() { - return this.mTitlePadding; - } - - public void setTitlePadding(float titlePadding) { - mTitlePadding = titlePadding; - invalidate(); - } - - public float getClipPadding() { - return this.mClipPadding; - } - - public void setClipPadding(float clipPadding) { - mClipPadding = clipPadding; - invalidate(); - } - - /* - * (non-Javadoc) - * - * @see android.view.View#onDraw(android.graphics.Canvas) - */ - @Override - protected void onDraw(Canvas canvas) { - super.onDraw(canvas); - - //Calculate views bounds - ArrayList bounds = calculateAllBounds(mPaintText); - - final int count = mViewPager.getAdapter().getCount(); - final int countMinusOne = count - 1; - final int halfWidth = getWidth() / 2; - final int left = getLeft(); - final int width = getWidth(); - final int height = getHeight(); - final int leftPlusWidth = left + width; - - //Verify if the current view must be clipped to the screen - Rect curViewBound = bounds.get(mCurrentPage); - int curViewWidth = curViewBound.right - curViewBound.left; - if (curViewBound.left < 0) { - //Try to clip to the screen (left side) - clipViewOnTheLeft(curViewBound, curViewWidth); - } - if (curViewBound.right > leftPlusWidth) { - //Try to clip to the screen (right side) - clipViewOnTheRight(curViewBound, curViewWidth, leftPlusWidth); - } - - //Left views starting from the current position - if (mCurrentPage > 0) { - for (int i = mCurrentPage - 1; i >= 0; i--) { - Rect bound = bounds.get(i); - int w = bound.right - bound.left; - //Is left side is outside the screen - if (bound.left < 0) { - //Try to clip to the screen (left side) - clipViewOnTheLeft(bound, w); - //Except if there's an intersection with the right view - if (i < countMinusOne && mCurrentPage != i) { - Rect rightBound = bounds.get(i + 1); - //Intersection - if (bound.right + (int)mTitlePadding > rightBound.left) { - bound.left = rightBound.left - (w + (int)mTitlePadding); - } - } - } - } - } - //Right views starting from the current position - if (mCurrentPage < countMinusOne) { - for (int i = mCurrentPage + 1 ; i < count; i++) { - Rect bound = bounds.get(i); - int w = bound.right - bound.left; - //If right side is outside the screen - if (bound.right > leftPlusWidth) { - //Try to clip to the screen (right side) - clipViewOnTheRight(bound, w, leftPlusWidth); - //Except if there's an intersection with the left view - if (i > 0 && mCurrentPage != i) { - Rect leftBound = bounds.get(i - 1); - //Intersection - if (bound.left - (int)mTitlePadding < leftBound.right) { - bound.left = leftBound.right + (int)mTitlePadding; - } - } - } - } - } - - //Now draw views - for (int i = 0; i < count; i++) { - //Get the title - Rect bound = bounds.get(i); - //Only if one side is visible - if ((bound.left > left && bound.left < leftPlusWidth) || (bound.right > left && bound.right < leftPlusWidth)) { - Paint paint = mPaintText; - //Change the color is the title is closed to the center - int middle = (bound.left + bound.right) / 2; - if (Math.abs(middle - halfWidth) < 20) { - paint = mPaintSelected; - } - canvas.drawText(mTitleProvider.getTitle(i), bound.left, bound.bottom, paint); - } - } - - //Draw the footer line - mPath.reset(); - mPath.moveTo(0, height - mFooterLineHeight); - mPath.lineTo(width, height - mFooterLineHeight); - mPath.close(); - canvas.drawPath(mPath, mPaintFooterLine); - - switch (mFooterIndicatorStyle) { - default: - case Triangle: - mPath.reset(); - mPath.moveTo(halfWidth, height - mFooterLineHeight - mFooterIndicatorHeight); - mPath.lineTo(halfWidth + mFooterIndicatorHeight, height - mFooterLineHeight); - mPath.lineTo(halfWidth - mFooterIndicatorHeight, height - mFooterLineHeight); - mPath.close(); - canvas.drawPath(mPath, mPaintFooterIndicator); - break; - - case Underline: - float deltaPercentage = mCurrentOffset * 1.0f / width; - int alpha = 0xFF; - int page = mCurrentPage; - if (deltaPercentage <= UNDERLINE_FADE_PERCENTAGE) { - alpha = (int)(0xFF * ((UNDERLINE_FADE_PERCENTAGE - deltaPercentage) / UNDERLINE_FADE_PERCENTAGE)); - } else if (deltaPercentage >= (1 - UNDERLINE_FADE_PERCENTAGE)) { - alpha = (int)(0xFF * ((deltaPercentage - (1 - UNDERLINE_FADE_PERCENTAGE)) / UNDERLINE_FADE_PERCENTAGE)); - page += 1; //We are coming into the next page - } else if (mCurrentOffset != 0) { - break; //Not in underline scope - } - - Rect underlineBounds = bounds.get(page); - mPath.reset(); - mPath.moveTo(underlineBounds.left - mFooterIndicatorUnderlinePadding, height - mFooterLineHeight); - mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding, height - mFooterLineHeight); - mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding, height - mFooterLineHeight - mFooterIndicatorHeight); - mPath.lineTo(underlineBounds.left - mFooterIndicatorUnderlinePadding, height - mFooterLineHeight - mFooterIndicatorHeight); - mPath.close(); - - mPaintFooterIndicator.setAlpha(alpha); - canvas.drawPath(mPath, mPaintFooterIndicator); - mPaintFooterIndicator.setAlpha(0xFF); - break; - } - } - - @Override - public final boolean onTouch(View view, MotionEvent event) { - if ((view != this) || (event.getAction() != MotionEvent.ACTION_DOWN)) { - return false; - } - - final int count = mViewPager.getAdapter().getCount(); - final float halfWidth = getWidth() / 2; - final float sixthWidth = getWidth() / 6; - - if ((mCurrentPage > 0) && (event.getX() < halfWidth - sixthWidth)) { - mViewPager.setCurrentItem(mCurrentPage - 1); - return true; - } else if ((mCurrentPage < count - 1) && (event.getX() > halfWidth + sixthWidth)) { - mViewPager.setCurrentItem(mCurrentPage + 1); - return true; - } - - return false; - } - - @Override - public final void setOnTouchListener(OnTouchListener listener) { - throw new UnsupportedOperationException("This view does not support listening to its touch events."); - } - - /** - * Set bounds for the right textView including clip padding. - * - * @param curViewBound - * current bounds. - * @param curViewWidth - * width of the view. - */ - private void clipViewOnTheRight(Rect curViewBound, int curViewWidth, int leftPlusWidth) { - curViewBound.right = leftPlusWidth - (int)mClipPadding; - curViewBound.left = curViewBound.right - curViewWidth; - } - - /** - * Set bounds for the left textView including clip padding. - * - * @param curViewBound - * current bounds. - * @param curViewWidth - * width of the view. - */ - private void clipViewOnTheLeft(Rect curViewBound, int curViewWidth) { - curViewBound.left = 0 + (int)mClipPadding; - curViewBound.right = curViewWidth; - } - - /** - * Calculate views bounds and scroll them according to the current index - * - * @param paint - * @param currentIndex - * @return - */ - private ArrayList calculateAllBounds(Paint paint) { - ArrayList list = new ArrayList(); - //For each views (If no values then add a fake one) - final int count = mViewPager.getAdapter().getCount(); - final int width = getWidth(); - final int halfWidth = width / 2; - for (int i = 0; i < count; i++) { - Rect bounds = calcBounds(i, paint); - int w = (bounds.right - bounds.left); - int h = (bounds.bottom - bounds.top); - bounds.left = (halfWidth) - (w / 2) - mCurrentOffset + ((i - mCurrentPage) * width); - bounds.right = bounds.left + w; - bounds.top = 0; - bounds.bottom = h; - list.add(bounds); - } - - return list; - } - - /** - * Calculate the bounds for a view's title - * - * @param index - * @param paint - * @return - */ - private Rect calcBounds(int index, Paint paint) { - //Calculate the text bounds - Rect bounds = new Rect(); - bounds.right = (int)paint.measureText(mTitleProvider.getTitle(index)); - bounds.bottom = (int)(paint.descent() - paint.ascent()); - return bounds; - } - - @Override - public void setViewPager(ViewPager view) { - if (view.getAdapter() == null) { - throw new IllegalStateException("ViewPager does not have adapter instance."); - } - if (!(view.getAdapter() instanceof TitleProvider)) { - throw new IllegalStateException("ViewPager adapter must implement TitleProvider to be used with TitlePageIndicator."); - } - mViewPager = view; - mViewPager.setOnPageChangeListener(this); - mTitleProvider = (TitleProvider)mViewPager.getAdapter(); - invalidate(); - } - - @Override - public void setViewPager(ViewPager view, int initialPosition) { - setViewPager(view); - setCurrentItem(initialPosition); - } - - @Override - public void setCurrentItem(int item) { - if (mViewPager == null) { - throw new IllegalStateException("ViewPager has not been bound."); - } - mViewPager.setCurrentItem(item); - mCurrentPage = item; - invalidate(); - } - - @Override - public void onPageScrollStateChanged(int state) { - if (mListener != null) { - mListener.onPageScrollStateChanged(state); - } - } - - @Override - public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { - mCurrentPage = position; - mCurrentOffset = positionOffsetPixels; - invalidate(); - - if (mListener != null) { - mListener.onPageScrolled(position, positionOffset, positionOffsetPixels); - } - } - - @Override - public void onPageSelected(int position) { - if (mListener != null) { - mListener.onPageSelected(position); - } - } - - @Override - public void setOnPageChangeListener(ViewPager.OnPageChangeListener listener) { - mListener = listener; - } - - /* - * (non-Javadoc) - * - * @see android.view.View#onMeasure(int, int) - */ - @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec)); - } - - /** - * Determines the width of this view - * - * @param measureSpec - * A measureSpec packed into an int - * @return The width of the view, honoring constraints from measureSpec - */ - private int measureWidth(int measureSpec) { - int result = 0; - int specMode = MeasureSpec.getMode(measureSpec); - int specSize = MeasureSpec.getSize(measureSpec); - - if (specMode != MeasureSpec.EXACTLY) { - throw new IllegalStateException(getClass().getSimpleName() + " can only be used in EXACTLY mode."); - } - result = specSize; - return result; - } - - /** - * Determines the height of this view - * - * @param measureSpec - * A measureSpec packed into an int - * @return The height of the view, honoring constraints from measureSpec - */ - private int measureHeight(int measureSpec) { - float result = 0; - int specMode = MeasureSpec.getMode(measureSpec); - int specSize = MeasureSpec.getSize(measureSpec); - - if (specMode == MeasureSpec.EXACTLY) { - //We were told how big to be - result = specSize; - } else { - //Calculate the text bounds - Rect bounds = new Rect(); - bounds.bottom = (int) (mPaintText.descent()-mPaintText.ascent()); - result = bounds.bottom - bounds.top + mFooterLineHeight; - if (mFooterIndicatorStyle != IndicatorStyle.None) { - result += mFooterIndicatorHeight + mFooterIndicatorPadding; - } - } - return (int)result; - } - - @Override - public void onRestoreInstanceState(Parcelable state) { - SavedState savedState = (SavedState)state; - super.onRestoreInstanceState(savedState.getSuperState()); - mCurrentPage = savedState.currentPage; - requestLayout(); - } - - @Override - public Parcelable onSaveInstanceState() { - setFreezesText(true); - Parcelable superState = super.onSaveInstanceState(); - SavedState savedState = new SavedState(superState); - savedState.currentPage = mCurrentPage; - return savedState; - } - - static class SavedState extends BaseSavedState { - int currentPage; - - public SavedState(Parcelable superState) { - super(superState); - } - - private SavedState(Parcel in) { - super(in); - currentPage = in.readInt(); - } - - @Override - public void writeToParcel(Parcel dest, int flags) { - super.writeToParcel(dest, flags); - dest.writeInt(currentPage); - } - - public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { - @Override - public SavedState createFromParcel(Parcel in) { - return new SavedState(in); - } - - @Override - public SavedState[] newArray(int size) { - return new SavedState[size]; - } - }; - } + public enum IndicatorStyle { + None(0), Triangle(1), Underline(2); + + public static IndicatorStyle fromValue(final int value) { + for (final IndicatorStyle style : IndicatorStyle.values()) + if (style.value == value) + return style; + return null; + } + + public final int value; + + private IndicatorStyle(final int value) { + this.value = value; + } + } + + static class SavedState extends BaseSavedState { + int currentPage; + + public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { + @Override + public SavedState createFromParcel(final Parcel in) { + return new SavedState(in); + } + + @Override + public SavedState[] newArray(final int size) { + return new SavedState[size]; + } + }; + + private SavedState(final Parcel in) { + super(in); + currentPage = in.readInt(); + } + + public SavedState(final Parcelable superState) { + super(superState); + } + + @Override + public void writeToParcel(final Parcel dest, final int flags) { + super.writeToParcel(dest, flags); + dest.writeInt(currentPage); + } + } + + private static final float UNDERLINE_FADE_PERCENTAGE = 0.25f; + private ViewPager mViewPager; + private ViewPager.OnPageChangeListener mListener; + private TitleProvider mTitleProvider; + private int mCurrentPage; + private int mCurrentOffset; + private final Paint mPaintText; + private final Paint mPaintSelected; + private final Path mPath; + private final Paint mPaintFooterLine; + private IndicatorStyle mFooterIndicatorStyle; + private final Paint mPaintFooterIndicator; + private float mFooterIndicatorHeight; + private final float mFooterIndicatorPadding; + private final float mFooterIndicatorUnderlinePadding; + private float mTitlePadding; + /** Left and right side padding for not active view titles. */ + private float mClipPadding; + + private float mFooterLineHeight; + + public TitlePageIndicator(final Context context) { + this(context, null); + } + + public TitlePageIndicator(final Context context, final AttributeSet attrs) { + this(context, attrs, R.attr.titlePageIndicatorStyle); + } + + public TitlePageIndicator(final Context context, final AttributeSet attrs, final int defStyle) { + super(context, attrs, defStyle); + super.setOnTouchListener(this); + mPath = new Path(); + + // Load defaults from resources + final Resources res = getResources(); + final int defaultFooterColor = res.getColor(R.color.default_title_indicator_footer_color); + final float defaultFooterLineHeight = res.getDimension(R.dimen.default_title_indicator_footer_line_height); + final int defaultFooterIndicatorStyle = res.getInteger(R.integer.default_title_indicator_footer_indicator_style); + final float defaultFooterIndicatorHeight = res.getDimension(R.dimen.default_title_indicator_footer_indicator_height); + final float defaultFooterIndicatorPadding = res.getDimension(R.dimen.default_title_indicator_footer_indicator_padding); + final float defaultFooterIndicatorUnderlinePadding = res.getDimension(R.dimen.default_title_indicator_footer_indicator_underline_padding); + final int defaultSelectedColor = res.getColor(R.color.default_title_indicator_selected_color); + final boolean defaultSelectedBold = res.getBoolean(R.bool.default_title_indicator_selected_bold); + final int defaultTextColor = res.getColor(R.color.default_title_indicator_text_color); + final float defaultTextSize = res.getDimension(R.dimen.default_title_indicator_text_size); + final float defaultTitlePadding = res.getDimension(R.dimen.default_title_indicator_title_padding); + final float defaultClipPadding = res.getDimension(R.dimen.default_title_indicator_clip_padding); + + // Retrieve styles attributes + final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TitlePageIndicator, defStyle, R.style.Widget_TitlePageIndicator); + + // Retrieve the colors to be used for this view and apply them. + mFooterLineHeight = a.getDimension(R.styleable.TitlePageIndicator_footerLineHeight, defaultFooterLineHeight); + mFooterIndicatorStyle = IndicatorStyle.fromValue(a.getInteger(R.styleable.TitlePageIndicator_footerIndicatorStyle, defaultFooterIndicatorStyle)); + mFooterIndicatorHeight = a.getDimension(R.styleable.TitlePageIndicator_footerIndicatorHeight, defaultFooterIndicatorHeight); + mFooterIndicatorPadding = a.getDimension(R.styleable.TitlePageIndicator_footerIndicatorPadding, defaultFooterIndicatorPadding); + mFooterIndicatorUnderlinePadding = a.getDimension(R.styleable.TitlePageIndicator_footerIndicatorUnderlinePadding, defaultFooterIndicatorUnderlinePadding); + mTitlePadding = a.getDimension(R.styleable.TitlePageIndicator_titlePadding, defaultTitlePadding); + mClipPadding = a.getDimension(R.styleable.TitlePageIndicator_clipPadding, defaultClipPadding); + + final float textSize = a.getDimension(R.styleable.TitlePageIndicator_textSize, defaultTextSize); + final int footerColor = a.getColor(R.styleable.TitlePageIndicator_footerColor, defaultFooterColor); + mPaintText = new Paint(); + mPaintText.setColor(a.getColor(R.styleable.TitlePageIndicator_textColor, defaultTextColor)); + mPaintText.setTextSize(textSize); + mPaintText.setAntiAlias(true); + mPaintSelected = new Paint(); + mPaintSelected.setColor(a.getColor(R.styleable.TitlePageIndicator_selectedColor, defaultSelectedColor)); + mPaintSelected.setTextSize(textSize); + mPaintSelected.setFakeBoldText(a.getBoolean(R.styleable.TitlePageIndicator_selectedBold, defaultSelectedBold)); + mPaintSelected.setAntiAlias(true); + mPaintFooterLine = new Paint(); + mPaintFooterLine.setStyle(Paint.Style.FILL_AND_STROKE); + mPaintFooterLine.setStrokeWidth(mFooterLineHeight); + mPaintFooterLine.setColor(footerColor); + mPaintFooterIndicator = new Paint(); + mPaintFooterIndicator.setStyle(Paint.Style.FILL_AND_STROKE); + mPaintFooterIndicator.setColor(footerColor); + + a.recycle(); + } + + /** + * Calculate the bounds for a view's title + * + * @param index + * @param paint + * @return + */ + private Rect calcBounds(final int index, final Paint paint) { + // Calculate the text bounds + final Rect bounds = new Rect(); + bounds.right = (int) paint.measureText(mTitleProvider.getTitle(index)); + bounds.bottom = (int) (paint.descent() - paint.ascent()); + return bounds; + } + + /** + * Calculate views bounds and scroll them according to the current index + * + * @param paint + * @param currentIndex + * @return + */ + private ArrayList calculateAllBounds(final Paint paint) { + final ArrayList list = new ArrayList(); + // For each views (If no values then add a fake one) + final int count = mViewPager.getAdapter().getCount(); + final int width = getWidth(); + final int halfWidth = width / 2; + for (int i = 0; i < count; i++) { + final Rect bounds = calcBounds(i, paint); + final int w = bounds.right - bounds.left; + final int h = bounds.bottom - bounds.top; + bounds.left = halfWidth - w / 2 - mCurrentOffset + (i - mCurrentPage) * width; + bounds.right = bounds.left + w; + bounds.top = 0; + bounds.bottom = h; + list.add(bounds); + } + + return list; + } + + /** + * Set bounds for the left textView including clip padding. + * + * @param curViewBound + * current bounds. + * @param curViewWidth + * width of the view. + */ + private void clipViewOnTheLeft(final Rect curViewBound, final int curViewWidth) { + curViewBound.left = 0 + (int) mClipPadding; + curViewBound.right = curViewWidth; + } + + /** + * Set bounds for the right textView including clip padding. + * + * @param curViewBound + * current bounds. + * @param curViewWidth + * width of the view. + */ + private void clipViewOnTheRight(final Rect curViewBound, final int curViewWidth, final int leftPlusWidth) { + curViewBound.right = leftPlusWidth - (int) mClipPadding; + curViewBound.left = curViewBound.right - curViewWidth; + } + + public float getClipPadding() { + return mClipPadding; + } + + public int getFooterColor() { + return mPaintFooterLine.getColor(); + } + + public float getFooterIndicatorHeight() { + return mFooterIndicatorHeight; + } + + public IndicatorStyle getFooterIndicatorStyle() { + return mFooterIndicatorStyle; + } + + public float getFooterLineHeight() { + return mFooterLineHeight; + } + + public int getSelectedColor() { + return mPaintSelected.getColor(); + } + + public int getTextColor() { + return mPaintText.getColor(); + } + + @Override + public float getTextSize() { + return mPaintText.getTextSize(); + } + + public float getTitlePadding() { + return mTitlePadding; + } + + public boolean isSelectedBold() { + return mPaintSelected.isFakeBoldText(); + } + + /** + * Determines the height of this view + * + * @param measureSpec + * A measureSpec packed into an int + * @return The height of the view, honoring constraints from measureSpec + */ + private int measureHeight(final int measureSpec) { + float result = 0; + final int specMode = MeasureSpec.getMode(measureSpec); + final int specSize = MeasureSpec.getSize(measureSpec); + + if (specMode == MeasureSpec.EXACTLY) + // We were told how big to be + result = specSize; + else { + // Calculate the text bounds + final Rect bounds = new Rect(); + bounds.bottom = (int) (mPaintText.descent() - mPaintText.ascent()); + result = bounds.bottom - bounds.top + mFooterLineHeight; + if (mFooterIndicatorStyle != IndicatorStyle.None) + result += mFooterIndicatorHeight + mFooterIndicatorPadding; + } + return (int) result; + } + + /** + * Determines the width of this view + * + * @param measureSpec + * A measureSpec packed into an int + * @return The width of the view, honoring constraints from measureSpec + */ + private int measureWidth(final int measureSpec) { + int result = 0; + final int specMode = MeasureSpec.getMode(measureSpec); + final int specSize = MeasureSpec.getSize(measureSpec); + + if (specMode != MeasureSpec.EXACTLY) + throw new IllegalStateException(getClass().getSimpleName() + " can only be used in EXACTLY mode."); + result = specSize; + return result; + } + + /* + * (non-Javadoc) + * + * @see android.view.View#onDraw(android.graphics.Canvas) + */ + @Override + protected void onDraw(final Canvas canvas) { + super.onDraw(canvas); + + // Calculate views bounds + final ArrayList bounds = calculateAllBounds(mPaintText); + + final int count = mViewPager.getAdapter().getCount(); + final int countMinusOne = count - 1; + final int halfWidth = getWidth() / 2; + final int left = getLeft(); + final int width = getWidth(); + final int height = getHeight(); + final int leftPlusWidth = left + width; + + // Verify if the current view must be clipped to the screen + final Rect curViewBound = bounds.get(mCurrentPage); + final int curViewWidth = curViewBound.right - curViewBound.left; + if (curViewBound.left < 0) + // Try to clip to the screen (left side) + clipViewOnTheLeft(curViewBound, curViewWidth); + if (curViewBound.right > leftPlusWidth) + // Try to clip to the screen (right side) + clipViewOnTheRight(curViewBound, curViewWidth, leftPlusWidth); + + // Left views starting from the current position + if (mCurrentPage > 0) + for (int i = mCurrentPage - 1; i >= 0; i--) { + final Rect bound = bounds.get(i); + final int w = bound.right - bound.left; + // Is left side is outside the screen + if (bound.left < 0) { + // Try to clip to the screen (left side) + clipViewOnTheLeft(bound, w); + // Except if there's an intersection with the right view + if (i < countMinusOne && mCurrentPage != i) { + final Rect rightBound = bounds.get(i + 1); + // Intersection + if (bound.right + (int) mTitlePadding > rightBound.left) + bound.left = rightBound.left - (w + (int) mTitlePadding); + } + } + } + // Right views starting from the current position + if (mCurrentPage < countMinusOne) + for (int i = mCurrentPage + 1; i < count; i++) { + final Rect bound = bounds.get(i); + final int w = bound.right - bound.left; + // If right side is outside the screen + if (bound.right > leftPlusWidth) { + // Try to clip to the screen (right side) + clipViewOnTheRight(bound, w, leftPlusWidth); + // Except if there's an intersection with the left view + if (i > 0 && mCurrentPage != i) { + final Rect leftBound = bounds.get(i - 1); + // Intersection + if (bound.left - (int) mTitlePadding < leftBound.right) + bound.left = leftBound.right + (int) mTitlePadding; + } + } + } + + // Now draw views + for (int i = 0; i < count; i++) { + // Get the title + final Rect bound = bounds.get(i); + // Only if one side is visible + if (bound.left > left && bound.left < leftPlusWidth || bound.right > left && bound.right < leftPlusWidth) { + Paint paint = mPaintText; + // Change the color is the title is closed to the center + final int middle = (bound.left + bound.right) / 2; + if (Math.abs(middle - halfWidth) < 20) + paint = mPaintSelected; + canvas.drawText(mTitleProvider.getTitle(i), bound.left, bound.bottom, paint); + } + } + + // Draw the footer line + mPath.reset(); + mPath.moveTo(0, height - mFooterLineHeight); + mPath.lineTo(width, height - mFooterLineHeight); + mPath.close(); + canvas.drawPath(mPath, mPaintFooterLine); + + switch (mFooterIndicatorStyle) { + default: + case Triangle: + mPath.reset(); + mPath.moveTo(halfWidth, height - mFooterLineHeight - mFooterIndicatorHeight); + mPath.lineTo(halfWidth + mFooterIndicatorHeight, height - mFooterLineHeight); + mPath.lineTo(halfWidth - mFooterIndicatorHeight, height - mFooterLineHeight); + mPath.close(); + canvas.drawPath(mPath, mPaintFooterIndicator); + break; + + case Underline: + final float deltaPercentage = mCurrentOffset * 1.0f / width; + int alpha = 0xFF; + int page = mCurrentPage; + if (deltaPercentage <= UNDERLINE_FADE_PERCENTAGE) + alpha = (int) (0xFF * ((UNDERLINE_FADE_PERCENTAGE - deltaPercentage) / UNDERLINE_FADE_PERCENTAGE)); + else if (deltaPercentage >= 1 - UNDERLINE_FADE_PERCENTAGE) { + alpha = (int) (0xFF * ((deltaPercentage - (1 - UNDERLINE_FADE_PERCENTAGE)) / UNDERLINE_FADE_PERCENTAGE)); + page += 1; // We are coming into the next page + } else if (mCurrentOffset != 0) + break; // Not in underline scope + + final Rect underlineBounds = bounds.get(page); + mPath.reset(); + mPath.moveTo(underlineBounds.left - mFooterIndicatorUnderlinePadding, height - mFooterLineHeight); + mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding, height - mFooterLineHeight); + mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding, height - mFooterLineHeight - mFooterIndicatorHeight); + mPath.lineTo(underlineBounds.left - mFooterIndicatorUnderlinePadding, height - mFooterLineHeight - mFooterIndicatorHeight); + mPath.close(); + + mPaintFooterIndicator.setAlpha(alpha); + canvas.drawPath(mPath, mPaintFooterIndicator); + mPaintFooterIndicator.setAlpha(0xFF); + break; + } + } + + /* + * (non-Javadoc) + * + * @see android.view.View#onMeasure(int, int) + */ + @Override + protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) { + setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec)); + } + + @Override + public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) { + mCurrentPage = position; + mCurrentOffset = positionOffsetPixels; + invalidate(); + + if (mListener != null) + mListener.onPageScrolled(position, positionOffset, positionOffsetPixels); + } + + @Override + public void onPageScrollStateChanged(final int state) { + if (mListener != null) + mListener.onPageScrollStateChanged(state); + } + + @Override + public void onPageSelected(final int position) { + if (mListener != null) + mListener.onPageSelected(position); + } + + @Override + public void onRestoreInstanceState(final Parcelable state) { + final SavedState savedState = (SavedState) state; + super.onRestoreInstanceState(savedState.getSuperState()); + mCurrentPage = savedState.currentPage; + requestLayout(); + } + + @Override + public Parcelable onSaveInstanceState() { + setFreezesText(true); + final Parcelable superState = super.onSaveInstanceState(); + final SavedState savedState = new SavedState(superState); + savedState.currentPage = mCurrentPage; + return savedState; + } + + @Override + public final boolean onTouch(final View view, final MotionEvent event) { + if (view != this || event.getAction() != MotionEvent.ACTION_DOWN) + return false; + + final int count = mViewPager.getAdapter().getCount(); + final float halfWidth = getWidth() / 2; + final float sixthWidth = getWidth() / 6; + + if (mCurrentPage > 0 && event.getX() < halfWidth - sixthWidth) { + mViewPager.setCurrentItem(mCurrentPage - 1); + return true; + } else if (mCurrentPage < count - 1 && event.getX() > halfWidth + sixthWidth) { + mViewPager.setCurrentItem(mCurrentPage + 1); + return true; + } + + return false; + } + + public void setClipPadding(final float clipPadding) { + mClipPadding = clipPadding; + invalidate(); + } + + @Override + public void setCurrentItem(final int item) { + if (mViewPager == null) + throw new IllegalStateException("ViewPager has not been bound."); + mViewPager.setCurrentItem(item); + mCurrentPage = item; + invalidate(); + } + + public void setFooterColor(final int footerColor) { + mPaintFooterLine.setColor(footerColor); + invalidate(); + } + + public void setFooterIndicatorHeight(final float footerTriangleHeight) { + mFooterIndicatorHeight = footerTriangleHeight; + invalidate(); + } + + public void setFooterIndicatorStyle(final IndicatorStyle indicatorStyle) { + mFooterIndicatorStyle = indicatorStyle; + invalidate(); + } + + public void setFooterLineHeight(final float footerLineHeight) { + mFooterLineHeight = footerLineHeight; + invalidate(); + } + + @Override + public void setOnPageChangeListener(final ViewPager.OnPageChangeListener listener) { + mListener = listener; + } + + @Override + public final void setOnTouchListener(final OnTouchListener listener) { + throw new UnsupportedOperationException("This view does not support listening to its touch events."); + } + + public void setSelectedBold(final boolean selectedBold) { + mPaintSelected.setFakeBoldText(selectedBold); + invalidate(); + } + + public void setSelectedColor(final int selectedColor) { + mPaintSelected.setColor(selectedColor); + invalidate(); + } + + @Override + public void setTextColor(final int textColor) { + mPaintText.setColor(textColor); + invalidate(); + } + + @Override + public void setTextSize(final float textSize) { + mPaintText.setTextSize(textSize); + invalidate(); + } + + public void setTitlePadding(final float titlePadding) { + mTitlePadding = titlePadding; + invalidate(); + } + + @Override + public void setViewPager(final ViewPager view) { + if (view.getAdapter() == null) + throw new IllegalStateException("ViewPager does not have adapter instance."); + if (!(view.getAdapter() instanceof TitleProvider)) + throw new IllegalStateException("ViewPager adapter must implement TitleProvider to be used with TitlePageIndicator."); + mViewPager = view; + mViewPager.setOnPageChangeListener(this); + mTitleProvider = (TitleProvider) mViewPager.getAdapter(); + invalidate(); + } + + @Override + public void setViewPager(final ViewPager view, final int initialPosition) { + setViewPager(view); + setCurrentItem(initialPosition); + } } diff --git a/LocationRinger/src/com/jakewharton/android/viewpagerindicator/TitleProvider.java b/LocationRinger/src/com/jakewharton/android/viewpagerindicator/TitleProvider.java index 979ecb5..633d497 100644 --- a/LocationRinger/src/com/jakewharton/android/viewpagerindicator/TitleProvider.java +++ b/LocationRinger/src/com/jakewharton/android/viewpagerindicator/TitleProvider.java @@ -19,10 +19,11 @@ package com.jakewharton.android.viewpagerindicator; * A TitleProvider provides the title to display according to a view. */ public interface TitleProvider { - /** - * Returns the title of the view at position - * @param position - * @return - */ - public String getTitle(int position); + /** + * Returns the title of the view at position + * + * @param position + * @return + */ + public String getTitle(int position); } diff --git a/LocationRinger/src/com/jakewharton/android/viewpagerindicator/TitledFragmentAdapter.java b/LocationRinger/src/com/jakewharton/android/viewpagerindicator/TitledFragmentAdapter.java index de09700..dd3343d 100644 --- a/LocationRinger/src/com/jakewharton/android/viewpagerindicator/TitledFragmentAdapter.java +++ b/LocationRinger/src/com/jakewharton/android/viewpagerindicator/TitledFragmentAdapter.java @@ -13,26 +13,30 @@ import android.support.v4.app.FragmentManager; /** * This adaptor maintains a ViewPager title indicator. + * * @author ricky */ public class TitledFragmentAdapter extends FragmentAdapter implements TitleProvider { - - private String[] mTitles; - + + private final String[] mTitles; + /** * Creates a new TitleFragmentAdapter + * * @param fm - * @param fragments to be displayed - * @param titles for the fragments + * @param fragments + * to be displayed + * @param titles + * for the fragments * @author ricky barrette */ - public TitledFragmentAdapter(FragmentManager fm, ArrayList fragments, String[] titles) { + public TitledFragmentAdapter(final FragmentManager fm, final ArrayList fragments, final String[] titles) { super(fm, fragments); - this.mTitles = titles; + mTitles = titles; } @Override - public String getTitle(int position) { - return this.mTitles[position]; + public String getTitle(final int position) { + return mTitles[position]; } } \ No newline at end of file