How do I refresh the wallpaper image without reboot after restarting the Android SystemUIService?

StackOverflow https://stackoverflow.com/questions/22856662

  •  27-06-2023
  •  | 
  •  

Question

I'm creating an enterprise app for my company that will only run on a company provided 7" tablet which is rooted and running Android 4.2.2. A requirement is that when the employee logs in to the app, they should not be able to leave the app until they log out. I'm able to achieve this by hiding both the top system bar and bottom navigation bar when they log in and then I show both bars again when they log out. To hide the bars I execute the following command:

adb shell service call activity 42 s16 com.android.systemui

and to show the bars again I am execute this command:

adb shell am startservice -n com.android.systemui/.SystemUIService

When I hide the bars the wallpaper background image is removed, which is fine. When i show the bars again, the wallpaper image is not replaced until the device reboots which is not ideal.

So my question is this... can anyone tell me how to refresh or show the wallpaper image without needing to reboot the device after I start the SystemUIService?

Was it helpful?

Solution

After further investigation i discovered that when i run these commands, the wallpaper only disappears when it was an image and not a live wallpaper or video wallpaper.

I realise that this is quite specific to an uncommon scenario, but I thought I would share my solution in the event that it may help somebody else in the future. I hoped that there was a command that i could run that would resolve this but couldn't find anything so I decided to refresh the wallpaper programmatically. WallpaperManager doesn't have a refresh method at this stage so I just get the existing wallpaper image and then set the wallpaper to that image. A bit of a hack but in my opinion it is better than leaving the user with a black wallpaper.

private void refreshWallpaper() {
    try {
        WallpaperManager wallpaperManager = 
            WallpaperManager.getInstance(context);
        WallpaperInfo wallpaperInfo = wallpaperManager.getWallpaperInfo();

        // wallpaperInfo is null if a static wallpaper is selected and
        // is not null for live wallpaper & video wallpaper.
        if (wallpaperInfo == null) {
            // get the existing wallpaper drawable
            Drawable wallpaper = wallpaperManager.peekDrawable();

            // convert it to a bitmap
            Bitmap wallpaperBitmap = drawableToBitmap(wallpaper);

            // reset the bitmap to the current wallpaper
            wallpaperManager.setBitmap(wallpaperBitmap);    
        }
    } catch (Exception e) {
        // TODO: Handle exception as needed
        e.printStackTrace();
    }
}

private static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) { 
        return ((BitmapDrawable) drawable).getBitmap(); 
    }

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
        drawable.getIntrinsicHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top