Skip to content
Snippets Groups Projects
Commit cb9a934f authored by Adrien Béraud's avatar Adrien Béraud
Browse files

file transfer: use system API for file size

Change-Id: I2007c2f2f6a6a2a2d6ab6c4f207890216de8658b
parent 0d194185
No related branches found
No related tags found
No related merge requests found
......@@ -32,6 +32,7 @@ import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.text.format.DateUtils;
import android.text.format.Formatter;
import android.util.Log;
import android.util.TypedValue;
import android.view.ContextMenu;
......@@ -384,10 +385,10 @@ public class ConversationAdapter extends RecyclerView.Adapter<ConversationViewHo
viewHolder.compositeDisposable.add(timestampUpdateTimer.subscribe(t -> {
if (file.getStatus() == InteractionStatus.TRANSFER_FINISHED) {
viewHolder.mMsgDetailTxt.setText(String.format("%s - %s",
timeString, FileUtils.readableFileSize(file.getTotalSize())));
timeString, Formatter.formatFileSize(viewHolder.itemView.getContext(), file.getTotalSize())));
} else {
viewHolder.mMsgDetailTxt.setText(String.format("%s - %s - %s",
timeString, FileUtils.readableFileSize(file.getTotalSize()),
timeString, Formatter.formatFileSize(viewHolder.itemView.getContext(), file.getTotalSize()),
ResourceMapper.getReadableFileTransferStatus(conversationFragment.getActivity(), file.getStatus())));
}
}));
......
......@@ -35,6 +35,7 @@ import android.media.RingtoneManager;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.util.Log;
import android.util.SparseArray;
......@@ -731,7 +732,7 @@ public class NotificationServiceImpl implements NotificationService {
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
.setOnlyAlertOnce(true)
.setContentText(event == Interaction.InteractionStatus.TRANSFER_ONGOING ?
FileUtils.readableFileProgress(info.getBytesProgress(), info.getTotalSize()) :
Formatter.formatFileSize(mContext, info.getBytesProgress()) + " / " + Formatter.formatFileSize(mContext, info.getTotalSize()) :
info.getDisplayName() + ": " + ResourceMapper.getReadableFileTransferStatus(mContext, event))
.setContentIntent(PendingIntent.getService(mContext, random.nextInt(), intentConversation, 0))
.setColor(ResourcesCompat.getColor(mContext.getResources(), R.color.color_primary_dark, null));
......
......@@ -29,9 +29,6 @@ import java.text.DecimalFormat;
public class FileUtils {
private static final String TAG = FileUtils.class.getSimpleName();
private static final String[] SIZE_UNITS = new String[]{"B", "kB", "MB", "GB", "TB"};
private static final DecimalFormat SIZE_FORMAT = new DecimalFormat("#,##0.##");
public static void copyFile(InputStream in, OutputStream out) throws IOException {
// Buffer size based on https://stackoverflow.com/questions/10143731/android-optimal-buffer-size
byte[] buffer = new byte[64 * 1024];
......@@ -75,25 +72,4 @@ public class FileUtils {
Log.d(TAG, "moveFile: moved " + file + " to " + dest);
return true;
}
private static int getSizeDigitGroup(long size) {
return (int) (Math.log10(size) / Math.log10(1024));
}
public static String readableFileSize(long size) {
if (size <= 0) {
return "0";
}
int digitGroups = getSizeDigitGroup(size);
return SIZE_FORMAT.format(size / Math.pow(1024, digitGroups)) + " " + SIZE_UNITS[digitGroups];
}
public static CharSequence readableFileProgress(long progress, long total) {
if (progress < 0 || total < 0)
return "";
int digitGroups = getSizeDigitGroup(Math.max(progress, total));
double den = Math.pow(1024, digitGroups);
String unit = SIZE_UNITS[digitGroups];
return SIZE_FORMAT.format(progress / den) + " / " + SIZE_FORMAT.format(total / den) + " " + unit;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment