resetprop ro.boot.flash.locked 1
resetprop ro.boot.verifiedbootstate green
resetprop ro.secureboot.lockstate locked
resetprop ro.boot.vbmeta.device_state locked
resetprop ro.debuggable 0
resetprop -n persist.logd.size ""
resetprop -n persist.logd.size.crash ""
resetprop -n persist.logd.size.main ""
resetprop -n persist.log.tag ""
[[ " encrypted " == " $(getprop ro.crypto.state encrypted) " ]] && : || resetprop ro.crypto.state encrypted
SDK_Resolve() {
  settings delete global hidden_api_policy
  settings delete global hidden_api_policy_p_apps
  settings delete global hidden_api_policy_pre_p_apps
  settings delete global hidden_api_blacklist_exemptions
  settings delete global hidden_api_blacklist_exe
}
if [ $(settings get global hidden_api_policy) != "null" ]; then
  SDK_Resolve
fi
if [ $(settings get global hidden_api_policy_p_apps) != "null" ]; then
  SDK_Resolve
fi
if [ $(settings get global hidden_api_policy_pre_p_apps) != "null" ]; then
  SDK_Resolve
fi
if [ $(settings get global hidden_api_blacklist_exemptions) != "null" ]; then
  SDK_Resolve
fi
if [ $(settings get global hidden_api_blacklist_exe) != "null" ]; then
  SDK_Resolve
fi
IS_Virtual=0
DetectVirtualMachine() {
  if dmidecode -s system-product-name | grep -q -E "VMware|VirtualBox|KVM|QEMU"; then
    IS_Virtual=1
  fi
  if [[ $(df -h /sdcard /dev | awk 'NR==2 {print $3}' | sed 's/G$//') -le 10 ]]; then
    IS_Virtual=1
  fi
}
current_mode=$(getenforce)
if [ "$current_mode" == "Permissive" ]; then
  if [ ${IS_Virtual} == "0" ]; then
    setenforce 1
  fi
fi
check_reset_prop() {
  local NAME=$1
  local EXPECTED=$2
  local VALUE=$(resetprop $NAME)
  [ -z $VALUE ] || [ $VALUE = $EXPECTED ] || resetprop $NAME $EXPECTED
}
contains_reset_prop() {
  local NAME=$1
  local CONTAINS=$2
  local NEWVAL=$3
  [[ "$(resetprop $NAME)" = *"$CONTAINS"* ]] && resetprop $NAME $NEWVAL
}
resetprop -w sys.boot_completed 0
check_reset_prop "ro.boot.vbmeta.device_state" "locked"
check_reset_prop "ro.boot.verifiedbootstate" "green"
check_reset_prop "ro.boot.flash.locked" "1"
check_reset_prop "ro.boot.veritymode" "enforcing"
check_reset_prop "ro.boot.warranty_bit" "0"
check_reset_prop "ro.warranty_bit" "0"
check_reset_prop "ro.debuggable" "0"
check_reset_prop "ro.force.debuggable" "0"
check_reset_prop "ro.secure" "1"
check_reset_prop "ro.adb.secure" "1"
check_reset_prop "ro.build.type" "user"
check_reset_prop "ro.build.tags" "release-keys"
check_reset_prop "ro.vendor.boot.warranty_bit" "0"
check_reset_prop "ro.vendor.warranty_bit" "0"
check_reset_prop "vendor.boot.vbmeta.device_state" "locked"
check_reset_prop "vendor.boot.verifiedbootstate" "green"
check_reset_prop "sys.oem_unlock_allowed" "0"
# MIUI 相关
check_reset_prop "ro.secureboot.lockstate" "locked"
# Realme 相关
check_reset_prop "ro.boot.realmebootstate" "green"
check_reset_prop "ro.boot.realme.lockstate" "1"
# 当magisk处于恢复模式时
contains_reset_prop "ro.bootmode" "recovery" "unknown"
contains_reset_prop "ro.boot.bootmode" "recovery" "unknown"
contains_reset_prop "vendor.boot.bootmode" "recovery" "unknown"
#搬运代码区结束

