AsyncTask in infinite loop when using WallpaperMananger

0

My app strangely runs in infinite loop wildly. This is a simple code to download an image and apply as wallpaper.

But when removing this line of code, the loop disappears, ie asynctask returns to work as expected:

    WallpaperManager.getInstance(weakContext.get()).setBitmap(bitmap);

I already thought the problem could be WeakReference , but even running the code inside the onPostExecute() method using a Listener to execute the changes within the Fragment the problem persists.

public class WorkerTask extends AsyncTask<String, Void, Bitmap> {

    private static final String TAG = "WorkerTask";
    private WeakReference<Context> weakContext;
    private WeakReference<ImageView> weakImage;
    private Bitmap wallpaper;
    private WallpaperListener wallpaperListener;

    WorkerTask(Context context, ImageView imageView, WallpaperListener listener){
        weakContext = new WeakReference<>(context);
        weakImage = new WeakReference<>(imageView);
        wallpaperListener = listener;
    }

    @Override
    protected Bitmap doInBackground(String... url) {
        Log.d(TAG, "doInBackground: ");

        if (url[0].isEmpty()){
            throw new NullPointerException("URL Bad formatted");
        }

        /* Network */
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url[0])
                .build();
        Response response = null;
        try {
            response = client.newCall(request).execute();

            /* Generate Image */
            wallpaper = BitmapFactory.decodeStream(
                    Objects.requireNonNull(response.body()).byteStream());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                response.close();
            }
        }

        return wallpaper;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);

        try {
            WallpaperManager.getInstance(weakContext.get()).setBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Logcat:

    
asked by anonymous 01.09.2018 / 20:49

1 answer

0

I've figured out what's causing this bug, but not the main reason. Android 8.1 has a function of combining system colors with the time of day or based on the coloring of the wallpaper. When setting to match the wallpaper, the bug appears.

I'm testing on a device using a custom Rom (RRemix 6.1.0), but I'm not sure whether it applies to devices using a Stock version.

Function with Bug

.

DemonstrationGif

    
02.09.2018 / 03:37