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

68 lines
2.1 KiB
Java
Raw Normal View History

package com.topjohnwu.magisk.components;
import android.widget.Toast;
import com.androidnetworking.interfaces.DownloadProgressListener;
import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.Data;
2018-12-02 05:33:53 -05:00
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Utils;
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) {
2018-12-02 05:33:53 -05:00
MagiskManager mm = Data.MM();
mgr = NotificationManagerCompat.from(mm);
builder = Notifications.progress(title);
mgr.notify(Const.ID.DOWNLOAD_PROGRESS_ID, builder.build());
prevTime = System.currentTimeMillis();
2018-12-02 05:33:53 -05:00
Utils.toast(mm.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() {
mgr.notify(Const.ID.DOWNLOAD_PROGRESS_ID, 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(Data.MM().getString(R.string.download_complete))
.setSmallIcon(R.drawable.ic_check_circle);
update();
}
2018-12-02 05:33:53 -05:00
public void dlFail() {
builder.setProgress(0, 0, false)
.setContentText(Data.MM().getString(R.string.download_file_error))
.setSmallIcon(R.drawable.ic_cancel);
update();
}
2018-12-02 05:33:53 -05:00
public void dismiss() {
mgr.cancel(Const.ID.DOWNLOAD_PROGRESS_ID);
}
}