Search in sources :

Example 1 with BackgroundGradientDrawable

use of org.telegram.ui.Components.BackgroundGradientDrawable in project Telegram-FOSS by Telegram-FOSS-Team.

the class AndroidUtilities method calcDrawableColor.

public static int[] calcDrawableColor(Drawable drawable) {
    int bitmapColor = 0xff000000;
    int[] result = new int[4];
    try {
        if (drawable instanceof BitmapDrawable) {
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
            bitmapColor = calcBitmapColor(bitmap);
        } else if (drawable instanceof ColorDrawable) {
            bitmapColor = ((ColorDrawable) drawable).getColor();
        } else if (drawable instanceof BackgroundGradientDrawable) {
            int[] colors = ((BackgroundGradientDrawable) drawable).getColorsList();
            if (colors != null) {
                if (colors.length > 1) {
                    bitmapColor = getAverageColor(colors[0], colors[1]);
                } else if (colors.length > 0) {
                    bitmapColor = colors[0];
                }
            }
        } else if (drawable instanceof MotionBackgroundDrawable) {
            result[0] = result[2] = Color.argb(0x2D, 0, 0, 0);
            result[1] = result[3] = Color.argb(0x3D, 0, 0, 0);
            return result;
        }
    } catch (Exception e) {
        FileLog.e(e);
    }
    double[] hsv = rgbToHsv((bitmapColor >> 16) & 0xff, (bitmapColor >> 8) & 0xff, bitmapColor & 0xff);
    hsv[1] = Math.min(1.0, hsv[1] + 0.05 + 0.1 * (1.0 - hsv[1]));
    double v = Math.max(0, hsv[2] * 0.65);
    int[] rgb = hsvToRgb(hsv[0], hsv[1], v);
    result[0] = Color.argb(0x66, rgb[0], rgb[1], rgb[2]);
    result[1] = Color.argb(0x88, rgb[0], rgb[1], rgb[2]);
    double v2 = Math.max(0, hsv[2] * 0.72);
    rgb = hsvToRgb(hsv[0], hsv[1], v2);
    result[2] = Color.argb(0x66, rgb[0], rgb[1], rgb[2]);
    result[3] = Color.argb(0x88, rgb[0], rgb[1], rgb[2]);
    return result;
}
Also used : Bitmap(android.graphics.Bitmap) ColorDrawable(android.graphics.drawable.ColorDrawable) BackgroundGradientDrawable(org.telegram.ui.Components.BackgroundGradientDrawable) MotionBackgroundDrawable(org.telegram.ui.Components.MotionBackgroundDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point) IOException(java.io.IOException)

Example 2 with BackgroundGradientDrawable

use of org.telegram.ui.Components.BackgroundGradientDrawable in project Telegram-FOSS by Telegram-FOSS-Team.

the class ThemePreviewActivity method setCurrentImage.

