359 lines
12 KiB
Java
359 lines
12 KiB
Java
package com.gh.gamecenter.adapter;
|
||
|
||
import android.content.pm.*;
|
||
import android.graphics.Color;
|
||
import android.graphics.drawable.Drawable;
|
||
import android.os.Environment;
|
||
import android.os.Handler;
|
||
import android.support.v4.util.ArrayMap;
|
||
import android.view.View;
|
||
import android.view.ViewGroup;
|
||
import android.widget.CheckBox;
|
||
import android.widget.TextView;
|
||
import com.gh.common.util.*;
|
||
import com.gh.gamecenter.CleanApkActivity;
|
||
import com.gh.gamecenter.R;
|
||
import com.gh.gamecenter.adapter.viewholder.KcSelectGameViewHolder;
|
||
import com.gh.gamecenter.entity.InstallGameEntity;
|
||
|
||
import java.io.File;
|
||
import java.text.DecimalFormat;
|
||
import java.util.*;
|
||
|
||
/**
|
||
* Created by khy on 2017/1/24.
|
||
*/
|
||
public class CleanApkAdapter extends BaseRecyclerAdapter<KcSelectGameViewHolder> {
|
||
|
||
private static int INSTALLED = 0; // 表示已经安装
|
||
private static int UNINSTALLED = 1; // 表示未安装
|
||
private CheckBox mApkSelectAll;
|
||
private TextView mApkDeleteBtn;
|
||
private List<InstallGameEntity> mApkList;
|
||
private OnScanListener mScanListener;
|
||
private ArrayMap<Integer, Boolean> mSelectPosition;
|
||
private List<String> mApkPath;
|
||
private boolean mIsStopScan;
|
||
|
||
private boolean mIsScanOver;
|
||
|
||
private Handler mHandler = new Handler();
|
||
|
||
public CleanApkAdapter(CleanApkActivity activity, TextView apkDeleteBtn, CheckBox apkSelectAll) {
|
||
super(activity);
|
||
this.mContext = activity;
|
||
this.mApkDeleteBtn = apkDeleteBtn;
|
||
this.mApkSelectAll = apkSelectAll;
|
||
|
||
mScanListener = activity;
|
||
|
||
mApkList = new ArrayList<>();
|
||
mSelectPosition = new ArrayMap<>();
|
||
mApkPath = new ArrayList<>();
|
||
mIsStopScan = false;
|
||
mIsScanOver = false;
|
||
|
||
init();
|
||
}
|
||
|
||
private void init() {
|
||
new Thread(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
// 扫描和获取apk数据 分步操作 尽量避免 StackoverflowError
|
||
FindAllAPKPath(Environment.getExternalStorageDirectory());
|
||
LoadApkData();
|
||
|
||
mHandler.post(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
mIsScanOver = true;
|
||
mScanListener.onScanOver();
|
||
if (mApkList.size() == 0) {
|
||
mScanListener.noData();
|
||
}
|
||
notifyItemRangeChanged(0, getItemCount());
|
||
}
|
||
});
|
||
}
|
||
}).start();
|
||
}
|
||
|
||
/**
|
||
* 根据apk文件路径检查可用的apk和获取apk数据
|
||
*/
|
||
private void LoadApkData() {
|
||
for (String apk_path : mApkPath) {
|
||
InstallGameEntity apkEntity = new InstallGameEntity();
|
||
PackageManager pm = mContext.getApplicationContext().getPackageManager();
|
||
PackageInfo packageInfo = pm.getPackageArchiveInfo(apk_path, PackageManager.GET_ACTIVITIES);
|
||
if (packageInfo == null) continue;
|
||
|
||
ApplicationInfo appInfo = packageInfo.applicationInfo;
|
||
|
||
|
||
/**获取apk的图标 */
|
||
appInfo.sourceDir = apk_path;
|
||
appInfo.publicSourceDir = apk_path;
|
||
Drawable apk_icon = appInfo.loadIcon(pm);
|
||
apkEntity.setGameBm(BitmapUtils.drawableToBitmap(apk_icon));
|
||
/** apk的绝对路劲 */
|
||
apkEntity.setGamePath(apk_path);
|
||
/** apk的版本名称 String */
|
||
String versionName = packageInfo.versionName;
|
||
apkEntity.setGameVersion(versionName);
|
||
|
||
String apkName = packageInfo.applicationInfo.loadLabel(pm).toString();
|
||
apkEntity.setGameName(apkName);
|
||
|
||
File file = new File(apk_path);
|
||
apkEntity.setGameSize(file.length());
|
||
|
||
/**安装处理类型*/
|
||
/** 得到包名 */
|
||
String packageName = packageInfo.packageName;
|
||
int type = doType(pm, packageName);
|
||
apkEntity.setInstallStatus(type);
|
||
|
||
mApkList.add(apkEntity);
|
||
mSelectPosition.put((mApkList.size() - 1), false);
|
||
|
||
mHandler.post(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
notifyItemChanged((mApkList.size() - 1));
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 扫描内存卡 获取apk文件路径
|
||
*
|
||
* @param file 文件路径
|
||
*/
|
||
public void FindAllAPKPath(File file) {
|
||
|
||
if (mIsStopScan) {
|
||
return;
|
||
}
|
||
if (file.isFile()) {
|
||
// Utils.log("===== 是文件" + file.getAbsolutePath().toString());
|
||
String name_s = file.getName();
|
||
String apk_path = null;
|
||
if (name_s.toLowerCase().endsWith(".apk")) {
|
||
apk_path = file.getAbsolutePath();// apk文件的绝对路劲
|
||
mApkPath.add(apk_path);
|
||
}
|
||
} else {
|
||
// Utils.log("===== 是文件夹" + file.getAbsolutePath().toString());
|
||
File[] files = file.listFiles();
|
||
if (files != null && files.length > 0) {
|
||
for (File file_str : files) {
|
||
FindAllAPKPath(file_str);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private int doType(PackageManager pm, String packageName) {
|
||
List<PackageInfo> pakageinfos = pm.getInstalledPackages(0);
|
||
for (PackageInfo pi : pakageinfos) {
|
||
if ((pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
|
||
String pi_packageName = pi.packageName;
|
||
//如果这个包名在系统已经安装过的应用中存在
|
||
if (packageName.endsWith(pi_packageName)) {
|
||
return INSTALLED;
|
||
}
|
||
}
|
||
}
|
||
return UNINSTALLED;
|
||
}
|
||
|
||
|
||
@Override
|
||
public KcSelectGameViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||
View view = mLayoutInflater.inflate(R.layout.kc_game_select_item, parent, false);
|
||
return new KcSelectGameViewHolder(view);
|
||
}
|
||
|
||
@Override
|
||
public void onBindViewHolder(final KcSelectGameViewHolder holder, final int position) {
|
||
final InstallGameEntity gameEntity = mApkList.get(position);
|
||
// // 第一个
|
||
// if (position == 0) {
|
||
// ((CardLinearLayout) holder.itemView).setmTop(cardMargin);
|
||
// } else {
|
||
// ((CardLinearLayout) holder.itemView).setmTop(0);
|
||
// }
|
||
//
|
||
// // 最后一个
|
||
// if (position == mApkList.size() - 1) {
|
||
// ((CardLinearLayout) holder.itemView).setBottom(true);
|
||
// } else {
|
||
// ((CardLinearLayout) holder.itemView).setBottom(false);
|
||
// }
|
||
|
||
double size = (((float) gameEntity.getGameSize() / 1024) / 1024);
|
||
DecimalFormat df = new DecimalFormat("#.00");
|
||
String sizeName = df.format(size) + "MB";
|
||
|
||
// Spanned spanned = Html.fromHtml("大小:" + "<font color=\"#00B7FA\">" + sizeName + "</font>");
|
||
// holder.gameSize.setText(spanned);
|
||
holder.checkBoxRl.setVisibility(View.VISIBLE);
|
||
holder.selectCB.setVisibility(View.VISIBLE);
|
||
if (mSelectPosition.get(position)) {
|
||
holder.selectCB.setChecked(true);
|
||
} else {
|
||
holder.selectCB.setChecked(false);
|
||
}
|
||
|
||
Utils.log(gameEntity.getGamePath() + "=========" + gameEntity.getGameName());
|
||
holder.gameNameAndSize.setText(gameEntity.getGameName());
|
||
holder.gameDes.setText("版本:V" + gameEntity.getGameVersion() + " | " + sizeName);
|
||
holder.gameDes.setTextColor(mContext.getResources().getColor(R.color.content));
|
||
|
||
holder.gameThumb.setImageBitmap(gameEntity.getGameBm());
|
||
|
||
if (gameEntity.getInstallStatus() == INSTALLED) {
|
||
holder.gameSize.setText("已安装");
|
||
holder.gameSize.setTextColor(mContext.getResources().getColor(R.color.theme));
|
||
} else {
|
||
holder.gameSize.setText("未安装");
|
||
holder.gameSize.setTextColor(Color.parseColor("#ff4147"));
|
||
}
|
||
|
||
if (mIsScanOver) {
|
||
holder.selectCB.setEnabled(true);
|
||
} else {
|
||
holder.selectCB.setEnabled(false);
|
||
}
|
||
|
||
holder.checkBoxRl.setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
if (mSelectPosition.get(position)) {
|
||
checkBoxControl(false, position);
|
||
holder.selectCB.setChecked(false);
|
||
} else {
|
||
checkBoxControl(true, position);
|
||
holder.selectCB.setChecked(true);
|
||
}
|
||
}
|
||
});
|
||
|
||
holder.selectCB.setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
if (mSelectPosition.get(position)) {
|
||
checkBoxControl(false, position);
|
||
holder.selectCB.setChecked(false);
|
||
} else {
|
||
checkBoxControl(true, position);
|
||
holder.selectCB.setChecked(true);
|
||
}
|
||
}
|
||
});
|
||
|
||
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
||
@Override
|
||
public void onClick(View v) {
|
||
if (mIsScanOver) {
|
||
PackageUtils.launchSetup(mContext, gameEntity.getGamePath());
|
||
}
|
||
}
|
||
});
|
||
|
||
}
|
||
|
||
public void checkBoxControl(boolean isChecked, int position) {
|
||
if (isChecked) {
|
||
mSelectPosition.put(position, true);
|
||
} else {
|
||
mSelectPosition.put(position, false);
|
||
}
|
||
|
||
long gameSize = 0;
|
||
int i = 0;
|
||
|
||
if (mApkList.size() == 0) return;
|
||
for (Integer integer : mSelectPosition.keySet()) {
|
||
if (mSelectPosition.get(integer)) {
|
||
gameSize = gameSize + mApkList.get(integer).getGameSize();
|
||
i++;
|
||
}
|
||
}
|
||
|
||
if (i == mApkList.size()) {
|
||
mApkSelectAll.setChecked(true);
|
||
} else {
|
||
mApkSelectAll.setChecked(false);
|
||
}
|
||
|
||
if (gameSize == 0) {
|
||
mApkDeleteBtn.setText("一键删除");
|
||
} else {
|
||
double size = (((float) gameSize / 1024) / 1024);
|
||
DecimalFormat df = new DecimalFormat("0.00");
|
||
String sizeName = df.format(size) + "MB";
|
||
mApkDeleteBtn.setText("一键删除(" + i + "个,释放" + sizeName + ")");
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public int getItemCount() {
|
||
return mApkList.size();
|
||
}
|
||
|
||
public List<InstallGameEntity> getApkList() {
|
||
return mApkList;
|
||
}
|
||
|
||
public ArrayMap<Integer, Boolean> getSelectPosition() {
|
||
return mSelectPosition;
|
||
}
|
||
|
||
public void setSelectPosition(ArrayMap<Integer, Boolean> selectPosition) {
|
||
this.mSelectPosition = selectPosition;
|
||
}
|
||
|
||
public void isStopScan() {
|
||
this.mIsStopScan = true;
|
||
}
|
||
|
||
public void deleteApk(int position) {
|
||
mApkList.remove(position);
|
||
|
||
Iterator<Map.Entry<Integer, Boolean>> iterator = mSelectPosition.entrySet().iterator();
|
||
while (iterator.hasNext()) {
|
||
Map.Entry<Integer, Boolean> entry = iterator.next();
|
||
Integer key = entry.getKey();
|
||
|
||
if (key == position) {
|
||
iterator.remove();
|
||
}
|
||
}
|
||
|
||
ArrayMap<Integer, Boolean> newMap = new ArrayMap<>(mSelectPosition);
|
||
for (Integer integer : newMap.keySet()) {
|
||
if (integer > position) {
|
||
int newPosition = integer - 1;
|
||
mSelectPosition.put(newPosition, mSelectPosition.get(integer));
|
||
}
|
||
}
|
||
|
||
if (mSelectPosition.size() > mApkList.size()) {
|
||
mSelectPosition.removeAt(mSelectPosition.size() - 1);
|
||
}
|
||
|
||
notifyItemRemoved(position);
|
||
}
|
||
|
||
public interface OnScanListener {
|
||
void onScanOver();
|
||
|
||
void noData();
|
||
}
|
||
|
||
}
|