hf.java 源代码


package com.inmobi.media;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.PowerManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import org.json.JSONObject;

public class hf {

    private static final String f15698a = "hf";

    @SuppressLint({"MissingPermission"})
    public static boolean a() {
        Context c6 = gy.c();
        if (c6 == null) {
            return false;
        }
        try {
            NetworkInfo activeNetworkInfo = ((ConnectivityManager) c6.getSystemService("connectivity")).getActiveNetworkInfo();
            if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
                if (!b()) {
                    return true;
                }
            }
        } catch (Exception unused) {
        }
        return false;
    }

    private static boolean b() {
        boolean isDeviceIdleMode;
        Context c6 = gy.c();
        if (c6 == null) {
            return false;
        }
        try {
            PowerManager powerManager = (PowerManager) c6.getSystemService("power");
            if (Build.VERSION.SDK_INT > 22 && powerManager != null) {
                isDeviceIdleMode = powerManager.isDeviceIdleMode();
                return isDeviceIdleMode;
            }
            return false;
        } catch (Exception unused) {
            return false;
        }
    }

    public static String a(Map<String, String> map, String str) {
        StringBuilder sb = new StringBuilder();
        if (map != null) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                if (sb.length() > 0) {
                    sb.append(str);
                }
                sb.append(String.format(Locale.US, "%s=%s", a(entry.getKey()), a(entry.getValue())));
            }
        }
        return sb.toString();
    }

    private static String a(String str) {
        try {
            return URLEncoder.encode(str, "UTF-8");
        } catch (UnsupportedEncodingException unused) {
            return "";
        }
    }

    public static void a(Map<String, String> map) {
        if (map != null) {
            HashMap hashMap = new HashMap();
            for (Map.Entry<String, String> entry : map.entrySet()) {
                if (entry.getValue() != null && entry.getValue().trim().length() != 0 && entry.getKey() != null && entry.getKey().trim().length() != 0) {
                    hashMap.put(entry.getKey().trim(), entry.getValue().trim());
                }
            }
            map.clear();
            map.putAll(hashMap);
        }
    }

    @NonNull
    public static HashMap<String, String> a(@Nullable JSONObject jSONObject) {
        HashMap<String, String> hashMap = new HashMap<>();
        if (jSONObject != null) {
            try {
                Iterator<String> keys = jSONObject.keys();
                while (keys.hasNext()) {
                    String next = keys.next();
                    hashMap.put(next, jSONObject.getString(next));
                }
            } catch (Exception unused) {
            }
        }
        return hashMap;
    }

    public static String a(String str, Map<String, String> map) {
        if (map != null && map.size() > 0) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                str = str.replace(entry.getKey(), entry.getValue());
            }
        }
        return str;
    }

    public static byte[] a(@NonNull byte[] bArr) {
        GZIPInputStream gZIPInputStream;
        Throwable th;
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr);
        byte[] bArr2 = null;
        try {
            gZIPInputStream = new GZIPInputStream(byteArrayInputStream);
            try {
                try {
                    bArr2 = a((InputStream) gZIPInputStream);
                } catch (IOException e6) {
                    e = e6;
                    he.a((byte) 2, f15698a, "Failed to decompress response", e);
                    a((Closeable) byteArrayInputStream);
                    a((Closeable) gZIPInputStream);
                    return bArr2;
                }
            } catch (Throwable th2) {
                th = th2;
                a((Closeable) byteArrayInputStream);
                a((Closeable) gZIPInputStream);
                throw th;
            }
        } catch (IOException e7) {
            e = e7;
            gZIPInputStream = null;
        } catch (Throwable th3) {
            gZIPInputStream = null;
            th = th3;
            a((Closeable) byteArrayInputStream);
            a((Closeable) gZIPInputStream);
            throw th;
        }
        a((Closeable) byteArrayInputStream);
        a((Closeable) gZIPInputStream);
        return bArr2;
    }

    public static byte[] a(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bArr = new byte[4096];
        while (true) {
            try {
                int read = inputStream.read(bArr);
                if (-1 != read) {
                    byteArrayOutputStream.write(bArr, 0, read);
                } else {
                    return byteArrayOutputStream.toByteArray();
                }
            } finally {
                a(byteArrayOutputStream);
            }
        }
    }

    public static void a(HttpURLConnection httpURLConnection) {
        try {
            a((Closeable) httpURLConnection.getInputStream());
        } catch (IOException unused) {
        }
    }

    public static void a(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException unused) {
            }
        }
    }
}