private void setCurrentImage(boolean setThumb) {
    if (screenType == SCREEN_TYPE_PREVIEW && accent == null) {
        backgroundImage.setBackground(Theme.getCachedWallpaper());
    } else if (screenType == SCREEN_TYPE_CHANGE_BACKGROUND) {
        if (currentWallpaper instanceof TLRPC.TL_wallPaper) {
            TLRPC.TL_wallPaper wallPaper = (TLRPC.TL_wallPaper) currentWallpaper;
            TLRPC.PhotoSize thumb = setThumb ? FileLoader.getClosestPhotoSizeWithSize(wallPaper.document.thumbs, 100) : null;
            backgroundImage.setImage(ImageLocation.getForDocument(wallPaper.document), imageFilter, ImageLocation.getForDocument(thumb, wallPaper.document), "100_100_b", "jpg", wallPaper.document.size, 1, wallPaper);
        } else if (currentWallpaper instanceof WallpapersListActivity.ColorWallpaper) {
            WallpapersListActivity.ColorWallpaper wallPaper = (WallpapersListActivity.ColorWallpaper) currentWallpaper;
            backgroundRotation = wallPaper.gradientRotation;
            setBackgroundColor(wallPaper.color, 0, true, false);
            if (wallPaper.gradientColor1 != 0) {
                setBackgroundColor(wallPaper.gradientColor1, 1, true, false);
            }
            setBackgroundColor(wallPaper.gradientColor2, 2, true, false);
            setBackgroundColor(wallPaper.gradientColor3, 3, true, false);
            if (selectedPattern != null) {
                backgroundImage.setImage(ImageLocation.getForDocument(selectedPattern.document), imageFilter, null, null, "jpg", selectedPattern.document.size, 1, selectedPattern);
            } else if (Theme.DEFAULT_BACKGROUND_SLUG.equals(wallPaper.slug)) {
                int w = Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y);
                int h = Math.max(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y);
                int patternColor;
                if (Build.VERSION.SDK_INT >= 29) {
                    patternColor = 0x57000000;
                } else {
                    patternColor = MotionBackgroundDrawable.getPatternColor(wallPaper.color, wallPaper.gradientColor1, wallPaper.gradientColor2, wallPaper.gradientColor3);
                }
                backgroundImage.setImageBitmap(SvgHelper.getBitmap(R.raw.default_pattern, w, h, patternColor));
            }
        } else if (currentWallpaper instanceof WallpapersListActivity.FileWallpaper) {
            if (currentWallpaperBitmap != null) {
                backgroundImage.setImageBitmap(currentWallpaperBitmap);
            } else {
                WallpapersListActivity.FileWallpaper wallPaper = (WallpapersListActivity.FileWallpaper) currentWallpaper;
                if (wallPaper.originalPath != null) {
                    backgroundImage.setImage(wallPaper.originalPath.getAbsolutePath(), imageFilter, null);
                } else if (wallPaper.path != null) {
                    backgroundImage.setImage(wallPaper.path.getAbsolutePath(), imageFilter, null);
                } else if (Theme.THEME_BACKGROUND_SLUG.equals(wallPaper.slug)) {
                    backgroundImage.setImageDrawable(Theme.getThemedWallpaper(false, backgroundImage));
                } else if (wallPaper.resId != 0) {
                    backgroundImage.setImageResource(wallPaper.resId);
                }
            }
        } else if (currentWallpaper instanceof MediaController.SearchImage) {
            MediaController.SearchImage wallPaper = (MediaController.SearchImage) currentWallpaper;
            if (wallPaper.photo != null) {
                TLRPC.PhotoSize thumb = FileLoader.getClosestPhotoSizeWithSize(wallPaper.photo.sizes, 100);
                TLRPC.PhotoSize image = FileLoader.getClosestPhotoSizeWithSize(wallPaper.photo.sizes, maxWallpaperSize, true);
                if (image == thumb) {
                    image = null;
                }
                int size = image != null ? image.size : 0;
                backgroundImage.setImage(ImageLocation.getForPhoto(image, wallPaper.photo), imageFilter, ImageLocation.getForPhoto(thumb, wallPaper.photo), "100_100_b", "jpg", size, 1, wallPaper);
            } else {
                backgroundImage.setImage(wallPaper.imageUrl, imageFilter, wallPaper.thumbUrl, "100_100_b");
            }
        }
    } else {
        if (backgroundGradientDisposable != null) {
            backgroundGradientDisposable.dispose();
            backgroundGradientDisposable = null;
        }
        int defaultBackground = Theme.getDefaultAccentColor(Theme.key_chat_wallpaper);
        int backgroundOverrideColor = (int) accent.backgroundOverrideColor;
        int backgroundColor = backgroundOverrideColor != 0 ? backgroundOverrideColor : defaultBackground;
        int defaultBackgroundGradient1 = Theme.getDefaultAccentColor(Theme.key_chat_wallpaper_gradient_to1);
        int backgroundGradientOverrideColor1 = (int) accent.backgroundGradientOverrideColor1;
        int color1;
        if (backgroundGradientOverrideColor1 == 0 && accent.backgroundGradientOverrideColor1 != 0) {
            color1 = 0;
        } else {
            color1 = backgroundGradientOverrideColor1 != 0 ? backgroundGradientOverrideColor1 : defaultBackgroundGradient1;
        }
        int defaultBackgroundGradient2 = Theme.getDefaultAccentColor(Theme.key_chat_wallpaper_gradient_to2);
        int backgroundGradientOverrideColor2 = (int) accent.backgroundGradientOverrideColor2;
        int color2;
        if (backgroundGradientOverrideColor2 == 0 && accent.backgroundGradientOverrideColor2 != 0) {
            color2 = 0;
        } else {
            color2 = backgroundGradientOverrideColor2 != 0 ? backgroundGradientOverrideColor2 : defaultBackgroundGradient2;
        }
        int defaultBackgroundGradient3 = Theme.getDefaultAccentColor(Theme.key_chat_wallpaper_gradient_to3);
        int backgroundGradientOverrideColor3 = (int) accent.backgroundGradientOverrideColor3;
        int color3;
        if (backgroundGradientOverrideColor3 == 0 && accent.backgroundGradientOverrideColor3 != 0) {
            color3 = 0;
        } else {
            color3 = backgroundGradientOverrideColor3 != 0 ? backgroundGradientOverrideColor3 : defaultBackgroundGradient3;
        }
        if (!TextUtils.isEmpty(accent.patternSlug) && !Theme.hasCustomWallpaper()) {
            Drawable backgroundDrawable;
            if (color2 != 0) {
                Drawable currentBackground = backgroundImage.getBackground();
                MotionBackgroundDrawable motionBackgroundDrawable;
                if (currentBackground instanceof MotionBackgroundDrawable) {
                    motionBackgroundDrawable = (MotionBackgroundDrawable) currentBackground;
                } else {
                    motionBackgroundDrawable = new MotionBackgroundDrawable();
                    motionBackgroundDrawable.setParentView(backgroundImage);
                    if (rotatePreview) {
                        motionBackgroundDrawable.rotatePreview(false);
                    }
                }
                motionBackgroundDrawable.setColors(backgroundColor, color1, color2, color3);
                backgroundDrawable = motionBackgroundDrawable;
            } else if (color1 != 0) {
                final BackgroundGradientDrawable.Orientation orientation = BackgroundGradientDrawable.getGradientOrientation(accent.backgroundRotation);
                final BackgroundGradientDrawable backgroundGradientDrawable = new BackgroundGradientDrawable(orientation, new int[] { backgroundColor, color1 });
                final BackgroundGradientDrawable.Listener listener = new BackgroundGradientDrawable.ListenerAdapter() {

                    @Override
                    public void onSizeReady(int width, int height) {
                        final boolean isOrientationPortrait = AndroidUtilities.displaySize.x <= AndroidUtilities.displaySize.y;
                        final boolean isGradientPortrait = width <= height;
                        if (isOrientationPortrait == isGradientPortrait) {
                            backgroundImage.invalidate();
                        }
                    }
                };
                backgroundGradientDisposable = backgroundGradientDrawable.startDithering(BackgroundGradientDrawable.Sizes.ofDeviceScreen(), listener, 100);
                backgroundDrawable = backgroundGradientDrawable;
            } else {
                backgroundDrawable = new ColorDrawable(backgroundColor);
            }
            backgroundImage.setBackground(backgroundDrawable);
            if (selectedPattern != null) {
                backgroundImage.setImage(ImageLocation.getForDocument(selectedPattern.document), imageFilter, null, null, "jpg", selectedPattern.document.size, 1, selectedPattern);
            }
        } else {
            Drawable backgroundDrawable = Theme.getCachedWallpaper();
            if (backgroundDrawable != null) {
                if (backgroundDrawable instanceof MotionBackgroundDrawable) {
                    ((MotionBackgroundDrawable) backgroundDrawable).setParentView(backgroundImage);
                }
                backgroundImage.setBackground(backgroundDrawable);
            }
        }
        if (color1 == 0) {
            patternColor = checkColor = AndroidUtilities.getPatternColor(backgroundColor);
        } else {
            if (color2 != 0) {
                patternColor = MotionBackgroundDrawable.getPatternColor(backgroundColor, color1, color2, color3);
                checkColor = 0x2D000000;
            } else {
                patternColor = checkColor = AndroidUtilities.getPatternColor(AndroidUtilities.getAverageColor(backgroundColor, color1));
            }
        }
        if (backgroundImage != null) {
            backgroundImage.getImageReceiver().setColorFilter(new PorterDuffColorFilter(patternColor, blendMode));
            backgroundImage.getImageReceiver().setAlpha(Math.abs(currentIntensity));
            backgroundImage.invalidate();
            if (Theme.getActiveTheme().isDark() && backgroundImage.getBackground() instanceof MotionBackgroundDrawable) {
                if (intensitySeekBar != null) {
                    intensitySeekBar.setTwoSided(true);
                }
                if (currentIntensity < 0) {
                    backgroundImage.getImageReceiver().setGradientBitmap(((MotionBackgroundDrawable) backgroundImage.getBackground()).getBitmap());
                }
            } else {
                backgroundImage.getImageReceiver().setGradientBitmap(null);
                if (intensitySeekBar != null) {
                    intensitySeekBar.setTwoSided(false);
                }
            }
            if (intensitySeekBar != null) {
                intensitySeekBar.setProgress(currentIntensity);
            }
        }
        if (backgroundCheckBoxView != null) {
            for (int a = 0; a < backgroundCheckBoxView.length; a++) {
                backgroundCheckBoxView[a].setColor(0, backgroundColor);
                backgroundCheckBoxView[a].setColor(1, color1);
                backgroundCheckBoxView[a].setColor(2, color2);
                backgroundCheckBoxView[a].setColor(3, color3);
            }
        }
        if (backgroundPlayAnimationImageView != null) {
            backgroundPlayAnimationImageView.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_chat_serviceText), PorterDuff.Mode.MULTIPLY));
        }
        if (messagesPlayAnimationImageView != null) {
            messagesPlayAnimationImageView.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_chat_serviceText), PorterDuff.Mode.MULTIPLY));
        }
        if (backgroundButtonsContainer != null) {
            for (int a = 0, N = backgroundButtonsContainer.getChildCount(); a < N; a++) {
                backgroundButtonsContainer.getChildAt(a).invalidate();
            }
        }
        if (messagesButtonsContainer != null) {
            for (int a = 0, N = messagesButtonsContainer.getChildCount(); a < N; a++) {
                messagesButtonsContainer.getChildAt(a).invalidate();
            }
        }
    }
    rotatePreview = false;
}
Also used : MediaController(org.telegram.messenger.MediaController) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) MediaActionDrawable(org.telegram.ui.Components.MediaActionDrawable) CombinedDrawable(org.telegram.ui.Components.CombinedDrawable) MenuDrawable(org.telegram.ui.ActionBar.MenuDrawable) BackgroundGradientDrawable(org.telegram.ui.Components.BackgroundGradientDrawable) GradientDrawable(android.graphics.drawable.GradientDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) BackDrawable(org.telegram.ui.ActionBar.BackDrawable) MotionBackgroundDrawable(org.telegram.ui.Components.MotionBackgroundDrawable) MotionBackgroundDrawable(org.telegram.ui.Components.MotionBackgroundDrawable) PorterDuffColorFilter(android.graphics.PorterDuffColorFilter) TLRPC(org.telegram.tgnet.TLRPC) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) SuppressLint(android.annotation.SuppressLint) AnimatorListenerAdapter(android.animation.AnimatorListenerAdapter) ColorDrawable(android.graphics.drawable.ColorDrawable) BackgroundGradientDrawable(org.telegram.ui.Components.BackgroundGradientDrawable)

