Skip to content

Commit

Permalink
Rework file size units
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishiranu committed Sep 16, 2020
1 parent 74a571d commit 03991f1
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 63 deletions.
35 changes: 11 additions & 24 deletions src-api/chan/util/StringUtils.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
/*
* Copyright 2014-2017 Fukurou Mishiranu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package chan.util;

import java.util.ArrayList;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.text.SpannableStringBuilder;

import chan.annotation.Extendable;
import chan.annotation.Public;

import com.mishiranu.dashchan.text.HtmlParser;
import java.util.ArrayList;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Public
public class StringUtils {
Expand Down Expand Up @@ -65,7 +46,7 @@ public static String nullIfEmpty(String string) {
return isEmpty(string) ? null : string;
}

@SuppressWarnings("StringEquality")
@SuppressWarnings({"StringEquality", "EqualsReplaceableByObjectsCall"})
@Public
public static boolean equals(String first, String second) {
return first == second || first != null && first.equals(second);
Expand Down Expand Up @@ -153,6 +134,12 @@ public static String replaceAll(String string, Pattern pattern, ReplacementCallb
return string;
}

public static String formatFileSize(long size, boolean upperCase) {
size /= 1000;
return size >= 1000 ? String.format(Locale.US, "%.1f", size / 1000f) + " MB"
: size + (upperCase ? " KB" : " kB");
}

public static SpannableStringBuilder appendSpan(SpannableStringBuilder builder, CharSequence text,
Object... spans) {
int start = builder.length();
Expand Down
8 changes: 4 additions & 4 deletions src/com/mishiranu/dashchan/content/CacheManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ private void syncCache() {

private void cleanupAsync(boolean thumbnails, boolean media, boolean pages) {
int maxCache = MAX_THUMBNAILS_PART + MAX_MEDIA_PART + MAX_PAGES_PART;
int maxCacheSizeMb = Preferences.getCacheSize();
long maxCacheSize = Preferences.getCacheSize() * 1000L * 1000L;
ArrayList<CacheItem> cleanupCacheItems = null;
if (thumbnails) {
synchronized (thumbnailsCache) {
long maxSize = MAX_THUMBNAILS_PART * maxCacheSizeMb * 1024L * 1024L / maxCache;
long maxSize = MAX_THUMBNAILS_PART * maxCacheSize / maxCache;
if (thumbnailsCacheSize > maxSize) {
if (cleanupCacheItems == null) {
cleanupCacheItems = new ArrayList<>();
Expand All @@ -223,7 +223,7 @@ private void cleanupAsync(boolean thumbnails, boolean media, boolean pages) {
}
if (media) {
synchronized (mediaCache) {
long maxSize = MAX_MEDIA_PART * maxCacheSizeMb * 1024L * 1024L / maxCache;
long maxSize = MAX_MEDIA_PART * maxCacheSize / maxCache;
if (mediaCacheSize > maxSize) {
if (cleanupCacheItems == null) {
cleanupCacheItems = new ArrayList<>();
Expand All @@ -235,7 +235,7 @@ private void cleanupAsync(boolean thumbnails, boolean media, boolean pages) {
}
if (pages) {
synchronized (pagesCache) {
long maxSize = MAX_PAGES_PART * maxCacheSizeMb * 1024L * 1024L / maxCache;
long maxSize = MAX_PAGES_PART * maxCacheSize / maxCache;
if (pagesCacheSize > maxSize) {
if (cleanupCacheItems == null) {
cleanupCacheItems = new ArrayList<>();
Expand Down
4 changes: 2 additions & 2 deletions src/com/mishiranu/dashchan/content/async/SendPostTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void onProgressChange(MultipartEntity.Openable openable, long progress, l
public interface Callback<Key> {
public void onSendPostChangeProgressState(Key key, ProgressState progressState,
int attachmentIndex, int attachmentsCount);
public void onSendPostChangeProgressValue(Key key, int progress, int progressMax);
public void onSendPostChangeProgressValue(Key key, long progress, long progressMax);
public void onSendPostSuccess(Key key, ChanPerformer.SendPostData data,
String chanName, String threadNumber, String postNumber);
public void onSendPostFail(Key key, ChanPerformer.SendPostData data, String chanName, ErrorItem errorItem,
Expand Down Expand Up @@ -108,7 +108,7 @@ private void updateProgressValue(int index, long progress, long progressMax) {
switchProgressState(ProgressState.PROCESSING, 0, false);
}
if (progressMode && callback != null) {
callback.onSendPostChangeProgressValue(key, (int) (progress / 1024), (int) (progressMax / 1024));
callback.onSendPostChangeProgressValue(key, progress, progressMax);
}
}

Expand Down
9 changes: 2 additions & 7 deletions src/com/mishiranu/dashchan/content/model/AttachmentItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public String getDescription(AttachmentItem.FormatMode formatMode) {
if (builder.length() > 0) {
builder.append(' ');
}
builder.append(formatSize(size));
builder.append(StringUtils.formatFileSize(size, true));
}
break;
}
Expand All @@ -175,7 +175,7 @@ public String getDescription(AttachmentItem.FormatMode formatMode) {
}
if (size > 0) {
builder.append(formatMode == FormatMode.THREE_LINES ? '\n' : ' ');
builder.append(formatSize(size));
builder.append(StringUtils.formatFileSize(size, true));
}
if (width > 0 && height > 0) {
builder.append('\n').append(width).append('×').append(height);
Expand Down Expand Up @@ -348,11 +348,6 @@ private static ArrayList<String> getAllCodes(String... codes) {
return null;
}

public static String formatSize(int size) {
size /= 1024;
return size >= 1024 ? String.format(Locale.US, "%.1f", size / 1024f) + " MB" : size + " KB";
}

public enum FormatMode {LONG, SIMPLE, TWO_LINES, THREE_LINES}

private static FileAttachmentItem obtainFileAttachmentItem(Binder binder, ChanLocator locator,
Expand Down
4 changes: 2 additions & 2 deletions src/com/mishiranu/dashchan/content/model/PostItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public String getAttachmentsDescription(Context context, AttachmentItem.FormatMo
}
StringBuilder builder = new StringBuilder();
if (size > 0) {
builder.append(AttachmentItem.formatSize(size)).append(' ');
builder.append(StringUtils.formatFileSize(size, true)).append(' ');
}
builder.append(context.getResources().getQuantityString(R.plurals
.number_files__format, count, count));
Expand Down Expand Up @@ -677,7 +677,7 @@ public String formatThreadCardDescription(Context context, boolean repliesOnly)
} else {
hasInformation = true;
}
builder.append(AttachmentItem.formatSize(size).replace(' ', '\u00a0'));
builder.append(StringUtils.formatFileSize(size, true).replace(' ', '\u00a0'));
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/com/mishiranu/dashchan/content/service/PostingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ private static class TaskState {
private int attachmentIndex = 0;
private int attachmentsCount = 0;

private int progress = 0;
private int progressMax = 0;
private long progress = 0;
private long progressMax = 0;

public TaskState(Key key, SendPostTask<Key> task, Context context, String chanName,
ChanPerformer.SendPostData data) {
Expand Down Expand Up @@ -237,7 +237,9 @@ public int onStartCommand(Intent intent, int flags, int startId) {
}
case SENDING: {
if (progressMode) {
builder.setProgress(taskState.progressMax, taskState.progress, taskState.progressMax <= 0);
int max = 1000;
int progress = (int) (taskState.progress * max / taskState.progressMax);
builder.setProgress(max, progress, taskState.progressMax <= 0);
builder.setContentTitle(getString(R.string.sending_number_of_number__ellipsis_format,
taskState.attachmentIndex + 1, taskState.attachmentsCount));
} else {
Expand Down Expand Up @@ -270,7 +272,7 @@ public Binder onBind(Intent intent) {
public interface Callback {
void onState(boolean progressMode, SendPostTask.ProgressState progressState,
int attachmentIndex, int attachmentsCount);
void onProgress(int progress, int progressMax);
void onProgress(long progress, long progressMax);
void onStop(boolean success);
}

Expand Down Expand Up @@ -404,7 +406,7 @@ public void onSendPostChangeProgressState(Key key, SendPostTask.ProgressState pr
}

@Override
public void onSendPostChangeProgressValue(Key key, int progress, int progressMax) {
public void onSendPostChangeProgressValue(Key key, long progress, long progressMax) {
TaskState taskState = this.taskState;
if (taskState != null && taskState.key.equals(key)) {
taskState.progress = progress;
Expand Down
10 changes: 5 additions & 5 deletions src/com/mishiranu/dashchan/text/WakabaLikeHtmlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,12 @@ private void appendFile(FileItem fileItem, boolean multiple) {
if (fileItem.size > 0) {
float sizeFloat;
String dim;
if (fileItem.size >= 2 * 1024 * 1024) {
sizeFloat = fileItem.size / 1024f / 1024f;
if (fileItem.size >= 2 * 1000 * 1000) {
sizeFloat = fileItem.size / 1000f / 1000f;
dim = "MB";
} else if (fileItem.size >= 2 * 1024) {
sizeFloat = fileItem.size / 1024f;
dim = "KB";
} else if (fileItem.size >= 2 * 1000) {
sizeFloat = fileItem.size / 1000f;
dim = "kB";
} else {
sizeFloat = fileItem.size;
dim = "B";
Expand Down
3 changes: 1 addition & 2 deletions src/com/mishiranu/dashchan/ui/gallery/GalleryOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import com.mishiranu.dashchan.R;
import com.mishiranu.dashchan.content.MainApplication;
import com.mishiranu.dashchan.content.Preferences;
import com.mishiranu.dashchan.content.model.AttachmentItem;
import com.mishiranu.dashchan.content.model.GalleryItem;
import com.mishiranu.dashchan.content.service.DownloadService;
import com.mishiranu.dashchan.graphics.GalleryBackgroundDrawable;
Expand Down Expand Up @@ -568,7 +567,7 @@ private void setTitle(GalleryItem galleryItem, int position, int size) {
int count = instance.galleryItems.size();
StringBuilder builder = new StringBuilder().append(position + 1).append('/').append(count);
if (size > 0) {
builder.append(", ").append(AttachmentItem.formatSize(size));
builder.append(", ").append(StringUtils.formatFileSize(size, false));
}
titleSubtitle = new Pair<>(fileName, builder);
GalleryDialog dialog = getDialog();
Expand Down
3 changes: 1 addition & 2 deletions src/com/mishiranu/dashchan/ui/gallery/ListUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.mishiranu.dashchan.R;
import com.mishiranu.dashchan.content.CacheManager;
import com.mishiranu.dashchan.content.ImageLoader;
import com.mishiranu.dashchan.content.model.AttachmentItem;
import com.mishiranu.dashchan.content.model.GalleryItem;
import com.mishiranu.dashchan.graphics.SelectorBorderDrawable;
import com.mishiranu.dashchan.graphics.SelectorCheckDrawable;
Expand Down Expand Up @@ -418,7 +417,7 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
GalleryItem galleryItem = getItem(position);
holder.attachmentInfo.setText(StringUtils
.getFileExtension(galleryItem.getFileName(locator)).toUpperCase(Locale.getDefault()) +
(galleryItem.size > 0 ? " " + AttachmentItem.formatSize(galleryItem.size) : ""));
(galleryItem.size > 0 ? " " + StringUtils.formatFileSize(galleryItem.size, true) : ""));
Uri thumbnailUri = galleryItem.getThumbnailUri(locator);
if (thumbnailUri != null) {
CacheManager cacheManager = CacheManager.getInstance();
Expand Down
11 changes: 5 additions & 6 deletions src/com/mishiranu/dashchan/ui/posting/PostingFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;

public class PostingFragment extends Fragment implements ActivityHandler, CaptchaForm.Callback, AsyncManager.Callback,
ReadCaptchaTask.Callback, PostingDialogCallback {
Expand Down Expand Up @@ -927,7 +926,7 @@ private void dismissSendPost() {
public void onState(boolean progressMode, SendPostTask.ProgressState progressState,
int attachmentIndex, int attachmentsCount) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(requireContext(), progressMode ? "%1$d / %2$d KB" : null);
progressDialog = new ProgressDialog(requireContext(), progressMode ? "%1$d / %2$d kB" : null);
progressDialog.setCancelable(true);
progressDialog.setOnCancelListener(sendPostCancelListener);
progressDialog.setButton(ProgressDialog.BUTTON_POSITIVE, getString(R.string.minimize),
Expand Down Expand Up @@ -960,10 +959,10 @@ public void onState(boolean progressMode, SendPostTask.ProgressState progressSta
}

@Override
public void onProgress(int progress, int progressMax) {
public void onProgress(long progress, long progressMax) {
if (progressDialog != null) {
progressDialog.setMax(progressMax);
progressDialog.setValue(progress);
progressDialog.setMax((int) (progressMax / 1000));
progressDialog.setValue((int) (progress / 1000));
}
}

Expand Down Expand Up @@ -1327,7 +1326,7 @@ private void addAttachment(String hash, String name, String rating, boolean opti
holder.reencoding = reencoding;
holder.fileName.setText(name);
int size = fileHolder != null ? fileHolder.getSize() : 0;
String fileSize = String.format(Locale.US, "%.2f", size / 1024f) + " KB";
String fileSize = StringUtils.formatFileSize(size, false);
Bitmap bitmap = null;
ChanLocator locator = ChanLocator.getDefault();
DisplayMetrics metrics = getResources().getDisplayMetrics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {

addHeader(R.string.additional);
addSeek(Preferences.KEY_CACHE_SIZE, Preferences.DEFAULT_CACHE_SIZE,
getString(R.string.cache_size), "%d MB", 50, 400, 10, Preferences.MULTIPLIER_CACHE_SIZE);
getString(R.string.cache_size), "%d MB", 50, 750, 10, Preferences.MULTIPLIER_CACHE_SIZE);
clearCachePreference = addButton(getString(R.string.clear_cache), p -> {
long cacheSize = CacheManager.getInstance().getCacheSize();
return String.format(Locale.US, "%.2f", cacheSize / 1024. / 1024.) + " MB";
return String.format(Locale.US, "%.2f", cacheSize / 1000f / 1000f) + " MB";
});
clearCachePreference.setOnClickListener(p -> {
ClearCacheFragment fragment = new ClearCacheFragment();
Expand Down
5 changes: 3 additions & 2 deletions src/com/mishiranu/dashchan/ui/preference/UpdateFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void setTarget(Context context, List<ReadUpdateTask.UpdateItem> updateIte
target = context.getString(R.string.__enumeration_format, target, updateItem.name);
if (updateItem.length > 0) {
target = context.getString(R.string.__enumeration_format, target,
(updateItem.length / 1024) + " KB");
StringUtils.formatFileSize(updateItem.length, false));
}
}
this.target = target;
Expand Down Expand Up @@ -339,7 +339,8 @@ public void onPrepareOptionsMenu(@NonNull Menu menu) {
}
String downloadTitle = getString(R.string.download_files);
if (length > 0) {
downloadTitle = getString(R.string.__enumeration_format, downloadTitle, (length / 1024) + " KB");
downloadTitle = getString(R.string.__enumeration_format, downloadTitle,
StringUtils.formatFileSize(length, false));
}
menu.findItem(R.id.menu_download).setTitle(downloadTitle);
menu.findItem(R.id.menu_check_on_start).setChecked(Preferences.isCheckUpdatesOnStart());
Expand Down

0 comments on commit 03991f1

Please sign in to comment.