Fixed incorrect service intents, and made centralized static convice

methods for starting the location service

closes #113

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-06-21 10:04:36 -04:00
parent 73dbdc0c0a
commit 10b9420a2f
7 changed files with 64 additions and 66 deletions

View File

@@ -2,8 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.TwentyCodes.android.LocationRinger" package="com.TwentyCodes.android.LocationRinger"
android:installLocation="internalOnly" android:installLocation="internalOnly"
android:versionCode="29" android:versionCode="35"
android:versionName="b71dc8a" > android:versionName="73dbdc0" >
<uses-sdk android:minSdkVersion="7" /> <uses-sdk android:minSdkVersion="7" />

View File

@@ -19,7 +19,6 @@ import com.TwentyCodes.android.LocationRinger.R;
import com.TwentyCodes.android.LocationRinger.debug.Debug; import com.TwentyCodes.android.LocationRinger.debug.Debug;
import com.TwentyCodes.android.LocationRinger.services.LocationService; import com.TwentyCodes.android.LocationRinger.services.LocationService;
import com.TwentyCodes.android.LocationRinger.ui.SettingsActivity; import com.TwentyCodes.android.LocationRinger.ui.SettingsActivity;
import com.TwentyCodes.android.debug.LocationLibraryConstants;
/** /**
* 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
@@ -50,9 +49,7 @@ public class GetLocationWidget extends AppWidgetProvider {
for (int i=0; i<N; i++) { for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i]; int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, LocationService.class) Intent intent = LocationService.getSingleShotServiceIntent(context);
.putExtra(LocationService.INTENT_EXTRA_REQUIRED_ACCURACY, Debug.ACCURACY)
.setAction(LocationLibraryConstants.INTENT_ACTION_UPDATE);
//create a pending intent to start the post activity //create a pending intent to start the post activity
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0); PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

View File

@@ -16,7 +16,6 @@ import android.util.Log;
import com.TwentyCodes.android.LocationRinger.debug.Debug; import com.TwentyCodes.android.LocationRinger.debug.Debug;
import com.TwentyCodes.android.LocationRinger.services.LocationService; import com.TwentyCodes.android.LocationRinger.services.LocationService;
import com.TwentyCodes.android.LocationRinger.ui.SettingsActivity; import com.TwentyCodes.android.LocationRinger.ui.SettingsActivity;
import com.TwentyCodes.android.debug.LocationLibraryConstants;
import com.TwentyCodes.android.location.PassiveLocationListener; import com.TwentyCodes.android.location.PassiveLocationListener;
/** /**
@@ -43,17 +42,14 @@ public class SystemReceiver extends BroadcastReceiver {
if(Debug.DEBUG) if(Debug.DEBUG)
Log.d(TAG, "onReceive() ~"+intent.getAction()); Log.d(TAG, "onReceive() ~"+intent.getAction());
final SharedPreferences systemEventHistory = context.getSharedPreferences(TAG, Debug.SHARED_PREFS_MODE); 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, Debug.ACCURACY)
.setAction(LocationLibraryConstants.INTENT_ACTION_UPDATE);
/* /*
* 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, Debug.SHARED_PREFS_MODE).getBoolean(SettingsActivity.START_ON_BOOT, false)){ if(context.getSharedPreferences(SettingsActivity.SETTINGS, Debug.SHARED_PREFS_MODE).getBoolean(SettingsActivity.START_ON_BOOT, false)){
context.startService(i); LocationService.startMultiShotService(context);
PassiveLocationListener.requestPassiveLocationUpdates(context, new Intent(context, PassiveLocationChangedReceiver.class)); PassiveLocationListener.requestPassiveLocationUpdates(context, new Intent(context, PassiveLocationChangedReceiver.class));
} }
} }
@@ -77,7 +73,7 @@ public class SystemReceiver extends BroadcastReceiver {
if(intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)){ if(intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)){
if (systemEventHistory.getBoolean(BATTERY_LOW, false)) { if (systemEventHistory.getBoolean(BATTERY_LOW, false)) {
systemEventHistory.edit().remove(BATTERY_LOW).commit(); systemEventHistory.edit().remove(BATTERY_LOW).commit();
context.startService(i); LocationService.startMultiShotService(context);
} }
} }
} }

View File

@@ -9,6 +9,7 @@ package com.TwentyCodes.android.LocationRinger.services;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
@@ -18,6 +19,7 @@ import com.TwentyCodes.android.LocationRinger.debug.Debug;
import com.TwentyCodes.android.LocationRinger.ui.ListActivity; import com.TwentyCodes.android.LocationRinger.ui.ListActivity;
import com.TwentyCodes.android.LocationRinger.ui.SettingsActivity; import com.TwentyCodes.android.LocationRinger.ui.SettingsActivity;
import com.TwentyCodes.android.SkyHook.SkyHookService; import com.TwentyCodes.android.SkyHook.SkyHookService;
import com.TwentyCodes.android.debug.LocationLibraryConstants;
import com.TwentyCodes.android.exception.ExceptionHandler; import com.TwentyCodes.android.exception.ExceptionHandler;
/** /**
@@ -30,27 +32,18 @@ public class LocationService extends SkyHookService {
private SharedPreferences mSettings; private SharedPreferences mSettings;
private NotificationManager mNotificationManager; private NotificationManager mNotificationManager;
/* (non-Javadoc) /**
* @see com.TwentyCodes.android.SkyHook.SkyHookService#onStartCommand(android.content.Intent, int, int) * convince method for getting the single shot service intent
* @param context
* @return service intent
* @author ricky barrette * @author ricky barrette
*/ */
@Override public static Intent getSingleShotServiceIntent(Context context) {
public int onStartCommand(Intent intent, int flags, int startId) { return new Intent(context, LocationService.class)
this.mPeriod = Debug.UPDATE_INTERVAL; .putExtra(LocationLibraryConstants.INTENT_EXTRA_REQUIRED_ACCURACY, Debug.ACCURACY)
return super.onStartCommand(intent, flags, startId); .setAction(LocationLibraryConstants.INTENT_ACTION_UPDATE);
} }
/* (non-Javadoc)
* @see com.TwentyCodes.android.SkyHook.SkyHookService#onDestroy()
* @author ricky barrette
*/
@Override
public void onDestroy() {
this.mSettings.edit().remove(SettingsActivity.IS_SERVICE_STARTED).commit();
this.mNotificationManager.cancel(this.GATHERING_LOCATION_ONGING_NOTIFICATION_ID);
super.onDestroy();
}
/* (non-Javadoc) /* (non-Javadoc)
* @see com.TwentyCodes.android.SkyHook.SkyHookService#onCreate() * @see com.TwentyCodes.android.SkyHook.SkyHookService#onCreate()
* @author ricky barrette * @author ricky barrette
@@ -64,6 +57,41 @@ public class LocationService extends SkyHookService {
super.onCreate(); super.onCreate();
} }
/* (non-Javadoc)
* @see com.TwentyCodes.android.SkyHook.SkyHookService#onDestroy()
* @author ricky barrette
*/
@Override
public void onDestroy() {
this.mSettings.edit().remove(SettingsActivity.IS_SERVICE_STARTED).commit();
this.mNotificationManager.cancel(this.GATHERING_LOCATION_ONGING_NOTIFICATION_ID);
super.onDestroy();
}
/* (non-Javadoc)
* @see com.TwentyCodes.android.SkyHook.SkyHookService#onStartCommand(android.content.Intent, int, int)
* @author ricky barrette
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.mPeriod = Debug.UPDATE_INTERVAL;
return super.onStartCommand(intent, flags, startId);
}
/**
* Starts the location service in multi shot mode
* @param context
* @return
* @author ricky barrette
*/
public static ComponentName startMultiShotService(final Context context){
Intent i = new Intent(context, LocationService.class)
.putExtra(LocationLibraryConstants.INTENT_EXTRA_PERIOD_BETWEEN_UPDATES, Debug.UPDATE_INTERVAL)
.putExtra(LocationLibraryConstants.INTENT_EXTRA_REQUIRED_ACCURACY, Debug.ACCURACY)
.setAction(LocationLibraryConstants.INTENT_ACTION_UPDATE);
return context.startService(i);
}
/** /**
* starts a simple ongoing notification to inform the user that we are gathering location * starts a simple ongoing notification to inform the user that we are gathering location
* @author ricky barrette * @author ricky barrette
@@ -76,4 +104,14 @@ public class LocationService extends SkyHookService {
notifyDetails.flags |= Notification.FLAG_ONGOING_EVENT; notifyDetails.flags |= Notification.FLAG_ONGOING_EVENT;
this.mNotificationManager.notify(this.GATHERING_LOCATION_ONGING_NOTIFICATION_ID, notifyDetails); this.mNotificationManager.notify(this.GATHERING_LOCATION_ONGING_NOTIFICATION_ID, notifyDetails);
} }
/**
* starts the service in single shot mode
* @param context
* @return
* @author ricky barrette
*/
public static ComponentName startSingleShotService(final Context context){
return context.startService(getSingleShotServiceIntent(context));
}
} }

