package com.ifpdos.daemonserver;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.SystemProperties;
import android.support.v4.app.v;
import android.util.Log;
import com.ifpdos.daemonserver.router.o;
import com.ifpdos.mosquitto.R;

/* JADX INFO: loaded from: classes.dex */
public class DaemonService extends Service {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final Handler f1493b = new Handler(Looper.getMainLooper());

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final BroadcastReceiver f1494c = new a(this);

    class a extends BroadcastReceiver {
        a(DaemonService daemonService) {
        }

        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            if ("com.ifpdos.fake_reset_monitor".equals(intent.getAction()) || "com.ifpdos.soft_reset_router".equals(intent.getAction())) {
                o.b().j();
            } else if ("com.ifpdos.debugmenu.export_xbug".equals(intent.getAction())) {
                com.ifpdos.daemonserver.j.f.d();
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    class b extends Thread {
        private b() {
        }

        @Override // java.lang.Thread, java.lang.Runnable
        public void run() {
            if (com.ifpdos.daemonserver.j.f.w(DaemonService.this)) {
                Handler handler = DaemonService.this.f1493b;
                final DaemonService daemonService = DaemonService.this;
                handler.post(new Runnable() { // from class: com.ifpdos.daemonserver.d
                    @Override // java.lang.Runnable
                    public final void run() {
                        daemonService.f();
                    }
                });
            } else {
                Handler handler2 = DaemonService.this.f1493b;
                final DaemonService daemonService2 = DaemonService.this;
                handler2.postDelayed(new Runnable() { // from class: com.ifpdos.daemonserver.c
                    @Override // java.lang.Runnable
                    public final void run() {
                        daemonService2.e();
                    }
                }, 3000L);
            }
        }

        /* synthetic */ b(DaemonService daemonService, a aVar) {
            this();
        }
    }

    private void d() {
        if (Build.VERSION.SDK_INT >= 26) {
            NotificationChannel notificationChannel = new NotificationChannel("channel_id", "MQTT-Broker", 3);
            notificationChannel.setDescription("Channel Description");
            ((NotificationManager) App.f1492b.getSystemService(NotificationManager.class)).createNotificationChannel(notificationChannel);
        }
        v.b bVar = new v.b(this, "channel_id");
        bVar.e(getString(R.string.str_server_name));
        bVar.d(getString(R.string.str_running));
        bVar.f(R.drawable.icon_setting);
        startForeground(1, bVar.a());
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void e() {
        new b(this, null).start();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void f() {
        if (!com.ifpdos.daemonserver.j.f.l()) {
            com.ifpdos.daemonserver.j.f.z(false, true);
        }
        if (com.ifpdos.daemonserver.j.f.l() || com.ifpdos.daemonserver.j.f.j()) {
            com.ifpdos.daemonserver.j.f.A(this);
        }
        com.ifpdos.daemonserver.j.f.x(this);
        g();
    }

    private void g() {
        e eVar = new e();
        eVar.c(new com.ifpdos.daemonserver.h.e());
        eVar.c(new com.ifpdos.daemonserver.h.d());
        if (com.ifpdos.daemonserver.j.f.l()) {
            return;
        }
        eVar.c(new com.ifpdos.daemonserver.h.b());
        eVar.c(new com.ifpdos.daemonserver.h.f());
    }

    @Override // android.app.Service
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override // android.app.Service
    public void onCreate() {
        super.onCreate();
        Log.d("DaemonService", "onCreate");
        if (!SystemProperties.getBoolean("persist.sys.mqtt.daemon", true)) {
            stopSelf();
            return;
        }
        if (com.ifpdos.daemonserver.j.f.l()) {
            f();
        } else {
            e();
        }
        IntentFilter intentFilter = new IntentFilter("com.ifpdos.soft_reset_router");
        intentFilter.addAction("com.ifpdos.fake_reset_monitor");
        if (Build.VERSION.SDK_INT >= 33) {
            intentFilter.addAction("com.ifpdos.debugmenu.export_xbug");
        }
        registerReceiver(this.f1494c, intentFilter);
    }

    @Override // android.app.Service
    public int onStartCommand(Intent intent, int i, int i2) {
        super.onStartCommand(intent, i, i2);
        if (getResources().getBoolean(R.bool.persistent)) {
            return 1;
        }
        com.ifpdos.daemonserver.i.c.a("DaemonService", "not persistent,createForegroundService");
        d();
        return 1;
    }
}
