|
|
@@ -1,37 +1,36 @@
|
|
|
-package com.atmob.task.adapter;
|
|
|
+package com.atmob.rewardtask;
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
+import android.content.Context;
|
|
|
+import android.graphics.drawable.Drawable;
|
|
|
import android.os.Bundle;
|
|
|
import android.text.TextUtils;
|
|
|
+import android.util.Log;
|
|
|
import android.view.LayoutInflater;
|
|
|
import android.view.View;
|
|
|
import android.view.ViewGroup;
|
|
|
+import android.widget.ImageView;
|
|
|
+import android.widget.TextView;
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
+import androidx.annotation.IntegerRes;
|
|
|
import androidx.annotation.NonNull;
|
|
|
import androidx.annotation.Nullable;
|
|
|
import androidx.recyclerview.widget.AsyncListDiffer;
|
|
|
import androidx.recyclerview.widget.DiffUtil;
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
|
-import com.atmob.http.BaseHttpObserver;
|
|
|
-import com.atmob.manager.HttpFailManager;
|
|
|
-import com.atmob.task.AppTaskAdapter;
|
|
|
-import com.atmob.task.AppTaskView.AppTaskViewHolder;
|
|
|
-import com.atmob.task.bean.AdAppInfoData;
|
|
|
-import com.atmob.task.bean.AppTaskBean;
|
|
|
-import com.atmob.task.bean.AppTaskDownloadWrapper;
|
|
|
-import com.atmob.task.bean.AppTaskUpdateRequest;
|
|
|
-import com.atmob.task.bean.AppTaskUpdateResponse;
|
|
|
-import com.atmob.task.data.LocalData;
|
|
|
-import com.atmob.task.data.NetworkClient;
|
|
|
-import com.atmob.task.utils.ATLog;
|
|
|
-import com.atmob.task.utils.AppTaskEvent;
|
|
|
-import com.atmob.task.utils.InstallFaultToleranceUtils;
|
|
|
-import com.atmob.task.utils.RetrofitDownloader;
|
|
|
-import com.atmob.utils.AppInfoUtils;
|
|
|
-import com.atmob.utils.RxSchedulersUtils;
|
|
|
-import com.atmob.utils.Utils;
|
|
|
+import com.atmob.rewardtask.bean.AppTaskBean;
|
|
|
+import com.atmob.rewardtask.bean.AppTaskDownloadWrapper;
|
|
|
+import com.atmob.rewardtask.utils.AppInfoUtils;
|
|
|
+import com.atmob.rewardtask.utils.InstallFaultToleranceUtils;
|
|
|
+import com.atmob.rewardtask.utils.RetrofitDownloader;
|
|
|
+import com.atmob.rewardtask.view.NormalProgressBar;
|
|
|
+import com.bumptech.glide.Glide;
|
|
|
+import com.bumptech.glide.RequestBuilder;
|
|
|
+import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
|
|
|
+import com.bumptech.glide.request.RequestOptions;
|
|
|
+import com.ishumei.smantifraud.SmAntiFraud;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.util.ArrayList;
|
|
|
@@ -46,16 +45,15 @@ import atmob.io.reactivex.rxjava3.core.Completable;
|
|
|
import atmob.io.reactivex.rxjava3.core.CompletableObserver;
|
|
|
import atmob.io.reactivex.rxjava3.core.Observable;
|
|
|
import atmob.io.reactivex.rxjava3.core.Observer;
|
|
|
-import atmob.io.reactivex.rxjava3.core.SingleObserver;
|
|
|
import atmob.io.reactivex.rxjava3.disposables.CompositeDisposable;
|
|
|
import atmob.io.reactivex.rxjava3.disposables.Disposable;
|
|
|
import atmob.io.reactivex.rxjava3.schedulers.Schedulers;
|
|
|
|
|
|
-public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerView.Adapter<T> {
|
|
|
+public class AppTaskItemAdapter extends RecyclerView.Adapter<AppTaskItemAdapter.AppTaskItemViewHolder> {
|
|
|
|
|
|
private static final String TAG = AppTaskItemAdapter.class.getSimpleName();
|
|
|
|
|
|
- private static final String DIR_PATH = Utils.getContext().getExternalCacheDir().getPath();
|
|
|
+ private static String DIR_PATH;
|
|
|
|
|
|
/**
|
|
|
* 安装成功后, 是否强制打开
|
|
|
@@ -68,87 +66,62 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
|
|
|
|
|
|
private final CompositeDisposable compositeDisposable;
|
|
|
|
|
|
- private RecyclerView recyclerView;
|
|
|
+ private final Context context;
|
|
|
|
|
|
private OnItemActionCallback onItemActionCallback;
|
|
|
|
|
|
- private AppTaskAdapter<T> appTaskAdapter;
|
|
|
+ private ViewHolderCreator viewHolderCreator;
|
|
|
|
|
|
- private int maxLength = Integer.MAX_VALUE;
|
|
|
-
|
|
|
- private List<AppTaskDownloadWrapper> dataCache = Collections.EMPTY_LIST;
|
|
|
-
|
|
|
- public AppTaskItemAdapter(RecyclerView recyclerView, AppTaskAdapter<T> appTaskAdapter, CompositeDisposable compositeDisposable) {
|
|
|
+ public AppTaskItemAdapter(ViewHolderCreator viewHolderCreator, CompositeDisposable compositeDisposable, Context context) {
|
|
|
+ this.context = context;
|
|
|
+ this.viewHolderCreator = viewHolderCreator;
|
|
|
this.compositeDisposable = compositeDisposable;
|
|
|
- this.appTaskAdapter = appTaskAdapter;
|
|
|
- this.recyclerView = recyclerView;
|
|
|
+ File cacheDir = context.getExternalCacheDir();
|
|
|
+ if (cacheDir == null) {
|
|
|
+ cacheDir = context.getCacheDir();
|
|
|
+ }
|
|
|
+ DIR_PATH = cacheDir.getPath();
|
|
|
}
|
|
|
|
|
|
@NonNull
|
|
|
@Override
|
|
|
- public T onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
- if (appTaskAdapter == null) {
|
|
|
- throw new IllegalStateException("appTaskAdapter can't not be null.");
|
|
|
- }
|
|
|
- T appTaskViewHolder = appTaskAdapter.onCreateViewHolder(LayoutInflater.from(parent.getContext()), parent);
|
|
|
- bindClickListener(appTaskViewHolder);
|
|
|
- return appTaskViewHolder;
|
|
|
- }
|
|
|
-
|
|
|
- private void bindClickListener(T appTaskViewHolder) {
|
|
|
- if (appTaskAdapter == null) {
|
|
|
- throw new IllegalStateException("appTaskAdapter can't not be null.");
|
|
|
- }
|
|
|
- View[] clickViews = appTaskAdapter.getClickViews(appTaskViewHolder);
|
|
|
- if (clickViews == null || clickViews.length == 0) {
|
|
|
- return;
|
|
|
- }
|
|
|
- View.OnClickListener onClickListener = v -> onAppTaskItemClick((AppTaskDownloadWrapper) v.getTag());
|
|
|
- for (View clickView : clickViews) {
|
|
|
- clickView.setOnClickListener(onClickListener);
|
|
|
- }
|
|
|
+ public AppTaskItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
+ AppTaskItemViewHolder appTaskItemViewHolder = viewHolderCreator.onCreateViewHolder(LayoutInflater.from(parent.getContext()), parent);
|
|
|
+ appTaskItemViewHolder.getTriggerBtn().setOnClickListener(v -> {
|
|
|
+ SmAntiFraud.track("onViewItemClick", String.valueOf(v.getId()), null);
|
|
|
+ onAppTaskItemClick((AppTaskDownloadWrapper) v.getTag());
|
|
|
+ });
|
|
|
+ return appTaskItemViewHolder;
|
|
|
}
|
|
|
|
|
|
@SuppressLint("ResourceType")
|
|
|
@Override
|
|
|
- public void onBindViewHolder(@NonNull T holder, int position) {
|
|
|
- if (appTaskAdapter == null) {
|
|
|
- throw new IllegalStateException("appTaskAdapter can't not be null.");
|
|
|
- }
|
|
|
+ public void onBindViewHolder(@NonNull AppTaskItemViewHolder holder, int position) {
|
|
|
AppTaskDownloadWrapper appTaskDownloadWrapper = asyncListDiffer.getCurrentList().get(position);
|
|
|
- // download status
|
|
|
- appTaskAdapter.onDownloadStatusChanged(holder, appTaskDownloadWrapper.isDownloading());
|
|
|
// app name
|
|
|
- appTaskAdapter.onRenderAppName(holder, appTaskDownloadWrapper, appTaskDownloadWrapper.getAppName());
|
|
|
+ holder.getAppName().setText(appTaskDownloadWrapper.getAppName());
|
|
|
// app icon
|
|
|
- appTaskAdapter.onRenderAppIcon(holder, appTaskDownloadWrapper, appTaskDownloadWrapper.getAppIcon());
|
|
|
- // click view
|
|
|
- setTag2ClickViews(holder, appTaskDownloadWrapper);
|
|
|
- // task status
|
|
|
- appTaskAdapter.onRenderTaskStatus(holder, appTaskDownloadWrapper, appTaskDownloadWrapper.getTaskStatus());
|
|
|
- // download progress
|
|
|
- appTaskAdapter.onRenderDownloadProgress(holder, appTaskDownloadWrapper.getTotal(), appTaskDownloadWrapper.getProgress());
|
|
|
- }
|
|
|
-
|
|
|
- private void setTag2ClickViews(T holder, AppTaskDownloadWrapper appTaskDownloadWrapper) {
|
|
|
- if (appTaskAdapter == null) {
|
|
|
- throw new IllegalStateException("appTaskAdapter can't not be null.");
|
|
|
- }
|
|
|
- View[] clickViews = appTaskAdapter.getClickViews(holder);
|
|
|
- if (clickViews != null && clickViews.length > 0) {
|
|
|
- for (View clickView : clickViews) {
|
|
|
- clickView.setTag(appTaskDownloadWrapper);
|
|
|
- }
|
|
|
+ RequestBuilder<Drawable> requestBuilder = Glide.with(holder.getAppIcon()).load(appTaskDownloadWrapper.getAppIcon());
|
|
|
+ if (holder.getAppIconConfig().round > 0) {
|
|
|
+ requestBuilder.apply(RequestOptions.bitmapTransform(new RoundedCorners(holder.getAppIconConfig().round)));
|
|
|
}
|
|
|
+ requestBuilder.placeholder(holder.getAppIconConfig().placeholderResId).into(holder.getAppIcon());
|
|
|
+ // btn related
|
|
|
+ holder.getTriggerBtn().setTag(appTaskDownloadWrapper);
|
|
|
+ holder.getTriggerBtn().setText(holder.getBtnText(appTaskDownloadWrapper.getTaskStatus()));
|
|
|
+ holder.getTriggerBtn().setBackground(holder.getBtnBackground(appTaskDownloadWrapper.getTaskStatus(), appTaskDownloadWrapper.getRewardRatio() > 1));
|
|
|
+ holder.getTriggerBtn().setVisibility(appTaskDownloadWrapper.isDownloading() ? View.GONE : View.VISIBLE);
|
|
|
+ // progress related
|
|
|
+ holder.getProgressBar().setVisibility(appTaskDownloadWrapper.isDownloading() ? View.VISIBLE : View.GONE);
|
|
|
+ holder.getProgressBar().setProgress(appTaskDownloadWrapper.getTotal(), appTaskDownloadWrapper.getProgress());
|
|
|
+ // reward text
|
|
|
+ holder.onRenderReward(appTaskDownloadWrapper.getRewardRatio());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void onBindViewHolder(@NonNull T holder, int position, @NonNull List<Object> payloads) {
|
|
|
- if (appTaskAdapter == null) {
|
|
|
- throw new IllegalStateException("appTaskAdapter can't not be null.");
|
|
|
- }
|
|
|
+ public void onBindViewHolder(@NonNull AppTaskItemViewHolder holder, int position, @NonNull List<Object> payloads) {
|
|
|
AppTaskDownloadWrapper appTaskDownloadWrapper = asyncListDiffer.getCurrentList().get(position);
|
|
|
- setTag2ClickViews(holder, appTaskDownloadWrapper);
|
|
|
+ holder.getTriggerBtn().setTag(appTaskDownloadWrapper);
|
|
|
if (payloads.size() == 0) {
|
|
|
onBindViewHolder(holder, position);
|
|
|
} else {
|
|
|
@@ -159,19 +132,22 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
|
|
|
String key = iterator.next();
|
|
|
switch (key) {
|
|
|
case "payload_app_name":
|
|
|
- appTaskAdapter.onRenderAppName(holder, appTaskDownloadWrapper, bundle.getString(key));
|
|
|
+ holder.getAppName().setText(bundle.getString(key));
|
|
|
break;
|
|
|
case "payload_task_status":
|
|
|
- appTaskAdapter.onRenderTaskStatus(holder, appTaskDownloadWrapper, bundle.getInt(key));
|
|
|
+ holder.getTriggerBtn().setText(holder.getBtnText(bundle.getInt(key)));
|
|
|
+ holder.getTriggerBtn().setBackground(holder.getBtnBackground(bundle.getInt(key), appTaskDownloadWrapper.getRewardRatio() > 1));
|
|
|
break;
|
|
|
case "payload_downloading":
|
|
|
- appTaskAdapter.onDownloadStatusChanged(holder, bundle.getBoolean(key));
|
|
|
+ holder.getTriggerBtn().setVisibility(bundle.getBoolean(key) ? View.GONE : View.VISIBLE);
|
|
|
+ holder.getProgressBar().setVisibility(bundle.getBoolean(key) ? View.VISIBLE : View.GONE);
|
|
|
break;
|
|
|
case "payload_progress":
|
|
|
+ case "payload_total":
|
|
|
if (bundle.get("payload_progress") == null || bundle.get("payload_total") == null) {
|
|
|
break;
|
|
|
}
|
|
|
- appTaskAdapter.onRenderDownloadProgress(holder, bundle.getLong("payload_total"), bundle.getLong("payload_progress"));
|
|
|
+ holder.getProgressBar().startProgress(bundle.getLong("payload_total"), bundle.getLong("payload_progress"));
|
|
|
iterator.remove();
|
|
|
break;
|
|
|
}
|
|
|
@@ -193,13 +169,13 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
|
|
|
case AppTaskBean.AppTaskStatus.Initial:
|
|
|
case AppTaskBean.AppTaskStatus.Installed:
|
|
|
toOpen(appTaskDownloadWrapper);
|
|
|
- ATLog.d(TAG, "onAppTaskItemClick: toOpen.");
|
|
|
+ Log.d(TAG, "onAppTaskItemClick: toOpen.");
|
|
|
break;
|
|
|
case AppTaskBean.AppTaskStatus.Opened:
|
|
|
if (onItemActionCallback != null) {
|
|
|
onItemActionCallback.getReward(appTaskDownloadWrapper);
|
|
|
}
|
|
|
- ATLog.d(TAG, "onAppTaskItemClick: getReward.");
|
|
|
+ Log.d(TAG, "onAppTaskItemClick: getReward.");
|
|
|
break;
|
|
|
case AppTaskBean.AppTaskStatus.Finish:
|
|
|
//ignore
|
|
|
@@ -208,53 +184,29 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
|
|
|
if (onItemActionCallback != null) {
|
|
|
onItemActionCallback.onClick(appTaskDownloadWrapper);
|
|
|
}
|
|
|
- reportClickEvent(appTaskDownloadWrapper);
|
|
|
- }
|
|
|
-
|
|
|
- private void reportClickEvent(AppTaskBean appTaskBean) {
|
|
|
- switch (appTaskBean.getTaskStatus()) {
|
|
|
- case AppTaskBean.AppTaskStatus.Finish:
|
|
|
- break;
|
|
|
- case AppTaskBean.AppTaskStatus.Initial:
|
|
|
- case AppTaskBean.AppTaskStatus.Installed:
|
|
|
- AppTaskEvent.report(AppTaskEvent.ATE_CLICK_ITEM_INITIAL);
|
|
|
- break;
|
|
|
- case AppTaskBean.AppTaskStatus.Opened:
|
|
|
- AppTaskEvent.report(AppTaskEvent.ATE_CLICK_ITEM_REWARD);
|
|
|
- break;
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
private void toOpen(@NonNull AppTaskDownloadWrapper appTaskDownloadWrapper) {
|
|
|
String appPkgName = appTaskDownloadWrapper.getAppPkgName();
|
|
|
- if (AppInfoUtils.isExistPackage(Utils.getContext(), appPkgName)) {
|
|
|
+ if (AppInfoUtils.isExistPackage(context, appPkgName)) {
|
|
|
AppTaskEvent.report(AppTaskEvent.ATE_LAUNCH);
|
|
|
- AppInfoUtils.launchApp(appPkgName);
|
|
|
- updateStatus(appTaskDownloadWrapper.getId(), AppTaskBean.AppTaskStatus.Opened);
|
|
|
+ AppInfoUtils.launchApp(context, appPkgName);
|
|
|
+ if (onItemActionCallback != null) {
|
|
|
+ onItemActionCallback.updateStatus(appTaskDownloadWrapper.getId(), AppTaskBean.AppTaskStatus.Opened);
|
|
|
+ }
|
|
|
removeFromCheckList(appTaskDownloadWrapper.getId());
|
|
|
- ATLog.d(TAG, "toOpen: app exist, open it.");
|
|
|
+ Log.d(TAG, "toOpen: app exist, open it.");
|
|
|
} else {
|
|
|
AppTaskEvent.report(AppTaskEvent.ATE_DOWNLOAD);
|
|
|
toInstall(appTaskDownloadWrapper);
|
|
|
- ATLog.d(TAG, "toOpen: app not exist, try to download.");
|
|
|
+ Log.d(TAG, "toOpen: app not exist, try to download.");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void toInstall(@NonNull AppTaskDownloadWrapper appTaskDownloadWrapper) {
|
|
|
// first of all, check local apk file is exists
|
|
|
Observable.just(appTaskDownloadWrapper.getAppPkgName())
|
|
|
- .map(appPackageName -> {
|
|
|
- // this step is check apk file that download by sdk is exists
|
|
|
- AdAppInfoData adAppInfoData = LocalData.queryAdAppInfoByPackageName(appPackageName);
|
|
|
- if (adAppInfoData == null) {
|
|
|
- return new File("");
|
|
|
- }
|
|
|
- String apkPath = adAppInfoData.getApkPath();
|
|
|
- File file = new File(apkPath);
|
|
|
- file = file.exists() && file.isFile() ? file : new File("");
|
|
|
- ATLog.d(TAG, "toInstall: sdk apk path ==> " + apkPath + ", file available ==> " + (!"".equals(file.getPath())));
|
|
|
- return file;
|
|
|
- })
|
|
|
+ .map(appPackageName -> new File(""))// 去除原有逻辑: 查找SDK安装包(未维护)
|
|
|
.map(apkFile -> {
|
|
|
// if sdk download file is exist just use it
|
|
|
if ("".equals(apkFile.getPath())) {
|
|
|
@@ -263,7 +215,7 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
|
|
|
if (downloadFile.exists() && downloadFile.isFile() && !checkFileOccupy(downloadFile)) {
|
|
|
apkFile = downloadFile;
|
|
|
}
|
|
|
- ATLog.d(TAG, "toInstall: download apk path ==> " + downloadFile.getPath() + ", file available ==> " + (!"".equals(apkFile.getPath())));
|
|
|
+ Log.d(TAG, "toInstall: download apk path ==> " + downloadFile.getPath() + ", file available ==> " + (!"".equals(apkFile.getPath())));
|
|
|
}
|
|
|
if ("".equals(apkFile.getPath())) {
|
|
|
throw new Exception("none available apk file");
|
|
|
@@ -271,7 +223,7 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
|
|
|
return apkFile;
|
|
|
}
|
|
|
})
|
|
|
- .compose(RxSchedulersUtils.observableIO2Main())
|
|
|
+ .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
|
|
|
.subscribe(new Observer<File>() {
|
|
|
@Override
|
|
|
public void onSubscribe(@atmob.io.reactivex.rxjava3.annotations.NonNull Disposable d) {
|
|
|
@@ -281,10 +233,10 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
|
|
|
@Override
|
|
|
public void onNext(@atmob.io.reactivex.rxjava3.annotations.NonNull File file) {
|
|
|
// local file exists, install it
|
|
|
- InstallFaultToleranceUtils.installApk(Utils.getContext(), file, () -> {
|
|
|
+ InstallFaultToleranceUtils.installApk(context, file, () -> {
|
|
|
AppTaskEvent.report(AppTaskEvent.ATE_DOWNLOAD);
|
|
|
toDownload(appTaskDownloadWrapper);
|
|
|
- ATLog.d(TAG, "toInstall: apk install failed, try to download");
|
|
|
+ Log.d(TAG, "toInstall: apk install failed, try to download");
|
|
|
});
|
|
|
addToCheckList(appTaskDownloadWrapper);
|
|
|
}
|
|
|
@@ -294,7 +246,7 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
|
|
|
// local file not exist, try to download
|
|
|
AppTaskEvent.report(AppTaskEvent.ATE_DOWNLOAD);
|
|
|
toDownload(appTaskDownloadWrapper);
|
|
|
- ATLog.d(TAG, "toInstall: None available apk file, try to download, msg ==> " + e.getMessage());
|
|
|
+ Log.d(TAG, "toInstall: None available apk file, try to download, msg ==> " + e.getMessage());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -312,8 +264,8 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
|
|
|
appTaskDownloadWrapper.setDownloading(true);
|
|
|
Bundle bundle = new Bundle();
|
|
|
bundle.putBoolean("payload_downloading", true);
|
|
|
- notifyItem(appTaskDownloadWrapper, bundle);
|
|
|
- ATLog.d(TAG, "toDownload onStart: download start, task ==> " + appTaskDownloadWrapper);
|
|
|
+ notifyItemChanged(asyncListDiffer.getCurrentList().indexOf(appTaskDownloadWrapper), bundle);
|
|
|
+ Log.d(TAG, "toDownload onStart: download start, task ==> " + appTaskDownloadWrapper);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -325,105 +277,104 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
|
|
|
bundle.putBoolean("payload_downloading", true);
|
|
|
bundle.putLong("payload_progress", progress);
|
|
|
bundle.putLong("payload_total", total);
|
|
|
- notifyItem(appTaskDownloadWrapper, bundle);
|
|
|
+ notifyItemChanged(asyncListDiffer.getCurrentList().indexOf(appTaskDownloadWrapper), bundle);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onComplete(File file) {
|
|
|
appTaskDownloadWrapper.setDownloading(false);
|
|
|
- InstallFaultToleranceUtils.installApk(Utils.getContext(), file, null);
|
|
|
+ InstallFaultToleranceUtils.installApk(context, file, null);
|
|
|
addToCheckList(appTaskDownloadWrapper);
|
|
|
Bundle bundle = new Bundle();
|
|
|
bundle.putBoolean("payload_downloading", false);
|
|
|
- notifyItem(appTaskDownloadWrapper, bundle);
|
|
|
- ATLog.d(TAG, "toDownload onComplete: download complete, task ==> " + appTaskDownloadWrapper);
|
|
|
+ notifyItemChanged(asyncListDiffer.getCurrentList().indexOf(appTaskDownloadWrapper), bundle);
|
|
|
+ Log.d(TAG, "toDownload onComplete: download complete, task ==> " + appTaskDownloadWrapper);
|
|
|
AppTaskEvent.report(AppTaskEvent.ATE_DOWNLOAD_SUCCESS);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void onError(String message) {
|
|
|
appTaskDownloadWrapper.setDownloading(false);
|
|
|
- Toast.makeText(Utils.getContext(), "下载失败,请重试", Toast.LENGTH_SHORT).show();
|
|
|
+ Toast.makeText(context, "下载失败,请重试", Toast.LENGTH_SHORT).show();
|
|
|
Bundle bundle = new Bundle();
|
|
|
bundle.putBoolean("payload_downloading", false);
|
|
|
- notifyItem(appTaskDownloadWrapper, bundle);
|
|
|
- ATLog.d(TAG, "toDownload onError: download error, msg ==> " + message + ", task ==> " + appTaskDownloadWrapper);
|
|
|
+ notifyItemChanged(asyncListDiffer.getCurrentList().indexOf(appTaskDownloadWrapper), bundle);
|
|
|
+ Log.d(TAG, "toDownload onError: download error, msg ==> " + message + ", task ==> " + appTaskDownloadWrapper);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- private void notifyItem(AppTaskDownloadWrapper item, Bundle payload) {
|
|
|
- recyclerView.setHasFixedSize(true);
|
|
|
- int position = asyncListDiffer.getCurrentList().indexOf(item);
|
|
|
- notifyItemChanged(position, payload);
|
|
|
- }
|
|
|
-
|
|
|
private boolean checkFileOccupy(File downloadFile) {
|
|
|
return !downloadFile.renameTo(downloadFile);
|
|
|
}
|
|
|
|
|
|
public void update(ArrayList<AppTaskBean> appTaskBeans) {
|
|
|
- ATLog.d(TAG, "update() called with: appTaskBeans = [" + appTaskBeans + "]");
|
|
|
+ ArrayList<AppTaskDownloadWrapper> appTaskDownloadWrappers = new ArrayList<>();
|
|
|
Observable.fromIterable(appTaskBeans)
|
|
|
.filter(appTaskBean -> appTaskBean.getTaskStatus() != AppTaskBean.AppTaskStatus.Finish)
|
|
|
.map(AppTaskDownloadWrapper::new)
|
|
|
- .doOnNext(appTaskDownloadWrapper -> {
|
|
|
- if (RetrofitDownloader.isActiveTaskExist(appTaskDownloadWrapper.getAppLink())) {
|
|
|
- toDownload(appTaskDownloadWrapper);
|
|
|
- }
|
|
|
- })
|
|
|
- .toList()
|
|
|
.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
|
|
|
- .subscribe(new SingleObserver<List<AppTaskDownloadWrapper>>() {
|
|
|
+ .subscribe(new Observer<AppTaskDownloadWrapper>() {
|
|
|
@Override
|
|
|
- public void onSubscribe(Disposable d) {
|
|
|
+ public void onSubscribe(@atmob.io.reactivex.rxjava3.annotations.NonNull Disposable d) {
|
|
|
compositeDisposable.add(d);
|
|
|
- ATLog.d(TAG, "onSubscribe() called with: d = [" + d + "]");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void onSuccess(List<AppTaskDownloadWrapper> list) {
|
|
|
- ATLog.d(TAG, "onComplete: app task list update success, total length ==> " + list.size());
|
|
|
- Collections.reverse(list);
|
|
|
- submit(list);
|
|
|
+ public void onNext(@atmob.io.reactivex.rxjava3.annotations.NonNull AppTaskDownloadWrapper appTaskDownloadWrapper) {
|
|
|
+ if (RetrofitDownloader.isActiveTaskExist(appTaskDownloadWrapper.getAppLink())) {
|
|
|
+ toDownload(appTaskDownloadWrapper);
|
|
|
+ }
|
|
|
+ appTaskDownloadWrappers.add(appTaskDownloadWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(@atmob.io.reactivex.rxjava3.annotations.NonNull Throwable e) {
|
|
|
+ Log.e(TAG, "onError: AppTaskBean transform to AppTaskDownloadWrapper error", e);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void onError(Throwable e) {
|
|
|
- ATLog.e(TAG, "onError: AppTaskBean transform to AppTaskDownloadWrapper error", e);
|
|
|
+ public void onComplete() {
|
|
|
+ Log.d(TAG, "onComplete: app task list update success, total length ==> " + appTaskDownloadWrappers.size());
|
|
|
+ Collections.reverse(appTaskDownloadWrappers);
|
|
|
+ asyncListDiffer.submitList(appTaskDownloadWrappers);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
public void checkAppExist() {
|
|
|
- ATLog.d(TAG, "app resume, start checkAppExist()");
|
|
|
+ Log.d(TAG, "app resume, start checkAppExist()");
|
|
|
Completable.fromAction(() -> {
|
|
|
- Iterator<Map.Entry<Long, String>> iterator = potentialInstalledList.entrySet().iterator();
|
|
|
- while (iterator.hasNext()) {
|
|
|
- Map.Entry<Long, String> next = iterator.next();
|
|
|
- Long appTaskId = next.getKey();
|
|
|
- String appTaskPackageName = next.getValue();
|
|
|
- if (TextUtils.isEmpty(appTaskPackageName)) {
|
|
|
- iterator.remove();
|
|
|
- continue;
|
|
|
- }
|
|
|
- boolean existPackage = AppInfoUtils.isExistPackage(Utils.getContext(), appTaskPackageName);
|
|
|
- if (existPackage) {
|
|
|
- if (FORCE_OPEN) {
|
|
|
- ATLog.d(TAG, "checkAppExist: app exist, open it. (id ==> " + appTaskId + ", packageName ==> " + appTaskPackageName + ")");
|
|
|
- AppTaskEvent.report(AppTaskEvent.ATE_CONFIRM_INSTALLED_LAUNCH);
|
|
|
- AppInfoUtils.launchApp(appTaskPackageName);
|
|
|
- updateStatus(appTaskId, AppTaskBean.AppTaskStatus.Opened);
|
|
|
- } else {
|
|
|
- ATLog.d(TAG, "checkAppExist: app exist, just report. (id ==> " + appTaskId + ", packageName ==> " + appTaskPackageName + ")");
|
|
|
- updateStatus(appTaskId, AppTaskBean.AppTaskStatus.Installed);
|
|
|
+ Iterator<Map.Entry<Long, String>> iterator = potentialInstalledList.entrySet().iterator();
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ Map.Entry<Long, String> next = iterator.next();
|
|
|
+ Long appTaskId = next.getKey();
|
|
|
+ String appTaskPackageName = next.getValue();
|
|
|
+ if (TextUtils.isEmpty(appTaskPackageName)) {
|
|
|
+ iterator.remove();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ boolean existPackage = AppInfoUtils.isExistPackage(context, appTaskPackageName);
|
|
|
+ if (existPackage) {
|
|
|
+ if (FORCE_OPEN) {
|
|
|
+ Log.d(TAG, "checkAppExist: app exist, open it. (id ==> " + appTaskId + ", packageName ==> " + appTaskPackageName + ")");
|
|
|
+ AppTaskEvent.report(AppTaskEvent.ATE_CONFIRM_INSTALLED_LAUNCH);
|
|
|
+ AppInfoUtils.launchApp(context, appTaskPackageName);
|
|
|
+ if (onItemActionCallback != null) {
|
|
|
+ onItemActionCallback.updateStatus(appTaskId, AppTaskBean.AppTaskStatus.Opened);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Log.d(TAG, "checkAppExist: app exist, just report. (id ==> " + appTaskId + ", packageName ==> " + appTaskPackageName + ")");
|
|
|
+ if (onItemActionCallback != null) {
|
|
|
+ onItemActionCallback.updateStatus(appTaskId, AppTaskBean.AppTaskStatus.Installed);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ iterator.remove();
|
|
|
+ } else {
|
|
|
+ Log.d(TAG, "checkAppExist: app not exist. (id ==> " + appTaskId + ", packageName ==> " + appTaskPackageName + ")");
|
|
|
+ }
|
|
|
}
|
|
|
- iterator.remove();
|
|
|
- } else {
|
|
|
- ATLog.d(TAG, "checkAppExist: app not exist. (id ==> " + appTaskId + ", packageName ==> " + appTaskPackageName + ")");
|
|
|
- }
|
|
|
- }
|
|
|
- })
|
|
|
+ })
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
.observeOn(Schedulers.io())
|
|
|
.subscribe(new CompletableObserver() {
|
|
|
@@ -449,12 +400,12 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
|
|
|
return;
|
|
|
}
|
|
|
potentialInstalledList.put(appTaskBeanGet.getId(), appTaskBeanGet.getAppPkgName());
|
|
|
- ATLog.d(TAG, "addToCheckList() called with: appTaskBeanGet = [" + appTaskBeanGet + "]");
|
|
|
+ Log.d(TAG, "addToCheckList() called with: appTaskBeanGet = [" + appTaskBeanGet + "]");
|
|
|
}
|
|
|
|
|
|
private void removeFromCheckList(long id) {
|
|
|
potentialInstalledList.remove(id);
|
|
|
- ATLog.d(TAG, "removeFromCheckList() called with: id = [" + id + "]");
|
|
|
+ Log.d(TAG, "removeFromCheckList() called with: id = [" + id + "]");
|
|
|
}
|
|
|
|
|
|
public void setOnItemActionCallback(OnItemActionCallback onItemActionCallback) {
|
|
|
@@ -463,7 +414,8 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
|
|
|
|
|
|
public void release() {
|
|
|
RetrofitDownloader.clearAllObserver();
|
|
|
- ATLog.d(TAG, "release: clear all observer.");
|
|
|
+ viewHolderCreator = null;
|
|
|
+ Log.d(TAG, "release: clear all observer.");
|
|
|
}
|
|
|
|
|
|
public void triggerFirstTask() {
|
|
|
@@ -474,69 +426,115 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
|
|
|
}
|
|
|
if (appTaskDownloadWrapper.getTaskStatus() != AppTaskBean.AppTaskStatus.Opened && !appTaskDownloadWrapper.isDownloading()) {
|
|
|
onAppTaskItemClick(appTaskDownloadWrapper);
|
|
|
- ATLog.d(TAG, "triggerFirstTask: task ==> " + appTaskDownloadWrapper);
|
|
|
+ Log.d(TAG, "triggerFirstTask: task ==> " + appTaskDownloadWrapper);
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
- ATLog.d(TAG, "triggerFirstTask: not has available task.");
|
|
|
+ Log.d(TAG, "triggerFirstTask: not has available task.");
|
|
|
}
|
|
|
|
|
|
- private void updateStatus(long id, int targetStatus) {
|
|
|
- NetworkClient.api().updateAppTaskStatus(new AppTaskUpdateRequest(id, targetStatus))
|
|
|
- .compose(RxSchedulersUtils.observableIO2Main())
|
|
|
- .subscribe(new BaseHttpObserver<AppTaskUpdateResponse>() {
|
|
|
+ /**
|
|
|
+ * 列表内部通过这个类获取item必要组件
|
|
|
+ */
|
|
|
+ public abstract static class AppTaskItemViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
|
- @Override
|
|
|
- public void onGotDisposable(Disposable disposable) {
|
|
|
- compositeDisposable.add(disposable);
|
|
|
- }
|
|
|
+ public AppTaskItemViewHolder(@NonNull View itemView) {
|
|
|
+ super(itemView);
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
- public void onSuccess(AppTaskUpdateResponse data) {
|
|
|
- List<AppTaskDownloadWrapper> currentList = asyncListDiffer.getCurrentList();
|
|
|
- for (AppTaskDownloadWrapper wrapper : currentList) {
|
|
|
- if (wrapper == null) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (wrapper.getId() == id) {
|
|
|
- int i = currentList.indexOf(wrapper);
|
|
|
- wrapper.setTaskStatus(targetStatus);
|
|
|
- notifyItemChanged(i);
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
- ATLog.d(TAG, "app task status update success, id ==> " + id + ", target status ==> " + targetStatus);
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * @return 用于显示app名字的TextView
|
|
|
+ */
|
|
|
+ @NonNull
|
|
|
+ public abstract TextView getAppName();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return 用于显示app图标的ImageView
|
|
|
+ */
|
|
|
+ @NonNull
|
|
|
+ public abstract ImageView getAppIcon();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return 触发积分墙任务的按钮, 这里后面考虑换成View, 因为ui可能并不是以按钮的形式出现
|
|
|
+ */
|
|
|
+ @NonNull
|
|
|
+ public abstract TextView getTriggerBtn();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据不同的任务状态, 触发按钮上显示的文字提示
|
|
|
+ * <p>
|
|
|
+ * 如果触发按钮以后换成View, 这里需要看下怎么修改
|
|
|
+ *
|
|
|
+ * @param appTaskStatus 任务当前状态
|
|
|
+ * @return 文字提示
|
|
|
+ */
|
|
|
+ public abstract String getBtnText(@AppTaskBean.AppTaskStatus int appTaskStatus);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据不同的任务状态, 触发按钮的背景
|
|
|
+ * <p>
|
|
|
+ * 如果触发按钮以后换成View, 这里需要看下怎么修改
|
|
|
+ *
|
|
|
+ * @param appTaskStatus 任务当前状态
|
|
|
+ * @return {@link Drawable}
|
|
|
+ */
|
|
|
+ public abstract Drawable getBtnBackground(@AppTaskBean.AppTaskStatus int appTaskStatus, boolean isSpecial);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return 下载时用的进度条
|
|
|
+ */
|
|
|
+ @NonNull
|
|
|
+ public abstract NormalProgressBar getProgressBar();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * app图标的样式设置
|
|
|
+ *
|
|
|
+ * @return {@link AppIconConfig}
|
|
|
+ */
|
|
|
+ public AppIconConfig getAppIconConfig() {
|
|
|
+ return new AppIconConfig();
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
- public void onFailed(int code, String msg) {
|
|
|
- HttpFailManager.handleHttpFail(code, msg);
|
|
|
- ATLog.e(TAG, "app task status update failed, code ==> " + code + ", msg ==> " + msg);
|
|
|
- }
|
|
|
- });
|
|
|
+ public void onRenderReward(float radio) {
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public void setMaxLength(int length) {
|
|
|
- maxLength = length < 0 ? Integer.MAX_VALUE : length;
|
|
|
- if (getItemCount() == 0) {
|
|
|
- return;
|
|
|
+ public static class AppIconConfig {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 默认图标
|
|
|
+ */
|
|
|
+ @IntegerRes
|
|
|
+ private int placeholderResId;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 图标的圆角 in dip
|
|
|
+ */
|
|
|
+ private int round;
|
|
|
+
|
|
|
+ @SuppressWarnings("ResourceType")
|
|
|
+ public AppIconConfig() {
|
|
|
+ placeholderResId = R.drawable.atmob_default_icon;
|
|
|
+ round = 0;
|
|
|
}
|
|
|
- if (dataCache.size() == getItemCount() && getItemCount() < maxLength) {
|
|
|
- return;
|
|
|
+
|
|
|
+ public AppIconConfig placeholderResId(int placeholderResId) {
|
|
|
+ this.placeholderResId = placeholderResId;
|
|
|
+ return this;
|
|
|
}
|
|
|
- if (getItemCount() == maxLength) {
|
|
|
- return;
|
|
|
+
|
|
|
+ public AppIconConfig round(int round) {
|
|
|
+ this.round = round;
|
|
|
+ return this;
|
|
|
}
|
|
|
- submit(dataCache);
|
|
|
}
|
|
|
|
|
|
- private void submit(List<AppTaskDownloadWrapper> list) {
|
|
|
- dataCache = list;
|
|
|
- List<AppTaskDownloadWrapper> subList = dataCache.subList(0, Math.min(maxLength, dataCache.size()));
|
|
|
- if (subList.size() != asyncListDiffer.getCurrentList().size()) {
|
|
|
- recyclerView.setHasFixedSize(false);
|
|
|
- }
|
|
|
- asyncListDiffer.submitList(subList);
|
|
|
+ /**
|
|
|
+ * 用于构建积分墙item, see{@link AppTaskItemViewHolder}
|
|
|
+ */
|
|
|
+ public interface ViewHolderCreator {
|
|
|
+ AppTaskItemViewHolder onCreateViewHolder(LayoutInflater layoutInflater, ViewGroup parent);
|
|
|
}
|
|
|
|
|
|
private static class DiffCallback extends DiffUtil.ItemCallback<AppTaskDownloadWrapper> {
|
|
|
@@ -591,6 +589,8 @@ public class AppTaskItemAdapter<T extends AppTaskViewHolder> extends RecyclerVie
|
|
|
public interface OnItemActionCallback {
|
|
|
void onClick(AppTaskBean appTaskBean);
|
|
|
|
|
|
+ void updateStatus(long id, int targetStatus);
|
|
|
+
|
|
|
void getReward(AppTaskBean appTaskBean);
|
|
|
}
|
|
|
}
|