/*
 * Designed and developed by 2017 skydoves (Jaewoong Eum)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.skydoves.colorpickerview;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import androidx.annotation.Nullable;
import com.skydoves.colorpickerview.sliders.AlphaTileDrawable;

/** AlphaTileView visualizes ARGB color on the canvas using {@link AlphaTileDrawable}. */
@SuppressWarnings("unused")
public class AlphaTileView extends View {

  private Paint colorPaint;
  private Bitmap backgroundBitmap;
  private AlphaTileDrawable.Builder builder = new AlphaTileDrawable.Builder();

  public AlphaTileView(Context context) {
    super(context);
    onCreate();
    draw();
  }

  public AlphaTileView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    onCreate();
    getAttrs(attrs);
    draw();
  }

  public AlphaTileView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    onCreate();
    getAttrs(attrs);
    draw();
  }

  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public AlphaTileView(
      Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    onCreate();
    getAttrs(attrs);
    draw();
  }

  private void onCreate() {
    this.colorPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    this.setBackgroundColor(Color.WHITE);
  }

  private void getAttrs(AttributeSet attrs) {
    TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.AlphaTileView);
    try {
      if (a.hasValue(R.styleable.AlphaTileView_tileSize))
        builder.setTileSize(a.getInt(R.styleable.AlphaTileView_tileSize, builder.getTileSize()));
      if (a.hasValue(R.styleable.AlphaTileView_tileOddColor))
        builder.setTileOddColor(
            a.getInt(R.styleable.AlphaTileView_tileOddColor, builder.getTileOddColor()));
      if (a.hasValue(R.styleable.AlphaTileView_tileEvenColor))
        builder.setTileEvenColor(
            a.getInt(R.styleable.AlphaTileView_tileEvenColor, builder.getTileEvenColor()));
    } finally {
      a.recycle();
    }
  }

  private void draw() {
    getViewTreeObserver()
        .addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
              @Override
              public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT < 16) {
                  getViewTreeObserver().removeGlobalOnLayoutListener(this);
                } else {
                  getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
                AlphaTileDrawable drawable = builder.build();
                backgroundBitmap =
                    Bitmap.createBitmap(
                        getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
                Canvas backgroundCanvas = new Canvas(backgroundBitmap);
                drawable.setBounds(0, 0, backgroundCanvas.getWidth(), backgroundCanvas.getHeight());
                drawable.draw(backgroundCanvas);
              }
            });
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(backgroundBitmap, 0, 0, null);
    canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), colorPaint);
  }

  public void setPaintColor(int color) {
    colorPaint.setColor(color);
    invalidate();
  }

  @Override
  public void setBackgroundColor(int color) {
    setPaintColor(color);
  }
}

@@ -975,46 +975,8 @@
 ew;%0A
-import android.view.ViewTreeObserver;%0A
 impo
@@ -1008,16 +1008,16 @@
 llable;%0A
+
 import c
@@ -1454,28 +1454,16 @@
 eate();%0A
-    draw();%0A
   %7D%0A%0A  p
@@ -1591,36 +1591,24 @@
 trs(attrs);%0A
-    draw();%0A
   %7D%0A%0A  publi
@@ -1764,36 +1764,24 @@
 trs(attrs);%0A
-    draw();%0A
   %7D%0A%0A  @Targ
@@ -2025,20 +2025,8 @@
 s);%0A
-    draw();%0A
   %7D%0A
@@ -2360,16 +2360,18 @@
 leSize))
+ %7B
 %0A       
@@ -2457,24 +2457,32 @@
 leSize()));%0A
+      %7D%0A
       if (a.
@@ -2526,24 +2526,26 @@
 leOddColor))
+ %7B
 %0A        bui
@@ -2652,24 +2652,32 @@
 dColor()));%0A
+      %7D%0A
       if (a.
@@ -2726,16 +2726,18 @@
 nColor))
+ %7B
 %0A       
@@ -2851,24 +2851,32 @@
 nColor()));%0A
+      %7D%0A
     %7D finall
@@ -2915,275 +2915,107 @@
 %0A%0A  
-private void draw() %7B%0A    getViewTreeObserver()%0A        .addOnGlobalLayoutListener(%0A            new ViewTreeObserver.OnGlobalLayoutListener() %7B%0A              @Override%0A              public void onGlobalLayout() %7B%0A                if (Build.VERSION.SDK_INT %3C 16
+@Override%0A  protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight
 ) %7B%0A    
     
@@ -3014,211 +3014,65 @@
     
-  
-            getViewTreeObserver().removeGlobalOnLayoutListener(this);%0A                %7D else %7B%0A                  getViewTreeObserver().removeOnGlobalLayoutListener(this);%0A                %7D%0A            
+super.onSizeChanged(width, height, oldWidth, oldHeight);%0A
     
@@ -3117,28 +3117,16 @@
 uild();%0A
-            
     back
@@ -3139,36 +3139,16 @@
 Bitmap =
-%0A                   
  Bitmap.
@@ -3164,72 +3164,21 @@
 map(
-%0A                        getMeasuredWidth(), getMeasuredH
+width, h
 eight
-()
 , Bi
@@ -3201,30 +3201,90 @@
 _8888);%0A    
-      
+if (backgroundBitmap != null && !backgroundBitmap.isRecycled()) %7B%0A
       Canvas
@@ -3333,26 +3333,16 @@
 itmap);%0A
-          
       dr
@@ -3424,26 +3424,16 @@
 ght());%0A
-          
       dr
@@ -3471,35 +3471,9 @@
     
-          %7D%0A            %7D);
+%7D
 %0A  %7D

Refactor onDraw into onSizeChanged in AlphaTileView