228 lines
8.5 KiB
Java
228 lines
8.5 KiB
Java
package com.gh.gamecenter;
|
||
|
||
import android.content.Intent;
|
||
import android.os.Bundle;
|
||
import android.support.v4.util.ArrayMap;
|
||
import android.support.v7.widget.LinearLayoutManager;
|
||
import android.support.v7.widget.RecyclerView;
|
||
import android.text.Html;
|
||
import android.view.View;
|
||
import android.widget.*;
|
||
import butterknife.BindView;
|
||
import butterknife.OnClick;
|
||
import com.gh.base.BaseActivity;
|
||
import com.gh.common.util.DialogUtils;
|
||
import com.gh.common.util.Utils;
|
||
import com.gh.common.view.VerticalItemDecoration;
|
||
import com.gh.gamecenter.adapter.CleanApkAdapter;
|
||
import com.gh.gamecenter.entity.InstallGameEntity;
|
||
import com.gh.gamecenter.eventbus.EBSkip;
|
||
import de.greenrobot.event.EventBus;
|
||
|
||
import java.io.File;
|
||
import java.text.DecimalFormat;
|
||
import java.util.List;
|
||
|
||
/**
|
||
* Created by khy on 2017/1/24.
|
||
*/
|
||
public class CleanApkActivity extends BaseActivity implements CleanApkAdapter.OnScanListener {
|
||
|
||
@BindView(R.id.apk_count)
|
||
TextView mApkCount;
|
||
@BindView(R.id.apk_rv)
|
||
RecyclerView mApkRv;
|
||
@BindView(R.id.delete_btn)
|
||
TextView mApkDeleteBtn;
|
||
@BindView(R.id.select_game_all)
|
||
CheckBox mApkSelectAll;
|
||
@BindView(R.id.scan_pb)
|
||
ProgressBar mScanPb;
|
||
@BindView(R.id.reuse_nodata_skip_tv_hint)
|
||
TextView mNodataSkipTv;
|
||
@BindView(R.id.reuse_nodata_skip_tv_btn)
|
||
TextView mNodataSkipBtn;
|
||
@BindView(R.id.reuse_nodata_skip)
|
||
LinearLayout mNodataSkipLl;
|
||
@BindView(R.id.delete_bottom)
|
||
LinearLayout mDeleteBottom;
|
||
@BindView(R.id.select_game_installed_ll)
|
||
RelativeLayout mInstallAllData;
|
||
|
||
private CleanApkAdapter mAdapter;
|
||
|
||
@Override
|
||
protected void onCreate(Bundle savedInstanceState) {
|
||
super.onCreate(savedInstanceState);
|
||
View contentView = View.inflate(this, R.layout.activity_clean_apk, null);
|
||
init(contentView, "安装包清理");
|
||
|
||
mNodataSkipLl.setVisibility(View.GONE);
|
||
|
||
mAdapter = new CleanApkAdapter(this, mApkDeleteBtn, mApkSelectAll);
|
||
mApkRv.setLayoutManager(new LinearLayoutManager(this));
|
||
mApkRv.addItemDecoration(new VerticalItemDecoration(this, 8, true));
|
||
mApkRv.setAdapter(mAdapter);
|
||
|
||
mApkSelectAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||
@Override
|
||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||
ArrayMap<Integer, Boolean> selectPosition = new ArrayMap<>();
|
||
ArrayMap<Integer, Boolean> selectPosition1 = mAdapter.getSelectPosition();
|
||
int itemCount = mAdapter.getItemCount();
|
||
if (isChecked) {
|
||
for (int i = 0; i < itemCount; i++) {
|
||
selectPosition.put(i, true);
|
||
}
|
||
} else {
|
||
int selectCount = 0;
|
||
for (Integer integer : selectPosition1.keySet()) {
|
||
if (selectPosition1.get(integer)) {
|
||
selectCount++;
|
||
}
|
||
}
|
||
|
||
if (selectCount != itemCount) {
|
||
return;
|
||
}
|
||
|
||
for (int i = 0; i < itemCount; i++) {
|
||
selectPosition.put(i, false);
|
||
}
|
||
}
|
||
|
||
mAdapter.setSelectPosition(selectPosition);
|
||
mAdapter.notifyItemRangeChanged(0, itemCount);
|
||
mAdapter.checkBoxControl(isChecked, 0);
|
||
}
|
||
});
|
||
|
||
mNodataSkipBtn.setText("去首页看看");
|
||
mNodataSkipTv.setText("安装包已清理干净\n快去发现更多好玩的游戏吧!");
|
||
mNodataSkipTv.setLineSpacing(0, 1.5f);
|
||
|
||
}
|
||
|
||
@OnClick({R.id.delete_btn, R.id.reuse_nodata_skip_tv_btn})
|
||
public void onClick(View view) {
|
||
switch (view.getId()) {
|
||
case R.id.delete_btn: {
|
||
String s = mApkDeleteBtn.getText().toString();
|
||
if ("停止扫描".equals(s)) {
|
||
mAdapter.isStopScan();
|
||
} else {
|
||
if (s.equals("一键删除")) {
|
||
Utils.toast(CleanApkActivity.this, "请选择需要删除的安装包");
|
||
return;
|
||
}
|
||
|
||
final List<InstallGameEntity> apkList = mAdapter.getApkList();
|
||
final ArrayMap<Integer, Boolean> selectPosition = mAdapter.getSelectPosition();
|
||
|
||
for (int i = 0; i < apkList.size(); i++) {
|
||
if (selectPosition.get(i) && apkList.get(i).getInstallStatus() == 1) {
|
||
DialogUtils.showWarningDialog(CleanApkActivity.this, "删除安装包"
|
||
, Html.fromHtml("你选择的安装包中包含" + "<font color=\"#ff0000\">未安装</font>"
|
||
+ "的应用,确定删除吗?"), "取消 ", "确定"
|
||
, new DialogUtils.ConfiremListener() {
|
||
@Override
|
||
public void onConfirem() {
|
||
deleteApk(apkList, selectPosition);
|
||
}
|
||
}, null);
|
||
return;
|
||
}
|
||
}
|
||
|
||
deleteApk(apkList, selectPosition);
|
||
}
|
||
break;
|
||
}
|
||
case R.id.reuse_nodata_skip_tv_btn: {
|
||
Intent intent = new Intent(CleanApkActivity.this, MainActivity.class);
|
||
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||
startActivity(intent);
|
||
new Thread() {
|
||
@Override
|
||
public void run() {
|
||
try {
|
||
sleep(300);
|
||
} catch (InterruptedException e) {
|
||
e.printStackTrace();
|
||
}
|
||
EventBus.getDefault().post(new EBSkip("GameFragment", 0));
|
||
}
|
||
}.start();
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
private void deleteApk(List<InstallGameEntity> apkList, ArrayMap<Integer, Boolean> selectPosition) {
|
||
long size = 0;
|
||
for (int i = 0; i < apkList.size(); i++) {
|
||
if (selectPosition.get(i)) {
|
||
InstallGameEntity installGameEntity = apkList.get(i);
|
||
size = size + installGameEntity.getGameSize();
|
||
File file = new File(installGameEntity.getGamePath());
|
||
if (file.isFile() && file.exists()) {
|
||
file.delete();
|
||
mAdapter.deleteApk(i);
|
||
i--;
|
||
}
|
||
}
|
||
}
|
||
|
||
double allSize = (((float) size / 1024) / 1024);
|
||
DecimalFormat df = new DecimalFormat("#.00");
|
||
String sizeName = df.format(allSize) + "MB";
|
||
Utils.toast(CleanApkActivity.this, "删除成功,已为您节省" + sizeName + "空间");
|
||
mApkDeleteBtn.setText("一键删除");
|
||
mAdapter.notifyDataSetChanged(); // 刷新Adapter position
|
||
|
||
updateScanData();
|
||
|
||
if (apkList.size() == 0) { // 全部删除
|
||
mNodataSkipLl.setVisibility(View.VISIBLE);
|
||
mInstallAllData.setVisibility(View.GONE);
|
||
mDeleteBottom.setVisibility(View.GONE);
|
||
}
|
||
}
|
||
|
||
private void updateScanData() {
|
||
long allSize = 0;
|
||
List<InstallGameEntity> apkList = mAdapter.getApkList();
|
||
for (InstallGameEntity installGameEntity : apkList) {
|
||
allSize = allSize + installGameEntity.getGameSize();
|
||
}
|
||
|
||
double size = (((float) allSize / 1024) / 1024);
|
||
DecimalFormat df = new DecimalFormat("0.00");
|
||
String sizeName = df.format(size) + "MB";
|
||
|
||
mApkCount.setText(Html.fromHtml("找到" + apkList.size() + "个安装包,占用"
|
||
+ "<font color=\"#ff4147\">" + sizeName + "</font>" + "空间"));
|
||
}
|
||
|
||
@Override
|
||
public void onScanOver() {
|
||
mApkDeleteBtn.setText("立即删除");
|
||
mApkDeleteBtn.setBackgroundResource(R.drawable.game_item_btn_red_style);
|
||
|
||
updateScanData();
|
||
|
||
mScanPb.setVisibility(View.GONE);
|
||
mApkSelectAll.setVisibility(View.VISIBLE);
|
||
mApkSelectAll.setChecked(true);
|
||
}
|
||
|
||
@Override
|
||
public void noData() {
|
||
mNodataSkipBtn.setText("去首页看看");
|
||
mNodataSkipTv.setText("暂无安装包");
|
||
mNodataSkipLl.setVisibility(View.VISIBLE);
|
||
mInstallAllData.setVisibility(View.GONE);
|
||
mDeleteBottom.setVisibility(View.GONE);
|
||
}
|
||
}
|