fix some progress vars that were still int rather than long

This completes the work started in 195aaae7e52dc1c47741965904ed17bdc816a71c

closes #1395
closes #1400

# Conflicts:
#	app/src/main/java/org/fdroid/fdroid/UpdateService.java
This commit is contained in:
Hans-Christoph Steiner 2018-04-05 23:28:49 +02:00
parent 2c6ed51a35
commit 688057b3e7
8 changed files with 64 additions and 40 deletions

View File

@ -115,8 +115,8 @@ public final class AppUpdateStatusManager {
public final Apk apk; public final Apk apk;
public Status status; public Status status;
public PendingIntent intent; public PendingIntent intent;
public int progressCurrent; public long progressCurrent;
public int progressMax; public long progressMax;
public String errorText; public String errorText;
AppUpdateStatus(App app, Apk apk, Status status, PendingIntent intent) { AppUpdateStatus(App app, Apk apk, Status status, PendingIntent intent) {
@ -143,8 +143,8 @@ public final class AppUpdateStatusManager {
apk = in.readParcelable(getClass().getClassLoader()); apk = in.readParcelable(getClass().getClassLoader());
intent = in.readParcelable(getClass().getClassLoader()); intent = in.readParcelable(getClass().getClassLoader());
status = (Status) in.readSerializable(); status = (Status) in.readSerializable();
progressCurrent = in.readInt(); progressCurrent = in.readLong();
progressMax = in.readInt(); progressMax = in.readLong();
errorText = in.readString(); errorText = in.readString();
} }
@ -154,8 +154,8 @@ public final class AppUpdateStatusManager {
dest.writeParcelable(apk, 0); dest.writeParcelable(apk, 0);
dest.writeParcelable(intent, 0); dest.writeParcelable(intent, 0);
dest.writeSerializable(status); dest.writeSerializable(status);
dest.writeInt(progressCurrent); dest.writeLong(progressCurrent);
dest.writeInt(progressMax); dest.writeLong(progressMax);
dest.writeString(errorText); dest.writeString(errorText);
} }
@ -391,7 +391,7 @@ public final class AppUpdateStatusManager {
} }
} }
public void updateApkProgress(String key, int max, int current) { public void updateApkProgress(String key, long max, long current) {
synchronized (appMapping) { synchronized (appMapping) {
AppUpdateStatus entry = appMapping.get(key); AppUpdateStatus entry = appMapping.get(key);
if (entry != null) { if (entry != null) {

View File

@ -327,7 +327,8 @@ class NotificationHelper {
if (entry.progressMax == 0) { if (entry.progressMax == 0) {
builder.setProgress(100, 0, true); builder.setProgress(100, 0, true);
} else { } else {
builder.setProgress(entry.progressMax, entry.progressCurrent, false); builder.setProgress(Utils.bytesToKb(entry.progressMax),
Utils.bytesToKb(entry.progressCurrent), false);
} }
} else if (status == AppUpdateStatusManager.Status.Installing) { } else if (status == AppUpdateStatusManager.Status.Installing) {
builder.setProgress(100, 0, true); // indeterminate bar builder.setProgress(100, 0, true); // indeterminate bar

View File

@ -54,7 +54,6 @@ import org.fdroid.fdroid.views.main.MainActivity;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@SuppressWarnings("LineLength")
public class UpdateService extends IntentService { public class UpdateService extends IntentService {
private static final String TAG = "UpdateService"; private static final String TAG = "UpdateService";
@ -239,7 +238,7 @@ public class UpdateService extends IntentService {
case STATUS_INFO: case STATUS_INFO:
notificationBuilder.setContentText(message) notificationBuilder.setContentText(message)
.setCategory(NotificationCompat.CATEGORY_SERVICE); .setCategory(NotificationCompat.CATEGORY_SERVICE);
if (progress != -1) { if (progress > -1) {
notificationBuilder.setProgress(100, progress, false); notificationBuilder.setProgress(100, progress, false);
} else { } else {
notificationBuilder.setProgress(100, 0, true); notificationBuilder.setProgress(100, 0, true);
@ -449,7 +448,7 @@ public class UpdateService extends IntentService {
} }
if (!changes) { if (!changes) {
Utils.debugLog(TAG, "Not checking app details or compatibility, because all repos were up to date."); Utils.debugLog(TAG, "Not checking app details or compatibility, because repos were up to date.");
} else { } else {
notifyContentProviders(); notifyContentProviders();
@ -523,7 +522,7 @@ public class UpdateService extends IntentService {
String downloadedSizeFriendly = Utils.getFriendlySize(bytesRead); String downloadedSizeFriendly = Utils.getFriendlySize(bytesRead);
int percent = -1; int percent = -1;
if (totalBytes > 0) { if (totalBytes > 0) {
percent = (int) (bytesRead / (totalBytes * 100L)); percent = Utils.getPercent(bytesRead, totalBytes);
} }
String message; String message;
if (totalBytes == -1) { if (totalBytes == -1) {
@ -545,7 +544,7 @@ public class UpdateService extends IntentService {
String totalSize = Utils.getFriendlySize(totalBytes); String totalSize = Utils.getFriendlySize(totalBytes);
int percent = -1; int percent = -1;
if (totalBytes > 0) { if (totalBytes > 0) {
percent = (int) (bytesRead / (totalBytes * 100L)); percent = Utils.getPercent(bytesRead, totalBytes);
} }
String message = context.getString(R.string.status_processing_xml_percent, String message = context.getString(R.string.status_processing_xml_percent,
updater.indexUrl, downloadedSize, totalSize, percent); updater.indexUrl, downloadedSize, totalSize, percent);
@ -553,16 +552,20 @@ public class UpdateService extends IntentService {
} }
/** /**
* If an updater is unable to know how many apps it has to process (i.e. it is streaming apps to the database or * If an updater is unable to know how many apps it has to process (i.e. it
* performing a large database query which touches all apps, but is unable to report progress), then it call this * is streaming apps to the database or performing a large database query
* listener with `totalBytes = 0`. Doing so will result in a message of "Saving app details" sent to the user. If * which touches all apps, but is unable to report progress), then it call
* you know how many apps you have processed, then a message of "Saving app details (x/total)" is displayed. * this listener with `totalBytes = 0`. Doing so will result in a message of
* "Saving app details" sent to the user. If you know how many apps you have
* processed, then a message of "Saving app details (x/total)" is displayed.
*/ */
public static void reportProcessingAppsProgress(Context context, RepoUpdater updater, int appsSaved, int totalApps) { public static void reportProcessingAppsProgress(Context context, RepoUpdater updater,
int appsSaved, int totalApps) {
Utils.debugLog(TAG, "Committing " + updater.indexUrl + "(" + appsSaved + "/" + totalApps + ")"); Utils.debugLog(TAG, "Committing " + updater.indexUrl + "(" + appsSaved + "/" + totalApps + ")");
if (totalApps > 0) { if (totalApps > 0) {
String message = context.getString(R.string.status_inserting_x_apps, appsSaved, totalApps, updater.indexUrl); String message = context.getString(R.string.status_inserting_x_apps,
sendStatus(context, STATUS_INFO, message, (int) ((double) appsSaved / totalApps * 100)); appsSaved, totalApps, updater.indexUrl);
sendStatus(context, STATUS_INFO, message, Utils.getPercent(appsSaved, totalApps));
} else { } else {
String message = context.getString(R.string.status_inserting_apps); String message = context.getString(R.string.status_inserting_apps);
sendStatus(context, STATUS_INFO, message); sendStatus(context, STATUS_INFO, message);

View File

@ -693,6 +693,27 @@ public final class Utils {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()); return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
} }
/**
* Converts a {@code long} bytes value, like from {@link File#length()}, to
* an {@code int} value that is kilobytes, suitable for things like
* {@link android.widget.ProgressBar#setMax(int)} or
* {@link android.support.v4.app.NotificationCompat.Builder#setProgress(int, int, boolean)}
*/
public static int bytesToKb(long bytes) {
return (int) (bytes / 1024);
}
/**
* Converts two {@code long} bytes values, like from {@link File#length()}, to
* an {@code int} value that is a percentage, suitable for things like
* {@link android.widget.ProgressBar#setMax(int)} or
* {@link android.support.v4.app.NotificationCompat.Builder#setProgress(int, int, boolean)}.
* {@code total} must never be zero!
*/
public static int getPercent(long current, long total) {
return (int) ((100L * current + total / 2) / total);
}
@SuppressWarnings("unused") @SuppressWarnings("unused")
public static class Profiler { public static class Profiler {
public final long startTime = System.currentTimeMillis(); public final long startTime = System.currentTimeMillis();

View File

@ -238,8 +238,8 @@ public class InstallManagerService extends Service {
Utils.debugLog(TAG, action + " " + intent); Utils.debugLog(TAG, action + " " + intent);
} else if (Downloader.ACTION_PROGRESS.equals(action)) { } else if (Downloader.ACTION_PROGRESS.equals(action)) {
int bytesRead = intent.getIntExtra(Downloader.EXTRA_BYTES_READ, 0); long bytesRead = intent.getLongExtra(Downloader.EXTRA_BYTES_READ, 0);
int totalBytes = intent.getIntExtra(Downloader.EXTRA_TOTAL_BYTES, 0); long totalBytes = intent.getLongExtra(Downloader.EXTRA_TOTAL_BYTES, 0);
appUpdateStatusManager.updateApkProgress(urlString, totalBytes, bytesRead); appUpdateStatusManager.updateApkProgress(urlString, totalBytes, bytesRead);
} else if (Downloader.ACTION_COMPLETE.equals(action)) { } else if (Downloader.ACTION_COMPLETE.equals(action)) {
localBroadcastManager.unregisterReceiver(this); localBroadcastManager.unregisterReceiver(this);
@ -307,8 +307,8 @@ public class InstallManagerService extends Service {
appUpdateStatusManager.updateApk(urlString, AppUpdateStatusManager.Status.Downloading, action); appUpdateStatusManager.updateApk(urlString, AppUpdateStatusManager.Status.Downloading, action);
break; break;
case Downloader.ACTION_PROGRESS: case Downloader.ACTION_PROGRESS:
int bytesRead = intent.getIntExtra(Downloader.EXTRA_BYTES_READ, 0); long bytesRead = intent.getLongExtra(Downloader.EXTRA_BYTES_READ, 0);
int totalBytes = intent.getIntExtra(Downloader.EXTRA_TOTAL_BYTES, 0); long totalBytes = intent.getLongExtra(Downloader.EXTRA_TOTAL_BYTES, 0);
appUpdateStatusManager.updateApkProgress(urlString, totalBytes, bytesRead); appUpdateStatusManager.updateApkProgress(urlString, totalBytes, bytesRead);
break; break;
case Downloader.ACTION_COMPLETE: case Downloader.ACTION_COMPLETE:

View File

@ -46,7 +46,6 @@ import org.fdroid.fdroid.privileged.views.AppDiff;
import org.fdroid.fdroid.privileged.views.AppSecurityPermissions; import org.fdroid.fdroid.privileged.views.AppSecurityPermissions;
import org.fdroid.fdroid.views.main.MainActivity; import org.fdroid.fdroid.views.main.MainActivity;
import java.text.NumberFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -205,7 +204,7 @@ public class AppDetailsRecyclerViewAdapter
setProgress(0, 0, 0); setProgress(0, 0, 0);
} }
public void setProgress(int bytesDownloaded, int totalBytes, int resIdString) { public void setProgress(long bytesDownloaded, long totalBytes, int resIdString) {
if (headerView != null) { if (headerView != null) {
headerView.setProgress(bytesDownloaded, totalBytes, resIdString); headerView.setProgress(bytesDownloaded, totalBytes, resIdString);
} }
@ -358,14 +357,14 @@ public class AppDetailsRecyclerViewAdapter
}); });
} }
public void setProgress(int bytesDownloaded, int totalBytes, int resIdString) { public void setProgress(long bytesDownloaded, long totalBytes, int resIdString) {
if (bytesDownloaded == 0 && totalBytes == 0) { if (bytesDownloaded == 0 && totalBytes == 0) {
// Remove progress bar // Remove progress bar
progressLayout.setVisibility(View.GONE); progressLayout.setVisibility(View.GONE);
buttonLayout.setVisibility(View.VISIBLE); buttonLayout.setVisibility(View.VISIBLE);
} else { } else {
progressBar.setMax(totalBytes); progressBar.setMax(Utils.bytesToKb(totalBytes));
progressBar.setProgress(bytesDownloaded); progressBar.setProgress(Utils.bytesToKb(bytesDownloaded));
progressBar.setIndeterminate(totalBytes == -1); progressBar.setIndeterminate(totalBytes == -1);
progressLabel.setContentDescription(""); progressLabel.setContentDescription("");
if (resIdString != 0) { if (resIdString != 0) {
@ -373,12 +372,12 @@ public class AppDetailsRecyclerViewAdapter
progressLabel.setContentDescription(context.getString(R.string.downloading)); progressLabel.setContentDescription(context.getString(R.string.downloading));
progressPercent.setText(""); progressPercent.setText("");
} else if (totalBytes > 0 && bytesDownloaded >= 0) { } else if (totalBytes > 0 && bytesDownloaded >= 0) {
float percent = (float) bytesDownloaded / totalBytes; int percent = Utils.getPercent(bytesDownloaded, totalBytes);
progressLabel.setText(Utils.getFriendlySize(bytesDownloaded) + " / " + Utils.getFriendlySize(totalBytes)); progressLabel.setText(Utils.getFriendlySize(bytesDownloaded)
progressLabel.setContentDescription(context.getString(R.string.app__tts__downloading_progress, (int) percent)); + " / " + Utils.getFriendlySize(totalBytes));
NumberFormat format = NumberFormat.getPercentInstance(); progressLabel.setContentDescription(context.getString(R.string.app__tts__downloading_progress,
format.setMaximumFractionDigits(0); percent));
progressPercent.setText(format.format(percent)); progressPercent.setText(String.format(Locale.ENGLISH, "%d%%", percent));
} else if (bytesDownloaded >= 0) { } else if (bytesDownloaded >= 0) {
progressLabel.setText(Utils.getFriendlySize(bytesDownloaded)); progressLabel.setText(Utils.getFriendlySize(bytesDownloaded));
progressLabel.setContentDescription(context.getString(R.string.downloading)); progressLabel.setContentDescription(context.getString(R.string.downloading));

View File

@ -365,7 +365,8 @@ public abstract class AppListItemController extends RecyclerView.ViewHolder {
return new AppListItemState(app) return new AppListItemState(app)
.setMainText(mainText) .setMainText(mainText)
.setProgress(currentStatus.progressCurrent, currentStatus.progressMax); .setProgress(Utils.bytesToKb(currentStatus.progressCurrent),
Utils.bytesToKb(currentStatus.progressMax));
} }
protected AppListItemState getViewStateReadyToInstall(@NonNull App app) { protected AppListItemState getViewStateReadyToInstall(@NonNull App app) {

View File

@ -253,13 +253,12 @@ public class SwapAppsView extends ListView implements
if (progressView.getVisibility() != View.VISIBLE) { if (progressView.getVisibility() != View.VISIBLE) {
showProgress(); showProgress();
} }
int read = intent.getIntExtra(Downloader.EXTRA_BYTES_READ, 0); long read = intent.getLongExtra(Downloader.EXTRA_BYTES_READ, 0);
int total = intent.getIntExtra(Downloader.EXTRA_TOTAL_BYTES, 0); long total = intent.getLongExtra(Downloader.EXTRA_TOTAL_BYTES, 0);
if (total > 0) { if (total > 0) {
int progress = (int) ((double) read / total * 100);
progressView.setIndeterminate(false); progressView.setIndeterminate(false);
progressView.setMax(100); progressView.setMax(100);
progressView.setProgress(progress); progressView.setProgress(Utils.getPercent(read, total));
} else { } else {
progressView.setIndeterminate(true); progressView.setIndeterminate(true);
} }