128 lines
4.8 KiB
Java
128 lines
4.8 KiB
Java
package com.liulishuo.filedownloader.services;
|
|
|
|
import android.content.ComponentName;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.ServiceConnection;
|
|
import android.os.Binder;
|
|
import android.os.IBinder;
|
|
import android.os.IInterface;
|
|
import android.os.RemoteException;
|
|
import com.liulishuo.filedownloader.FileDownloadEventPool;
|
|
import com.liulishuo.filedownloader.IFileDownloadServiceProxy;
|
|
import com.liulishuo.filedownloader.event.DownloadServiceConnectChangedEvent;
|
|
import com.liulishuo.filedownloader.util.FileDownloadLog;
|
|
import com.liulishuo.filedownloader.util.FileDownloadUtils;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
|
|
/* loaded from: classes.dex */
|
|
public abstract class BaseFileServiceUIGuard<CALLBACK extends Binder, INTERFACE extends IInterface> implements IFileDownloadServiceProxy, ServiceConnection {
|
|
private final CALLBACK a;
|
|
private volatile INTERFACE b;
|
|
private final Class<?> c;
|
|
private final List<Context> d;
|
|
private final ArrayList<Runnable> e;
|
|
|
|
protected BaseFileServiceUIGuard(Class<?> cls) {
|
|
new HashMap();
|
|
this.d = new ArrayList();
|
|
this.e = new ArrayList<>();
|
|
this.c = cls;
|
|
this.a = a();
|
|
}
|
|
|
|
private void a(boolean z) {
|
|
if (!z && this.b != null) {
|
|
try {
|
|
b(this.b, this.a);
|
|
} catch (RemoteException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
if (FileDownloadLog.a) {
|
|
FileDownloadLog.a(this, "release connect resources %s", this.b);
|
|
}
|
|
this.b = null;
|
|
FileDownloadEventPool.a().a(new DownloadServiceConnectChangedEvent(z ? DownloadServiceConnectChangedEvent.ConnectStatus.lost : DownloadServiceConnectChangedEvent.ConnectStatus.disconnected, this.c));
|
|
}
|
|
|
|
protected abstract CALLBACK a();
|
|
|
|
protected abstract INTERFACE a(IBinder iBinder);
|
|
|
|
protected abstract void a(INTERFACE r1, CALLBACK callback) throws RemoteException;
|
|
|
|
protected abstract void b(INTERFACE r1, CALLBACK callback) throws RemoteException;
|
|
|
|
@Override // com.liulishuo.filedownloader.IFileDownloadServiceProxy
|
|
public boolean b() {
|
|
return c() != null;
|
|
}
|
|
|
|
protected INTERFACE c() {
|
|
return this.b;
|
|
}
|
|
|
|
@Override // android.content.ServiceConnection
|
|
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
|
|
this.b = a(iBinder);
|
|
if (FileDownloadLog.a) {
|
|
FileDownloadLog.a(this, "onServiceConnected %s %s", componentName, this.b);
|
|
}
|
|
try {
|
|
a((BaseFileServiceUIGuard<CALLBACK, INTERFACE>) this.b, (INTERFACE) this.a);
|
|
} catch (RemoteException e) {
|
|
e.printStackTrace();
|
|
}
|
|
List list = (List) this.e.clone();
|
|
this.e.clear();
|
|
Iterator it = list.iterator();
|
|
while (it.hasNext()) {
|
|
((Runnable) it.next()).run();
|
|
}
|
|
FileDownloadEventPool.a().a(new DownloadServiceConnectChangedEvent(DownloadServiceConnectChangedEvent.ConnectStatus.connected, this.c));
|
|
}
|
|
|
|
@Override // android.content.ServiceConnection
|
|
public void onServiceDisconnected(ComponentName componentName) {
|
|
if (FileDownloadLog.a) {
|
|
FileDownloadLog.a(this, "onServiceDisconnected %s %s", componentName, this.b);
|
|
}
|
|
a(true);
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.IFileDownloadServiceProxy
|
|
public void a(Context context) {
|
|
a(context, (Runnable) null);
|
|
}
|
|
|
|
public void a(Context context, Runnable runnable) {
|
|
if (!FileDownloadUtils.c(context)) {
|
|
if (FileDownloadLog.a) {
|
|
FileDownloadLog.a(this, "bindStartByContext %s", context.getClass().getSimpleName());
|
|
}
|
|
Intent intent = new Intent(context, this.c);
|
|
if (runnable != null && !this.e.contains(runnable)) {
|
|
this.e.add(runnable);
|
|
}
|
|
if (!this.d.contains(context)) {
|
|
this.d.add(context);
|
|
}
|
|
context.bindService(intent, this, 1);
|
|
if (FileDownloadUtils.e(context)) {
|
|
if (FileDownloadLog.a) {
|
|
FileDownloadLog.a(this, "start foreground service", new Object[0]);
|
|
}
|
|
context.startForegroundService(intent);
|
|
return;
|
|
}
|
|
context.startService(intent);
|
|
return;
|
|
}
|
|
throw new IllegalStateException("Fatal-Exception: You can't bind the FileDownloadService in :filedownloader process.\n It's the invalid operation and is likely to cause unexpected problems.\n Maybe you want to use non-separate process mode for FileDownloader, More detail about non-separate mode, please move to wiki manually: https://github.com/lingochamp/FileDownloader/wiki/filedownloader.properties");
|
|
}
|
|
}
|