Skip to content

Commit

Permalink
show wallpaper
Browse files Browse the repository at this point in the history
Fixes app crash when toggling wallpaper from the theme configuration activity.

`WallpaperManager.getDrawable` requires MANAGE_EXTERNAL_STORAGE, so use `WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER` instead.
  • Loading branch information
forrestguice committed Jan 6, 2025
1 parent c6e6adc commit 32bba6a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -48,6 +49,7 @@
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
Expand Down Expand Up @@ -248,6 +250,7 @@ public void onCreate(Bundle icicle)
{
AppSettings.setTheme(this, AppSettings.loadThemePref(this));
super.onCreate(icicle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
initLocale();
setResult(RESULT_CANCELED);
setContentView(R.layout.layout_themeconfig);
Expand Down Expand Up @@ -1849,14 +1852,25 @@ protected static boolean validateThemeDisplayText(Context context, EditText edit

protected void initWallpaper()
{
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
if (wallpaperManager != null)
ImageView background = (ImageView)findViewById(R.id.preview_background);

if (Build.VERSION.SDK_INT > 18)
{
ImageView background = (ImageView)findViewById(R.id.preview_background);
Drawable wallpaper = wallpaperManager.getDrawable();
if (background != null && wallpaper != null)
{
background.setImageDrawable(wallpaper);
background.setVisibility(View.GONE);
getWindow().setBackgroundDrawable(new ColorDrawable(0));

} else {
try {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
if (wallpaperManager != null)
{
Drawable wallpaper = wallpaperManager.getDrawable();
if (background != null && wallpaper != null) {
background.setImageDrawable(wallpaper);
}
}
} catch (Exception e) {
Log.e("initWallpaper", "failed to init wallpaper; " + e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
Expand All @@ -51,6 +52,7 @@
import android.view.MenuItem;
import android.view.View;

import android.view.WindowManager;
import android.widget.AdapterView;

import android.widget.GridView;
Expand Down Expand Up @@ -127,6 +129,11 @@ public void onCreate(Bundle icicle)
{
AppSettings.setTheme(this, AppSettings.loadThemePref(this));
super.onCreate(icicle);
if (Build.VERSION.SDK_INT > 18)
{
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
getWindow().setBackgroundDrawable(new ColorDrawable(0));
}
initLocale();
setResult(RESULT_CANCELED);
setContentView(R.layout.layout_activity_themelist);
Expand Down Expand Up @@ -193,6 +200,14 @@ public void onClick(View v)
}
});
}

if (Build.VERSION.SDK_INT > 18)
{
ImageView background = (ImageView)findViewById(R.id.themegrid_background);
if (background != null) {
background.setAlpha(1f);
}
}
}

protected void initActionBar( Context context )
Expand Down Expand Up @@ -845,9 +860,10 @@ public void onRestoreInstanceState(@NonNull Bundle savedState)
public void onResume()
{
super.onResume();
if (useWallpaper)
{
if (useWallpaper) {
initWallpaper(false);
} else {
hideWallpaper();
}
if (isExporting && exportTask != null)
{
Expand All @@ -874,12 +890,28 @@ public void onResume()
* Set activity background to match home screen wallpaper.
*/
protected void initWallpaper(boolean animate)
{
if (Build.VERSION.SDK_INT > 18)
{
ImageView shade = (ImageView)findViewById(R.id.themegrid_background);
shade.animate().alpha(0f).setDuration(WALLPAPER_DELAY);

} else {
try {
initWallpaperLegacy(animate);
} catch (Exception e) {
Log.e("initWallpaper", "failed to init wallpaper; " + e);
}
}
}
@Deprecated
protected void initWallpaperLegacy(boolean animate)
{
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
if (wallpaperManager != null)
{
ImageView background = (ImageView)findViewById(R.id.themegrid_background);
Drawable wallpaper = wallpaperManager.getDrawable();
Drawable wallpaper = wallpaperManager.getDrawable(); // requires MANAGE_EXTERNAL_STORAGE
if (background != null && wallpaper != null)
{
background.setImageDrawable(wallpaper);
Expand All @@ -903,8 +935,11 @@ protected void hideWallpaper()
ImageView background = (ImageView)findViewById(R.id.themegrid_background);
if (background != null)
{
if (Build.VERSION.SDK_INT >= 12)
{
if (Build.VERSION.SDK_INT > 18) {
ImageView shade = background;
shade.animate().alpha(1f).setDuration(WALLPAPER_DELAY);

} else if (Build.VERSION.SDK_INT >= 12) {
background.animate().alpha(0f).setDuration(WALLPAPER_DELAY);

} else if (Build.VERSION.SDK_INT >= 11) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/layout_themeconfig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
android:layout_width="match_parent" android:layout_height="match_parent">

<ImageView
android:id="@+id/preview_background"
android:id="@+id/preview_background" android:visibility="gone"
android:layout_width="match_parent" android:layout_height="match_parent"
android:src="?android:attr/windowBackground" android:scaleType="centerCrop"
tools:ignore="ContentDescription" />
Expand Down

0 comments on commit 32bba6a

Please sign in to comment.