package jp.hazuki.yuzubrowser.speeddial;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Base64;

import java.io.Serializable;

import jp.hazuki.yuzubrowser.utils.ImageUtils;

/**
 * Created by hazuki on 17/02/19.
 */

public class WebIcon implements Serializable {
    private final byte[] iconBytes;

    public WebIcon(byte[] icon) {
        iconBytes = icon;
    }

    private WebIcon(Bitmap bitmap) {
        iconBytes = ImageUtils.bmp2byteArray(bitmap, Bitmap.CompressFormat.PNG, 100);
    }

    public byte[] getIconBytes() {
        return iconBytes;
    }

    public String getIconBase64() {
        return "data:image/webp;base64," + new String(Base64.encode(iconBytes, Base64.DEFAULT));
    }

    public Bitmap getBitmap() {
        return BitmapFactory.decodeByteArray(iconBytes, 0, iconBytes.length);
    }

    public static WebIcon createIcon(Bitmap icon) {
        if (icon == null) return null;

        int x = icon.getWidth();
        int y = icon.getHeight();

        if (x == y && x <= 200) {
            return new WebIcon(icon);
        }

        float scale;
        if (Math.max(x, y) <= 200) {
            scale = 1f;
        } else {
            scale = Math.max((float) 200 / x, (float) 200 / y);
        }
        int size = Math.min(x, y);
        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        Bitmap bmp = Bitmap.createBitmap(icon, (x - size) / 2, (y - size) / 2, size, size, matrix, true);

        return new WebIcon(bmp);
    }
}

@@ -1,8 +1,603 @@
+/*%0A * Copyright (C) 2017 Hazuki%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0A
 package 
@@ -844,51 +844,8 @@
 s;%0A%0A
-/**%0A * Created by hazuki on 17/02/19.%0A */%0A%0A
 publ
@@ -1256,12 +1256,11 @@
 age/
-webp
+png
 ;bas

fix speed dial icon's mime type