米游社.apk(点击下载) / RLogConfig.java


package io.rong.common.rlog;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import io.rong.imlib.common.SavePathUtils;
import java.io.File;
import java.io.FileFilter;
import java.util.Comparator;
import java.util.TreeSet;

public class RLogConfig {
    public static final long DEFAULT_MAX_SIZE = 1048576;
    private static final String LOG_DIR = "RLog";
    public static final String LOG_SUFFIX = ".log";
    private static final String R_FILE_MAX_SIZE = "r_file_max_size";
    private static final String R_LOG_LEVEL = "r_log_level";
    private static final String R_START_TIME = "r_start_time";
    private static final String R_UPLOAD_URL = "r_upload_url";
    private static final String R_ZIP_CONFIG = "r_zip_config";
    private static final String R_ZIP_MAX_SIZE = "r_zip_max_size";
    private static final String SP_NAME = "FwLog";
    private static final String TAG = "RLogConfig";
    public static final String ZIP_SUFFIX = ".gz";
    private static SharedPreferences mSp;
    private boolean isDebugMode;
    private boolean isFirstFileMaxSize;
    private boolean isFirstGetLevel;
    private boolean isFirstStartTime;
    private boolean isFirstZipMaxSize;
    private String mAppKey;
    private long mFileMaxSize = 1048576;
    private String mFileName = "r.log";
    private int mLogLevel = 0;
    private String mLogPath;
    private String mSdkVersion;
    private long mStartTime;
    private ZipConfig mZipConfig;
    private long mZipMaxSize = 1048576;
    private String uploadUrl;
    private String userId;

    public static class ZipConfig {
        private long currentSize;
        private TreeSet<File> zipFiles;

        public ZipConfig(long j10, TreeSet<File> treeSet) {
            this.currentSize = j10;
            this.zipFiles = treeSet;
        }

        public void addFile(File file) {
            this.zipFiles.add(file);
        }

        public void clear() {
            TreeSet<File> treeSet = this.zipFiles;
            if (treeSet != null) {
                treeSet.clear();
                this.zipFiles = null;
            }
        }

        public long getCurrentSize() {
            return this.currentSize;
        }

        public TreeSet<File> getZipFiles() {
            return this.zipFiles;
        }

        public void setCurrentSize(long j10) {
            this.currentSize = j10;
        }
    }

    public RLogConfig(Context context, String str, String str2) {
        boolean z10 = true;
        this.isFirstGetLevel = true;
        this.isFirstFileMaxSize = true;
        this.isFirstZipMaxSize = true;
        this.isFirstStartTime = true;
        ApplicationInfo applicationInfo = context.getApplicationInfo();
        this.isDebugMode = (applicationInfo == null || (applicationInfo.flags & 2) == 0) ? false : z10;
        File file = null;
        this.mAppKey = str;
        this.mSdkVersion = str2;
        mSp = context.getSharedPreferences("FwLog", 0);
        if (!SavePathUtils.isSavePathEmpty()) {
            RLog.e(TAG, SavePathUtils.getSavePath());
            this.mLogPath = SavePathUtils.getSavePath() + File.separator + LOG_DIR;
            File file2 = new File(this.mLogPath);
            if (!file2.exists() && !file2.mkdirs()) {
                RLog.e(TAG, "mkdirs failed!");
            }
        } else {
            try {
                file = context.getExternalFilesDir(LOG_DIR);
            } catch (ArrayIndexOutOfBoundsException e10) {
                RLog.e(TAG, TAG, e10);
            }
            if (file != null) {
                this.mLogPath = file.getAbsolutePath();
            } else {
                try {
                    this.mLogPath = context.getFilesDir().getAbsoluteFile() + File.separator + LOG_DIR;
                } catch (Exception e11) {
                    RLog.e(TAG, TAG, e11);
                }
            }
        }
        initZipConfig();
    }

    private ZipConfig refreshZipConfig() {
        File file = new File(this.mLogPath);
        if (!(file.exists() && file.isDirectory())) {
            return null;
        }
        File[] listFiles = file.listFiles(new FileFilter() {
            /* class io.rong.common.rlog.RLogConfig.AnonymousClass1 */

            public boolean accept(File file) {
                if (!file.isFile()) {
                    return false;
                }
                return file.getName().endsWith(RLogConfig.ZIP_SUFFIX);
            }
        });
        TreeSet treeSet = new TreeSet(new Comparator<File>() {
            /* class io.rong.common.rlog.RLogConfig.AnonymousClass2 */

            public int compare(File file, File file2) {
                return file.lastModified() - file2.lastModified() < 0 ? -1 : 1;
            }
        });
        long j10 = 0;
        if (listFiles != null) {
            for (File file2 : listFiles) {
                j10 += file2.length();
                treeSet.add(file2);
            }
        }
        return new ZipConfig(j10, treeSet);
    }

