package com.ss.android.ugc.aweme.livewallpaper;

import X.0HH;
import X.2k6;
import X.2kf;
import X.805;
import X.9EH;
import X.C3570JbN;
import X.C3575JbS;
import X.C3577JbU;
import X.C3578JbV;
import X.C3581JbY;
import X.C3583Jba;
import X.C3613Jc4;
import X.M12;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.bytedance.helios.sdk.utils.ServiceLifecycle;
import com.ss.android.ugc.aweme.livewallpaper.model.LiveWallPaperBean;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

public class AmeLiveWallpaper extends WallpaperService implements C3613Jc4 {
    public LiveWallPaperBean LIZ;
    public final ArrayList<C3577JbU> LIZIZ = new ArrayList<>();
    public ContentResolver LIZJ;
    public C3581JbY LIZLLL;
    public Handler LJ = new C3583Jba(this);
    public SimpleDateFormat LJFF = new SimpleDateFormat("yyyy-MM-dd");

    static {
        Covode.recordClassIndex(91387);
    }

    private void LIZJ() {
        this.LJ.removeMessages(1);
    }

    public WallpaperService.Engine onCreateEngine() {
        M12.LIZ("AmeLiveWallpaper", "AmeLiveWallpaper onCreateEngine: service = ".concat(String.valueOf(this)));
        C3577JbU jbU = new C3577JbU(this, (byte) 0);
        this.LIZIZ.add(jbU);
        return jbU;
    }

    public void onDestroy() {
        M12.LIZ("AmeLiveWallpaper", "onDestroy: service = ".concat(String.valueOf(this)));
        LIZJ();
        super.onDestroy();
    }

    public void onCreate() {
        super.onCreate();
        M12.LIZ("AmeLiveWallpaper", "AmeLiveWallpaper onCreate");
        this.LIZJ = getContentResolver();
        this.LIZLLL = (C3581JbY) 2k6.LIZ(9EH.LJJIFFI.LIZ(), C3581JbY.class);
        LIZJ();
        LIZ();
    }

    public final void LIZ() {
        if (!this.LJFF.format(new Date(System.currentTimeMillis())).equals(this.LJFF.format(Long.valueOf(C3578JbV.LIZ.LIZJ())))) {
            C3578JbV.LIZ.LJIIJ();
            String[] LIZIZ2 = C3578JbV.LIZ.LIZIZ();
            if (LIZIZ2 == null || LIZIZ2.length <= 0) {
                C3578JbV jbV = C3578JbV.LIZ;
                StringBuilder sb = new StringBuilder();
                sb.append(System.currentTimeMillis());
                jbV.LIZ(new String[]{sb.toString()});
            } else {
                if (!this.LJFF.format(new Date(Long.parseLong(LIZIZ2[LIZIZ2.length - 1]))).equals(this.LJFF.format(new Date(System.currentTimeMillis())))) {
                    ArrayList arrayList = new ArrayList(Arrays.asList(LIZIZ2));
                    StringBuilder sb2 = new StringBuilder();
                    sb2.append(System.currentTimeMillis());
                    arrayList.add(sb2.toString());
                    C3578JbV.LIZ.LIZ((String[]) arrayList.toArray(new String[0]));
                }
            }
        }
        this.LJ.sendEmptyMessageDelayed(1, TimeUnit.HOURS.toMillis((long) C3578JbV.LIZ.LJ()));
    }

    public final void LIZIZ() {
        ContentResolver contentResolver;
        if (this.LIZLLL == null) {
            this.LIZLLL = (C3581JbY) 2k6.LIZ(9EH.LJJIFFI.LIZ(), C3581JbY.class);
        }
        ContentResolver contentResolver2 = this.LIZJ;
        if (contentResolver2 != null) {
            this.LIZ = C3570JbN.LIZ.LIZ(contentResolver2.getType(WallPaperDataProvider.LJFF));
        }
        if (this.LIZ == null) {
            this.LIZ = LiveWallPaperBean.buildEmptyBean();
        }
        try {
            if (TextUtils.isEmpty(this.LIZ.getVideoPath())) {
                C3581JbY jbY = this.LIZLLL;
                if (jbY != null) {
                    this.LIZ.setVideoPath(jbY.LIZJ(""));
                }
            }
            if (!805.LIZIZ(this.LIZ.getVideoPath()) && (contentResolver = this.LIZJ) != null) {
                this.LIZ.setVideoPath(contentResolver.getType(WallPaperDataProvider.LJI));
            }
            if (this.LIZ.getWidth() <= 0) {
                this.LIZ.setWidth(this.LIZLLL.LIZ());
            }
            if (this.LIZ.getHeight() <= 0) {
                this.LIZ.setHeight(this.LIZLLL.LIZIZ());
            }
            if (TextUtils.isEmpty(this.LIZ.getSource())) {
                this.LIZ.setSource(this.LIZLLL.LJ(""));
            }
        } catch (Exception unused) {
        }
    }

    public static String LIZ(Intent intent, String str) {
        try {
            return intent.getStringExtra(str);
        } catch (Exception unused) {
            return null;
        }
    }

    public final void LIZ(boolean z, String str, String str2) {
        if (this.LIZJ != null) {
            ContentValues contentValues = new ContentValues();
            contentValues.put("result", Boolean.valueOf(z));
            contentValues.put("source", str);
            contentValues.put("message", str2);
            try {
                this.LIZJ.insert(WallPaperDataProvider.LJII, contentValues);
            } catch (Exception e) {
                0HH.LIZ(e);
                2kf.LIZ("LiveWallPaper insert failed " + e.getMessage());
            }
        }
    }

    public int onStartCommand(Intent intent, int i, int i2) {
        ServiceLifecycle.onStartCommand(this, intent, i, i2);
        M12.LIZ("AmeLiveWallpaper", "AmeLiveWallpaper onStartCommand");
        if (intent != null) {
            if ("action_update_volume".equals(LIZ(intent, "action"))) {
                float LIZ2 = C3575JbS.LIZ(C3578JbV.LIZ.LJI());
                Iterator<C3577JbU> it = this.LIZIZ.iterator();
                while (it.hasNext()) {
                    it.next().LIZ.LIZ(LIZ2);
                }
                return super.onStartCommand(intent, i, i2);
            }
            this.LIZ = C3570JbN.LIZ.LIZ(LIZ(intent, "wallpaper_json"));
        }
        LiveWallPaperBean liveWallPaperBean = this.LIZ;
        if (liveWallPaperBean == null || !805.LIZIZ(liveWallPaperBean.getVideoPath()) || this.LIZ.getWidth() <= 0 || this.LIZ.getHeight() <= 0) {
            LIZIZ();
        }
        Iterator<C3577JbU> it2 = this.LIZIZ.iterator();
        while (it2.hasNext()) {
            C3577JbU next = it2.next();
            if (next.LIZIZ.LIZ != null && !TextUtils.isEmpty(next.LIZIZ.LIZ.getSource())) {
                next.LIZ.LIZ(next.LIZIZ.LIZ);
                next.LIZ.LIZ(next.LIZIZ.LIZ.getVideoPath(), next.LIZIZ.LIZ.getWidth(), next.LIZIZ.LIZ.getHeight());
            }
        }
        LIZJ();
        LIZ();
        return super.onStartCommand(intent, i, i2);
    }
}
