导航菜单

页面标题

页面副标题

Housing v14.7.8 - SubscribeService.java 源代码

正在查看: Housing v14.7.8 应用的 SubscribeService.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.quickblox.messages.services;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;
import androidx.core.app.j;
import com.quickblox.auth.session.QBSession;
import com.quickblox.auth.session.QBSessionManager;
import com.quickblox.auth.session.QBSessionParameters;
import com.quickblox.auth.session.QBSettings;
import com.quickblox.chat.Consts;
import com.quickblox.core.SubscribePushStrategy;
import com.quickblox.core.exception.QBResponseException;
import com.quickblox.core.exception.QBRuntimeException;
import com.quickblox.core.helper.Lo;
import com.quickblox.core.helper.Utils;
import com.quickblox.messages.QBPushNotifications;
import com.quickblox.messages.model.QBEnvironment;
import com.quickblox.messages.model.QBNotificationChannel;
import com.quickblox.messages.model.QBSubscription;
import com.quickblox.messages.services.a.d;
import com.quickblox.messages.services.a.e;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;

public class SubscribeService extends j {
    private static final String a = "SubscribeService: ";
    private a b;
    private String c;
    private QBEnvironment d;
    private boolean e;

    public enum a {
        GCM,
        FCM
    }

    private e a(a aVar) {
        return (aVar == a.GCM ? new d(this) : new com.quickblox.messages.services.a.b()).a();
    }

    private void b() {
        if (c()) {
            Lo.g(a + "subscribed already");
            return;
        }
        if (a()) {
            d();
        } else {
            c("extraSubscribeTask");
        }
    }

    private void c(String str) {
        Lo.g(a + "startSchedulerTask");
        SubscribeTaskWorker.a(this, str);
    }

    private void d() {
        if (b((Context) this)) {
            a(this.b, this.c, this.d);
            return;
        }
        Lo.g(a + "Your meta-data are not set, auto push subscribe unable");
        QBPushManager.getInstance().notifySubscriptionError(new QBRuntimeException("Your meta-data are not set, auto push subscribe unable"), -1);
    }

    private void e() {
        if (!c()) {
            Lo.g(a + "you are not subscribed");
            QBPushManager.getInstance().notifySubscriptionDeleted(false);
            return;
        }
        String str = (String) b.a((Context) this).b(Consts.CHAT_SENDER_ID, "");
        a valueOf = a.valueOf((String) b.a((Context) this).b("registration_type_cm", ""));
        int intValue = ((Integer) b.a((Context) this).b("subscription_id", -1)).intValue();
        StringBuilder sb = new StringBuilder();
        a.D(sb, a, "unsubscribe with senderId=", str, ", typeCM=");
        sb.append(valueOf);
        Lo.g(sb.toString());
        j();
        a(intValue);
    }

    private Integer f() {
        Integer g = g();
        if (a(g)) {
            return g;
        }
        Integer h = h();
        if (a(h)) {
            return h;
        }
        throw new com.quickblox.messages.b.b("the user session doesn't exist");
    }

    private Integer g() {
        QBSession activeSession = QBSessionManager.getInstance().getActiveSession();
        if (activeSession != null) {
            return activeSession.getUserId();
        }
        throw new com.quickblox.messages.b.b("the user session doesn't exist");
    }

    private Integer h() {
        QBSessionParameters sessionParameters = QBSessionManager.getInstance().getSessionParameters();
        if (sessionParameters != null) {
            return sessionParameters.getUserId();
        }
        throw new com.quickblox.messages.b.b("the user session doesn't exist");
    }

    private void i() {
        if (TextUtils.isEmpty(QBSettings.getInstance().getApplicationId())) {
            throw new com.quickblox.messages.b.a("applicationId is null");
        }
        if (TextUtils.isEmpty(QBSettings.getInstance().getAuthorizationKey())) {
            throw new com.quickblox.messages.b.a("authorizationKey is null");
        }
        if (TextUtils.isEmpty(QBSettings.getInstance().getAuthorizationSecret())) {
            throw new com.quickblox.messages.b.a("authorizationSecret is null");
        }
    }

    private void j() {
        b.a((Context) this).a("registration_id");
        b.a((Context) this).a(Consts.CHAT_SENDER_ID);
        b.a((Context) this).a("registration_type_cm");
        b.a((Context) this).a("subscription_id");
    }

