Skip to content

Commit

Permalink
Only attempt to move service to fore/background if it's the opposite.
Browse files Browse the repository at this point in the history
Cuts down on log messages and wasteful work.
  • Loading branch information
peplin committed Feb 13, 2014
1 parent b8e9945 commit 79251d7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
2 changes: 1 addition & 1 deletion enabler/src/com/openxc/enabler/BluetoothReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class BluetoothReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Recieved intent Event: " + intent.getAction());
Log.d(TAG, "Recieved intent, event: " + intent.getAction());

// If a Bluetooth device with the OpenXC device name prefix is
// connected, start the service if it's not already started
Expand Down
3 changes: 3 additions & 0 deletions openxc/src/com/openxc/VehicleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,9 @@ public void onServiceDisconnected(ComponentName className) {

private void bindRemote() {
Log.i(TAG, "Binding to VehicleService");
// TODO Android warns us that this implicit intent is unsafe, but if we
// try creating an explicit intent (with out context), the server is
// never bound. What gives?
Intent intent = new Intent(VehicleServiceInterface.class.getName());
try {
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
Expand Down
57 changes: 32 additions & 25 deletions openxc/src/com/openxc/remote/VehicleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class VehicleService extends Service implements DataPipeline.Operator {
// https://code.google.com/p/android/issues/detail?id=12122
public static boolean sIsUnderTest = false;

private boolean mForeground = false;
private DataPipeline mPipeline = new DataPipeline(this);
private ApplicationSource mApplicationSource = new ApplicationSource();
private CopyOnWriteArrayList<VehicleInterface> mInterfaces =
Expand Down Expand Up @@ -100,37 +101,43 @@ public IBinder onBind(Intent intent) {
}

private void moveToForeground(){
Log.i(TAG, "Moving service to foreground.");
if(!mForeground) {
Log.i(TAG, "Moving service to foreground.");

try {
Intent intent = new Intent(this,
Class.forName("com.openxc.enabler.OpenXcEnablerActivity"));
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, intent, 0);

NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(getString(R.string.openxc_name))
.setContentInfo(getString(R.string.notification_content))
.setSmallIcon(R.drawable.openxc_notification_icon_small_white)
.setContentIntent(pendingIntent);

startForeground(SERVICE_NOTIFICATION_ID,
notificationBuilder.build());
} catch (ClassNotFoundException e) {
// TODO Special action if enabler is not installed

Log.e(TAG, "Could not find OpenXcEnablerActivity class.", e);
try {
Intent intent = new Intent(this,
Class.forName("com.openxc.enabler.OpenXcEnablerActivity"));
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, intent, 0);

NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(getString(R.string.openxc_name))
.setContentInfo(getString(R.string.notification_content))
.setSmallIcon(R.drawable.openxc_notification_icon_small_white)
.setContentIntent(pendingIntent);

startForeground(SERVICE_NOTIFICATION_ID,
notificationBuilder.build());
} catch (ClassNotFoundException e) {
// TODO Special action if enabler is not installed

Log.e(TAG, "Could not find OpenXcEnablerActivity class.", e);
}
mForeground = true;
}
}

private void removeFromForeground(){
Log.i(TAG, "Removing service from foreground.");
if(mForeground) {
Log.i(TAG, "Removing service from foreground.");

if(!sIsUnderTest) {
stopForeground(true);
if(!sIsUnderTest) {
stopForeground(true);
}
mForeground = false;
}
}

Expand Down

0 comments on commit 79251d7

Please sign in to comment.