Started to clean up the shared prefs entries through out the application

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-06-02 15:32:48 -04:00
parent 6af417f7dd
commit 0a90c4220b
6 changed files with 65 additions and 41 deletions

View File

@@ -6,12 +6,32 @@
*/ */
package com.TwentyCodes.android.LocationRinger.debug; package com.TwentyCodes.android.LocationRinger.debug;
import android.content.Context;
/** /**
* A convince class containing debugging variables * A convince class containing debugging variables
* @author ricky barrette * @author ricky barrette
*/ */
public class Debug { 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 * Sets the logging output of this application
*/ */
@@ -31,4 +51,6 @@ public class Debug {
* Max radius that can be set by a ringer * Max radius that can be set by a ringer
*/ */
public static final int MAX_RADIUS_IN_METERS = 600; public static final int MAX_RADIUS_IN_METERS = 600;
} }

View File

@@ -39,10 +39,10 @@ public class SystemReceiver extends BroadcastReceiver {
* @author ricky barrette * @author ricky barrette
*/ */
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(final Context context, final Intent intent) {
if(Debug.DEBUG) if(Debug.DEBUG)
Log.d(TAG, "onReceive() ~"+intent.getAction()); 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) 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_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"))) .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 the phone finishes booting, then start the service if the user enabled it
*/ */
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){ 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); context.startService(i);
PassiveLocationListener.requestPassiveLocationUpdates(context, new Intent(context, PassiveLocationChangedReceiver.class)); PassiveLocationListener.requestPassiveLocationUpdates(context, new Intent(context, PassiveLocationChangedReceiver.class));
} }

View File

