package com.seewo.remote.e.c;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import com.seewo.remote.g.w;
import com.seewo.vcommons.data.d;
import e.c.e.f.e;
import io.netty.channel.Channel;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.ConcurrentLinkedQueue;

/* JADX INFO: compiled from: ChannelHolder.java */
/* JADX INFO: loaded from: classes.dex */
public class a {

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    @SuppressLint({"StaticFieldLeak"})
    private static a f39e;
    private Context b;
    private final ConcurrentLinkedQueue<Channel> a = new ConcurrentLinkedQueue<>();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private boolean f40c = d.o("persist.rs232.test", false);

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private BroadcastReceiver f41d = new C0003a();

    /* JADX INFO: renamed from: com.seewo.remote.e.c.a$a, reason: collision with other inner class name */
    /* JADX INFO: compiled from: ChannelHolder.java */
    class C0003a extends BroadcastReceiver {
        C0003a() {
        }

        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            a.this.l();
        }
    }

    private a() {
    }

    public static a c() {
        return f39e;
    }

    private boolean d(String str) {
        return str.contains("127.0.0.1") || str.contains("::1");
    }

    public static a e() {
        a aVar = new a();
        f39e = aVar;
        return aVar;
    }

    public void a() {
        for (Channel channel : this.a) {
            if (channel != null && channel.isActive()) {
                Log.i("ChannelHolder", "disconnectAllClientChannel channel: " + channel);
                channel.close();
            }
        }
    }

    public boolean b() {
        return !this.a.isEmpty();
    }

    public void f(Channel channel) {
        Log.i("ChannelHolder", "onChannelActive channel: " + channel);
        if (channel != null && !this.a.contains(channel)) {
            this.a.add(channel);
        }
        l();
    }

    public void g(Channel channel) {
        Log.i("ChannelHolder", "onChannelNotActive channel: " + channel);
        if (channel == null || !this.a.contains(channel)) {
            return;
        }
        this.a.remove(channel);
    }

    public void h(Context context) {
        this.b = context;
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.DATE_CHANGED");
        intentFilter.addAction("android.intent.action.TIME_SET");
        intentFilter.addAction("android.intent.action.TIMEZONE_CHANGED");
        this.b.registerReceiver(this.f41d, intentFilter);
    }

    public void i() {
        this.b.unregisterReceiver(this.f41d);
        this.b = null;
    }

    public void j(byte[] bArr) {
        for (Channel channel : this.a) {
            if (channel != null && channel.isActive()) {
                channel.writeAndFlush(bArr);
            }
        }
    }

    public void k(byte[] bArr) {
        if (this.f40c) {
            Log.v("ChannelHolder", "inRS232AutoTest, return");
            return;
        }
        for (Channel channel : this.a) {
            if (channel != null && channel.isActive()) {
                String string = channel.remoteAddress().toString();
                if (w.k() || e.c.e.b.a.g(6) || !d(string)) {
                    channel.writeAndFlush(bArr);
                    e.a("ChannelHolder", com.seewo.remote.e.e.e.w(bArr));
                }
            }
        }
    }

    public void l() {
        String str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis())) + " 0" + Calendar.getInstance().get(7);
        for (Channel channel : this.a) {
            if (channel != null && channel.isActive() && com.seewo.remote.d.a.p()) {
                channel.writeAndFlush(str.getBytes());
            }
        }
    }

    public void m(boolean z) {
        this.f40c = z;
        d.D("persist.rs232.test", String.valueOf(z));
    }
}