    public static void subscribeToPushes(Context context, boolean z) {
        if (!QBSessionManager.getInstance().isActiveUserSession()) {
            Lo.g("No active user session");
            return;
        }
        if (QBSettings.getInstance().getSubscribePushStrategy() == SubscribePushStrategy.NEVER) {
            Lo.g(a + "Subscribe disabled");
            return;
        }
        if (z) {
            a(context);
        }
        Intent intent = new Intent(context, (Class<?>) SubscribeService.class);
        intent.putExtra("extraSubscribe", true);
        j.enqueueWork(context, SubscribeService.class, 1500, intent);
    }

    public static void unSubscribeFromPushes(Context context) {
        Intent intent = new Intent(context, (Class<?>) SubscribeService.class);
        intent.putExtra("extraSubscribe", false);
        j.enqueueWork(context, SubscribeService.class, 1500, intent);
    }

    public void onCreate() {
        super.onCreate();
        Lo.g(a + "SubscribeService created");
    }

    public void onDestroy() {
        Lo.g(a + "Service onDestroy");
        super.onDestroy();
    }

    public void onHandleWork(@NonNull Intent intent) {
        a(intent);
        StringBuilder sb = new StringBuilder();
        sb.append(a);
        sb.append("onHandleIntent start: ");
        sb.append(this.e ? "register to " : "unregister from ");
        sb.append("pushes");
        Lo.g(sb.toString());
        if (this.e) {
            b();
        } else {
            e();
        }
    }

    public void onTaskRemoved(Intent intent) {
        Lo.g(a + "Service onTaskRemoved");
        super/*android.app.Service*/.onTaskRemoved(intent);
    }

    private boolean b(Context context) {
        try {
            Bundle bundle = getPackageManager().getApplicationInfo(context.getPackageName(), 128).metaData;
            String string = bundle.getString("com.quickblox.messages.TYPE");
            String string2 = bundle.getString("com.quickblox.messages.SENDER_ID");
            String string3 = bundle.getString("com.quickblox.messages.QB_ENVIRONMENT");
            if (!a(string, string2, string3)) {
                return false;
            }
            this.b = a.valueOf(string);
            this.c = string2;
            this.d = QBEnvironment.valueOf(string3);
            Lo.g(a + " get settings from meta-data: typeCM=" + this.b + ", senderId=" + this.c + ", qbEnvironment=" + this.d);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            Lo.g(a + "Failed to load meta-data, NameNotFound: " + e.getMessage());
            return false;
        }
    }

    private boolean c() {
        String str;
        String str2 = (String) b.a((Context) this).b("registration_id", "");
        Integer num = (Integer) b.a((Context) this).b("subscription_id", -1);
        int intValue = num.intValue();
        if (TextUtils.isEmpty(str2) || intValue == -1 || !b((Context) this)) {
            return false;
        }
        try {
            str = a(this.b).a(this.c);
        } catch (IOException unused) {
            Lo.g(a + "Error to get cloud message token");
            str = null;
        }
        if (!str2.equals(str)) {
            return false;
        }
        try {
            i();
            ArrayList<QBSubscription> perform = QBPushNotifications.getSubscriptions().perform();
            String str3 = f() + "_" + Utils.generateDeviceId();
            Iterator<QBSubscription> it = perform.iterator();
            while (it.hasNext()) {
                QBSubscription next = it.next();
                if (next.getId().equals(num) && str3.equals(next.getDevice().getId())) {
                    return true;
                }
            }
            return false;
        } catch (QBResponseException | com.quickblox.messages.b.a | com.quickblox.messages.b.b e) {
            Lo.g(a + "Error to get subscriptions: " + e.getMessage());
            return false;
        }
    }

    public void a(int i) {
        try {
            i();
            if (i == -1) {
                QBPushManager.getInstance().notifySubscriptionDeleted(false);
                Lo.g(a + "No subscription for this device");
                return;
            }
            QBPushNotifications.deleteSubscription(i).perform();
            Lo.g(a + "Subscription successfully deleted from QB");
            QBPushManager.getInstance().notifySubscriptionDeleted(true);
        } catch (QBResponseException e) {
            e = e;
            Lo.g(a + "Unable delete subscription: " + e.getMessage());
            QBPushManager.getInstance().notifySubscriptionDeleted(false);
        } catch (com.quickblox.messages.b.a e2) {
            e = e2;
            Lo.g(a + "Unable delete subscription: " + e.getMessage());
            QBPushManager.getInstance().notifySubscriptionDeleted(false);
        }
    }

    private static void a(Context context) {
        b.a(context).a("registration_id");
    }

    private boolean b(String str) {
        Locale locale = Locale.ENGLISH;
        return TextUtils.equals(str.toLowerCase(locale), QBEnvironment.DEVELOPMENT.toString()) || TextUtils.equals(str.toLowerCase(locale), QBEnvironment.PRODUCTION.toString());
    }

