/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * 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.google.android.material.internal;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;

import android.content.Context;
import androidx.annotation.RestrictTo;
import androidx.core.view.AccessibilityDelegateCompat;
import androidx.core.view.ViewCompat;
import androidx.core.view.accessibility.AccessibilityEventCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import androidx.appcompat.widget.AppCompatImageButton;
import android.util.AttributeSet;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Checkable;

/** @hide */
@RestrictTo(LIBRARY_GROUP)
public class CheckableImageButton extends AppCompatImageButton implements Checkable {

  private static final int[] DRAWABLE_STATE_CHECKED = new int[] {android.R.attr.state_checked};

  private boolean checked;

  public CheckableImageButton(Context context) {
    this(context, null);
  }

  public CheckableImageButton(Context context, AttributeSet attrs) {
    this(context, attrs, androidx.appcompat.R.attr.imageButtonStyle);
  }

  public CheckableImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    ViewCompat.setAccessibilityDelegate(
        this,
        new AccessibilityDelegateCompat() {
          @Override
          public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
            super.onInitializeAccessibilityEvent(host, event);
            event.setChecked(isChecked());
          }

          @Override
          public void onInitializeAccessibilityNodeInfo(
              View host, AccessibilityNodeInfoCompat info) {
            super.onInitializeAccessibilityNodeInfo(host, info);
            info.setCheckable(true);
            info.setChecked(isChecked());
          }
        });
  }

  @Override
  public void setChecked(boolean checked) {
    if (this.checked != checked) {
      this.checked = checked;
      refreshDrawableState();
      sendAccessibilityEvent(AccessibilityEventCompat.TYPE_WINDOW_CONTENT_CHANGED);
    }
  }

  @Override
  public boolean isChecked() {
    return checked;
  }

  @Override
  public void toggle() {
    setChecked(!checked);
  }

  @Override
  public int[] onCreateDrawableState(int extraSpace) {
    if (checked) {
      return mergeDrawableStates(
          super.onCreateDrawableState(extraSpace + DRAWABLE_STATE_CHECKED.length),
          DRAWABLE_STATE_CHECKED);
    } else {
      return super.onCreateDrawableState(extraSpace);
    }
  }
}

@@ -774,39 +774,178 @@
  android
-x.annotation.RestrictTo
+.os.Parcel;%0Aimport android.os.Parcelable;%0Aimport androidx.annotation.NonNull;%0Aimport androidx.annotation.RestrictTo;%0Aimport androidx.customview.view.AbsSavedState
 ;%0Aimport
@@ -3310,11 +3310,1633 @@
    %7D%0A  %7D
+%0A%0A  @Override%0A  protected Parcelable onSaveInstanceState() %7B%0A    Parcelable superState = super.onSaveInstanceState();%0A    SavedState savedState = new SavedState(superState);%0A    savedState.checked = checked;%0A    return savedState;%0A  %7D%0A%0A  @Override%0A  protected void onRestoreInstanceState(Parcelable state) %7B%0A    if (!(state instanceof SavedState)) %7B%0A      super.onRestoreInstanceState(state);%0A      return;%0A    %7D%0A    SavedState savedState = (SavedState) state;%0A    super.onRestoreInstanceState(savedState.getSuperState());%0A    setChecked(savedState.checked);%0A  %7D%0A%0A  static class SavedState extends AbsSavedState %7B%0A%0A    boolean checked;%0A%0A    public SavedState(Parcelable superState) %7B%0A      super(superState);%0A    %7D%0A%0A    public SavedState(Parcel source, ClassLoader loader) %7B%0A      super(source, loader);%0A      readFromParcel(source);%0A    %7D%0A%0A    @Override%0A    public void writeToParcel(@NonNull Parcel out, int flags) %7B%0A      super.writeToParcel(out, flags);%0A      out.writeInt(checked ? 1 : 0);%0A    %7D%0A%0A    private void readFromParcel(Parcel in) %7B%0A      checked = in.readInt() == 1;%0A    %7D%0A%0A    public static final Creator%3CSavedState%3E CREATOR =%0A        new ClassLoaderCreator%3CSavedState%3E() %7B%0A          @Override%0A          public SavedState createFromParcel(Parcel in, ClassLoader loader) %7B%0A            return new SavedState(in, loader);%0A          %7D%0A%0A          @Override%0A          public SavedState createFromParcel(Parcel in) %7B%0A            return new SavedState(in, null);%0A          %7D%0A%0A          @Override%0A          public SavedState%5B%5D newArray(int size) %7B%0A            return new SavedState%5Bsize%5D;%0A          %7D%0A        %7D;%0A  %7D
 %0A%7D%0A

Update CheckableImageButton to save instance state