Example 3 with BackgroundGradientDrawable

use of org.telegram.ui.Components.BackgroundGradientDrawable in project Telegram-FOSS by Telegram-FOSS-Team.

the class Theme method getThemedWallpaper.

public static Drawable getThemedWallpaper(boolean thumb, View ownerView) {
    Integer backgroundColor = currentColors.get(key_chat_wallpaper);
    File file = null;
    MotionBackgroundDrawable motionBackgroundDrawable = null;
    int offset = 0;
    if (backgroundColor != null) {
        Integer gradientToColor1 = currentColors.get(key_chat_wallpaper_gradient_to1);
        Integer gradientToColor2 = currentColors.get(key_chat_wallpaper_gradient_to2);
        Integer gradientToColor3 = currentColors.get(key_chat_wallpaper_gradient_to3);
        Integer rotation = currentColors.get(key_chat_wallpaper_gradient_rotation);
        if (rotation == null) {
            rotation = 45;
        }
        if (gradientToColor1 == null) {
            return new ColorDrawable(backgroundColor);
        } else {
            ThemeAccent accent = currentTheme.getAccent(false);
            if (accent != null && !TextUtils.isEmpty(accent.patternSlug) && previousTheme == null) {
                File wallpaperFile = accent.getPathToWallpaper();
                if (wallpaperFile != null && wallpaperFile.exists()) {
                    file = wallpaperFile;
                }
            }
            if (gradientToColor2 != null) {
                motionBackgroundDrawable = new MotionBackgroundDrawable(backgroundColor, gradientToColor1, gradientToColor2, gradientToColor3 != null ? gradientToColor3 : 0, true);
                if (file == null) {
                    return motionBackgroundDrawable;
                }
            } else if (file == null) {
                final int[] colors = { backgroundColor, gradientToColor1 };
                final GradientDrawable.Orientation orientation = BackgroundGradientDrawable.getGradientOrientation(rotation);
                final BackgroundGradientDrawable backgroundGradientDrawable = new BackgroundGradientDrawable(orientation, colors);
                final BackgroundGradientDrawable.Sizes sizes;
                if (!thumb) {
                    sizes = BackgroundGradientDrawable.Sizes.ofDeviceScreen();
                } else {
                    sizes = BackgroundGradientDrawable.Sizes.ofDeviceScreen(BackgroundGradientDrawable.DEFAULT_COMPRESS_RATIO / 4f, BackgroundGradientDrawable.Sizes.Orientation.PORTRAIT);
                }
                final BackgroundGradientDrawable.Listener listener;
                if (ownerView != null) {
                    listener = new BackgroundGradientDrawable.ListenerAdapter() {

                        @Override
                        public void onSizeReady(int width, int height) {
                            if (!thumb) {
                                final boolean isOrientationPortrait = AndroidUtilities.displaySize.x <= AndroidUtilities.displaySize.y;
                                final boolean isGradientPortrait = width <= height;
                                if (isOrientationPortrait == isGradientPortrait) {
                                    ownerView.invalidate();
                                }
                            } else {
                                ownerView.invalidate();
                            }
                        }
                    };
                } else {
                    listener = null;
                }
                backgroundGradientDrawable.startDithering(sizes, listener);
                return backgroundGradientDrawable;
            }
        }
    } else if (themedWallpaperFileOffset > 0 && (currentTheme.pathToFile != null || currentTheme.assetName != null)) {
        if (currentTheme.assetName != null) {
            file = getAssetFile(currentTheme.assetName);
        } else {
            file = new File(currentTheme.pathToFile);
        }
        offset = themedWallpaperFileOffset;
    }
    if (file != null) {
        FileInputStream stream = null;
        try {
            int currentPosition = 0;
            stream = new FileInputStream(file);
            stream.getChannel().position(offset);
            BitmapFactory.Options opts = new BitmapFactory.Options();
            int scaleFactor = 1;
            if (thumb) {
                opts.inJustDecodeBounds = true;
                float photoW = opts.outWidth;
                float photoH = opts.outHeight;
                int maxWidth = AndroidUtilities.dp(100);
                while (photoW > maxWidth || photoH > maxWidth) {
                    scaleFactor *= 2;
                    photoW /= 2;
                    photoH /= 2;
                }
            }
            opts.inJustDecodeBounds = false;
            opts.inSampleSize = scaleFactor;
            Bitmap bitmap = BitmapFactory.decodeStream(stream, null, opts);
            if (motionBackgroundDrawable != null) {
                int intensity;
                ThemeAccent accent = currentTheme.getAccent(false);
                if (accent != null) {
                    intensity = (int) (accent.patternIntensity * 100);
                } else {
                    intensity = 100;
                }
                motionBackgroundDrawable.setPatternBitmap(intensity, bitmap);
                motionBackgroundDrawable.setPatternColorFilter(motionBackgroundDrawable.getPatternColor());
                return motionBackgroundDrawable;
            }
            if (bitmap != null) {
                return new BitmapDrawable(bitmap);
            }
        } catch (Throwable e) {
            FileLog.e(e);
        } finally {
            try {
                if (stream != null) {
                    stream.close();
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
        }
    }
    return null;
}
Also used : SensorEventListener(android.hardware.SensorEventListener) MotionBackgroundDrawable(org.telegram.ui.Components.MotionBackgroundDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) SuppressLint(android.annotation.SuppressLint) FileInputStream(java.io.FileInputStream) Bitmap(android.graphics.Bitmap) ColorDrawable(android.graphics.drawable.ColorDrawable) BackgroundGradientDrawable(org.telegram.ui.Components.BackgroundGradientDrawable) BitmapFactory(android.graphics.BitmapFactory) File(java.io.File)

Example 4 with BackgroundGradientDrawable

use of org.telegram.ui.Components.BackgroundGradientDrawable in project Telegram-FOSS by Telegram-FOSS-Team.

the class Theme method createBackgroundDrawable.

public static BackgroundDrawableSettings createBackgroundDrawable(ThemeInfo currentTheme, OverrideWallpaperInfo overrideWallpaper, HashMap<String, Integer> currentColors, File wallpaperFile, String themedWallpaperLink, int themedWallpaperFileOffset, int intensity, int previousPhase, boolean defaultTheme, boolean hasPreviousTheme, boolean isApplyingAccent, boolean wallpaperMotion, TLRPC.Document wallpaperDocument) {
    BackgroundDrawableSettings settings = new BackgroundDrawableSettings();
    settings.wallpaper = wallpaper;
    boolean overrideTheme = (!hasPreviousTheme || isApplyingAccent) && overrideWallpaper != null;
    if (overrideWallpaper != null) {
        settings.isWallpaperMotion = overrideWallpaper.isMotion;
        settings.isPatternWallpaper = overrideWallpaper.color != 0 && !overrideWallpaper.isDefault() && !overrideWallpaper.isColor();
    } else {
        settings.isWallpaperMotion = currentTheme.isMotion;
        settings.isPatternWallpaper = currentTheme.patternBgColor != 0;
    }
    if (!overrideTheme) {
        Integer backgroundColor = defaultTheme ? null : currentColors.get(key_chat_wallpaper);
        Integer gradientToColor3 = currentColors.get(key_chat_wallpaper_gradient_to3);
        if (gradientToColor3 == null) {
            gradientToColor3 = 0;
        }
        Integer gradientToColor2 = currentColors.get(key_chat_wallpaper_gradient_to2);
        gradientToColor2 = currentColors.get(key_chat_wallpaper_gradient_to2);
        Integer gradientToColor1 = currentColors.get(key_chat_wallpaper_gradient_to1);
        if (wallpaperFile != null && wallpaperFile.exists()) {
            try {
                if (backgroundColor != null && gradientToColor1 != null && gradientToColor2 != null) {
                    MotionBackgroundDrawable motionBackgroundDrawable = new MotionBackgroundDrawable(backgroundColor, gradientToColor1, gradientToColor2, gradientToColor3, false);
                    motionBackgroundDrawable.setPatternBitmap(intensity, BitmapFactory.decodeFile(wallpaperFile.getAbsolutePath()));
                    motionBackgroundDrawable.setPatternColorFilter(motionBackgroundDrawable.getPatternColor());
                    settings.wallpaper = motionBackgroundDrawable;
                } else {
                    settings.wallpaper = Drawable.createFromPath(wallpaperFile.getAbsolutePath());
                }
                settings.isWallpaperMotion = wallpaperMotion;
                settings.isPatternWallpaper = true;
                settings.isCustomTheme = true;
            } catch (Throwable e) {
                FileLog.e(e);
            }
        } else if (backgroundColor != null) {
            Integer rotation = currentColors.get(key_chat_wallpaper_gradient_rotation);
            if (rotation == null) {
                rotation = 45;
            }
            if (gradientToColor1 != null && gradientToColor2 != null) {
                MotionBackgroundDrawable motionBackgroundDrawable = new MotionBackgroundDrawable(backgroundColor, gradientToColor1, gradientToColor2, gradientToColor3, false);
                Bitmap pattensBitmap = null;
                if (wallpaperFile != null && wallpaperDocument != null) {
                    File f = FileLoader.getPathToAttach(wallpaperDocument, true);
                    pattensBitmap = SvgHelper.getBitmap(f, AndroidUtilities.dp(360), AndroidUtilities.dp(640), false);
                    if (pattensBitmap != null) {
                        FileOutputStream stream = null;
                        try {
                            stream = new FileOutputStream(wallpaperFile);
                            pattensBitmap.compress(Bitmap.CompressFormat.PNG, 87, stream);
                            stream.close();
                        } catch (Exception e) {
                            FileLog.e(e);
                            e.printStackTrace();
                        }
                    }
                }
                motionBackgroundDrawable.setPatternBitmap(intensity, pattensBitmap);
                motionBackgroundDrawable.setPhase(previousPhase);
                settings.wallpaper = motionBackgroundDrawable;
            } else if (gradientToColor1 == null || gradientToColor1.equals(backgroundColor)) {
                settings.wallpaper = new ColorDrawable(backgroundColor);
            } else {
                final int[] colors = { backgroundColor, gradientToColor1 };
                final BackgroundGradientDrawable.Orientation orientation = BackgroundGradientDrawable.getGradientOrientation(rotation);
                final BackgroundGradientDrawable backgroundGradientDrawable = new BackgroundGradientDrawable(orientation, colors);
                final BackgroundGradientDrawable.Listener listener = new BackgroundGradientDrawable.ListenerAdapter() {

                    @Override
                    public void onSizeReady(int width, int height) {
                        final boolean isOrientationPortrait = AndroidUtilities.displaySize.x <= AndroidUtilities.displaySize.y;
                        final boolean isGradientPortrait = width <= height;
                        if (isOrientationPortrait == isGradientPortrait) {
                            NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.didSetNewWallpapper);
                        }
                    }
                };
                backgroundGradientDisposable = backgroundGradientDrawable.startDithering(BackgroundGradientDrawable.Sizes.ofDeviceScreen(), listener, 100);
                settings.wallpaper = backgroundGradientDrawable;
            }
            settings.isCustomTheme = true;
        } else if (themedWallpaperLink != null) {
            try {
                File pathToWallpaper = new File(ApplicationLoader.getFilesDirFixed(), Utilities.MD5(themedWallpaperLink) + ".wp");
                Bitmap bitmap = loadScreenSizedBitmap(new FileInputStream(pathToWallpaper), 0);
                if (bitmap != null) {
                    settings.wallpaper = new BitmapDrawable(bitmap);
                    settings.themedWallpaper = settings.wallpaper;
                    settings.isCustomTheme = true;
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
        } else if (themedWallpaperFileOffset > 0 && (currentTheme.pathToFile != null || currentTheme.assetName != null)) {
            try {
                File file;
                if (currentTheme.assetName != null) {
                    file = getAssetFile(currentTheme.assetName);
                } else {
                    file = new File(currentTheme.pathToFile);
                }
                Bitmap bitmap = loadScreenSizedBitmap(new FileInputStream(file), themedWallpaperFileOffset);
                if (bitmap != null) {
                    settings.wallpaper = settings.themedWallpaper = wallpaper = new BitmapDrawable(bitmap);
                    settings.isCustomTheme = true;
                }
            } catch (Throwable e) {
                FileLog.e(e);
            }
        }
    }
    if (settings.wallpaper == null) {
        int selectedColor = overrideWallpaper != null ? overrideWallpaper.color : 0;
        try {
            if (overrideWallpaper == null || overrideWallpaper.isDefault()) {
                settings.wallpaper = createDefaultWallpaper();
                settings.isCustomTheme = false;
            } else if (!overrideWallpaper.isColor() || overrideWallpaper.gradientColor1 != 0) {
                if (selectedColor != 0 && (!isPatternWallpaper || overrideWallpaper.gradientColor2 != 0)) {
                    if (overrideWallpaper.gradientColor1 != 0 && overrideWallpaper.gradientColor2 != 0) {
                        MotionBackgroundDrawable motionBackgroundDrawable = new MotionBackgroundDrawable(overrideWallpaper.color, overrideWallpaper.gradientColor1, overrideWallpaper.gradientColor2, overrideWallpaper.gradientColor3, false);
                        motionBackgroundDrawable.setPhase(previousPhase);
                        if (settings.isPatternWallpaper) {
                            File toFile = new File(ApplicationLoader.getFilesDirFixed(), overrideWallpaper.fileName);
                            if (toFile.exists()) {
                                motionBackgroundDrawable.setPatternBitmap((int) (overrideWallpaper.intensity * 100), loadScreenSizedBitmap(new FileInputStream(toFile), 0));
                                settings.isCustomTheme = true;
                            }
                        }
                        settings.wallpaper = motionBackgroundDrawable;
                    } else if (overrideWallpaper.gradientColor1 != 0) {
                        final int[] colors = { selectedColor, overrideWallpaper.gradientColor1 };
                        final BackgroundGradientDrawable.Orientation orientation = BackgroundGradientDrawable.getGradientOrientation(overrideWallpaper.rotation);
                        final BackgroundGradientDrawable backgroundGradientDrawable = new BackgroundGradientDrawable(orientation, colors);
                        final BackgroundGradientDrawable.Listener listener = new BackgroundGradientDrawable.ListenerAdapter() {

                            @Override
                            public void onSizeReady(int width, int height) {
                                final boolean isOrientationPortrait = AndroidUtilities.displaySize.x <= AndroidUtilities.displaySize.y;
                                final boolean isGradientPortrait = width <= height;
                                if (isOrientationPortrait == isGradientPortrait) {
                                    NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.didSetNewWallpapper);
                                }
                            }
                        };
                        backgroundGradientDisposable = backgroundGradientDrawable.startDithering(BackgroundGradientDrawable.Sizes.ofDeviceScreen(), listener, 100);
                        settings.wallpaper = backgroundGradientDrawable;
                    } else {
                        settings.wallpaper = new ColorDrawable(selectedColor);
                    }
                } else {
                    File toFile = new File(ApplicationLoader.getFilesDirFixed(), overrideWallpaper.fileName);
                    if (toFile.exists()) {
                        Bitmap bitmap = loadScreenSizedBitmap(new FileInputStream(toFile), 0);
                        if (bitmap != null) {
                            settings.wallpaper = new BitmapDrawable(bitmap);
                            settings.isCustomTheme = true;
                        }
                    }
                    if (settings.wallpaper == null) {
                        settings.wallpaper = createDefaultWallpaper();
                        settings.isCustomTheme = false;
                    }
                }
            }
        } catch (Throwable throwable) {
        // ignore
        }
        if (settings.wallpaper == null) {
            if (selectedColor == 0) {
                selectedColor = -2693905;
            }
            settings.wallpaper = new ColorDrawable(selectedColor);
        }
    }
    return settings;
}
Also used : MotionBackgroundDrawable(org.telegram.ui.Components.MotionBackgroundDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) FileInputStream(java.io.FileInputStream) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) SuppressLint(android.annotation.SuppressLint) Bitmap(android.graphics.Bitmap) ColorDrawable(android.graphics.drawable.ColorDrawable) BackgroundGradientDrawable(org.telegram.ui.Components.BackgroundGradientDrawable) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 5 with BackgroundGradientDrawable

use of org.telegram.ui.Components.BackgroundGradientDrawable in project Telegram-FOSS by Telegram-FOSS-Team.

the class ThemePreviewMessagesCell method onDraw.

@Override
protected void onDraw(Canvas canvas) {
    Drawable newDrawable = Theme.getCachedWallpaperNonBlocking();
    if (newDrawable != backgroundDrawable && newDrawable != null) {
        if (Theme.isAnimatingColor()) {
            oldBackgroundDrawable = backgroundDrawable;
            oldBackgroundGradientDisposable = backgroundGradientDisposable;
        } else if (backgroundGradientDisposable != null) {
            backgroundGradientDisposable.dispose();
            backgroundGradientDisposable = null;
        }
        backgroundDrawable = newDrawable;
    }
    float themeAnimationValue = parentLayout.getThemeAnimationValue();
    for (int a = 0; a < 2; a++) {
        Drawable drawable = a == 0 ? oldBackgroundDrawable : backgroundDrawable;
        if (drawable == null) {
            continue;
        }
        if (a == 1 && oldBackgroundDrawable != null && parentLayout != null) {
            drawable.setAlpha((int) (255 * themeAnimationValue));
        } else {
            drawable.setAlpha(255);
        }
        if (drawable instanceof ColorDrawable || drawable instanceof GradientDrawable || drawable instanceof MotionBackgroundDrawable) {
            drawable.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
            if (drawable instanceof BackgroundGradientDrawable) {
                final BackgroundGradientDrawable backgroundGradientDrawable = (BackgroundGradientDrawable) drawable;
                backgroundGradientDisposable = backgroundGradientDrawable.drawExactBoundsSize(canvas, this);
            } else {
                drawable.draw(canvas);
            }
        } else if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if (bitmapDrawable.getTileModeX() == Shader.TileMode.REPEAT) {
                canvas.save();
                float scale = 2.0f / AndroidUtilities.density;
                canvas.scale(scale, scale);
                drawable.setBounds(0, 0, (int) Math.ceil(getMeasuredWidth() / scale), (int) Math.ceil(getMeasuredHeight() / scale));
            } else {
                int viewHeight = getMeasuredHeight();
                float scaleX = (float) getMeasuredWidth() / (float) drawable.getIntrinsicWidth();
                float scaleY = (float) (viewHeight) / (float) drawable.getIntrinsicHeight();
                float scale = Math.max(scaleX, scaleY);
                int width = (int) Math.ceil(drawable.getIntrinsicWidth() * scale);
                int height = (int) Math.ceil(drawable.getIntrinsicHeight() * scale);
                int x = (getMeasuredWidth() - width) / 2;
                int y = (viewHeight - height) / 2;
                canvas.save();
                canvas.clipRect(0, 0, width, getMeasuredHeight());
                drawable.setBounds(x, y, x + width, y + height);
            }
            drawable.draw(canvas);
            canvas.restore();
        }
        if (a == 0 && oldBackgroundDrawable != null && themeAnimationValue >= 1.0f) {
            if (oldBackgroundGradientDisposable != null) {
                oldBackgroundGradientDisposable.dispose();
                oldBackgroundGradientDisposable = null;
            }
            oldBackgroundDrawable = null;
            invalidate();
        }
    }
    shadowDrawable.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
    shadowDrawable.draw(canvas);
}
Also used : ColorDrawable(android.graphics.drawable.ColorDrawable) BackgroundGradientDrawable(org.telegram.ui.Components.BackgroundGradientDrawable) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) BackgroundGradientDrawable(org.telegram.ui.Components.BackgroundGradientDrawable) GradientDrawable(android.graphics.drawable.GradientDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) MotionBackgroundDrawable(org.telegram.ui.Components.MotionBackgroundDrawable) MotionBackgroundDrawable(org.telegram.ui.Components.MotionBackgroundDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) SuppressLint(android.annotation.SuppressLint) BackgroundGradientDrawable(org.telegram.ui.Components.BackgroundGradientDrawable) GradientDrawable(android.graphics.drawable.GradientDrawable)

Aggregations

SuppressLint (android.annotation.SuppressLint)6 BackgroundGradientDrawable (org.telegram.ui.Components.BackgroundGradientDrawable)6 Paint (android.graphics.Paint)5 BitmapDrawable (android.graphics.drawable.BitmapDrawable)5 ColorDrawable (android.graphics.drawable.ColorDrawable)5 MotionBackgroundDrawable (org.telegram.ui.Components.MotionBackgroundDrawable)5 TextPaint (android.text.TextPaint)4 Bitmap (android.graphics.Bitmap)3 GradientDrawable (android.graphics.drawable.GradientDrawable)3 Drawable (android.graphics.drawable.Drawable)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 AnimatorListenerAdapter (android.animation.AnimatorListenerAdapter)1 Intent (android.content.Intent)1 BitmapFactory (android.graphics.BitmapFactory)1 Canvas (android.graphics.Canvas)1 Point (android.graphics.Point)1 PorterDuffColorFilter (android.graphics.PorterDuffColorFilter)1 SensorEventListener (android.hardware.SensorEventListener)1 MotionEvent (android.view.MotionEvent)1