Skip to content

Commit

Permalink
profiles: resolve null pointer if service is down
Browse files Browse the repository at this point in the history
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
Blechd0se committed Sep 14, 2014
1 parent 73e8bbe commit 7446295
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 120 deletions.
45 changes: 10 additions & 35 deletions AeroControl/src/com/aero/control/fragments/ProfileFragment.java
Expand Up @@ -65,6 +65,7 @@ public class ProfileFragment extends PreferenceFragment implements UndoBarContro
private Context mContext;
private List<ApplicationInfo> mPackages;
private ProgressDialog mProgressDialog;
private boolean mWarning;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Expand All @@ -74,6 +75,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
setHasOptionsMenu(true);

mContext = getActivity();
mWarning = false;

mPerAppPrefs = mContext.getSharedPreferences(perAppProfileHandler, Context.MODE_PRIVATE);
final View v = inflater.inflate(R.layout.profile_fragment, null);
Expand All @@ -85,27 +87,9 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
// Load all available profiles;
loadProfiles();

// Load default profiles;
//addDefaultProfiles(new EditText(mContext));

return mContainerView;
}

/*
* Can be used later to create default profiles, placeholder for now
*/
private final void addDefaultProfiles(EditText editText) {

// If the profile doesn't exist, create it;
final File prefFile = new File (AeroActivity.files.sharedPrefsPath + "performance.xml");
if(prefFile.exists()) {
Log.e(LOG_TAG, "Performance Profile exists already!");
} else {
editText.setText("performance");
addProfile(editText.getText().toString(), true);
}
}

private final void loadProfiles() {

mCompleteProfiles = AeroActivity.shell.getDirInfo(AeroActivity.files.sharedPrefsPath, true);
Expand All @@ -122,24 +106,19 @@ private final void loadProfiles() {
mContainerView.findViewById(R.id.empty_image).setVisibility(View.GONE);
}
}
// Sometime we are just too fast and would throw a null pointer, better save than sorry
try {
// User has assigned apps, but no service is running;
if (!(AeroActivity.perAppService.getState()) && checkAllStates()) {
// User has assigned apps, but no service is running;
if (AeroActivity.perAppService == null || !(AeroActivity.perAppService.getState())) {
if (checkAllStates() && !mWarning) {
AppRate.with(getActivity())
.text(R.string.pref_profile_service_not_running)
.fromTop(false)
.delay(1000)
.autoHide(15000)
.allowPlayLink(false)
.forceShow();
mWarning = true;
}
} catch (NullPointerException e) {
Log.e(LOG_TAG, "Object wasn't available, we are too fast!", e);
// We should start the recovery process here if the service hasn't
// come up, but should be up
}

}

// Create our options menu;
Expand Down Expand Up @@ -548,22 +527,18 @@ public void onClick(DialogInterface dialog, int id) {
updateStatus(txtViewSummary, false);
}

// Sometime we are just too fast and would throw a null pointer, better save than sorry
try {
// User has assigned apps, but no service is running;
if (!(AeroActivity.perAppService.getState()) && checkAllStates()) {
// User has assigned apps, but no service is running;
if (AeroActivity.perAppService == null || !(AeroActivity.perAppService.getState())) {
if (checkAllStates() && !mWarning) {
AppRate.with(getActivity())
.text(R.string.pref_profile_service_not_running)
.fromTop(false)
.delay(1000)
.autoHide(15000)
.allowPlayLink(false)
.forceShow();
mWarning = true;
}
} catch (NullPointerException e) {
Log.e(LOG_TAG, "Object wasn't available, we are too fast!", e);
// We should start the recovery process here if the service hasn't
// come up, but should be up
}

}
Expand Down
168 changes: 83 additions & 85 deletions AeroControl/src/com/aero/control/service/PerAppServiceHelper.java
@@ -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!");
}
}

0 comments on commit 7446295

Please sign in to comment.