Extract method for formatting size to AndroidUtils

This commit is contained in:
Alex Sytnyk 2018-04-17 12:39:04 +03:00
parent fcfc1a2bac
commit ed18a08cdb
2 changed files with 18 additions and 15 deletions

View file

@ -29,6 +29,7 @@ import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import net.osmand.plus.R;
import net.osmand.plus.download.DownloadActivity;
import java.io.BufferedReader;
import java.io.File;
@ -128,6 +129,20 @@ public class AndroidUtils {
return DateFormat.getTimeFormat(ctx).format(new Date(time));
}
public static String formatSize(long sizeBytes) {
int sizeKb = (int) ((sizeBytes + 512) >> 10);
if (sizeKb > 0) {
if (sizeKb > 1 << 20) {
return DownloadActivity.formatGb.format(new Object[]{(float) sizeKb / (1 << 20)});
} else if (sizeBytes > (100 * (1 << 10))) {
return DownloadActivity.formatMb.format(new Object[]{(float) sizeBytes / (1 << 20)});
} else {
return DownloadActivity.formatKb.format(new Object[]{(float) sizeBytes / (1 << 10)});
}
}
return "";
}
public static View findParentViewById(View view, int id) {
ViewParent viewParent = view.getParent();

View file

@ -467,24 +467,12 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
public String getExtendedDescription(Context ctx) {
DateFormat dateFormat = android.text.format.DateFormat.getMediumDateFormat(ctx);
String date = dateFormat.format(file.lastModified());
int size = (int) ((file.length() + 512) >> 10);
String sz = "";
if (size > 0) {
if (size > 1 << 20) {
sz = DownloadActivity.formatGb.format(new Object[]{(float) size / (1 << 20)});
} else {
if (file.length() > (100 * (1 << 10))) {
sz = DownloadActivity.formatMb.format(new Object[]{(float) file.length() / (1 << 20)});
} else {
sz = DownloadActivity.formatKb.format(new Object[]{(float) file.length() / (1 << 10)});
}
}
}
String size = AndroidUtils.formatSize(file.length());
if (isPhoto()) {
return date + "" + sz;
return date + "" + size;
}
updateInternalDescription();
return date + "" + sz + "" + getDuration(ctx, false);
return date + "" + size + "" + getDuration(ctx, false);
}
public String getTypeWithDuration(Context ctx) {