Magisk/app/src/full/java/com/topjohnwu/magisk/components/ProgressNotification.java

66 lines
2.0 KiB
Java
Raw Normal View History

package com.topjohnwu.magisk.components;
import android.widget.Toast;
import com.topjohnwu.core.App;
import com.topjohnwu.core.utils.Utils;
2018-12-02 05:33:53 -05:00
import com.topjohnwu.magisk.R;
2018-12-12 05:51:45 -05:00
import com.topjohnwu.net.DownloadProgressListener;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
2018-12-03 01:44:13 -05:00
public class ProgressNotification implements DownloadProgressListener {
private NotificationManagerCompat mgr;
private NotificationCompat.Builder builder;
private long prevTime;
2018-12-03 01:44:13 -05:00
public ProgressNotification(String title) {
mgr = NotificationManagerCompat.from(App.self);
builder = Notifications.progress(title);
prevTime = System.currentTimeMillis();
2018-12-03 02:28:20 -05:00
update();
Utils.toast(App.self.getString(R.string.downloading_toast, title), Toast.LENGTH_SHORT);
}
@Override
public void onProgress(long bytesDownloaded, long totalBytes) {
long cur = System.currentTimeMillis();
if (cur - prevTime >= 1000) {
prevTime = cur;
2018-12-02 15:15:42 -05:00
int progress = (int) (bytesDownloaded * 100 / totalBytes);
builder.setProgress(100, progress, false);
builder.setContentText(progress + "%");
update();
}
}
2018-12-02 15:15:42 -05:00
public NotificationCompat.Builder getNotification() {
return builder;
}
public void update() {
2018-12-03 02:28:20 -05:00
mgr.notify(hashCode(), builder.build());
}
2018-12-02 05:33:53 -05:00
public void dlDone() {
2018-12-02 15:15:42 -05:00
builder.setProgress(0, 0, false)
.setContentText(App.self.getString(R.string.download_complete))
2018-12-03 01:52:36 -05:00
.setSmallIcon(R.drawable.ic_check_circle)
.setOngoing(false);
update();
}
2018-12-02 05:33:53 -05:00
public void dlFail() {
builder.setProgress(0, 0, false)
.setContentText(App.self.getString(R.string.download_file_error))
2018-12-03 01:52:36 -05:00
.setSmallIcon(R.drawable.ic_cancel)
.setOngoing(false);
update();
}
2018-12-02 05:33:53 -05:00
public void dismiss() {
2018-12-03 02:28:20 -05:00
mgr.cancel(hashCode());
2018-12-02 05:33:53 -05:00
}
}