From f887648f48268b85dd678678aaa75e200fe42bd5 Mon Sep 17 00:00:00 2001 From: gdlbo <41114720+gdlbo@users.noreply.github.com> Date: Fri, 26 Aug 2022 23:03:35 +0300 Subject: [PATCH] I love NullPointerException NullPointerException NullPointerException NullPointerException NullPointerException NullPointerException --- .../lite/ui/wallpapers/ImageFilters.java | 99 +++++++++++++++++-- .../lite/ui/wallpapers/WallpapersHooks.java | 21 +++- 2 files changed, 111 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/ru/vtosters/lite/ui/wallpapers/ImageFilters.java b/app/src/main/java/ru/vtosters/lite/ui/wallpapers/ImageFilters.java index e771f9a59..92d3bc450 100644 --- a/app/src/main/java/ru/vtosters/lite/ui/wallpapers/ImageFilters.java +++ b/app/src/main/java/ru/vtosters/lite/ui/wallpapers/ImageFilters.java @@ -1,5 +1,6 @@ package ru.vtosters.lite.ui.wallpapers; +import static ru.vtosters.lite.ui.wallpapers.WallpapersHooks.removeWallpaper; import static ru.vtosters.lite.utils.AndroidUtils.getPreferences; import static ru.vtosters.lite.utils.AndroidUtils.getResources; import static ru.vtosters.lite.utils.AndroidUtils.sendToast; @@ -13,6 +14,7 @@ import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; +import android.util.Log; import com.vk.medianative.MediaNative; @@ -28,7 +30,23 @@ public class ImageFilters { public static Drawable getFilteredDrawable(Drawable orig) { if (orig == null) return null; if (!(orig instanceof BitmapDrawable)) return orig; - Bitmap instance = ((BitmapDrawable) orig).getBitmap().copy(Bitmap.Config.ARGB_8888, true); + Bitmap bm = ((BitmapDrawable) orig).getBitmap(); + + if (bm == null) { + Log.d("Wallpaper", "bitmap null"); + sendToast("Ошибка при установке фильтра. Сбрасываем настройки на по умолчанию"); + removeWallpaper(); + return orig; + } + + Bitmap instance = bm.copy(Bitmap.Config.ARGB_8888, true); + + if (instance == null) { + Log.d("Wallpaper", "bitmap null"); + sendToast("Ошибка при установке фильтра. Сбрасываем настройки на по умолчанию"); + removeWallpaper(); + return orig; + } if (getPreferences().getBoolean("msg_invert", false)) { instance = applyInvertEffect(instance); @@ -61,7 +79,24 @@ public class ImageFilters { try { if (orig == null) return null; if (!(orig instanceof BitmapDrawable)) return orig; // we need only BitmapDrawable - Bitmap instance = ((BitmapDrawable) orig).getBitmap().copy(Bitmap.Config.ARGB_8888, true); + Bitmap bm = ((BitmapDrawable) orig).getBitmap(); + + if (bm == null) { + Log.d("Wallpaper", "bitmap null"); + sendToast("Ошибка при установке фильтра. Сбрасываем настройки на по умолчанию"); + removeWallpaper(); + return orig; + } + + Bitmap instance = bm.copy(Bitmap.Config.ARGB_8888, true); + + if (instance == null) { + Log.d("Wallpaper", "bitmap null"); + sendToast("Ошибка при установке фильтра. Сбрасываем настройки на по умолчанию"); + removeWallpaper(); + return orig; + } + MediaNative.blurBitmap(instance, radius); return new BitmapDrawable(getResources(), instance); } catch (Exception e) { @@ -75,7 +110,24 @@ public class ImageFilters { if (orig == null) return null; if (!(orig instanceof BitmapDrawable)) return orig; - Bitmap instance = ((BitmapDrawable) orig).getBitmap().copy(Bitmap.Config.ARGB_8888, true); + Bitmap bm = ((BitmapDrawable) orig).getBitmap(); + + if (bm == null) { + Log.d("Wallpaper", "bitmap null"); + sendToast("Ошибка при установке фильтра. Сбрасываем настройки на по умолчанию"); + removeWallpaper(); + return orig; + } + + Bitmap instance = bm.copy(Bitmap.Config.ARGB_8888, true); + + if (instance == null) { + Log.d("Wallpaper", "bitmap null"); + sendToast("Ошибка при установке фильтра. Сбрасываем настройки на по умолчанию"); + removeWallpaper(); + return orig; + } + String radius = getPreferences().getString("msg_mosaic", "disabled"); switch (radius) { @@ -98,7 +150,24 @@ public class ImageFilters { public static Drawable getDimmed(Drawable orig) { if (orig == null) return null; if (!(orig instanceof BitmapDrawable)) return orig; - Bitmap instance = ((BitmapDrawable) orig).getBitmap().copy(Bitmap.Config.ARGB_8888, true); + Bitmap bm = ((BitmapDrawable) orig).getBitmap(); + + if (bm == null) { + Log.d("Wallpaper", "bitmap null"); + sendToast("Ошибка при установке фильтра. Сбрасываем настройки на по умолчанию"); + removeWallpaper(); + return orig; + } + + Bitmap instance = bm.copy(Bitmap.Config.ARGB_8888, true); + + if (instance == null) { + Log.d("Wallpaper", "bitmap null"); + sendToast("Ошибка при установке фильтра. Сбрасываем настройки на по умолчанию"); + removeWallpaper(); + return orig; + } + String radius = getPreferences().getString("msg_dim", "disabled"); switch (radius) { case "dim_black": @@ -112,7 +181,23 @@ public class ImageFilters { public static Drawable getMonochrome(Drawable orig) { if (orig == null) return null; if (!(orig instanceof BitmapDrawable)) return orig; - Bitmap instance = ((BitmapDrawable) orig).getBitmap().copy(Bitmap.Config.ARGB_8888, true); + Bitmap bm = ((BitmapDrawable) orig).getBitmap(); + + if (bm == null) { + Log.d("Wallpaper", "bitmap null"); + sendToast("Ошибка при установке фильтра. Сбрасываем настройки на по умолчанию"); + removeWallpaper(); + return orig; + } + + Bitmap instance = bm.copy(Bitmap.Config.ARGB_8888, true); + + if (instance == null) { + Log.d("Wallpaper", "bitmap null"); + sendToast("Ошибка при установке фильтра. Сбрасываем настройки на по умолчанию"); + removeWallpaper(); + return orig; + } Bitmap bwBitmap = Bitmap.createBitmap(instance.getWidth(), instance.getHeight(), Bitmap.Config.RGB_565); float[] hsv = new float[3]; @@ -138,7 +223,7 @@ public class ImageFilters { p.setColorFilter(filter); canvas.drawBitmap(bm, new Matrix(), p); } catch (Exception e) { - sendToast("Ошибка при работе затемнения обоев"); + Log.d("WallpaperD", e.getMessage()); } } @@ -150,7 +235,7 @@ public class ImageFilters { p.setColorFilter(filter); canvas.drawBitmap(bm, new Matrix(), p); } catch (Exception e) { - sendToast("Ошибка при работе осветления обоев"); + Log.d("WallpaperL", e.getMessage()); } } diff --git a/app/src/main/java/ru/vtosters/lite/ui/wallpapers/WallpapersHooks.java b/app/src/main/java/ru/vtosters/lite/ui/wallpapers/WallpapersHooks.java index dcfa5d7cd..c9062f294 100644 --- a/app/src/main/java/ru/vtosters/lite/ui/wallpapers/WallpapersHooks.java +++ b/app/src/main/java/ru/vtosters/lite/ui/wallpapers/WallpapersHooks.java @@ -14,13 +14,16 @@ import static ru.vtosters.lite.utils.Preferences.hasVerification; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; +import android.net.Uri; import android.util.Log; import android.view.View; import android.widget.ImageView; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import ru.vtosters.lite.utils.AndroidUtils; @@ -114,10 +117,24 @@ public class WallpapersHooks { public static Drawable getWallpaper() { if (mWallpaper == null || mUpdateWallpaperRequested) { + String url = getWallpaperUrl(); + + if (url.equals("default") || url.isEmpty()) { + return null; + } + + Drawable drawable = Drawable.createFromPath(getWallpaperUrl()); + + if (drawable == null || ((BitmapDrawable) drawable).getBitmap() == null) { + Log.d("Wallpaper", "drawable null"); + removeWallpaper(); + return null; + } + if (!getPreferences().getString("msg_blur_radius", "disabled").equals("disabled")) { - mWallpaper = getBlurredWallpaper(Drawable.createFromPath(getWallpaperUrl()), getRadius()); + mWallpaper = getBlurredWallpaper(drawable, getRadius()); } else { - mWallpaper = Drawable.createFromPath(getWallpaperUrl()); + mWallpaper = drawable; } if (!getPreferences().getString("msg_dim", "off").equals("off")) { mWallpaper = getDimmed(mWallpaper);