# 等待系统启动完成
while [ "$(getprop sys.boot_completed)" != "1" ]; do sleep 2; done
resetprop -n init.svc.adbd stopped
contains_reset_prop "ro.build.tags" "test-keys" "release-keys"
contains_reset_prop "ro.build.type" "userdebug" "user"
sleep 5
while true; do
  [[ -d /data/user/0/android ]] && break || sleep 5
done
resetprop -n init.svc.adbd stopped
if [ -d /data/adb/modules/tricky_store ]; then
  if [ -f $MODPATH/TrickyStoreKeyBox_Task ]; then
    mkdir -p /data/adb/tricky_store
    mv "$MODPATH/keybox.xml" /data/adb/tricky_store
    cp -r "$MODPATH/keybox.xml" /data/adb/
    mv /data/adb/keybox.xml /data/adb/tricky_store
    rm -rf $MODPATH/TrickyStoreKeyBox_Task
  fi
  mkdir -p /data/adb/tricky_store
  rm -f /data/adb/tricky_store/target.txt
  folder_path="/data/user/0/"
  find "$folder_path" -type d -maxdepth 1 -print0 | while IFS= read -r -d $'\0' directory; do
    folder_name=$(basename "$directory")
    if [[ ! " 0 " == " $folder_name " ]]; then
      echo "$folder_name" >>/data/adb/tricky_store/target.txt
    fi
  done
fi
[[ -d /data/user/0/com.sevtinge.cemiuiler ]] && setprop persist.hyperceiler.log.level "" || :
if [ -d /storage/emulated/0/TWRP ]; then
  rm -rf /storage/emulated/0/TWRP
fi
if [ -d /data/adb/modules/zn_magisk_compat/ ]; then
  rm -rf /data/adb/modules/zn_magisk_compat/
fi
rm -rf /data/local/tmp/shizuku/
rm -rf /data/local/tmp/shizuku_starter
rm -f /data/local/tmp/shizuku/
rm -f /data/local/tmp/shizuku_starter
resetprop -n init.svc.adbd stopped
while true; do
  CurrentFocus=$(dumpsys window | grep -E "mCurrentFocus")
  if echo "$CurrentFocus" | grep -q -E "launcher|lawnchair"; then
    break
  else
    sleep 2
  fi
done
sleep 10
FixHashServer() {
  am start-foreground-service -n com.reveny.vbmetafix.service/.FixerService
  HashTextFile="/data/data/com.reveny.vbmetafix.service/cache/boot.hash"
  TimeOut=5
  Counter=0
  while [ $Counter -lt $TimeOut ]; do
    if [ -f "$HashTextFile" ]; then
      hash=$(cat "$HashTextFile")
      resetprop ro.boot.vbmeta.digest ${hash}
      break
    else
      sleep 1
      Counter=$((Counter + 1))
    fi
  done
  pm uninstall com.reveny.vbmetafix.service
  rm -f "${HashTextFile}"
  rm -rf /data/user/0/com.reveny.vbmetafix.service/
}
[[ -d /data/user/0/com.reveny.vbmetafix.service ]] && FixHashServer || sleep 1
GetPathFolderCount() {
  local Path="$1"
  local Count=0
  for s in "$Path"/*; do
    if [[ -d "$s" ]]; then
      local FolderName=$(basename "$s")
      if [[ "$FolderName" != "0" ]]; then
        Count=$((Count + 1))
      fi
    fi
  done
  echo "$Count"
}
if [ -f /data/adb/modules/wjw_hiderootauxiliarymod/TrickyStoreListDTGX_Task ]; then
  AppDataPath="/data/user/0/"
  APP_Ner=$(GetPathFolderCount "$AppDataPath")
  while true; do
    New_APP_Ner=$(GetPathFolderCount "$AppDataPath")
    if [ "$New_APP_Ner" != "$APP_Ner" ]; then
      mkdir -p /data/adb/tricky_store
      rm -f /data/adb/tricky_store/target.txt
      find "$AppDataPath" -type d -maxdepth 1 -print0 | while IFS= read -r -d $'\0' directory; do
        folder_name=$(basename "$directory")
        if [[ ! " 0 " == " $folder_name " ]]; then
          echo "$folder_name" >>/data/adb/tricky_store/target.txt
        fi
      done
      APP_Ner=${New_APP_Ner}
    fi
  done
fi
