Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 67b8c1cc authored by Roman Birg's avatar Roman Birg
Browse files

Settings: lockscreen wallpaper crash fix



If a user backs out fast enough after setting a
custom wallpaper, the fragment becomes detached
from the activity, so any calls to getActivity()
were throwing exceptions.

The solution is to use the application Context
for toassts to still inform the user of the
status, and not update any UI elements
after the fragment is no longer attached.

Change-Id: Ia9f61184fb484d3af25565bc3c56e8b7b63ff4eb
Signed-off-by: default avatarRoman Birg <roman@cyngn.com>
parent 3b89d5bd
Loading
Loading
Loading
Loading
+19 −12
Original line number Diff line number Diff line
@@ -302,9 +302,9 @@ public class LockscreenInterface extends SettingsPreferenceFragment implements
                if (uri == null) {
                    uri = Uri.fromFile(mTempWallpaper);
                }
                new SaveUserWallpaperTask().execute(uri);
                new SaveUserWallpaperTask(getActivity().getApplicationContext()).execute(uri);
            } else {
                toastLockscreenWallpaperStatus(false);
                toastLockscreenWallpaperStatus(getActivity(), false);
            }
        }
    }
@@ -360,9 +360,9 @@ public class LockscreenInterface extends SettingsPreferenceFragment implements
                intent.putExtra("return-data", false);
                getActivity().startActivityFromFragment(this, intent, REQUEST_PICK_WALLPAPER);
            } catch (IOException e) {
                toastLockscreenWallpaperStatus(false);
                toastLockscreenWallpaperStatus(getActivity(), false);
            } catch (ActivityNotFoundException e) {
                toastLockscreenWallpaperStatus(false);
                toastLockscreenWallpaperStatus(getActivity(), false);
            }
        } else if (index == LockscreenBackgroundUtil.LOCKSCREEN_STYLE_DEFAULT) {
            // Sets background to default
@@ -371,25 +371,30 @@ public class LockscreenInterface extends SettingsPreferenceFragment implements
            if (mWallpaper.exists()) {
                mWallpaper.delete();
            }
            updateKeyguardWallpaper();
            updateKeyguardWallpaper(getActivity());
            updateBackgroundPreference();
        }
    }

    private void toastLockscreenWallpaperStatus(boolean success) {
        Toast.makeText(getActivity(), getResources().getString(
    private static void toastLockscreenWallpaperStatus(Context context, boolean success) {
        Toast.makeText(context, context.getResources().getString(
                success ? R.string.background_result_successful
                        : R.string.background_result_not_successful),
                Toast.LENGTH_LONG).show();
    }

    private void updateKeyguardWallpaper() {
        getActivity().sendBroadcast(new Intent(Intent.ACTION_KEYGUARD_WALLPAPER_CHANGED));
    private static void updateKeyguardWallpaper(Context context) {
        context.sendBroadcast(new Intent(Intent.ACTION_KEYGUARD_WALLPAPER_CHANGED));
    }

    private class SaveUserWallpaperTask extends AsyncTask<Uri, Void, Boolean> {

        private Toast mToast;
        Context mContext;

        public SaveUserWallpaperTask(Context ctx) {
            mContext = ctx;
        }

        @Override
        protected void onPreExecute() {
@@ -433,14 +438,16 @@ public class LockscreenInterface extends SettingsPreferenceFragment implements
        @Override
        protected void onPostExecute(Boolean result) {
            mToast.cancel();
            toastLockscreenWallpaperStatus(result);
            toastLockscreenWallpaperStatus(mContext, result);
            if (result) {
                Settings.System.putInt(getContentResolver(),
                        Settings.System.LOCKSCREEN_BACKGROUND_STYLE,
                        LockscreenBackgroundUtil.LOCKSCREEN_STYLE_IMAGE);
                updateKeyguardWallpaper();
                updateKeyguardWallpaper(mContext);
                if (!isDetached()) {
                    updateBackgroundPreference();
                }
            }
        }
    }
}