1.说明
存储池分区是在某个存储池建立的分区,是创建文件的根基之一,因为文件夹的创建是建立在存储池和存储池分区的基础上,可以说存储池和存储池分区时文件夹/文件的载体。
处理系统存储池下的存储池分区之外,我们都可以对存储池分区进行增删改操作和不一定可以操作成功。
同样,添加、编辑和删除存储池需要一定时间,所以存储池分区存在添加中、修改中、删除中、添加失败、修改失败和删除失败的状态。
如果添加或者修改失败,我们可以重试或者取消添加和修改;
如果删除失败的话,我们只能进行重试操作。
添加/编辑存储池分区时,容量单位只能选择MB、GB和TB,在单位选择MB时,容量必须时4的倍数。
注:由于存储池分区在存储池的子级,所以存储池分区也是只有家庭拥有者才能操作
2. 主要代码实现
/**
* 硬盘和存储池分区
*/
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
...
}
3.存储池分区列表适配器
/**
* 存储池详情的存储池分区列表
*/
public class StoragePoolDetailAdapter extends BaseQuickAdapter<DiskBean, BaseViewHolder> {
public StoragePoolDetailAdapter() {
super(R.layout.item_storage_pool_detail);
}
@Override
protected void convert(BaseViewHolder helper, DiskBean item) {
helper.addOnClickListener(R.id.tvRetry);
helper.addOnClickListener( R.id.tvCancel);
String capacity = FileUtil.getReadableFileSize(item.getUse_capacity())+"/"+FileUtil.getReadableFileSize(item.getCapacity());
String allCapacity = UiUtil.getString(R.string.mine_all_size)+FileUtil.getReadableFileSizeSaveTow(item.getCapacity());
String availableCapacity = UiUtil.getString(R.string.mine_available_capacity)+FileUtil.getReadableFileSizeSaveTow(item.getCapacity() - item.getUse_capacity());
helper.setText(R.id.tvName, item.getName())
.setText(R.id.tvAllSize, allCapacity)
.setText(R.id.tvAvailable, availableCapacity);
ProgressBar rb = helper.getView(R.id.rb);
int progress = item.getCapacity() == 0 ? 100 : (int) (StringUtil.long2Double(item.getUse_capacity())/StringUtil.long2Double(item.getCapacity())*100);
rb.setProgress(progress);
rb.setVisibility(View.VISIBLE);
ImageView ivDot = helper.getView(R.id.ivDot);
LinearLayout llResult = helper.getView(R.id.llResult);
TextView tvRetry= helper.getView(R.id.tvRetry);
TextView tvCancel = helper.getView(R.id.tvCancel);
TextView tvTips = helper.getView(R.id.tvTips);
ImageView ivStatus = helper.getView(R.id.ivStatus);
llResult.setVisibility(View.GONE);
ivStatus.setVisibility(View.GONE);
tvCancel.setVisibility(View.GONE);
String status = item.getStatus();
ivDot.setVisibility(TextUtils.isEmpty(status) ? View.VISIBLE : View.GONE);
if (!TextUtils.isEmpty(status)){
switch (status){
case Constant.PARTITION_ADDING: // 添加中
case Constant.PARTITION_UPDATING: // 修改中
case Constant.PARTITION_DELETING: // 删除中
ivStatus.setVisibility(View.VISIBLE);
llResult.setVisibility(View.GONE);
if (status.equals(Constant.PARTITION_DELETING)) { // 删除中
ivStatus.setImageResource(R.drawable.icon_folder_deleting);
}else if (status.equals(Constant.PARTITION_ADDING)) { // 添加中
ivStatus.setImageResource(R.drawable.icon_adding);
}else if (status.equals(Constant.PARTITION_UPDATING)) { // 修改中
ivStatus.setImageResource(R.drawable.icon_folder_updating);
}
break;
case Constant.PARTITION_ADD_FAIL: // 添加失败
case Constant.PARTITION_UPDATE_FAIL: // 修改失败
case Constant.PARTITION_DELETE_FAIL: // 删除失败
ivStatus.setVisibility(View.GONE);
tvRetry.setVisibility(View.VISIBLE);
llResult.setVisibility(View.VISIBLE);
if (status.equals(Constant.PARTITION_DELETE_FAIL)){ // 删除状态
tvCancel.setVisibility(View.GONE);
tvTips.setText(UiUtil.getString(R.string.mine_partition_del_fail));
}else {
tvCancel.setVisibility(View.VISIBLE);
rb.setVisibility(View.GONE);
if (status.equals(Constant.PARTITION_ADD_FAIL)){ // 添加失败
tvCancel.setText(UiUtil.getString(R.string.mine_cancel_add));
tvTips.setText(UiUtil.getString(R.string.mine_partition_add_fail));
}else if (status.equals(Constant.PARTITION_UPDATE_FAIL)){ // 修改失败
tvCancel.setText(UiUtil.getString(R.string.mine_cancel_update));
tvTips.setText(UiUtil.getString(R.string.mine_partition_update_fail));
}
}
break;
}
}
}
}
4.添加、编辑和存储池分区
/**
* 添加/编辑分区
*/
public class AddPartitionActivity extends BaseMVPDBActivity<ActivityAddPartitionBinding, AddPartitionContract.View, AddPartitionPresenter> implements AddPartitionContract.View{
...
/**
* 删除确认弹窗
*/
private void showRemoveDialog(){
CenterAlertDialog centerAlertDialog = CenterAlertDialog.getInstance(UiUtil.getString(R.string.mine_remove_confirm), UiUtil.getString(R.string.mine_remove_partition_content),
UiUtil.getString(R.string.mine_remove_tips), R.color.color_ff0000, "", UiUtil.getString(R.string.mine_sure_remove));
centerAlertDialog.setConfirmListener(new CenterAlertDialog.OnConfirmListener() {
@Override
public void onConfirm() {
PoolNameRequest poolNameRequest = new PoolNameRequest(poolName);
mPresenter.removePartition(Constant.scope_token, diskBean.getName(), poolNameRequest);
centerAlertDialog.dismiss();
}
});
centerAlertDialog.show(this);
}
...
/**
* 点击事件
*/
public class OnClickHandler {
public void onClick(View view) {
int viewId = view.getId();
if (viewId == R.id.ivBack) { // 返回
finish();
}else if (viewId == R.id.tvDel){ // 删除
showRemoveDialog();
}else if (viewId == R.id.tvUnit){ // 单位
if (bottomListDialog!=null && !bottomListDialog.isShowing()){
bottomListDialog.show(AddPartitionActivity.this);
}
}else if (viewId == R.id.tvSave){ // 保存
capacityStr = unitIsMB() ? binding.tvResult.getText().toString().trim() : binding.etCapacity.getText().toString().trim();
long capacity = Long.parseLong(capacityStr);
if (diskBean == null) { // 添加
AddPartitionRequest addPartitionRequest = new AddPartitionRequest(binding.etName.getText().toString(), capacity, defaultUnit, poolName);
mPresenter.addPartition(Constant.scope_token, addPartitionRequest);
}else { // 更新
ModifyPartitionRequest modifyPartitionRequest = new ModifyPartitionRequest(binding.etName.getText().toString(), capacity, defaultUnit, poolName);
mPresenter.modifyPartition(Constant.scope_token, diskBean.getName(), modifyPartitionRequest);
}
}
}
}
}
/**
* 添加分区
*/
public class AddPartitionPresenter extends BasePresenter<AddPartitionModel, AddPartitionContract.View> implements AddPartitionContract.Presenter {
@Override
public AddPartitionModel createModel() {
return new AddPartitionModel();
}
/**
* 添加分区
* @param scopeToken
* @param addPartitionRequest
*/
@Override
public void addPartition(String scopeToken, AddPartitionRequest addPartitionRequest) {
executeObservable(mModel.addPartition(scopeToken, addPartitionRequest), new RequestDataCallback<Object>() {
@Override
public void onSuccess(Object response) {
super.onSuccess(response);
if (mView!=null){
mView.addPartitionSuccess();
}
}
@Override
public void onFailed(int errorCode, String errorMessage) {
super.onFailed(errorCode, errorMessage);
if (mView!=null){
mView.addPartitionFail(errorCode, errorMessage);
}
}
});
}
/**
* 编辑 存储池分区
* @param scopeToken
* @param name
* @param modifyPartitionRequest
*/
@Override
public void modifyPartition(String scopeToken, String name, ModifyPartitionRequest modifyPartitionRequest) {
executeObservable(mModel.modifyPartition(scopeToken, name, modifyPartitionRequest), new RequestDataCallback<Object>() {
@Override
public void onSuccess(Object response) {
super.onSuccess(response);
if (mView!=null){
mView.modifyPartitionSuccess();
}
}
@Override
public void onFailed(int errorCode, String errorMessage) {
super.onFailed(errorCode, errorMessage);
if (mView!=null){
mView.modifyPartitionFail(errorCode, errorMessage);
}
}
});
}
/**
* 删除 存储池分区
* @param scopeToken
* @param name
*/
@Override
public void removePartition(String scopeToken, String name, PoolNameRequest poolNameRequest) {
executeObservable(mModel.removePartition(scopeToken, name, poolNameRequest), new RequestDataCallback<Object>() {
@Override
public void onSuccess(Object response) {
super.onSuccess(response);
if (mView!=null){
mView.removePartitionSuccess();
}
}
@Override
public void onFailed(int errorCode, String errorMessage) {
super.onFailed(errorCode, errorMessage);
if (mView!=null){
mView.removePartitionFail(errorCode, errorMessage);
}
}
});
}
}