package com.kwad.components.ct.wallpaper.c; import android.app.Activity; import android.app.Service; import android.app.WallpaperInfo; import android.app.WallpaperManager; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.service.wallpaper.WallpaperService; import android.text.TextUtils; import android.view.Surface; import android.view.SurfaceHolder; import com.kwad.sdk.KsAdSDKImpl; import com.kwad.sdk.api.proxy.BaseProxyWallpaperService; import com.kwad.sdk.api.proxy.IWallpaperServiceProxy; import com.kwad.sdk.api.proxy.VideoWallpaperService; import com.kwad.sdk.core.e.b; import com.kwad.sdk.core.video.kwai.c; import com.kwad.sdk.core.video.kwai.d; import com.kwad.sdk.core.video.kwai.e; import com.kwad.sdk.utils.p; import com.kwad.sdk.utils.u; import com.kwad.sdk.utils.x; import java.io.File; import java.util.ArrayList; public class a implements IWallpaperServiceProxy { private c MH; private WallpaperService.Engine aGb; private String aGc; private long aGd = 0; private void EJ() { this.aGc = ""; try { c cVar = this.MH; if (cVar != null) { cVar.stop(); this.MH.release(); this.MH = null; } } catch (Exception e) { b.e("VideoWallpaperServiceProxy", "destroyMediaPlayer error", e); } } public static void a(Activity activity, String str, String str2, boolean z) { if (!TextUtils.isEmpty(str)) { if (z) { ComponentName componentName = new ComponentName("com.kwai.kwad.wallpaper", "com.kwai.kwad.wallpaper.MainActivity"); Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putString("wall_paper_path", str); bundle.putString("wall_paper_photo_id", str2); intent.putExtras(bundle); intent.setComponent(componentName); activity.startActivity(intent); if (k(activity, "com.kwai.kwad.wallpaper.service.KwaiWallPaperService")) { u.V(activity, "设置成功"); return; } return; } l(activity, str); if (!k(activity, VideoWallpaperService.class.getName())) { x.af(activity, str); Intent intent2 = new Intent("android.service.wallpaper.CHANGE_LIVE_WALLPAPER"); intent2.putExtra("android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT", new ComponentName(activity, VideoWallpaperService.class)); activity.startActivity(intent2); return; } Intent intent3 = new Intent(activity, VideoWallpaperService.class); intent3.putExtra("wall_paper_path", str); intent3.putExtra("wall_paper_photo_id", str2); activity.startService(intent3); } } private void a(Surface surface) { c cVar = this.MH; if (cVar != null) { cVar.b(new c.e() { /* class com.kwad.components.ct.wallpaper.c.a.AnonymousClass1 */ @Override // com.kwad.sdk.core.video.kwai.c.e public final void a(c cVar) { if (a.this.MH != null) { a.this.MH.seekTo(a.this.aGd); a.this.MH.start(); } } }); this.MH.setSurface(surface); this.MH.setLooping(true); this.MH.setVolume(0.0f, 0.0f); this.MH.prepareAsync(); } } private static WallpaperInfo aA(Context context) { try { return WallpaperManager.getInstance(context).getWallpaperInfo(); } catch (Exception e) { b.e("VideoWallpaperServiceProxy", "getWallpaperInfo error", e); return null; } } private static boolean bG(String str) { return new File(str).exists(); } private void i(Context context, String str) { if (!TextUtils.equals(str, this.aGc) && !TextUtils.isEmpty(str) && bG(str)) { try { EJ(); this.MH = j(context, str); a(this.aGb.getSurfaceHolder().getSurface()); x.af(context, str); this.aGc = str; } catch (Exception e) { b.e("VideoWallpaperServiceProxy", "VideoWallpaperServiceProxy set MediaPlayer error", e); } } } private static c j(Context context, String str) { d dVar; try { if (!e.Kg() || !com.kwad.sdk.core.config.d.Jf() || !e.Oy()) { b.i("MediaPlayerImpl", "constructPlayer AndroidMediaPlayer"); dVar = new com.kwad.sdk.core.video.kwai.b(); } else { b.i("MediaPlayerImpl", "constructPlayer KwaiMediaPlayer"); d dVar2 = new d(context); d dVar3 = dVar2; dVar2.bM(false); dVar = dVar2; } } catch (Throwable th) { b.e("MediaPlayerImpl", "constructPlayer exception, using AndroidMediaPlayer", th); dVar = new com.kwad.sdk.core.video.kwai.b(); } try { if (!TextUtils.isEmpty(str)) { dVar.setDataSource(str); } else { b.e("VideoWallpaperServiceProxy", "videoUrl is null"); } } catch (Exception e) { b.printStackTrace(e); } return dVar; } private static boolean k(Context context, String str) { WallpaperInfo aA = aA(context); return aA != null && TextUtils.equals(aA.getServiceName(), str); } private static void l(Context context, String str) { String cS = x.cS(context); String cS2 = x.cS(context); File parentFile = new File(str).getParentFile(); if (parentFile == null || !parentFile.isDirectory()) { b.e("VideoWallpaperServiceProxy", "video file parent should be dir"); return; } ArrayList arrayList = new ArrayList(); arrayList.add(cS); arrayList.add(cS2); arrayList.add(str); ArrayList arrayList2 = new ArrayList(); p.a(parentFile, arrayList, arrayList2); if (!arrayList2.isEmpty()) { b.e("VideoWallpaperServiceProxy", "delete other files failed " + arrayList2); } } @Override // com.kwad.sdk.api.proxy.IWallpaperServiceProxy public void onCreate(Service service) { } @Override // com.kwad.sdk.api.proxy.IWallpaperServiceProxy public void onDestroy(Service service) { } @Override // com.kwad.sdk.api.proxy.IWallpaperServiceProxy public void onEngineCreate(SurfaceHolder surfaceHolder) { } @Override // com.kwad.sdk.api.proxy.IWallpaperServiceProxy public void onEngineSurfaceCreated(SurfaceHolder surfaceHolder) { Context context; try { c cVar = this.MH; if ((cVar == null || !cVar.isPlaying()) && (context = KsAdSDKImpl.get().getContext()) != null) { String cS = x.cS(context); if (!TextUtils.isEmpty(cS)) { this.MH = j(context, cS); a(surfaceHolder.getSurface()); } } } catch (Exception unused) { } } @Override // com.kwad.sdk.api.proxy.IWallpaperServiceProxy public void onEngineSurfaceDestroyed(SurfaceHolder surfaceHolder) { EJ(); } @Override // com.kwad.sdk.api.proxy.IWallpaperServiceProxy public void onEngineVisibilityChanged(boolean z) { Context context; if (!z) { try { c cVar = this.MH; if (cVar != null) { this.aGd = (long) ((int) cVar.getCurrentPosition()); EJ(); } } catch (Exception unused) { this.MH = null; } } else if (this.MH == null && (context = KsAdSDKImpl.get().getContext()) != null) { String cS = x.cS(context); if (TextUtils.isEmpty(cS) || !bG(cS)) { try { WallpaperManager.getInstance(context).clear(); EJ(); } catch (Exception e) { b.e("VideoWallpaperServiceProxy", "WallpaperManager clear error", e); } } else { i(context, cS); } } } @Override // com.kwad.sdk.api.proxy.IWallpaperServiceProxy public void onRebind(Service service, Intent intent) { } @Override // com.kwad.sdk.api.proxy.IWallpaperServiceProxy public int onStartCommand(Service service, Intent intent, int i, int i2) { Context context = KsAdSDKImpl.get().getContext(); if (context != null) { String stringExtra = intent == null ? null : intent.getStringExtra("wall_paper_path"); if (TextUtils.isEmpty(stringExtra)) { stringExtra = x.cS(context); } i(context, stringExtra); return 1; } else if (service instanceof BaseProxyWallpaperService) { return ((BaseProxyWallpaperService) service).superOnStartCommand(intent, i, i2); } else { throw new RuntimeException(service + " must be BaseProxyWallpaperService"); } } @Override // com.kwad.sdk.api.proxy.IWallpaperServiceProxy public boolean onUnbind(Service service, Intent intent) { if (service instanceof BaseProxyWallpaperService) { return ((BaseProxyWallpaperService) service).superOnUnbind(intent); } throw new RuntimeException(service + " must be AbstractServiceProxy"); } @Override // com.kwad.sdk.api.proxy.IWallpaperServiceProxy public void setEngine(WallpaperService.Engine engine) { this.aGb = engine; } }