/*
 * Copyright 2019 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 io.material.catalog.windowpreferences;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.view.Window;
import com.google.android.material.internal.EdgeToEdgeUtils;

/** Helper that saves the current window preferences for the Catalog. */
public class WindowPreferencesManager {

  private static final String PREFERENCES_NAME = "window_preferences";
  private static final String KEY_EDGE_TO_EDGE_ENABLED = "edge_to_edge_enabled";

  private final Context context;

  public WindowPreferencesManager(Context context) {
    this.context = context;
  }

  @SuppressWarnings("ApplySharedPref")
  public void toggleEdgeToEdgeEnabled() {
    getSharedPreferences()
        .edit()
        .putBoolean(KEY_EDGE_TO_EDGE_ENABLED, !isEdgeToEdgeEnabled())
        .commit();
  }

  public boolean isEdgeToEdgeEnabled() {
    return getSharedPreferences()
        .getBoolean(KEY_EDGE_TO_EDGE_ENABLED, VERSION.SDK_INT >= VERSION_CODES.Q);
  }

  @SuppressWarnings("RestrictTo")
  public void applyEdgeToEdgePreference(Window window) {
    EdgeToEdgeUtils.applyEdgeToEdge(window, isEdgeToEdgeEnabled());
  }

  private SharedPreferences getSharedPreferences() {
    return context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
  }
}

@@ -731,16 +731,58 @@
 rences;%0A
+import android.content.res.Configuration;%0A
 import a
@@ -806,16 +806,16 @@
 ERSION;%0A
-
 import a
@@ -873,16 +873,143 @@
 Window;%0A
+import android.view.WindowInsets;%0Aimport androidx.core.view.OnApplyWindowInsetsListener;%0Aimport androidx.core.view.ViewCompat;%0A
 import c
@@ -1361,16 +1361,70 @@
 context;
+%0A  private final OnApplyWindowInsetsListener listener;
 %0A%0A  publ
@@ -1498,16 +1498,783 @@
 ontext;%0A
+    this.listener =%0A        (v, insets) -%3E %7B%0A          if (v.getResources().getConfiguration().orientation%0A              != Configuration.ORIENTATION_LANDSCAPE) %7B%0A            return insets;%0A          %7D%0A          if (VERSION.SDK_INT %3E= VERSION_CODES.R) %7B%0A            v.setPadding(%0A                insets.getInsets(WindowInsets.Type.systemBars()).left,%0A                0,%0A                insets.getInsets(WindowInsets.Type.systemBars()).right,%0A                insets.getInsets(WindowInsets.Type.systemBars()).bottom);%0A          %7D else %7B%0A            v.setPadding(%0A                insets.getStableInsetLeft(),%0A                0,%0A                insets.getStableInsetRight(),%0A                insets.getStableInsetBottom());%0A          %7D%0A          return insets;%0A        %7D;%0A
   %7D%0A%0A  @
@@ -2738,24 +2738,24 @@
 w window) %7B%0A
-
     EdgeToEd
@@ -2810,16 +2810,136 @@
 led());%0A
+    ViewCompat.setOnApplyWindowInsetsListener(%0A        window.getDecorView(), isEdgeToEdgeEnabled() ? listener : null);%0A
   %7D%0A%0A  p

Fix navigation bar overlaps ui #2927