From 0a90c4220bfd48f1b96f1e7a7c142703883cecb8 Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Sat, 2 Jun 2012 15:32:48 -0400 Subject: [PATCH] Started to clean up the shared prefs entries through out the application Signed-off-by: Ricky Barrette --- .../android/LocationRinger/debug/Debug.java | 22 +++++++++++ .../receivers/SystemReceiver.java | 6 +-- .../services/RingerProcessingService.java | 20 +++++----- .../LocationRinger/ui/ListActivity.java | 17 ++++---- .../ui/RingerInformationActivity.java | 2 +- .../LocationRinger/ui/SettingsActivity.java | 39 ++++++++++--------- 6 files changed, 65 insertions(+), 41 deletions(-) diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/debug/Debug.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/debug/Debug.java index e5c9640..1b74ab3 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/debug/Debug.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/debug/Debug.java @@ -6,12 +6,32 @@ */ package com.TwentyCodes.android.LocationRinger.debug; +import android.content.Context; + /** * A convince class containing debugging variables * @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; + + 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; + } + /** * Sets the logging output of this application */ @@ -31,4 +51,6 @@ public class Debug { * Max radius that can be set by a ringer */ public static final int MAX_RADIUS_IN_METERS = 600; + + } \ No newline at end of file diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/receivers/SystemReceiver.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/receivers/SystemReceiver.java index 6968153..ce8d090 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/receivers/SystemReceiver.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/receivers/SystemReceiver.java @@ -39,10 +39,10 @@ public class SystemReceiver extends BroadcastReceiver { * @author ricky barrette */ @Override - public void onReceive(Context context, Intent intent) { + public void onReceive(final Context context, final Intent intent) { if(Debug.DEBUG) Log.d(TAG, "onReceive() ~"+intent.getAction()); - SharedPreferences systemEventHistory = context.getSharedPreferences(TAG, 2); + final SharedPreferences systemEventHistory = context.getSharedPreferences(TAG, Debug.SHARED_PREFS_MODE); Intent i = new Intent(context, LocationService.class) // .putExtra(LocationService.INTENT_EXTRA_PERIOD_BETWEEN_UPDATES, (long) (60000 * Integer.parseInt(context.getSharedPreferences(SettingsActivity.SETTINGS, 2).getString(SettingsActivity.UPDATE_INTVERVAL , "10")))) .putExtra(LocationService.INTENT_EXTRA_REQUIRED_ACCURACY, Integer.parseInt(context.getSharedPreferences(SettingsActivity.SETTINGS, 2).getString(SettingsActivity.ACCURACY , "50"))) @@ -52,7 +52,7 @@ public class SystemReceiver extends BroadcastReceiver { * if the phone finishes booting, then start the service if the user enabled it */ if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){ - if(context.getSharedPreferences(SettingsActivity.SETTINGS, Context.MODE_WORLD_READABLE).getBoolean(SettingsActivity.START_ON_BOOT, false)){ + if(context.getSharedPreferences(SettingsActivity.SETTINGS, Debug.SHARED_PREFS_MODE).getBoolean(SettingsActivity.START_ON_BOOT, false)){ context.startService(i); PassiveLocationListener.requestPassiveLocationUpdates(context, new Intent(context, PassiveLocationChangedReceiver.class)); } diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/services/RingerProcessingService.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/services/RingerProcessingService.java index f2cf615..0445d5b 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/services/RingerProcessingService.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/services/RingerProcessingService.java @@ -58,17 +58,17 @@ public class RingerProcessingService extends Service { * @param id * @author ricky barrette */ - private void applyRinger(ContentValues values) { + private void applyRinger(final ContentValues values) { if(Debug.DEBUG) Log.d(TAG, "applyRigner()"); - String name = values.getAsString(RingerDatabase.KEY_RINGER_NAME); + final String name = values.getAsString(RingerDatabase.KEY_RINGER_NAME); /* * Make it toasty if the user wants to be notified. * This will display a toast msg "Applying " */ - if(this.getSharedPreferences(SettingsActivity.SETTINGS, 2).getBoolean(SettingsActivity.TOASTY, false)) + if(this.getSharedPreferences(SettingsActivity.SETTINGS, Debug.SHARED_PREFS_MODE).getBoolean(SettingsActivity.TOASTY, false)) Toast.makeText(this.getApplicationContext(), "Applying "+ name, Toast.LENGTH_SHORT).show(); /* @@ -150,7 +150,7 @@ public class RingerProcessingService extends Service { .setAction(LocationLibraryConstants.INTENT_ACTION_UPDATE); PendingIntent pi = PendingIntent.getService(this, LocationService.REQUEST_CODE, i, 0); - AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); + final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); /* * cancel the existing schedule @@ -243,8 +243,8 @@ public class RingerProcessingService extends Service { Log.d(TAG, "onCreate()"); super.onCreate(); this.mDb = new RingerDatabase(this); - this.mSettings = this.getSharedPreferences(SettingsActivity.SETTINGS, Context.MODE_WORLD_WRITEABLE); - PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); + this.mSettings = this.getSharedPreferences(SettingsActivity.SETTINGS, Debug.SHARED_PREFS_MODE); + final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); this.mWakeLock = (WakeLock) pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); this.mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); this.mWifiManager = (WifiManager) this.getSystemService(WIFI_SERVICE); @@ -313,15 +313,15 @@ public class RingerProcessingService extends Service { /* * get the default ringer information */ - ContentValues ringer = getRinger(1); + final ContentValues ringer = getRinger(1); - GeoPoint point = new GeoPoint((int) (mLocation.getLatitude() * 1E6), (int) (mLocation.getLongitude()*1E6)); + final GeoPoint point = new GeoPoint((int) (mLocation.getLatitude() * 1E6), (int) (mLocation.getLongitude()*1E6)); if(Debug.DEBUG){ Log.d(TAG, "Processing ringers"); Log.d(TAG, "Current location "+(int) (mLocation.getLatitude() * 1E6)+", "+(int) (mLocation.getLongitude() * 1E6)+" @ "+ new Float(mLocation.getAccuracy()) / 1000+"km"); } - Cursor c = mDb.getAllRingers(); + final Cursor c = mDb.getAllRingers(); c.moveToFirst(); if (c.moveToFirst()) { do { @@ -329,7 +329,7 @@ public class RingerProcessingService extends Service { Log.d(TAG, "Checking ringer "+c.getString(0)); if(RingerDatabase.parseBoolean(c.getString(1))){ - ContentValues info = this.mDb.getRingerInfo(c.getString(0)); + final ContentValues info = this.mDb.getRingerInfo(c.getString(0)); if(info.containsKey(RingerDatabase.KEY_LOCATION) && info.containsKey(RingerDatabase.KEY_RADIUS)){ final String[] pointInfo = info.getAsString(RingerDatabase.KEY_LOCATION).split(","); if(GeoUtils.isIntersecting(point, new Float(mLocation.getAccuracy()) / 1000, new GeoPoint(Integer.parseInt(pointInfo[0]), Integer.parseInt(pointInfo[1])), new Float(info.getAsInteger(RingerDatabase.KEY_RADIUS)) / 1000, Debug.FUDGE_FACTOR)){ diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/ListActivity.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/ListActivity.java index fc8ed15..6672b4e 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/ListActivity.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/ListActivity.java @@ -32,6 +32,7 @@ import android.widget.Toast; import com.TwentyCodes.android.LocationRinger.R; import com.TwentyCodes.android.LocationRinger.db.DatabaseListener; import com.TwentyCodes.android.LocationRinger.db.RingerDatabase; +import com.TwentyCodes.android.LocationRinger.debug.Debug; import com.TwentyCodes.android.LocationRinger.receivers.PassiveLocationChangedReceiver; import com.TwentyCodes.android.LocationRinger.services.LocationService; import com.TwentyCodes.android.SkyHook.SkyHookRegistration; @@ -127,7 +128,7 @@ public class ListActivity extends Activity implements OnItemClickListener, OnCli public boolean onContextItemSelected(MenuItem item) { switch(item.getItemId()) { case R.id.delete: - AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); + 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 @@ -187,7 +188,7 @@ public class ListActivity extends Activity implements OnItemClickListener, OnCli */ @Override public boolean onCreateOptionsMenu (Menu menu) { - MenuInflater inflater = getMenuInflater(); + final MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.ringer_list_menu, menu); return super.onCreateOptionsMenu(menu); @@ -242,13 +243,13 @@ public class ListActivity extends Activity implements OnItemClickListener, OnCli public void run(){ Looper.prepare(); - Intent i = new Intent(ListActivity.this, RingerInformationActivity.class) + final Intent i = new Intent(ListActivity.this, RingerInformationActivity.class) .putExtra(KEY_ROWID, id+1); /* * get the ringer */ - Cursor ringer = mDb.getRingerFromId(id+1); + final Cursor ringer = mDb.getRingerFromId(id+1); if (ringer.moveToFirst()){ ContentValues r = new ContentValues(); r.put(RingerDatabase.KEY_RINGER_NAME, ringer.getString(0)); @@ -333,15 +334,15 @@ public class ListActivity extends Activity implements OnItemClickListener, OnCli * @author ricky barrette */ private void restartService() { - if(! this.getSharedPreferences(SettingsActivity.SETTINGS, Context.MODE_WORLD_WRITEABLE).getBoolean(SettingsActivity.IS_SERVICE_STARTED, false)){ + final SharedPreferences sharedPrefs = this.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 Intent i = new Intent(this, LocationService.class) - .putExtra(LocationService.INTENT_EXTRA_REQUIRED_ACCURACY, Integer.parseInt(this.getSharedPreferences(SettingsActivity.SETTINGS, 2).getString(SettingsActivity.ACCURACY , "50"))) + .putExtra(LocationService.INTENT_EXTRA_REQUIRED_ACCURACY, Integer.parseInt(sharedPrefs.getString(SettingsActivity.ACCURACY , "50"))) .setAction(LocationLibraryConstants.INTENT_ACTION_UPDATE); this.startService(i); - } - + } } } \ 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 9129db3..ce76f20 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/RingerInformationActivity.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/RingerInformationActivity.java @@ -72,7 +72,7 @@ public class RingerInformationActivity extends FragmentActivity implements OnCon /* * Set up the action bar if required */ - if(Integer.valueOf(android.os.Build.VERSION.SDK_INT) > 11) + if(Debug.SUPPORTS_HONEYCOMB) getActionBar().setDisplayHomeAsUpEnabled(true); this.mData = new Intent().putExtras(RingerInformationActivity.this.getIntent()); diff --git a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/SettingsActivity.java b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/SettingsActivity.java index 71e6ffe..caba575 100644 --- a/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/SettingsActivity.java +++ b/LocationRinger/src/com/TwentyCodes/android/LocationRinger/ui/SettingsActivity.java @@ -27,6 +27,7 @@ import android.view.MenuItem; import com.TwentyCodes.android.LocationRinger.LocationRinger; import com.TwentyCodes.android.LocationRinger.R; +import com.TwentyCodes.android.LocationRinger.debug.Debug; /** * This is the settings activity for location ringer @@ -54,14 +55,14 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference * @return true if successful * @author ricky barrette */ - public static boolean backup(Context context){ - 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"); - File exportDir = new File(Environment.getExternalStorageDirectory(), "/"+context.getString(R.string.app_name)); + final File exportDir = new File(Environment.getExternalStorageDirectory(), "/"+context.getString(R.string.app_name)); if (!exportDir.exists()) { exportDir.mkdirs(); } - File file = new File(exportDir, dbFile.getName()); + final File file = new File(exportDir, dbFile.getName()); try { file.createNewFile(); @@ -80,9 +81,9 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference * @throws IOException * @author ricky barrette */ - private static void copyFile(File src, File dst) throws IOException { - FileChannel inChannel = new FileInputStream(src).getChannel(); - FileChannel outChannel = new FileOutputStream(dst).getChannel(); + private static 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 { @@ -98,14 +99,14 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference * @return true if successful * @author ricky barrette */ - public static void restore(Context context){ - 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"); - File exportDir = new File(Environment.getExternalStorageDirectory(), "/"+context.getString(R.string.app_name)); + final File exportDir = new File(Environment.getExternalStorageDirectory(), "/"+context.getString(R.string.app_name)); if (!exportDir.exists()) { exportDir.mkdirs(); } - File file = new File(exportDir, dbFile.getName()); + final File file = new File(exportDir, dbFile.getName()); try { file.createNewFile(); @@ -114,8 +115,7 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference e.printStackTrace(); } - context.getSharedPreferences(SETTINGS, Context.MODE_WORLD_WRITEABLE).edit().remove(IS_FIRST_RINGER_PROCESSING).remove(IS_DEFAULT).remove(IS_SERVICE_STARTED).commit(); - + context.getSharedPreferences(SETTINGS, Debug.SHARED_PREFS_MODE).edit().remove(IS_FIRST_RINGER_PROCESSING).remove(IS_DEFAULT).remove(IS_SERVICE_STARTED).commit(); } /** @@ -128,7 +128,7 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference /* * get the build information, and build the string */ - PackageManager pm = this.getPackageManager(); + final PackageManager pm = this.getPackageManager(); PackageInfo pi; try { pi = pm.getPackageInfo(this.getPackageName(), 0); @@ -139,9 +139,9 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference pi.versionCode = 1; } - Intent intent = new Intent(Intent.ACTION_SEND); - String theSubject = this.getString(R.string.app_name); - String theBody = "\n\n\n"+ Build.FINGERPRINT +"\n"+ this.getString(R.string.app_name)+" "+pi.versionName+" bulid "+pi.versionCode; + 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)}); intent.putExtra(Intent.EXTRA_TEXT, theBody); intent.putExtra(Intent.EXTRA_SUBJECT, theSubject); @@ -150,8 +150,9 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference } @Override - public void onCreate(Bundle savedInstanceState){ + public void onCreate(final Bundle savedInstanceState){ super.onCreate(savedInstanceState); + this.getPreferenceManager().setSharedPreferencesMode(Debug.SHARED_PREFS_MODE); this.getPreferenceManager().setSharedPreferencesName(SETTINGS); addPreferencesFromResource(R.xml.setings); this.findPreference(EMAIL).setOnPreferenceClickListener(this); @@ -159,7 +160,7 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference /* * Set up the action bar if required */ - if(Integer.valueOf(android.os.Build.VERSION.SDK_INT) > 11) + if(Debug.SUPPORTS_HONEYCOMB) getActionBar().setDisplayHomeAsUpEnabled(true); }