package org.ovirt.mobile.movirt.ui.snapshots;
import android.database.Cursor;
import android.view.View;
import android.widget.CursorAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.Receiver;
import org.ovirt.mobile.movirt.Broadcasts;
import org.ovirt.mobile.movirt.R;
import org.ovirt.mobile.movirt.model.Disk;
import org.ovirt.mobile.movirt.ui.ProgressBarResponse;
import org.ovirt.mobile.movirt.ui.listfragment.SnapshotEmbeddableVmBoundResumeSyncableBaseEntityListFragment;
import org.ovirt.mobile.movirt.ui.listfragment.spinner.ItemName;
import org.ovirt.mobile.movirt.ui.listfragment.spinner.SortEntry;
import org.ovirt.mobile.movirt.ui.listfragment.spinner.SortOrderType;
import org.ovirt.mobile.movirt.util.usage.MemorySize;
import java.util.List;
import static org.ovirt.mobile.movirt.provider.OVirtContract.Disk.NAME;
import static org.ovirt.mobile.movirt.provider.OVirtContract.Disk.SIZE;
import static org.ovirt.mobile.movirt.provider.OVirtContract.Disk.STATUS;
@EFragment(R.layout.fragment_base_entity_list)
public class SnapshotDisksFragment extends SnapshotEmbeddableVmBoundResumeSyncableBaseEntityListFragment<Disk> {
private static final String TAG = SnapshotDisksFragment.class.getSimpleName();
public SnapshotDisksFragment() {
super(Disk.class);
}
@Override
protected CursorAdapter createCursorAdapter() {
SimpleCursorAdapter diskListAdapter = new SimpleCursorAdapter(getActivity(),
R.layout.disk_list_item,
null,
new String[]{NAME, SIZE, STATUS},
new int[]{R.id.disk_name, R.id.disk_size, R.id.disk_status}, 0);
diskListAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
TextView textView = (TextView) view;
if (columnIndex == cursor.getColumnIndex(NAME)) {
String name = cursor.getString(columnIndex);
textView.setText(name);
} else if (columnIndex == cursor.getColumnIndex(SIZE)) {
long size = cursor.getLong(columnIndex);
String sizeText = (size == -1) ? getString(R.string.disk_unknown_size) : new MemorySize(size).toString();
textView.setText(sizeText);
} else if (columnIndex == cursor.getColumnIndex(STATUS)) {
String status = cursor.getString(columnIndex);
textView.setText(status == null ? getString(R.string.NA) : status.toUpperCase());
}
return true;
}
});
return diskListAdapter;
}
@Override
public SortEntry[] getSortEntries() {
return new SortEntry[]{
new SortEntry(new ItemName(Disk.NAME), SortOrderType.A_TO_Z),
new SortEntry(new ItemName(Disk.STATUS), SortOrderType.A_TO_Z)
};
}
@Background
@Receiver(actions = Broadcasts.IN_SYNC, registerAt = Receiver.RegisterAt.OnResumeOnPause)
protected void syncingChanged(@Receiver.Extra(Broadcasts.Extras.SYNCING) boolean syncing) {
if (syncing) {
entityFacade.syncAll(getVmId(), getSnapshotId());
}
}
@Background
@Override
public void onRefresh() {
entityFacade.syncAll(new ProgressBarResponse<List<Disk>>(this), getVmId(), getSnapshotId());
}
}