    @RestrictTo({RestrictTo.a.LIBRARY})
    public static void a(Context context, boolean z) {
        StringBuilder sb = new StringBuilder();
        String str = a;
        sb.append(str);
        sb.append("subscribeToPushesAutomatic");
        Lo.g(sb.toString());
        if (QBSettings.getInstance().getSubscribePushStrategy() == SubscribePushStrategy.ALWAYS) {
            subscribeToPushes(context, z);
            return;
        }
        Lo.g(str + "AutoSubscribe disabled");
    }

    private void a(Intent intent) {
        if (intent == null || intent.getExtras() == null) {
            return;
        }
        this.e = ((Boolean) intent.getSerializableExtra("extraSubscribe")).booleanValue();
    }

    private void a(a aVar, String str, QBEnvironment qBEnvironment) {
        String str2;
        try {
            str2 = a(aVar).a(str);
        } catch (IOException e) {
            QBPushManager.getInstance().notifySubscriptionError(e, -1);
            Lo.g(a + "getCloudMessageToken error: " + e.getMessage());
            if (a(e)) {
                c("extraSubscribeTask");
            }
            str2 = null;
        }
        if (TextUtils.isEmpty(str2)) {
            Lo.g(a + "Device wasn't registered in " + aVar + " because token = " + str2);
            return;
        }
        Lo.g(a + "Device registered in " + aVar + ", regId=" + str2);
        a(str2, qBEnvironment);
    }

    private void a(String str, QBEnvironment qBEnvironment) {
        int i;
        String str2;
        try {
            String str3 = f() + "_" + Utils.generateDeviceId();
            QBSubscription qBSubscription = new QBSubscription();
            qBSubscription.setNotificationChannel(QBNotificationChannel.GCM);
            qBSubscription.setDeviceUdid(str3);
            qBSubscription.setRegistrationID(str);
            qBSubscription.setEnvironment(qBEnvironment);
            try {
                i();
                Iterator<QBSubscription> it = QBPushNotifications.createSubscription(qBSubscription).perform().iterator();
                while (true) {
                    if (!it.hasNext()) {
                        i = -1;
                        break;
                    }
                    QBSubscription next = it.next();
                    if (next.getDevice().getId().equals(str3)) {
                        i = next.getId().intValue();
                        break;
                    }
                }
                if (i != -1) {
                    a(str, this.c, this.b, i);
                    QBPushManager.getInstance().notifySubscriptionCreated();
                    str2 = a + "Successfully subscribed for QB push messages";
                } else {
                    str2 = a + "Not subscribed for QB push messages, subscription id = " + i;
                }
                Lo.g(str2);
            } catch (QBResponseException e) {
                Lo.g(a + "Not subscribed for QB push messages: " + e.getMessage());
                QBPushManager.getInstance().notifySubscriptionError(e, -1);
                c("extraSubscribeTask");
            } catch (com.quickblox.messages.b.a e2) {
                Lo.g(a + "Not subscribed for QB push messages: " + e2.getMessage());
                QBPushManager.getInstance().notifySubscriptionError(e2, -1);
            }
        } catch (com.quickblox.messages.b.b e3) {
            Lo.g(a + "Not subscribed for QB push messages: " + e3.getMessage());
            QBPushManager.getInstance().notifySubscriptionError(e3, -1);
        }
    }

    private void a(String str, String str2, a aVar, int i) {
        b.a((Context) this).a("registration_id", str);
        b.a((Context) this).a(Consts.CHAT_SENDER_ID, str2);
        b.a((Context) this).a("registration_type_cm", aVar);
        b.a((Context) this).a("subscription_id", Integer.valueOf(i));
    }

    private boolean a() {
        if (com.quickblox.messages.services.a.a(this)) {
            return true;
        }
        int b = com.quickblox.messages.services.a.b(this);
        Lo.g(a + "Google Play services exception");
        QBPushManager.getInstance().notifySubscriptionError(new QBResponseException("Google Play services exception"), b);
        return false;
    }

    private boolean a(IOException iOException) {
        return iOException.getMessage() == null || !iOException.getMessage().contains("INVALID_SENDER");
    }

    private boolean a(Integer num) {
        return num != null && num.intValue() > 0;
    }

    private boolean a(String str) {
        return TextUtils.equals(str, a.GCM.toString()) || TextUtils.equals(str, a.FCM.toString());
    }

    private boolean a(String str, String str2, String str3) {
        return (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2) || TextUtils.isEmpty(str3) || !a(str) || !b(str3)) ? false : true;
    }
}