View File

@@ -6,11 +6,8 @@
*/ */
package com.TwentyCodes.android.LocationRinger.services; package com.TwentyCodes.android.LocationRinger.services;
import java.util.Calendar;
import java.util.Map.Entry; import java.util.Map.Entry;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.content.ContentValues; import android.content.ContentValues;
@@ -119,29 +116,6 @@ public class RingerProcessingService extends Service {
mBluetoothAdapter.enable(); mBluetoothAdapter.enable();
else else
mBluetoothAdapter.disable(); mBluetoothAdapter.disable();
/*
* update interval
*/
if(values.containsKey(RingerDatabase.KEY_UPDATE_INTERVAL))
if (values.get(RingerDatabase.KEY_UPDATE_INTERVAL) != null){
Intent i = new Intent(this, LocationService.class)
.putExtra(LocationService.INTENT_EXTRA_REQUIRED_ACCURACY, Debug.ACCURACY)
.setAction(LocationLibraryConstants.INTENT_ACTION_UPDATE);
PendingIntent pi = PendingIntent.getService(this, LocationService.REQUEST_CODE, i, 0);
final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
/*
* cancel the existing schedule
*/
am.cancel(pi);
/*
* reschedule the location service
*/
am.set(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis() + (long) (60000 * Integer.parseInt(values.getAsString(RingerDatabase.KEY_UPDATE_INTERVAL))), pi);
}
} }
/** /**

View File

@@ -77,10 +77,7 @@ public class CheckLocationShortcut extends Activity {
/* /*
* start the location service in single shot mode * start the location service in single shot mode
*/ */
Intent service = new Intent(this, LocationService.class) LocationService.startSingleShotService(this);
.putExtra(LocationService.INTENT_EXTRA_REQUIRED_ACCURACY, Debug.ACCURACY)
.setAction(LocationLibraryConstants.INTENT_ACTION_UPDATE);
this.startService(service);
this.finish(); this.finish();
} }

View File

@@ -39,7 +39,6 @@ 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;
import com.TwentyCodes.android.debug.LocationLibraryConstants;
import com.TwentyCodes.android.location.PassiveLocationListener; import com.TwentyCodes.android.location.PassiveLocationListener;
import com.skyhookwireless.wps.RegistrationCallback; import com.skyhookwireless.wps.RegistrationCallback;
import com.skyhookwireless.wps.WPSContinuation; import com.skyhookwireless.wps.WPSContinuation;
@@ -389,10 +388,7 @@ public class ListActivity extends Activity implements OnItemClickListener, OnCli
// 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) LocationService.startMultiShotService(this);
.putExtra(LocationService.INTENT_EXTRA_REQUIRED_ACCURACY, Debug.ACCURACY)
.setAction(LocationLibraryConstants.INTENT_ACTION_UPDATE);
this.startService(i);
} }
} }