package com.seewo.vcommons.utils;

import android.content.Context;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.WindowManager;
import com.seewo.vcommons.data.PropertiesUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public class WallpaperUtils {
    private static final String NIGHT = "night";
    private static final String TAG = "WallpaperUtils";
    private static final String VERTICAL = "vertical";
    private static final String WALLPAPER_DEFAULT_CONTAIN_NAME = "wallpaper_default";
    private static final String WALLPAPER_ROOT_PATH = "/vendor/etc/wallpaper";
    private static final String WALLPAPER_ROOT_PATH_KEY = "persist.sys.wallpaper.path";

    public static File getWallpaper(Context context, String str) {
        Map<String, File> wallpaperNameMap = getWallpaperNameMap(context, null, true);
        if (wallpaperNameMap.containsKey(str)) {
            return wallpaperNameMap.get(str);
        }
        return null;
    }

    public static List<String> getWallpaperNameList(Context context, String... strArr) {
        ArrayList arrayList = new ArrayList(getWallpaperNameMap(context, strArr, false).keySet());
        Collections.sort(arrayList, new Comparator<String>() { // from class: com.seewo.vcommons.utils.WallpaperUtils.1
            @Override // java.util.Comparator
            public int compare(String str, String str2) {
                return WallpaperUtils.compareWallpaperName(str, str2);
            }
        });
        return arrayList;
    }

    public static List<WallpaperFileInfo> getWallpaperFileInfoList(Context context, String... strArr) {
        Map<String, File> wallpaperNameMap = getWallpaperNameMap(context, strArr, false);
        ArrayList arrayList = new ArrayList();
        Iterator<String> it = wallpaperNameMap.keySet().iterator();
        while (it.hasNext()) {
            File file = wallpaperNameMap.get(it.next());
            if (file != null) {
                arrayList.add(new WallpaperFileInfo(file));
            }
        }
        Collections.sort(arrayList, new Comparator<WallpaperFileInfo>() { // from class: com.seewo.vcommons.utils.WallpaperUtils.2
            @Override // java.util.Comparator
            public int compare(WallpaperFileInfo wallpaperFileInfo, WallpaperFileInfo wallpaperFileInfo2) {
                return WallpaperUtils.compareWallpaperName(wallpaperFileInfo.mWallpaperFileName, wallpaperFileInfo2.mWallpaperFileName);
            }
        });
        return arrayList;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static int compareWallpaperName(String str, String str2) {
        if (str.contains(WALLPAPER_DEFAULT_CONTAIN_NAME) && str2.contains(WALLPAPER_DEFAULT_CONTAIN_NAME)) {
            return str.compareTo(str2);
        }
        if (str.contains(WALLPAPER_DEFAULT_CONTAIN_NAME)) {
            return -1;
        }
        if (str2.contains(WALLPAPER_DEFAULT_CONTAIN_NAME)) {
            return 1;
        }
        return str.compareTo(str2);
    }

    private static Map<String, File> getWallpaperNameMap(Context context, String[] strArr, boolean z) {
        boolean zIsSystemVertical = isSystemVertical(context);
        boolean zIsNight = isNight(context);
        HashMap map = new HashMap();
        setWallpaperFiles(map, getWallpaperList(getWallpaperRootPath(false, false)), strArr, z);
        if (zIsNight) {
            setWallpaperFiles(map, getWallpaperList(getWallpaperRootPath(true, false)), strArr, z);
        }
        if (zIsSystemVertical) {
            setWallpaperFiles(map, getWallpaperList(getWallpaperRootPath(false, true)), strArr, z);
        }
        if (zIsNight && zIsSystemVertical) {
            setWallpaperFiles(map, getWallpaperList(getWallpaperRootPath(true, true)), strArr, z);
        }
        return map;
    }

    private static String getWallpaperRootPath(boolean z, boolean z2) {
        File[] fileArrListFiles;
        String property = PropertiesUtils.getProperty(WALLPAPER_ROOT_PATH_KEY, WALLPAPER_ROOT_PATH);
        if ((z || z2) && (fileArrListFiles = new File(property).listFiles()) != null) {
            for (File file : fileArrListFiles) {
                if (file.isDirectory() && z && z2 && file.getName().contains(NIGHT) && file.getName().contains(VERTICAL)) {
                    return file.getPath();
                }
            }
            for (File file2 : fileArrListFiles) {
                if (file2.isDirectory() && z2 && file2.getName().contains(VERTICAL)) {
                    return file2.getPath();
                }
            }
            for (File file3 : fileArrListFiles) {
                if (file3.isDirectory() && z && file3.getName().contains(NIGHT)) {
                    return file3.getPath();
                }
            }
        }
        return property;
    }

    private static boolean isSystemVertical(Context context) {
        try {
            int i = Settings.System.getInt(context.getContentResolver(), "user_rotation");
            return i == 1 || i == 3;
        } catch (Settings.SettingNotFoundException e) {
            Log.w(TAG, "isSystemVertical: ", e);
            int rotation = ((WindowManager) context.getSystemService("window")).getDefaultDisplay().getRotation();
            return rotation == 1 || rotation == 3;
        }
    }

    private static boolean isNight(Context context) {
        return (context.getResources().getConfiguration().uiMode & 32) != 0;
    }

    private static List<File> getWallpaperList(String str) {
        File[] fileArrListFiles = new File(str).listFiles();
        ArrayList arrayList = new ArrayList();
        if (fileArrListFiles != null) {
            for (File file : fileArrListFiles) {
                if (!file.isDirectory()) {
                    arrayList.add(file);
                }
            }
        }
        return arrayList;
    }

    private static boolean isStringArrayEmpty(String[] strArr) {
        if (strArr == null) {
            return true;
        }
        for (String str : strArr) {
            if (!TextUtils.isEmpty(str)) {
                return false;
            }
        }
        return true;
    }

    private static void setWallpaperFiles(Map<String, File> map, List<File> list, String[] strArr, boolean z) {
        for (File file : list) {
            String str = file.getName().split("\\.")[0];
            if (z) {
                map.put(str, file);
            } else if (!isStringArrayEmpty(strArr)) {
                boolean z2 = false;
                for (String str2 : strArr) {
                    if (str.toLowerCase().endsWith(str2.toLowerCase())) {
                        z2 = true;
                    }
                }
                if (z2) {
                    map.put(str, file);
                }
            } else if (str.equalsIgnoreCase(WALLPAPER_DEFAULT_CONTAIN_NAME) || (str.toLowerCase().startsWith("wallpaper_") && str.length() == "wallpaper_00".length())) {
                map.put(str, file);
            }
        }
    }

    public static class WallpaperFileInfo {
        private final boolean mIsDefaultWallpaper;
        private final File mWallpaperFile;
        private final String mWallpaperFileName;

        private WallpaperFileInfo(File file) {
            String str = file.getName().split("\\.")[0];
            this.mWallpaperFileName = str;
            this.mWallpaperFile = file;
            this.mIsDefaultWallpaper = str.contains(WallpaperUtils.WALLPAPER_DEFAULT_CONTAIN_NAME);
        }

        public String getWallpaperFileName() {
            return this.mWallpaperFileName;
        }

        public File getWallpaperFile() {
            return this.mWallpaperFile;
        }

        public boolean isDefaultWallpaper() {
            return this.mIsDefaultWallpaper;
        }

        public String toString() {
            return "WallpaperFileInfo{" + this.mWallpaperFile + '}';
        }
    }
}
