智慧城市 v1.2.1版本的 MD5 值为:d4aeacb5153a2710d0b6882be5dd2b15

以下内容为反编译后的 PushRequestManager.java 源代码,内容仅作参考


package io.rong.push.pushconfig;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.text.TextUtils;
import android.util.ArrayMap;
import androidx.core.app.NotificationCompat;
import com.bumptech.glide.load.Key;
import io.rong.common.fwlog.FwLog;
import io.rong.imlib.common.DeviceUtils;
import io.rong.imlib.common.EncryptUtil;
import io.rong.imlib.common.NetUtils;
import io.rong.imlib.common.RongLibConst;
import io.rong.imlib.common.SignatureUtils;
import io.rong.push.PushErrorCode;
import io.rong.push.PushType;
import io.rong.push.common.PushCacheHelper;
import io.rong.push.common.PushConst;
import io.rong.push.common.RLog;
import io.rong.push.core.PushNaviClient;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import org.json.JSONObject;
public class PushRequestManager {
    private static final String HTTP = "http";
    private static final String TAG = "PushRequestClient";
    private static final String UP_TOKEN_SERVER_PATH = "setandroidpushtoken.json";
    private Context mContext;
    private PushConfig mPushConfig;
    private PushNaviClient pushNaviClient;
    private Handler workHandler;
    private ConcurrentHashMap<String, String> cacheTokenMap = new ConcurrentHashMap<>();
    private CopyOnWriteArrayList<Runnable> cacheRunnableList = new CopyOnWriteArrayList<>();

    public interface IGetServerInfoCallback {
        void onError(PushErrorCode pushErrorCode);

        void onSuccess();
    }

    public interface ISetPushTokenResultCallback {
        void onError(PushErrorCode pushErrorCode, String str);

        void onFail(PushType pushType, PushErrorCode pushErrorCode);

        void onSuccess(PushType pushType, String str);
    }

    public PushRequestManager(Context context, PushConfig pushConfig) {
        this.mContext = context;
        this.mPushConfig = pushConfig;
        HandlerThread handlerThread = new HandlerThread("Third_Push");
        handlerThread.start();
        this.workHandler = new Handler(handlerThread.getLooper());
        if (this.pushNaviClient == null) {
            PushNaviClient pushNaviClient = new PushNaviClient(context);
            this.pushNaviClient = pushNaviClient;
            pushNaviClient.setPushNaviUrl(pushConfig.getPushDomain());
        }
    }

    public void reportToken(final String str, final String str2, final ISetPushTokenResultCallback iSetPushTokenResultCallback, final long j) {
        this.workHandler.post(new Runnable() {
            @Override
            public void run() {
                if (PushCacheHelper.getInstance().isCacheValid(PushRequestManager.this.mContext, PushRequestManager.this.mPushConfig.getAppKey())) {
                    PushRequestManager.this.prepareTokenReport(str, str2, iSetPushTokenResultCallback, j);
                } else {
                    PushRequestManager.this.getPushServerInfo(new IGetServerInfoCallback() {
                        @Override
                        public void onSuccess() {
                            RLog.i(PushRequestManager.TAG, "get token server info success !");
                            PushRequestManager.this.prepareTokenReport(str, str2, iSetPushTokenResultCallback, j);
                        }

                        @Override
                        public void onError(PushErrorCode pushErrorCode) {
                            RLog.e(PushRequestManager.TAG, "error when config push. Will reConfig when network changed!");
                        }
                    }, j);
                }
            }
        });
    }

    public void getPushServerInfo(final IGetServerInfoCallback iGetServerInfoCallback, final long j) {
        PushCacheHelper.getInstance().clearPushConfigInfo(this.mContext);
        this.workHandler.post(new Runnable() {
            @Override
            public void run() {
                PushRequestManager.this.pushNaviClient.getPushServerInfo(PushRequestManager.this.mContext, PushRequestManager.this.mPushConfig.getAppKey(), false, new PushNaviObserver() {
                    @Override
                    public void onSuccess(ArrayList<String> arrayList) {
                        FwLog.info(FwLog.LogTag.L_PUSH_CONFIG_GET_NAVI_R, FwLog.param("id", Long.valueOf(j)).add("code", 0));
                        iGetServerInfoCallback.onSuccess();
                    }

                    @Override
                    public void onError(PushErrorCode pushErrorCode, String str) {
                        FwLog.info(FwLog.LogTag.L_PUSH_CONFIG_GET_NAVI_R, FwLog.param("id", Long.valueOf(j)).add("code", Integer.valueOf(pushErrorCode.getCode())).add("info", str));
                        iGetServerInfoCallback.onError(pushErrorCode);
                    }
                }, j);
            }
        });
    }

