From 744629594a8c4a00c1edd21899edc3877278c125 Mon Sep 17 00:00:00 2001 From: Blechd0se Date: Sun, 14 Sep 2014 17:48:18 +0200 Subject: [PATCH] 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 --- .../control/fragments/ProfileFragment.java | 45 ++--- .../control/service/PerAppServiceHelper.java | 168 +++++++++--------- 2 files changed, 93 insertions(+), 120 deletions(-) diff --git a/AeroControl/src/com/aero/control/fragments/ProfileFragment.java b/AeroControl/src/com/aero/control/fragments/ProfileFragment.java index 4296e61..7859876 100644 --- a/AeroControl/src/com/aero/control/fragments/ProfileFragment.java +++ b/AeroControl/src/com/aero/control/fragments/ProfileFragment.java @@ -65,6 +65,7 @@ public class ProfileFragment extends PreferenceFragment implements UndoBarContro private Context mContext; private List mPackages; private ProgressDialog mProgressDialog; + private boolean mWarning; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -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); @@ -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); @@ -122,10 +106,9 @@ 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) @@ -133,13 +116,9 @@ private final void loadProfiles() { .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; @@ -548,10 +527,9 @@ 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) @@ -559,11 +537,8 @@ public void onClick(DialogInterface dialog, int id) { .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 } } diff --git a/AeroControl/src/com/aero/control/service/PerAppServiceHelper.java b/AeroControl/src/com/aero/control/service/PerAppServiceHelper.java index 1a391f3..0b070ff 100644 --- a/AeroControl/src/com/aero/control/service/PerAppServiceHelper.java +++ b/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!"); + } +} \ No newline at end of file