1. 说明
存储池是存储池分区及文件操作的根基,只有存储池存在,才能操作存储池分区和文件夹,是数据存储的地方。
只要有闲置的存储池(硬盘),我们就可以把它添加到现有的存储池或者是新的存储池,然后回到存储池列表就可以看到刷新的数据。
除了添加和查看存储池之外,我们还可以修改存储池的名称、删除存储和添加存储池分区、查看存储池分区、辑存储池分区及删除存储池分区(存储池分区的操作将会在下一章节进行说明)。
由于删除存储池需要一定的时间,所以存在存储池删除中的状态;删除存储池不一定能删除成功,所以存在删除存储池失败的状态。
注:只有家庭的拥有者才能操作存储池
1.1主要代码实现
1.2存储池列表和存储详情的model类
/**
* 存储池列表
*/
public class StoragePoolListBean {
private List list; // tem为object,存储池
private PagerBean pager; // 分页数据
...
}
/**
* 存储池详情
*/
public class StoragePoolDetailBean implements MultiItemEntity, Serializable {
public static final int POOL = 1; // 自己定义字段添加存储池时是存储
public static final int ADD = 2; // 自己定义字段添加存储池时是最后的加号图片
private int itemType; // 自己定义字段,用于区分添加存储池时是存储还是最后的加号图片
private boolean selected; // 自己定义字段,用于表示存储添加存储池时是否选择
private String id;
private String name; // 名称
private double capacity; // 容量
private double use_capacity; //已用容量
/**
* // 异步任务状态, 为空则没有异步任务,
* TaskDelPool_0删除存储池失败,TaskDelPool_1删除存储池中,
* TaskAddPool_0添加存储池失败,TaskAddPool_1添加存储池中,
* TaskUpdatePool_0修改存储池失败,TaskUpdatePool_1修改存储池中,
*/
private String status;
private String task_id; //异步任务ID
private List pv; // 物理分区:就是所谓硬盘
private List lv; //逻辑分区:实际存储池分区
...
}
/**
* 硬盘和存储池分区
*/
public class DiskBean implements Serializable {
/**
* id : 4255
* name : 安但风张义
* capacity : 91.2
*/
private String id;
private String name;
private long capacity;
private long use_capacity; // 已用容量
private boolean selected;
/**
* 为空则没有任务状态,
* TaskAddPartition_1添加存储池分区中,TaskAddPartition_0添加存储池分区失败,
* TaskUpdatePartition_1修改存储池分区中,TaskUpdatePartition_0修改存储池分区失败,
* TaskDelPartition_1删除存储池分区中,TaskDelPartition_0删除存储池分区失败
*/
private String status;
private String task_id; // 异步任务id
...
}
2.2 存储池列表及闲置硬盘
/**
* 存储管理(存储池列表)
*/
public class StoragePoolListActivity extends BaseMVPDBActivity implements StoragePoolListContract.View {
...
/**
* 闲置硬盘
*/
private void initRvDisk(){
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(RecyclerView.HORIZONTAL);
binding.rvDisk.setLayoutManager(layoutManager);
diskAdapter = new DiskAdapter();
binding.rvDisk.setAdapter(diskAdapter);
diskAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
DiskBean diskBean = diskAdapter.getItem(position);
if (view.getId() == R.id.tvAdd){
Bundle bundle = new Bundle();
bundle.putString("diskName", diskBean.getName());
bundle.putLong("capacity", diskBean.getCapacity());
switchToActivity(AddToStoragePoolActivity.class, bundle);
}
}
});
}
/**
* 存储池
*/
private void initRvPool(){
...
storagePoolAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
StoragePoolDetailBean storagePoolDetailBean = storagePoolAdapter.getItem(position);
if (TextUtils.isEmpty(storagePoolDetailBean.getStatus())) { // 没有异步状态才可跳转
Bundle bundle = new Bundle();
bundle.putString("name", storagePoolDetailBean.getName());
switchToActivity(StoragePoolDetailActivity.class, bundle);
}
}
});
storagePoolAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
StoragePoolDetailBean storagePoolDetailBean = storagePoolAdapter.getItem(position);
int viewId = view.getId();
if (viewId == R.id.ivDot){ // 显示物理硬盘个数
showHardDiskDialog(storagePoolAdapter.getItem(position).getPv());
}else if (viewId == R.id.tvRetry){ // 重试
String status = storagePoolDetailBean.getStatus();
if (status!=null){
if (status.equals(Constant.STORAGE_POOL_DELETE_FAIL)){ // 删除失败
mPresenter.restartTask(Constant.scope_token, storagePoolDetailBean.getTask_id());
}
}
}
}
});
}
...
}
public class StoragePoolListPresenter extends BasePresenter implements StoragePoolListContract.Presenter {
...
/**
* 获取存储池列表
* @param scopeToken
* @param map
* @param showLoading
*/
@Override
public void getStoragePools(String scopeToken, Map map, boolean showLoading) {
executeObservable(mModel.getStoragePools(scopeToken, map), new RequestDataCallback(showLoading) {
@Override
public void onSuccess(StoragePoolListBean response) {
super.onSuccess(response);
if (mView!=null){
mView.getStoragePoolsSuccess(response);
}
}
@Override
public void onFailed(int errorCode, String errorMessage) {
super.onFailed(errorCode, errorMessage);
if (mView!=null){
mView.getStoragePoolsFail(errorCode, errorMessage);
}
}
});
}
/**
* 闲置硬盘列表
* @param scopeToken
*/
@Override
public void getDisks(String scopeToken) {
executeObservable(mModel.getDisks(scopeToken), new RequestDataCallback(false) {
@Override
public void onSuccess(DiskListBean response) {
super.onSuccess(response);
if (mView!=null){
mView.getDisksSuccess(response);
}
}
@Override
public void onFailed(int errorCode, String errorMessage) {
super.onFailed(errorCode, errorMessage);
if (mView!=null){
mView.getDisksFail(errorCode, errorMessage);
}
}
});
}
...
}
2.4 添加到存储池
...
/**
* 创建存储池
* @param scopeToken
* @param createStoragePoolRequest
*/
@Override
public void createStoragePool(String scopeToken, CreateStoragePoolRequest createStoragePoolRequest) {
executeObservable(mModel.createStoragePool(scopeToken, createStoragePoolRequest), new RequestDataCallback
}
public class AddToStoragePoolPresenter extends BasePresenter implements AddToStoragePoolContract.Presenter {
...
/**
* 创建存储池
* @param scopeToken
* @param createStoragePoolRequest
*/
@Override
public void createStoragePool(String scopeToken, CreateStoragePoolRequest createStoragePoolRequest) {
executeObservable(mModel.createStoragePool(scopeToken, createStoragePoolRequest), new RequestDataCallback() {
@Override
public void onSuccess(Object response) {
super.onSuccess(response);
if (mView!=null){
mView.createStoragePoolSuccess();
}
}
@Override
public void onFailed(int errorCode, String errorMessage) {
super.onFailed(errorCode, errorMessage);
if (mView!=null){
mView.createStoragePoolFail(errorCode, errorMessage);
}
}
});
}
/**
* 添加硬盘到存储池
* @param scopeToken
* @param addStoragePoolRequest
*/
@Override
public void addToStoragePool(String scopeToken, AddStoragePoolRequest addStoragePoolRequest) {
executeObservable(mModel.addToStoragePool(scopeToken, addStoragePoolRequest), new RequestDataCallback() {
@Override
public void onSuccess(Object response) {
super.onSuccess(response);
if (mView!=null){
mView.addToStoragePoolSuccess();
}
}
@Override
public void onFailed(int errorCode, String errorMessage) {
super.onFailed(errorCode, errorMessage);
if (mView!=null){
mView.addToStoragePoolFail(errorCode, errorMessage);
}
}
});
}
}
2.5 存储池详情
/**
* 存储池详情
*/
public class StoragePoolDetailActivity extends BaseMVPDBActivity implements StoragePoolDetailContract.View {
...
/**
* 获取存储池详情成功
* @param storagePoolDetailBean
*/
@Override
public void getStoragePoolDetailSuccess(StoragePoolDetailBean storagePoolDetailBean) {
binding.refreshLayout.finishRefresh();
if (storagePoolDetailBean!=null){
mStoragePoolDetailBean = storagePoolDetailBean;
poolId = storagePoolDetailBean.getId();
binding.tvName.setText(storagePoolDetailBean.getName());
double usedCapacity = storagePoolDetailBean.getUse_capacity();
double allCapacity = storagePoolDetailBean.getCapacity();
double availableCapacity = allCapacity - usedCapacity;
binding.tvSeparable.setText(FileUtil.getReadableFileSize(availableCapacity)+UiUtil.getString(R.string.mine_separable_capacity));
binding.tvAll.setText(FileUtil.getReadableFileSize(allCapacity)+UiUtil.getString(R.string.mine_all_capacity));
List hardDisk = storagePoolDetailBean.getPv(); // 硬盘列表
int hardDiskCount = CollectionUtil.isNotEmpty(hardDisk) ? hardDisk.size() : 0; // 硬盘个数
binding.tvCount.setText(StringUtil.getStringFormat(UiUtil.getString(R.string.mine_hard_disk_count), hardDiskCount));
int progress = (int) ((usedCapacity/allCapacity)*100); // 容量进度百分比
isSystemPool = storagePoolDetailBean.getName().equals(Constant.SYSTEM_POOL); // 是否是系统存储池
binding.tvDel.setVisibility(isSystemPool ? View.GONE : View.VISIBLE); // 是系统存储池可删除
binding.tvName.setCompoundDrawablesWithIntrinsicBounds(0, 0, isSystemPool ? 0 : R.drawable.icon_white_edit, 0); // 是系统存储池不可编辑
binding.tvCount.setCompoundDrawablesWithIntrinsicBounds(0, 0, isSystemPool ? 0 : R.drawable.icon_white_right_arrow, 0); // 是系统存储池查看磁盘不可操作
binding.tvCount.setEnabled(!isSystemPool);// 是系统存储池查看磁盘不可操作
binding.ivAdd.setVisibility(isSystemPool ? View.GONE : View.VISIBLE); // 是系统存储池不可添加存储分区
binding.rb.setProgress(progress);
storagePoolDetailAdapter.setNewData(storagePoolDetailBean.getLv());
setNullView(CollectionUtil.isEmpty(storagePoolDetailBean.getLv()));
binding.coordinatorLayout.setVisibility(View.VISIBLE);
}
}
...
}