Search in sources :

Example 1 with SingleButtonCallback

use of com.afollestad.materialdialogs.MaterialDialog.SingleButtonCallback in project XposedInstaller by rovo89.

the class DownloadsUtil method showDownloadDialog.

private static void showDownloadDialog(final Builder b, final long id) {
    final Context context = b.mContext;
    final DownloadDialog dialog = new DownloadDialog(new MaterialDialog.Builder(context).title(b.mTitle).content(R.string.download_view_waiting).progress(false, 0, true).progressNumberFormat(context.getString(R.string.download_progress)).canceledOnTouchOutside(false).negativeText(R.string.download_view_cancel).onNegative(new SingleButtonCallback() {

        @Override
        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            dialog.cancel();
        }
    }).cancelListener(new DialogInterface.OnCancelListener() {

        @Override
        public void onCancel(DialogInterface dialog) {
            removeById(context, id);
        }
    }));
    dialog.setShowProcess(false);
    dialog.show();
    new Thread("DownloadDialog") {

        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    return;
                }
                final DownloadInfo info = getById(context, id);
                if (info == null) {
                    dialog.cancel();
                    return;
                } else if (info.status == DownloadManager.STATUS_FAILED) {
                    dialog.cancel();
                    XposedApp.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            Toast.makeText(context, context.getString(R.string.download_view_failed, info.reason), Toast.LENGTH_LONG).show();
                        }
                    });
                    return;
                } else if (info.status == DownloadManager.STATUS_SUCCESSFUL) {
                    dialog.dismiss();
                    // Hack to reset stat information.
                    new File(info.localFilename).setExecutable(false);
                    if (b.mCallback != null) {
                        b.mCallback.onDownloadFinished(context, info);
                    }
                    return;
                }
                XposedApp.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if (info.totalSize <= 0 || info.status != DownloadManager.STATUS_RUNNING) {
                            dialog.setContent(R.string.download_view_waiting);
                            dialog.setShowProcess(false);
                        } else {
                            dialog.setContent(R.string.download_running);
                            dialog.setProgress(info.bytesDownloaded / 1024);
                            dialog.setMaxProgress(info.totalSize / 1024);
                            dialog.setShowProcess(true);
                        }
                    }
                });
            }
        }
    }.start();
}
Also used : Context(android.content.Context) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) DialogInterface(android.content.DialogInterface) UiThread(android.support.annotation.UiThread) DialogAction(com.afollestad.materialdialogs.DialogAction) SingleButtonCallback(com.afollestad.materialdialogs.MaterialDialog.SingleButtonCallback) File(java.io.File)

Aggregations

Context (android.content.Context)1 DialogInterface (android.content.DialogInterface)1 UiThread (android.support.annotation.UiThread)1 DialogAction (com.afollestad.materialdialogs.DialogAction)1 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1 SingleButtonCallback (com.afollestad.materialdialogs.MaterialDialog.SingleButtonCallback)1 File (java.io.File)1