    public void prepareTokenReport(String str, String str2, final ISetPushTokenResultCallback iSetPushTokenResultCallback, final long j) {
        if (PushType.RONG.getName().equals(str2)) {
            this.cacheTokenMap.put(str2, "");
        } else {
            this.cacheTokenMap.put(str2, str);
        }
        String registerType = PushCacheHelper.getInstance().getRegisterType(this.mContext);
        if (TextUtils.isEmpty(registerType)) {
            return;
        }
        final String[] split = registerType.split("\\|");
        Iterator<Runnable> it = this.cacheRunnableList.iterator();
        while (it.hasNext()) {
            this.workHandler.removeCallbacks(it.next());
        }
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                PushRequestManager pushRequestManager = PushRequestManager.this;
                pushRequestManager.reportToken(pushRequestManager.mContext, split, iSetPushTokenResultCallback, j);
            }
        };
        if (split.length > this.cacheTokenMap.size()) {
            this.workHandler.postDelayed(runnable, 3000L);
            this.cacheRunnableList.add(runnable);
            return;
        }
        this.workHandler.post(runnable);
    }

    public void reportToken(Context context, String[] strArr, final ISetPushTokenResultCallback iSetPushTokenResultCallback, long j) {
        String str = "";
        String str2 = "";
        for (String str3 : strArr) {
            if (str3.equals(PushType.GOOGLE_FCM.getName()) || str3.equals(PushType.GOOGLE_GCM.getName())) {
                str2 = this.cacheTokenMap.get(str3);
            } else {
                str = this.cacheTokenMap.get(str3);
            }
        }
        setAndroidPushToken(context, str, str2, this.mPushConfig, new ISetPushTokenResultCallback() {
            @Override
            public void onSuccess(PushType pushType, String str4) {
                PushRequestManager.this.cacheTokenMap.clear();
                PushRequestManager.this.cacheRunnableList.clear();
                ISetPushTokenResultCallback iSetPushTokenResultCallback2 = iSetPushTokenResultCallback;
                if (iSetPushTokenResultCallback2 != null) {
                    iSetPushTokenResultCallback2.onSuccess(pushType, str4);
                }
            }

            @Override
            public void onError(PushErrorCode pushErrorCode, String str4) {
                RLog.e(PushRequestManager.TAG, "setToken.onFailure.");
                ISetPushTokenResultCallback iSetPushTokenResultCallback2 = iSetPushTokenResultCallback;
                if (iSetPushTokenResultCallback2 != null) {
                    iSetPushTokenResultCallback2.onError(pushErrorCode, str4);
                }
            }

            @Override
            public void onFail(PushType pushType, PushErrorCode pushErrorCode) {
                RLog.e(PushRequestManager.TAG, "setToken.onFailure.");
                ISetPushTokenResultCallback iSetPushTokenResultCallback2 = iSetPushTokenResultCallback;
                if (iSetPushTokenResultCallback2 != null) {
                    iSetPushTokenResultCallback2.onFail(pushType, pushErrorCode);
                }
            }
        }, j);
    }

    private void setAndroidPushToken(Context context, String str, String str2, PushConfig pushConfig, ISetPushTokenResultCallback iSetPushTokenResultCallback, long j) {
        Throwable th;
        IOException iOException;
        String tokenServer = PushCacheHelper.getInstance().getTokenServer(context);
        if (TextUtils.isEmpty(tokenServer)) {
            RLog.i(TAG, "tokenServer is null");
            return;
        }
        String registerType = PushCacheHelper.getInstance().getRegisterType(context);
        FwLog.info(FwLog.LogTag.L_PUSH_CONFIG_REPORT_TOKEN_T, FwLog.param("id", Long.valueOf(j)).add(PushConst.PUSH_TYPE, registerType).add("tokenServer", tokenServer).add(RongLibConst.KEY_TOKEN, str).add("fcmToken", str2));
        ArrayMap<String, String> buildRequestParams = buildRequestParams(context, str, str2, pushConfig.getAppKey(), registerType);
        BufferedInputStream bufferedInputStream = null;
        String formatServerAddress = formatServerAddress(context, tokenServer, UP_TOKEN_SERVER_PATH);
        HttpURLConnection createConnection = createConnection(context, buildRequestParams, formatServerAddress, pushConfig, iSetPushTokenResultCallback);
        try {
            if (createConnection == null) {
                RLog.i(TAG, "HttpURLConnection is null");
                return;
            }
            try {
                RLog.i(TAG, "setAndroidPushToken url : " + formatServerAddress);
                createConnection.connect();
                int responseCode = createConnection.getResponseCode();
                RLog.d(TAG, "setAndroidPushToken responseCode: " + responseCode);
                if (responseCode == 200) {
                    BufferedInputStream bufferedInputStream2 = new BufferedInputStream(createConnection.getInputStream());
                    try {
                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(256);
                        while (true) {
                            int read = bufferedInputStream2.read();
                            if (read == -1) {
                                break;
                            }
                            byteArrayOutputStream.write(read);
                        }
                        JSONObject jSONObject = new JSONObject(byteArrayOutputStream.toString(Key.STRING_CHARSET_NAME));
                        int optInt = jSONObject.optInt("code");
                        String optString = jSONObject.optString(NotificationCompat.CATEGORY_MESSAGE);
                        PushType type = PushType.getType(jSONObject.optString(PushConst.PUSH_TYPE));
                        RLog.e(TAG, "setAndroidPushToken response body:code=" + optInt + " ;msg=" + optString + " ;pushType=" + type);
                        if (optInt == 200) {
                            iSetPushTokenResultCallback.onSuccess(type, getTokenToFinalPushType(buildRequestParams, type));
                        } else if (optInt == 10000) {
                            iSetPushTokenResultCallback.onFail(type, PushErrorCode.NOT_REGISTER_IN_ADMIN);
                        } else if (optInt == 10001) {
                            iSetPushTokenResultCallback.onFail(type, PushErrorCode.PARAMETER_ERROR);
                        } else {
                            iSetPushTokenResultCallback.onFail(type, PushErrorCode.UNKNOWN);
                        }
                        bufferedInputStream = bufferedInputStream2;
                    } catch (Exception e) {
                        e = e;
                        bufferedInputStream = bufferedInputStream2;
                        iSetPushTokenResultCallback.onError(PushErrorCode.CONNECT_EXCEPTION, e.getMessage());
                        RLog.e(TAG, "Exception when set push token.Retry again.");
                        e.printStackTrace();
                        if (bufferedInputStream != null) {
                            try {
                                bufferedInputStream.close();
                            } catch (IOException e2) {
                                iOException = e2;
                                RLog.e(TAG, "Exception when call responseStream.close()!");
                                iOException.printStackTrace();
                                createConnection.disconnect();
                            }
                        }
                        createConnection.disconnect();
                    } catch (Throwable th2) {
                        th = th2;
                        bufferedInputStream = bufferedInputStream2;
                        if (bufferedInputStream != null) {
                            try {
                                bufferedInputStream.close();
                            } catch (IOException e3) {
                                RLog.e(TAG, "Exception when call responseStream.close()!");
                                e3.printStackTrace();
                            }
                        }
                        createConnection.disconnect();
                        throw th;
                    }
                } else {
                    iSetPushTokenResultCallback.onError(PushErrorCode.CONNECT_EXCEPTION, String.valueOf(responseCode));
                }
                if (bufferedInputStream != null) {
                    try {
                        bufferedInputStream.close();
                    } catch (IOException e4) {
                        iOException = e4;
                        RLog.e(TAG, "Exception when call responseStream.close()!");
                        iOException.printStackTrace();
                        createConnection.disconnect();
                    }
                }
            } catch (Exception e5) {
                e = e5;
            }
            createConnection.disconnect();
        } catch (Throwable th3) {
            th = th3;
        }
    }

    private ArrayMap<String, String> buildRequestParams(Context context, String str, String str2, String str3, String str4) {
        ArrayMap<String, String> arrayMap = new ArrayMap<>();
        arrayMap.put("deviceId", DeviceUtils.getDeviceId(context, str3));
        if (TextUtils.isEmpty(str)) {
            str = "";
        }
        arrayMap.put(RongLibConst.KEY_TOKEN, str);
        if (TextUtils.isEmpty(str2)) {
            str2 = "";
        }
        arrayMap.put("fcmToken", str2);
        arrayMap.put("packageName", context.getPackageName().replace("-", "_"));
        arrayMap.put(PushConst.PUSH_TYPE, str4);
        arrayMap.put("osVersion", Build.MANUFACTURER + "_" + Build.MODEL);
        arrayMap.put("sdkVersion", "5.1.3.5");
        arrayMap.put("os", DeviceUtils.getDeviceManufacturer());
        return arrayMap;
    }

    private String formatServerAddress(Context context, String str, String str2) {
        String str3;
        if (str.toLowerCase().startsWith(HTTP)) {
            str3 = "%s/%s";
        } else {
            str3 = NetUtils.isHttpsEnable(context) ? "https://%s/%s" : "http://%s/%s";
        }
        return String.format(str3, str, str2);
    }

    private HttpURLConnection createConnection(Context context, ArrayMap<String, String> arrayMap, String str, PushConfig pushConfig, ISetPushTokenResultCallback iSetPushTokenResultCallback) {
        HttpURLConnection httpURLConnection;
        try {
            httpURLConnection = NetUtils.createURLConnection(str);
        } catch (Exception e) {
            e = e;
            httpURLConnection = null;
        }
        try {
            httpURLConnection.setConnectTimeout(3000);
            httpURLConnection.setReadTimeout(3000);
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("POST");
            Map<String, String> createHeader = createHeader(pushConfig);
            for (Map.Entry<String, String> entry : createHeader.entrySet()) {
                httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue());
            }
            String jSONObject = new JSONObject(arrayMap).toString();
            String encrypt = EncryptUtil.getInstance(context).encrypt(createSecretkey(pushConfig.getAppKey(), createHeader.get("Timestamp")), jSONObject);
            httpURLConnection.setRequestProperty("Content-Length", "" + encrypt.length());
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, Key.STRING_CHARSET_NAME));
            bufferedWriter.write(encrypt);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
        } catch (Exception e2) {
            e = e2;
            RLog.e(TAG, "Exception when set push token.Retry again.");
            iSetPushTokenResultCallback.onError(PushErrorCode.CONNECT_EXCEPTION, e.getMessage());
            e.printStackTrace();
            return httpURLConnection;
        }
        return httpURLConnection;
    }

    private String getTokenToFinalPushType(ArrayMap<String, String> arrayMap, PushType pushType) {
        if (pushType == PushType.RONG) {
            return null;
        }
        if (pushType == PushType.GOOGLE_FCM || pushType == PushType.GOOGLE_GCM) {
            return arrayMap.get("fcmToken");
        }
        return arrayMap.get(RongLibConst.KEY_TOKEN);
    }

    private Map<String, String> createHeader(PushConfig pushConfig) {
        HashMap hashMap = new HashMap(4);
        String num = Integer.toString(new Random().nextInt(10000));
        String l = Long.toString(System.currentTimeMillis());
        String stringBuffer = new StringBuffer(pushConfig.getAppKey()).reverse().toString();
        String hexSHA1 = SignatureUtils.hexSHA1(stringBuffer + num + l);
        hashMap.put("App-Key", pushConfig.getAppKey());
        hashMap.put("Nonce", num);
        hashMap.put("Timestamp", l);
        hashMap.put("Signature", hexSHA1);
        return hashMap;
    }

    private String createSecretkey(String str, String str2) {
        return str.substring(0, 8) + str2.substring(str2.length() - 8);
    }
}