Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
profiles: resolve null pointer if service is down
If the service helper, not the service itself, has no reference we would end up in a null pointer. Since its actually okay if the helper class is not instantiated we don't need to try-catch it. Also adding a warning boolean which fixes strange overlay issues if the same fragment is created several times. Signed-off-by: Blechd0se <alex.christ@hotmail.de>
- Loading branch information
Showing
2 changed files
with
93 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 83 additions & 85 deletions
168
AeroControl/src/com/aero/control/service/PerAppServiceHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,83 @@ | ||
package com.aero.control.service; | ||
|
||
import android.app.AlarmManager; | ||
import android.app.PendingIntent; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.SharedPreferences; | ||
import android.preference.PreferenceManager; | ||
import android.util.Log; | ||
|
||
import java.util.Calendar; | ||
|
||
/** | ||
* Created by Alexander Christ on 29.05.14. | ||
*/ | ||
public class PerAppServiceHelper { | ||
|
||
private Intent mBackgroundIntent = null; | ||
private AlarmManager mTimer = null; | ||
private PendingIntent mPendingIntent = null; | ||
private SharedPreferences mPrefs; | ||
private Context mContext; | ||
private boolean mState; | ||
|
||
public PerAppServiceHelper(Context context) { | ||
this.mContext = context; | ||
mBackgroundIntent = new Intent(mContext, PerAppService.class); | ||
mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext); | ||
} | ||
|
||
public final void setState(boolean state) { mState = state; } | ||
|
||
public final boolean getState() { | ||
return mState; | ||
} | ||
|
||
public final boolean shouldBeStarted() { | ||
|
||
final boolean tmp = mPrefs.getBoolean("per_app_service", false); | ||
|
||
if (!tmp) | ||
setState(false); | ||
else if (tmp) | ||
setState(true); | ||
|
||
return getState(); | ||
} | ||
|
||
public final void startService() { | ||
|
||
/* Start Service */ | ||
final Calendar cal = Calendar.getInstance(); | ||
Log.e("Aero", "Service should be started now!"); | ||
mBackgroundIntent = new Intent(mContext, PerAppService.class); | ||
mContext.startService(mBackgroundIntent); | ||
mPendingIntent = PendingIntent.getService(mContext, 0, mBackgroundIntent, 0); | ||
|
||
mTimer = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); | ||
mTimer.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 7 * 1000, mPendingIntent); | ||
|
||
mState = true; | ||
} | ||
|
||
public final void stopService() { | ||
|
||
if (mBackgroundIntent == null || mTimer == null || mPendingIntent == null) { | ||
// Nothing is running | ||
mState = false; | ||
Log.e("Aero", "Service wasn't even running!"); | ||
return; | ||
} else { | ||
|
||
// Cleanup; | ||
mContext.stopService(mBackgroundIntent); | ||
mTimer.cancel(mPendingIntent); | ||
|
||
mTimer = null; | ||
mBackgroundIntent = null; | ||
mPendingIntent = null; | ||
mState = false; | ||
} | ||
Log.e("Aero", "Service should be stopped now!"); | ||
} | ||
|
||
} | ||
package com.aero.control.service; | ||
|
||
import android.app.AlarmManager; | ||
import android.app.PendingIntent; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.SharedPreferences; | ||
import android.preference.PreferenceManager; | ||
import android.util.Log; | ||
|
||
import java.util.Calendar; | ||
|
||
/** | ||
* Created by Alexander Christ on 29.05.14. | ||
*/ | ||
public class PerAppServiceHelper { | ||
|
||
private Intent mBackgroundIntent = null; | ||
private AlarmManager mTimer = null; | ||
private PendingIntent mPendingIntent = null; | ||
private SharedPreferences mPrefs; | ||
private Context mContext; | ||
private boolean mState; | ||
|
||
public PerAppServiceHelper(Context context) { | ||
this.mContext = context; | ||
mBackgroundIntent = new Intent(mContext, PerAppService.class); | ||
mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext); | ||
} | ||
|
||
public final void setState(boolean state) { mState = state; } | ||
|
||
public final boolean getState() { | ||
return mState; | ||
} | ||
|
||
public final boolean shouldBeStarted() { | ||
|
||
final boolean tmp = mPrefs.getBoolean("per_app_service", false); | ||
|
||
if (!tmp) | ||
setState(false); | ||
else if (tmp) | ||
setState(true); | ||
|
||
return getState(); | ||
} | ||
|
||
public final void startService() { | ||
|
||
/* Start Service */ | ||
final Calendar cal = Calendar.getInstance(); | ||
Log.e("Aero", "Service should be started now!"); | ||
mBackgroundIntent = new Intent(mContext, PerAppService.class); | ||
mContext.startService(mBackgroundIntent); | ||
mPendingIntent = PendingIntent.getService(mContext, 0, mBackgroundIntent, 0); | ||
|
||
mTimer = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); | ||
mTimer.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 7 * 1000, mPendingIntent); | ||
|
||
setState(true); | ||
} | ||
|
||
public final void stopService() { | ||
|
||
if (mBackgroundIntent == null || mTimer == null || mPendingIntent == null) { | ||
// Nothing is running | ||
Log.e("Aero", "Service wasn't even running!"); | ||
return; | ||
} else { | ||
|
||
// Cleanup; | ||
mContext.stopService(mBackgroundIntent); | ||
mTimer.cancel(mPendingIntent); | ||
|
||
mTimer = null; | ||
mBackgroundIntent = null; | ||
mPendingIntent = null; | ||
} | ||
setState(false); | ||
Log.e("Aero", "Service should be stopped now!"); | ||
} | ||
} |