@@ -58,17 +58,17 @@ public class RingerProcessingService extends Service {
* @param id * @param id
* @author ricky barrette * @author ricky barrette
*/ */
private void applyRinger(ContentValues values) { private void applyRinger(final ContentValues values) {
if(Debug.DEBUG) if(Debug.DEBUG)
Log.d(TAG, "applyRigner()"); 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. * Make it toasty if the user wants to be notified.
* This will display a toast msg "Applying <ringer name>" * This will display a toast msg "Applying <ringer name>"
*/ */
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(); Toast.makeText(this.getApplicationContext(), "Applying "+ name, Toast.LENGTH_SHORT).show();
/* /*
@@ -150,7 +150,7 @@ public class RingerProcessingService extends Service {
.setAction(LocationLibraryConstants.INTENT_ACTION_UPDATE); .setAction(LocationLibraryConstants.INTENT_ACTION_UPDATE);
PendingIntent pi = PendingIntent.getService(this, LocationService.REQUEST_CODE, i, 0); 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 * cancel the existing schedule
@@ -243,8 +243,8 @@ public class RingerProcessingService extends Service {
Log.d(TAG, "onCreate()"); Log.d(TAG, "onCreate()");
super.onCreate(); super.onCreate();
this.mDb = new RingerDatabase(this); this.mDb = new RingerDatabase(this);
this.mSettings = this.getSharedPreferences(SettingsActivity.SETTINGS, Context.MODE_WORLD_WRITEABLE); this.mSettings = this.getSharedPreferences(SettingsActivity.SETTINGS, Debug.SHARED_PREFS_MODE);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.mWakeLock = (WakeLock) pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); this.mWakeLock = (WakeLock) pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
this.mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); this.mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
this.mWifiManager = (WifiManager) this.getSystemService(WIFI_SERVICE); this.mWifiManager = (WifiManager) this.getSystemService(WIFI_SERVICE);
@@ -313,15 +313,15 @@ public class RingerProcessingService extends Service {
/* /*
* get the default ringer information * 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){ if(Debug.DEBUG){
Log.d(TAG, "Processing ringers"); Log.d(TAG, "Processing ringers");
Log.d(TAG, "Current location "+(int) (mLocation.getLatitude() * 1E6)+", "+(int) (mLocation.getLongitude() * 1E6)+" @ "+ new Float(mLocation.getAccuracy()) / 1000+"km"); 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(); c.moveToFirst();
if (c.moveToFirst()) { if (c.moveToFirst()) {
do { do {
@@ -329,7 +329,7 @@ public class RingerProcessingService extends Service {
Log.d(TAG, "Checking ringer "+c.getString(0)); Log.d(TAG, "Checking ringer "+c.getString(0));
if(RingerDatabase.parseBoolean(c.getString(1))){ 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)){ if(info.containsKey(RingerDatabase.KEY_LOCATION) && info.containsKey(RingerDatabase.KEY_RADIUS)){
final String[] pointInfo = info.getAsString(RingerDatabase.KEY_LOCATION).split(","); 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)){ 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)){

View File

@@ -32,6 +32,7 @@ import android.widget.Toast;
import com.TwentyCodes.android.LocationRinger.R; import com.TwentyCodes.android.LocationRinger.R;
import com.TwentyCodes.android.LocationRinger.db.DatabaseListener; import com.TwentyCodes.android.LocationRinger.db.DatabaseListener;
import com.TwentyCodes.android.LocationRinger.db.RingerDatabase; 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.receivers.PassiveLocationChangedReceiver;
import com.TwentyCodes.android.LocationRinger.services.LocationService; import com.TwentyCodes.android.LocationRinger.services.LocationService;
import com.TwentyCodes.android.SkyHook.SkyHookRegistration; import com.TwentyCodes.android.SkyHook.SkyHookRegistration;
@@ -127,7 +128,7 @@ public class ListActivity extends Activity implements OnItemClickListener, OnCli
public boolean onContextItemSelected(MenuItem item) { public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) { switch(item.getItemId()) {
case R.id.delete: case R.id.delete:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
if(info.id == 0) if(info.id == 0)
Toast.makeText(this, this.getString(R.string.cant_delete_default), Toast.LENGTH_SHORT).show(); Toast.makeText(this, this.getString(R.string.cant_delete_default), Toast.LENGTH_SHORT).show();
else else
@@ -187,7 +188,7 @@ public class ListActivity extends Activity implements OnItemClickListener, OnCli
*/ */
@Override @Override
public boolean onCreateOptionsMenu (Menu menu) { public boolean onCreateOptionsMenu (Menu menu) {
MenuInflater inflater = getMenuInflater(); final MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.ringer_list_menu, menu); inflater.inflate(R.menu.ringer_list_menu, menu);
return super.onCreateOptionsMenu(menu); return super.onCreateOptionsMenu(menu);
@@ -242,13 +243,13 @@ public class ListActivity extends Activity implements OnItemClickListener, OnCli
public void run(){ public void run(){
Looper.prepare(); Looper.prepare();
Intent i = new Intent(ListActivity.this, RingerInformationActivity.class) final Intent i = new Intent(ListActivity.this, RingerInformationActivity.class)
.putExtra(KEY_ROWID, id+1); .putExtra(KEY_ROWID, id+1);
/* /*
* get the ringer * get the ringer
*/ */
Cursor ringer = mDb.getRingerFromId(id+1); final Cursor ringer = mDb.getRingerFromId(id+1);
if (ringer.moveToFirst()){ if (ringer.moveToFirst()){
ContentValues r = new ContentValues(); ContentValues r = new ContentValues();
r.put(RingerDatabase.KEY_RINGER_NAME, ringer.getString(0)); r.put(RingerDatabase.KEY_RINGER_NAME, ringer.getString(0));
@@ -333,15 +334,15 @@ public class ListActivity extends Activity implements OnItemClickListener, OnCli
* @author ricky barrette * @author ricky barrette
*/ */
private void restartService() { 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 // cancel the previous service
LocationService.stopService(this).run(); LocationService.stopService(this).run();
//start the new service //start the new service
Intent i = new Intent(this, LocationService.class) 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); .setAction(LocationLibraryConstants.INTENT_ACTION_UPDATE);
this.startService(i); this.startService(i);
} }
} }
} }

View File

@@ -72,7 +72,7 @@ public class RingerInformationActivity extends FragmentActivity implements OnCon
/* /*
* Set up the action bar if required * Set up the action bar if required
*/ */
if(Integer.valueOf(android.os.Build.VERSION.SDK_INT) > 11) if(Debug.SUPPORTS_HONEYCOMB)
getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setDisplayHomeAsUpEnabled(true);
this.mData = new Intent().putExtras(RingerInformationActivity.this.getIntent()); this.mData = new Intent().putExtras(RingerInformationActivity.this.getIntent());

View File

@@ -27,6 +27,7 @@ import android.view.MenuItem;
import com.TwentyCodes.android.LocationRinger.LocationRinger; import com.TwentyCodes.android.LocationRinger.LocationRinger;
import com.TwentyCodes.android.LocationRinger.R; import com.TwentyCodes.android.LocationRinger.R;
import com.TwentyCodes.android.LocationRinger.debug.Debug;
/** /**
* This is the settings activity for location ringer * This is the settings activity for location ringer
@@ -54,14 +55,14 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference
* @return true if successful * @return true if successful
* @author ricky barrette * @author ricky barrette
*/ */
public static boolean backup(Context context){ public static boolean backup(final Context context){
File dbFile = new File(Environment.getDataDirectory() + "/data/"+context.getPackageName()+"/shared_prefs/"+SETTINGS+".xml"); 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()) { if (!exportDir.exists()) {
exportDir.mkdirs(); exportDir.mkdirs();
} }
File file = new File(exportDir, dbFile.getName()); final File file = new File(exportDir, dbFile.getName());
try { try {
file.createNewFile(); file.createNewFile();
@@ -80,9 +81,9 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference
* @throws IOException * @throws IOException
* @author ricky barrette * @author ricky barrette
*/ */
private static void copyFile(File src, File dst) throws IOException { private static void copyFile(final File src, final File dst) throws IOException {
FileChannel inChannel = new FileInputStream(src).getChannel(); final FileChannel inChannel = new FileInputStream(src).getChannel();
FileChannel outChannel = new FileOutputStream(dst).getChannel(); final FileChannel outChannel = new FileOutputStream(dst).getChannel();
try { try {
inChannel.transferTo(0, inChannel.size(), outChannel); inChannel.transferTo(0, inChannel.size(), outChannel);
} finally { } finally {
@@ -98,14 +99,14 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference
* @return true if successful * @return true if successful
* @author ricky barrette * @author ricky barrette
*/ */
public static void restore(Context context){ public static void restore(final Context context){
File dbFile = new File(Environment.getDataDirectory() + "/data/"+context.getPackageName()+"/shared_prefs/"+SETTINGS+".xml"); 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()) { if (!exportDir.exists()) {
exportDir.mkdirs(); exportDir.mkdirs();
} }
File file = new File(exportDir, dbFile.getName()); final File file = new File(exportDir, dbFile.getName());
try { try {
file.createNewFile(); file.createNewFile();
@@ -114,8 +115,7 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference
e.printStackTrace(); 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 * get the build information, and build the string
*/ */
PackageManager pm = this.getPackageManager(); final PackageManager pm = this.getPackageManager();
PackageInfo pi; PackageInfo pi;
try { try {
pi = pm.getPackageInfo(this.getPackageName(), 0); pi = pm.getPackageInfo(this.getPackageName(), 0);
@@ -139,9 +139,9 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference
pi.versionCode = 1; pi.versionCode = 1;
} }
Intent intent = new Intent(Intent.ACTION_SEND); final Intent intent = new Intent(Intent.ACTION_SEND);
String theSubject = this.getString(R.string.app_name); final 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 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_EMAIL,new String[] {this.getString(R.string.email)});
intent.putExtra(Intent.EXTRA_TEXT, theBody); intent.putExtra(Intent.EXTRA_TEXT, theBody);
intent.putExtra(Intent.EXTRA_SUBJECT, theSubject); intent.putExtra(Intent.EXTRA_SUBJECT, theSubject);
@@ -150,8 +150,9 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference
} }
@Override @Override
public void onCreate(Bundle savedInstanceState){ public void onCreate(final Bundle savedInstanceState){
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
this.getPreferenceManager().setSharedPreferencesMode(Debug.SHARED_PREFS_MODE);
this.getPreferenceManager().setSharedPreferencesName(SETTINGS); this.getPreferenceManager().setSharedPreferencesName(SETTINGS);
addPreferencesFromResource(R.xml.setings); addPreferencesFromResource(R.xml.setings);
this.findPreference(EMAIL).setOnPreferenceClickListener(this); this.findPreference(EMAIL).setOnPreferenceClickListener(this);
@@ -159,7 +160,7 @@ public class SettingsActivity extends PreferenceActivity implements OnPreference
/* /*
* Set up the action bar if required * Set up the action bar if required
*/ */
if(Integer.valueOf(android.os.Build.VERSION.SDK_INT) > 11) if(Debug.SUPPORTS_HONEYCOMB)
getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setDisplayHomeAsUpEnabled(true);
} }