    public void clearZipConfig() {
        ZipConfig zipConfig = this.mZipConfig;
        if (zipConfig != null) {
            zipConfig.clear();
            this.mZipConfig = null;
        }
    }

    public String getAppKey() {
        return this.mAppKey;
    }

    public long getFileMaxSize() {
        SharedPreferences sharedPreferences;
        if (this.isFirstFileMaxSize && (sharedPreferences = mSp) != null) {
            this.mFileMaxSize = sharedPreferences.getLong(R_FILE_MAX_SIZE, 1048576);
            this.isFirstFileMaxSize = false;
        }
        return this.mFileMaxSize;
    }

    public String getFileName() {
        return this.mFileName;
    }

    public String getFilePath() {
        return this.mLogPath + File.separator + this.mFileName;
    }

    public int getLogLevel() {
        SharedPreferences sharedPreferences;
        if (this.isFirstGetLevel && (sharedPreferences = mSp) != null) {
            this.mLogLevel = sharedPreferences.getInt(R_LOG_LEVEL, 0);
            this.isFirstGetLevel = false;
        }
        return this.mLogLevel;
    }

    public String getLogPath() {
        return this.mLogPath;
    }

    public String getSdkVersion() {
        return this.mSdkVersion;
    }

    public long getStartTime() {
        SharedPreferences sharedPreferences;
        if (this.isFirstStartTime && (sharedPreferences = mSp) != null) {
            this.mStartTime = sharedPreferences.getLong(R_START_TIME, System.currentTimeMillis());
            this.isFirstStartTime = false;
        }
        return this.mStartTime;
    }

    public String getUploadUrl() {
        SharedPreferences sharedPreferences;
        if (this.uploadUrl == null && (sharedPreferences = mSp) != null) {
            this.uploadUrl = sharedPreferences.getString(R_UPLOAD_URL, "https://feedback.cn.ronghub.com");
        }
        return this.uploadUrl;
    }

    public String getUserId() {
        return this.userId;
    }

    public ZipConfig getZipConfig() {
        return this.mZipConfig;
    }

    public long getZipMaxSize() {
        SharedPreferences sharedPreferences;
        if (this.isFirstZipMaxSize && (sharedPreferences = mSp) != null) {
            this.mZipMaxSize = sharedPreferences.getLong(R_ZIP_MAX_SIZE, 1048576);
            this.isFirstZipMaxSize = false;
        }
        return this.mZipMaxSize;
    }

    public void initZipConfig() {
        this.mZipConfig = refreshZipConfig();
    }

    public boolean isDebugMode() {
        return this.isDebugMode;
    }

    public void setFileMaxSize(long j10) {
        this.mFileMaxSize = j10;
        SharedPreferences sharedPreferences = mSp;
        if (sharedPreferences != null) {
            sharedPreferences.edit().putLong(R_FILE_MAX_SIZE, j10).apply();
        }
    }

    public void setFileName(String str) {
        this.mFileName = str;
    }

    public void setLogLevel(int i10) {
        this.mLogLevel = i10;
        SharedPreferences sharedPreferences = mSp;
        if (sharedPreferences != null) {
            sharedPreferences.edit().putInt(R_LOG_LEVEL, i10).apply();
        }
    }

    public void setLogPath(String str) {
        this.mLogPath = str;
    }

    public void setStartTime(long j10) {
        this.mStartTime = j10;
        SharedPreferences sharedPreferences = mSp;
        if (sharedPreferences != null) {
            sharedPreferences.edit().putLong(R_START_TIME, j10).apply();
        }
    }

    public void setUploadUrl(String str) {
        this.uploadUrl = str;
        SharedPreferences sharedPreferences = mSp;
        if (sharedPreferences != null) {
            sharedPreferences.edit().putString(R_UPLOAD_URL, str).apply();
        }
    }

    public void setUserId(String str) {
        this.userId = str;
    }

    public void setZipMaxSize(long j10) {
        this.mZipMaxSize = j10;
        SharedPreferences sharedPreferences = mSp;
        if (sharedPreferences != null) {
            sharedPreferences.edit().putLong(R_ZIP_MAX_SIZE, j10).apply();
        }
    }
}