465 lines
17 KiB
Java
465 lines
17 KiB
Java
package com.baidu.cloud.media.download;
|
|
|
|
import android.content.Context;
|
|
import android.content.SharedPreferences;
|
|
import android.os.Build;
|
|
import android.os.Environment;
|
|
import android.util.Log;
|
|
import com.baidu.cloud.media.download.DownloadableVideoItem;
|
|
import com.baidu.cloud.media.download.c;
|
|
import com.baidu.cloud.media.download.d;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.ObjectInputStream;
|
|
import java.io.ObjectOutputStream;
|
|
import java.net.URL;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.Executors;
|
|
import org.json.JSONObject;
|
|
|
|
/* loaded from: classes.dex */
|
|
class b extends DownloadableVideoItem {
|
|
private volatile boolean m;
|
|
private ArrayList<String> n;
|
|
private String o;
|
|
private String p;
|
|
private Context q;
|
|
private ExecutorService k = null;
|
|
private volatile int l = 0;
|
|
private String r = null;
|
|
|
|
public b(Context context, String str, String str2, String str3, String str4, String str5) {
|
|
this.o = "";
|
|
this.p = "";
|
|
this.q = context;
|
|
this.b = str;
|
|
this.o = str2;
|
|
this.c = str3;
|
|
this.d = str4;
|
|
this.p = str5;
|
|
}
|
|
|
|
public static b a(Context context, String str, JSONObject jSONObject) {
|
|
b bVar;
|
|
try {
|
|
String string = jSONObject.getString("url");
|
|
String string2 = jSONObject.getString("urle");
|
|
String optString = jSONObject.optString("fold", null);
|
|
String string3 = jSONObject.getString("file");
|
|
int i = jSONObject.getInt("st");
|
|
int i2 = jSONObject.getInt("prgr");
|
|
int i3 = jSONObject.getInt("tsdl");
|
|
bVar = new b(context, string, string2, optString, string3, str);
|
|
try {
|
|
bVar.a(i, i2, i3);
|
|
} catch (Exception e) {
|
|
e = e;
|
|
Log.e("HLSVideoDownloader", "" + Log.getStackTraceString(e));
|
|
return bVar;
|
|
}
|
|
} catch (Exception e2) {
|
|
e = e2;
|
|
bVar = null;
|
|
}
|
|
return bVar;
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: private */
|
|
public void a(c.a aVar) {
|
|
if (this.l >= this.n.size()) {
|
|
i();
|
|
return;
|
|
}
|
|
c cVar = new c(this.n.get(this.l), this.c + "/" + (this.l + 1) + ".ts", aVar);
|
|
ExecutorService executorService = this.k;
|
|
if (executorService == null || executorService.isShutdown()) {
|
|
Log.d("HLSVideoDownloader", "new executor is created now");
|
|
this.k = Executors.newSingleThreadExecutor();
|
|
}
|
|
this.k.execute(cVar);
|
|
}
|
|
|
|
private void d() {
|
|
try {
|
|
if (!new URL(this.b).getPath().endsWith(".m3u8")) {
|
|
this.i = "only download m3u8 video";
|
|
this.j = 1;
|
|
a(DownloadableVideoItem.DownloadStatus.ERROR);
|
|
return;
|
|
}
|
|
String str = this.c;
|
|
if (str == null || str.equals("")) {
|
|
String downloadRootForCurrentUser = VideoDownloadManager.b().getDownloadRootForCurrentUser();
|
|
if (downloadRootForCurrentUser == null) {
|
|
this.i = "sdcard is unmounted";
|
|
this.j = 3;
|
|
a(DownloadableVideoItem.DownloadStatus.ERROR);
|
|
return;
|
|
} else {
|
|
this.c = downloadRootForCurrentUser + this.o + "/";
|
|
}
|
|
}
|
|
if (!this.m) {
|
|
if (!g()) {
|
|
if (!e()) {
|
|
a(DownloadableVideoItem.DownloadStatus.ERROR);
|
|
return;
|
|
}
|
|
Runnable runnable = new Runnable() { // from class: com.baidu.cloud.media.download.b.1
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
d.a aVar = new d.a() { // from class: com.baidu.cloud.media.download.b.1.1
|
|
@Override // com.baidu.cloud.media.download.d.a
|
|
public void a(int i) {
|
|
if (b.this.h == DownloadableVideoItem.DownloadStatus.DOWNLOADING) {
|
|
Log.d("HLSVideoDownloader", "Parse failed: error code is " + i);
|
|
b.this.i = "parse M3U8 failed, reason = " + DownloadableVideoItem.a[i];
|
|
b bVar = b.this;
|
|
bVar.j = i;
|
|
bVar.a(DownloadableVideoItem.DownloadStatus.ERROR);
|
|
}
|
|
}
|
|
|
|
@Override // com.baidu.cloud.media.download.d.a
|
|
public void a(List<String> list) {
|
|
if (b.this.h == DownloadableVideoItem.DownloadStatus.DOWNLOADING) {
|
|
b.this.n = (ArrayList) list;
|
|
b.this.f();
|
|
b.this.m = true;
|
|
b.this.h();
|
|
}
|
|
}
|
|
};
|
|
d dVar = new d(b.this.q, b.this.c + "/" + b.this.d);
|
|
b bVar = b.this;
|
|
dVar.a(bVar.b, bVar.r, aVar);
|
|
}
|
|
};
|
|
ExecutorService executorService = this.k;
|
|
if (executorService == null || executorService.isShutdown()) {
|
|
Log.d("HLSVideoDownloader", "new executor is created now to download m3u8 file");
|
|
this.k = Executors.newSingleThreadExecutor();
|
|
}
|
|
this.k.execute(runnable);
|
|
return;
|
|
}
|
|
this.m = true;
|
|
}
|
|
h();
|
|
} catch (Exception e) {
|
|
Log.d("HLSVideoDownloader", "" + e.getMessage());
|
|
this.i = "url format is invalid";
|
|
this.j = 1;
|
|
a(DownloadableVideoItem.DownloadStatus.ERROR);
|
|
}
|
|
}
|
|
|
|
static /* synthetic */ int e(b bVar) {
|
|
int i = bVar.l;
|
|
bVar.l = i + 1;
|
|
return i;
|
|
}
|
|
|
|
private boolean e() {
|
|
boolean z = true;
|
|
try {
|
|
if (!"mounted".equals(Environment.getExternalStorageState())) {
|
|
this.i = "save file failed, sdcard unmounted";
|
|
this.j = 3;
|
|
z = false;
|
|
}
|
|
if (a.a(this.q)) {
|
|
return z;
|
|
}
|
|
this.i = "network is not available";
|
|
this.j = 2;
|
|
return false;
|
|
} catch (Exception e) {
|
|
Log.d("HLSVideoDownloader", "checkEnvironment " + e.getMessage());
|
|
return z;
|
|
}
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: private */
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
public void f() {
|
|
ArrayList<String> arrayList = this.n;
|
|
if (arrayList == null || arrayList.size() <= 0) {
|
|
return;
|
|
}
|
|
FileOutputStream fileOutputStream = null;
|
|
fileOutputStream = null;
|
|
try {
|
|
try {
|
|
try {
|
|
FileOutputStream fileOutputStream2 = new FileOutputStream(this.c + "/tsdl.data");
|
|
try {
|
|
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream2);
|
|
objectOutputStream.writeObject(this.n);
|
|
fileOutputStream2.close();
|
|
fileOutputStream = objectOutputStream;
|
|
} catch (Exception e) {
|
|
e = e;
|
|
fileOutputStream = fileOutputStream2;
|
|
Log.d("HLSVideoDownloader", "" + e.getMessage());
|
|
if (fileOutputStream != null) {
|
|
fileOutputStream.close();
|
|
fileOutputStream = fileOutputStream;
|
|
}
|
|
} catch (Throwable th) {
|
|
th = th;
|
|
fileOutputStream = fileOutputStream2;
|
|
if (fileOutputStream != null) {
|
|
try {
|
|
fileOutputStream.close();
|
|
} catch (IOException unused) {
|
|
}
|
|
}
|
|
throw th;
|
|
}
|
|
} catch (Exception e2) {
|
|
e = e2;
|
|
}
|
|
} catch (IOException unused2) {
|
|
}
|
|
} catch (Throwable th2) {
|
|
th = th2;
|
|
}
|
|
}
|
|
|
|
private boolean g() {
|
|
if (!new File(this.c + "/" + this.d).exists()) {
|
|
return false;
|
|
}
|
|
if (this.n != null) {
|
|
return true;
|
|
}
|
|
FileInputStream fileInputStream = null;
|
|
try {
|
|
try {
|
|
FileInputStream fileInputStream2 = new FileInputStream(this.c + "/tsdl.data");
|
|
try {
|
|
this.n = (ArrayList) new ObjectInputStream(fileInputStream2).readObject();
|
|
try {
|
|
fileInputStream2.close();
|
|
return true;
|
|
} catch (IOException unused) {
|
|
return true;
|
|
}
|
|
} catch (Exception e) {
|
|
e = e;
|
|
fileInputStream = fileInputStream2;
|
|
Log.d("HLSVideoDownloader", "" + e.getMessage());
|
|
if (fileInputStream != null) {
|
|
try {
|
|
fileInputStream.close();
|
|
} catch (IOException unused2) {
|
|
}
|
|
}
|
|
return false;
|
|
} catch (Throwable th) {
|
|
th = th;
|
|
fileInputStream = fileInputStream2;
|
|
if (fileInputStream != null) {
|
|
try {
|
|
fileInputStream.close();
|
|
} catch (IOException unused3) {
|
|
}
|
|
}
|
|
throw th;
|
|
}
|
|
} catch (Throwable th2) {
|
|
th = th2;
|
|
}
|
|
} catch (Exception e2) {
|
|
e = e2;
|
|
}
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: private */
|
|
public void h() {
|
|
ArrayList<String> arrayList = this.n;
|
|
if (arrayList == null || arrayList.size() == 0) {
|
|
this.i = "tsList.size == 0";
|
|
this.j = 4;
|
|
a(DownloadableVideoItem.DownloadStatus.ERROR);
|
|
} else if (e()) {
|
|
a(new c.a() { // from class: com.baidu.cloud.media.download.b.2
|
|
@Override // com.baidu.cloud.media.download.c.a
|
|
public void a(int i, int i2, int i3) {
|
|
b bVar;
|
|
int i4;
|
|
if (i == 1) {
|
|
if (b.this.h == DownloadableVideoItem.DownloadStatus.PAUSED || b.this.h == DownloadableVideoItem.DownloadStatus.DELETED) {
|
|
return;
|
|
}
|
|
b.e(b.this);
|
|
b.this.a(i2, i3);
|
|
b.this.a(this);
|
|
return;
|
|
}
|
|
if (b.this.h == DownloadableVideoItem.DownloadStatus.PAUSED || b.this.h == DownloadableVideoItem.DownloadStatus.DELETED) {
|
|
return;
|
|
}
|
|
if (i == -2) {
|
|
bVar = b.this;
|
|
bVar.i = "network has problem";
|
|
i4 = 2;
|
|
} else {
|
|
b.this.i = "save the " + b.this.l + "th ts file - interrupted";
|
|
bVar = b.this;
|
|
i4 = 7;
|
|
}
|
|
bVar.j = i4;
|
|
b.this.a(DownloadableVideoItem.DownloadStatus.ERROR);
|
|
}
|
|
});
|
|
} else {
|
|
a(DownloadableVideoItem.DownloadStatus.ERROR);
|
|
}
|
|
}
|
|
|
|
private void i() {
|
|
a(DownloadableVideoItem.DownloadStatus.COMPLETED);
|
|
}
|
|
|
|
private void j() {
|
|
String k;
|
|
if (this.h == DownloadableVideoItem.DownloadStatus.DELETED || (k = k()) == null) {
|
|
return;
|
|
}
|
|
SharedPreferences.Editor edit = this.q.getSharedPreferences(this.p, 0).edit();
|
|
edit.putString(this.o, k);
|
|
if (Build.VERSION.SDK_INT >= 9) {
|
|
edit.apply();
|
|
} else {
|
|
edit.commit();
|
|
}
|
|
}
|
|
|
|
private String k() {
|
|
try {
|
|
JSONObject jSONObject = new JSONObject();
|
|
jSONObject.put("url", this.b);
|
|
jSONObject.put("fold", this.c);
|
|
jSONObject.put("file", this.d);
|
|
jSONObject.put("st", this.h.getCode());
|
|
jSONObject.put("prgr", this.e);
|
|
jSONObject.put("tsdl", this.l);
|
|
return jSONObject.toString();
|
|
} catch (Exception e) {
|
|
Log.e("HLSVideoDownloader", "" + e.getMessage());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
protected void a(int i, int i2) {
|
|
ArrayList<String> arrayList;
|
|
if (this.h == DownloadableVideoItem.DownloadStatus.DOWNLOADING && (arrayList = this.n) != null && arrayList.size() > 0) {
|
|
this.e = Math.round((this.l / this.n.size()) * 10000.0f);
|
|
this.f = i;
|
|
this.g += i2;
|
|
}
|
|
j();
|
|
setChanged();
|
|
notifyObservers();
|
|
}
|
|
|
|
protected void a(int i, int i2, int i3) {
|
|
this.e = i2;
|
|
this.l = i3;
|
|
DownloadableVideoItem.DownloadStatus downloadStatus = DownloadableVideoItem.DownloadStatus.values()[i];
|
|
if (downloadStatus == DownloadableVideoItem.DownloadStatus.DOWNLOADING || downloadStatus == DownloadableVideoItem.DownloadStatus.PENDING) {
|
|
downloadStatus = DownloadableVideoItem.DownloadStatus.PAUSED;
|
|
}
|
|
this.h = downloadStatus;
|
|
}
|
|
|
|
protected void a(DownloadableVideoItem.DownloadStatus downloadStatus) {
|
|
VideoDownloadManager b;
|
|
DownloadableVideoItem.DownloadStatus downloadStatus2 = this.h;
|
|
DownloadableVideoItem.DownloadStatus downloadStatus3 = DownloadableVideoItem.DownloadStatus.DOWNLOADING;
|
|
if (downloadStatus2 == downloadStatus3 && downloadStatus != downloadStatus3 && (b = VideoDownloadManager.b()) != null) {
|
|
b.c();
|
|
}
|
|
this.h = downloadStatus;
|
|
if (downloadStatus != DownloadableVideoItem.DownloadStatus.PAUSED && downloadStatus != DownloadableVideoItem.DownloadStatus.ERROR) {
|
|
this.i = "";
|
|
}
|
|
if (downloadStatus != DownloadableVideoItem.DownloadStatus.ERROR) {
|
|
this.j = 0;
|
|
}
|
|
a(0, 0);
|
|
}
|
|
|
|
public void a(String str) {
|
|
this.r = str;
|
|
}
|
|
|
|
public boolean a() {
|
|
DownloadableVideoItem.DownloadStatus downloadStatus = this.h;
|
|
DownloadableVideoItem.DownloadStatus downloadStatus2 = DownloadableVideoItem.DownloadStatus.DOWNLOADING;
|
|
if (downloadStatus != downloadStatus2) {
|
|
a(downloadStatus2);
|
|
d();
|
|
return true;
|
|
}
|
|
Log.e("HLSVideoDownloader", "start failed because downloadStatus = " + this.h.name());
|
|
return false;
|
|
}
|
|
|
|
public boolean b() {
|
|
if (this.h == DownloadableVideoItem.DownloadStatus.PAUSED || this.h == DownloadableVideoItem.DownloadStatus.COMPLETED || this.h == DownloadableVideoItem.DownloadStatus.DELETED) {
|
|
Log.e("HLSVideoDownloader", "pause not work, && downloadStatus = " + this.h.name());
|
|
return false;
|
|
}
|
|
ExecutorService executorService = this.k;
|
|
if (executorService != null && !executorService.isShutdown()) {
|
|
this.k.shutdownNow();
|
|
}
|
|
this.i = "manually pause";
|
|
a(DownloadableVideoItem.DownloadStatus.PAUSED);
|
|
return true;
|
|
}
|
|
|
|
public boolean c() {
|
|
try {
|
|
a(DownloadableVideoItem.DownloadStatus.DELETED);
|
|
if (this.k != null && !this.k.isTerminated() && !this.k.isShutdown()) {
|
|
this.k.shutdownNow();
|
|
}
|
|
SharedPreferences.Editor edit = this.q.getSharedPreferences(this.p, 0).edit();
|
|
edit.remove(this.o);
|
|
if (Build.VERSION.SDK_INT >= 9) {
|
|
edit.apply();
|
|
} else {
|
|
edit.commit();
|
|
}
|
|
if (this.c != null) {
|
|
File file = new File(this.c);
|
|
if (file.exists() && file.isDirectory()) {
|
|
File[] listFiles = file.listFiles();
|
|
for (int i = 0; i < listFiles.length; i++) {
|
|
if (listFiles[i].isFile()) {
|
|
listFiles[i].delete();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
this.c = "";
|
|
this.d = "";
|
|
this.e = 0;
|
|
this.j = 0;
|
|
this.i = "delete manually";
|
|
return true;
|
|
} catch (Exception e) {
|
|
Log.e("HLSVideoDownloader", "" + e.getMessage());
|
|
return true;
|
|
}
|
|
}
|
|
}
|