package com.algobase.share.compat;

import android.os.Build;
import android.provider.Settings;

import android.graphics.Insets;

import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowInsets;
import android.view.WindowInsetsController;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ContentResolver;
import android.content.res.Resources;
import android.content.pm.PackageManager;
import android.content.pm.ApplicationInfo;


public class Activity extends android.app.Activity { 

 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter)
 { if (Build.VERSION.SDK_INT >= 26)
     return super.registerReceiver(receiver,filter,RECEIVER_EXPORTED);
   else
     return super.registerReceiver(receiver,filter);
 }


/*
 public boolean gestureNavigation() 
 { ContentResolver cres = getContentResolver();
   return Settings.Secure.getInt(cres,"navigation_mode",0) == 2;
 }
*/

/*
public boolean gestureNavigation() 
{
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) return false;

  Window win = getWindow();

  Insets insets;
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
    insets = win.getDecorView().getRootWindowInsets().getInsets(
                                          WindowInsets.Type.systemGestures());
  else 
    insets = win.getDecorView().getRootWindowInsets().getSystemGestureInsets();

  return insets.left > 0 || insets.right > 0;
}
*/


 public void adjustLayout(View layout, int bg_clr)
 {
   Window win = getWindow();

   win.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

   if (Build.VERSION.SDK_INT < 35)
   { // set system bar color
     win.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
     win.setStatusBarColor(bg_clr);
/*
     // BUG introduced in hrv_monitor 1.7 for SDK < 35 
     win.setDecorFitsSystemWindows(false);
*/
     return;
    }

   if (layout == null) return;

   PackageManager pm  = getPackageManager();
   String pkg_name = getPackageName();

   try {
    ApplicationInfo ai = pm.getApplicationInfo(pkg_name,0);
    if (ai.targetSdkVersion < 35) return;
   } catch(Exception ex) {}


   win.setNavigationBarContrastEnforced(false);


   Resources res = getResources();

   int r1 = res.getIdentifier("status_bar_height","dimen","android");
   int statusBarHeight = res.getDimensionPixelSize(r1);

   int r2 = res.getIdentifier("navigation_bar_height","dimen","android");
   int naviBarHeight = res.getDimensionPixelSize(r2);

   layout.setPadding(0,statusBarHeight,0,naviBarHeight);

   layout.setBackgroundColor(bg_clr);


/*
   WindowInsetsController insetController = win.getInsetsController();
   insetController.setSystemBarsAppearance(
                      WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS,
                      WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS);

   insetController.setSystemBarsAppearance(
                      WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
                      WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS);
*/

  }

  public void adjustLayout(int res, int bg_clr) {
    adjustLayout(findViewById(res),bg_clr);
  }

  public void adjustContentView(int bg_clr)
  { View view = ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);
    adjustLayout(view,bg_clr);
   }


}


