嘿嘿连载.apk(点击下载) / BaseFileServiceUIGuard.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.Build;
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.ExtraKeys;
import com.liulishuo.filedownloader.util.FileDownloadLog;
import com.liulishuo.filedownloader.util.FileDownloadUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public abstract class BaseFileServiceUIGuard<CALLBACK extends Binder, INTERFACE extends IInterface> implements IFileDownloadServiceProxy, ServiceConnection {
/* renamed from: a reason: collision with root package name */
protected boolean f5681a = false;
private final List<Context> bindContexts = new ArrayList();
private final CALLBACK callback;
private final ArrayList<Runnable> connectedRunnableList = new ArrayList<>();
private volatile INTERFACE service;
private final Class<?> serviceClass;
private final HashMap<String, Object> uiCacheMap = new HashMap<>();
protected BaseFileServiceUIGuard(Class<?> cls) {
this.serviceClass = cls;
this.callback = a();
}
private void releaseConnect(boolean z) {
if (!z && this.service != null) {
try {
b(this.service, this.callback);
} catch (RemoteException e) {
e.printStackTrace();
}
}
if (FileDownloadLog.NEED_LOG) {
FileDownloadLog.d(this, "release connect resources %s", this.service);
}
this.service = null;
FileDownloadEventPool.getImpl().asyncPublishInNewThread(new DownloadServiceConnectChangedEvent(z ? DownloadServiceConnectChangedEvent.ConnectStatus.lost : DownloadServiceConnectChangedEvent.ConnectStatus.disconnected, this.serviceClass));
}
/* access modifiers changed from: protected */
public abstract CALLBACK a();
/* access modifiers changed from: protected */
public abstract INTERFACE a(IBinder iBinder);
/* access modifiers changed from: protected */
public Object a(String str) {
return this.uiCacheMap.remove(str);
}
/* access modifiers changed from: protected */
public String a(Object obj) {
if (obj == null) {
return null;
}
String obj2 = obj.toString();
this.uiCacheMap.put(obj2, obj);
return obj2;
}
/* access modifiers changed from: protected */
public abstract void a(INTERFACE v, CALLBACK callback2) throws RemoteException;
/* access modifiers changed from: protected */
public CALLBACK b() {
return this.callback;
}
/* access modifiers changed from: protected */
public abstract void b(INTERFACE v, CALLBACK callback2) throws RemoteException;
@Override // com.liulishuo.filedownloader.IFileDownloadServiceProxy
public void bindStartByContext(Context context) {
bindStartByContext(context, null);
}
@Override // com.liulishuo.filedownloader.IFileDownloadServiceProxy
public void bindStartByContext(Context context, Runnable runnable) {
if (!FileDownloadUtils.isDownloaderProcess(context)) {
if (FileDownloadLog.NEED_LOG) {
FileDownloadLog.d(this, "bindStartByContext %s", context.getClass().getSimpleName());
}
Intent intent = new Intent(context, this.serviceClass);
if (runnable != null && !this.connectedRunnableList.contains(runnable)) {
this.connectedRunnableList.add(runnable);
}
if (!this.bindContexts.contains(context)) {
this.bindContexts.add(context);
}
this.f5681a = FileDownloadUtils.needMakeServiceForeground(context);
intent.putExtra(ExtraKeys.IS_FOREGROUND, this.f5681a);
context.bindService(intent, this, 1);
if (this.f5681a) {
if (FileDownloadLog.NEED_LOG) {
FileDownloadLog.d(this, "start foreground service", new Object[0]);
}
if (Build.VERSION.SDK_INT >= 26) {
context.startForegroundService(intent);
return;
}
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");
}
/* access modifiers changed from: protected */
public INTERFACE c() {
return this.service;
}
@Override // com.liulishuo.filedownloader.IFileDownloadServiceProxy
public boolean isConnected() {
return c() != null;
}
@Override // com.liulishuo.filedownloader.IFileDownloadServiceProxy
public boolean isRunServiceForeground() {
return this.f5681a;
}
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
this.service = a(iBinder);
if (FileDownloadLog.NEED_LOG) {
FileDownloadLog.d(this, "onServiceConnected %s %s", componentName, this.service);
}
try {
a(this.service, this.callback);
} catch (RemoteException e) {
e.printStackTrace();
}
this.connectedRunnableList.clear();
for (Runnable runnable : (List) this.connectedRunnableList.clone()) {
runnable.run();
}
FileDownloadEventPool.getImpl().asyncPublishInNewThread(new DownloadServiceConnectChangedEvent(DownloadServiceConnectChangedEvent.ConnectStatus.connected, this.serviceClass));
}
public void onServiceDisconnected(ComponentName componentName) {
if (FileDownloadLog.NEED_LOG) {
FileDownloadLog.d(this, "onServiceDisconnected %s %s", componentName, this.service);
}
releaseConnect(true);
}
@Override // com.liulishuo.filedownloader.IFileDownloadServiceProxy
public void unbindByContext(Context context) {
if (this.bindContexts.contains(context)) {
if (FileDownloadLog.NEED_LOG) {
FileDownloadLog.d(this, "unbindByContext %s", context);
}
this.bindContexts.remove(context);
if (this.bindContexts.isEmpty()) {
releaseConnect(false);
}
Intent intent = new Intent(context, this.serviceClass);
context.unbindService(this);
context.stopService(intent);
}
}
}