Example usage for java.util Timer schedule

List of usage examples for java.util Timer schedule

Introduction

In this page you can find the example usage for java.util Timer schedule.

Prototype

public void schedule(TimerTask task, Date time) 

Source Link

Document

Schedules the specified task for execution at the specified time.

Usage

From source file:de.yaacc.player.AbstractPlayer.java

protected void executeCommand(TimerTask command, Date executionTime) {
    if (execTimer != null) {
        execTimer.cancel();/* w w w  .  ja  va  2 s . co m*/
    }
    Timer execTimer = new Timer();
    execTimer.schedule(command, executionTime);
}

From source file:name.marinchenko.lorryvision.activities.main.MainActivity.java

private void toVideoActivity() {
        final Timer timer = new Timer();
        final TimerTask jumpTask = new TimerTask() {
            @Override/*from ww w  .  ja  v  a  2 s . co m*/
            public void run() {
                final Intent videoIntent = new Intent(getApplicationContext(), VideoActivity.class);
                startActivity(videoIntent);
            }
        };
        timer.schedule(jumpTask, VideoActivity.JUMP_DELAY);
    }

From source file:jahirfiquitiva.iconshowcase.activities.ViewerActivity.java

private void saveWallpaperAction(final String name, String url) {

    if (downloadDialog != null) {
        downloadDialog.dismiss();//from www.  j a va  2  s. c  o  m
    }

    final boolean[] enteredDownloadTask = { false };

    downloadDialog = new MaterialDialog.Builder(context).content(R.string.downloading_wallpaper)
            .progress(true, 0).cancelable(false).onPositive(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    if (downloadDialog != null) {
                        downloadDialog.dismiss();
                    }
                }
            }).show();

    Glide.with(context).load(url).asBitmap().diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    if (resource != null && downloadDialog.isShowing()) {
                        enteredDownloadTask[0] = true;
                        saveWallpaper(context, name, downloadDialog, resource);
                    }
                }
            });

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            runOnUIThread(context, new Runnable() {
                @Override
                public void run() {
                    if (!enteredDownloadTask[0]) {
                        String newContent = context.getString(R.string.downloading_wallpaper) + "\n"
                                + context.getString(R.string.download_takes_longer);
                        downloadDialog.setContent(newContent);
                        downloadDialog.setActionButton(DialogAction.POSITIVE, android.R.string.cancel);
                    }
                }
            });
        }
    }, 15000);
}

From source file:org.openhab.binding.powermax.internal.message.PowerMaxCommDriver.java

/**
 * Send a message or delay the sending if time frame for receiving response is not ended
 *
 * @param msg/*from  w  ww.  jav a 2  s.c  o  m*/
 *            the message to be sent
 * @param immediate
 *            true if the message has to be send without considering timing
 * @param waitTime
 *            the delay in seconds to wait
 *
 * @return true if the message was sent or the sending is delayed; false in other cases
 */
private synchronized boolean sendMessage(PowerMaxBaseMessage msg, boolean immediate, int waitTime) {

    if ((waitTime > 0) && (msg != null)) {
        logger.debug("sendMessage(): delay ({} s) sending message (type {})", waitTime,
                msg.getSendType().toString());
        Timer timer = new Timer();
        // Don't queue the message
        timer.schedule(new DelayedSendTask(msg), waitTime * 1000);
        return true;
    }

    if (msg == null) {
        msg = msgQueue.peek();
        if (msg == null) {
            logger.debug("sendMessage(): nothing to send");
            return false;
        }
    }

    // Delay sending if time frame for receiving response is not ended
    long delay = WAITING_DELAY_FOR_RESPONSE - (System.currentTimeMillis() - connector.getWaitingForResponse());

    PowerMaxBaseMessage msgToSend = msg;

    if (!immediate) {
        msgToSend = msgQueue.peek();
        if (msgToSend != msg) {
            logger.debug("sendMessage(): add message in queue (type {})", msg.getSendType().toString());
            msgQueue.offer(msg);
            msgToSend = msgQueue.peek();
        }
        if ((msgToSend != msg) && (delay > 0)) {
            return true;
        } else if ((msgToSend == msg) && (delay > 0)) {
            if (delay < 100) {
                delay = 100;
            }
            logger.debug("sendMessage(): delay ({} ms) sending message (type {})", delay,
                    msgToSend.getSendType().toString());
            Timer timer = new Timer();
            timer.schedule(new DelayedSendTask(null), delay);
            return true;
        } else {
            msgToSend = msgQueue.poll();
        }
    }

    if (logger.isDebugEnabled()) {
        logger.debug("sendMessage(): sending {} message {}", msgToSend.getSendType().toString(),
                DatatypeConverter.printHexBinary(msgToSend.getRawData()));
    }
    boolean done = sendMessage(msgToSend.getRawData());
    if (done) {
        lastSendMsg = msgToSend;
        connector.setWaitingForResponse(System.currentTimeMillis());

        if (!immediate && (msgQueue.peek() != null)) {
            logger.debug("sendMessage(): delay sending next message (type {})",
                    msgQueue.peek().getSendType().toString());
            Timer timer = new Timer();
            timer.schedule(new DelayedSendTask(null), WAITING_DELAY_FOR_RESPONSE);
        }
    } else {
        logger.debug("sendMessage(): failed");
    }

    return done;
}

From source file:jahirfiquitiva.iconshowcase.activities.WallpaperViewerActivity.java

private void saveWallpaperAction(final String name, String url) {

    if (downloadDialog != null) {
        downloadDialog.dismiss();// w  w  w . ja va 2  s . c o  m
    }

    final boolean[] enteredDownloadTask = { false };

    downloadDialog = new MaterialDialog.Builder(context).content(R.string.downloading_wallpaper)
            .progress(true, 0).cancelable(false).onPositive(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    if (downloadDialog != null) {
                        downloadDialog.dismiss();
                    }
                }
            }).show();

    Glide.with(context).load(url).asBitmap().dontAnimate().diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    if (resource != null && downloadDialog.isShowing()) {
                        enteredDownloadTask[0] = true;
                        saveWallpaper(context, name, downloadDialog, resource);
                    }
                }
            });

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            runOnUIThread(context, new Runnable() {
                @Override
                public void run() {
                    if (!enteredDownloadTask[0]) {
                        String newContent = context.getString(R.string.downloading_wallpaper) + "\n"
                                + context.getString(R.string.download_takes_longer);
                        downloadDialog.setContent(newContent);
                        downloadDialog.setActionButton(DialogAction.POSITIVE, android.R.string.cancel);
                    }
                }
            });
        }
    }, 10000);
}

From source file:it_minds.dk.eindberetningmobil_android.views.UploadingView.java

private void TrySendReport(final DriveReport toSend) {
    final Timer timer = new Timer();

    ServerFactory.getInstance(this).sendReport(toSend, new ResultCallback<JSONObject>() {
        @Override/*from  w  w  w.ja  v a  2 s  .c  o  m*/
        public void onSuccess(JSONObject result) {
            Log.d("RESULT", result.toString());
            updateStatusText(getString(R.string.success));
            spinner.setVisibility(View.INVISIBLE);
            MainSettings.getInstance(UploadingView.this).removeSavedReport(saveableReport);
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            startActivity(new Intent(UploadingView.this, StartActivity.class));
                            finish();
                        }
                    });

                }
            }, WAIT_TIME_MS_SUCCESS_DISSAPEAR);
        }

        @Override
        public void onError(Exception error) {
            updateStatusText(getString(R.string.error));
            Log.e("temp", "error", error);
            new ConfirmationDialog(UploadingView.this, getString(R.string.error_dialog_title),
                    getString(R.string.send_report_error), getString(R.string.send_report_error_retry),
                    getString(R.string.send_report_error_cancel), null, new ResultCallback<Boolean>() {
                        @Override
                        public void onSuccess(Boolean result) {
                            TrySendReport(toSend);
                        }

                        @Override
                        public void onError(Exception error) {
                            startActivity(new Intent(UploadingView.this, StartActivity.class));
                            finish();
                        }
                    }).showDialog();
        }
    });

    /*
    ServerFactory.getInstance(this).sendReport(toSend, new ResultCallback<UserInfo>() {
    @Override
    public void onSuccess(UserInfo result) {
        updateStatusText(getString(R.string.success));
        spinner.setVisibility(View.INVISIBLE);
        MainSettings.getInstance(UploadingView.this).removeSavedReport(saveableReport);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        startActivity(new Intent(UploadingView.this, StartActivity.class));
                        finish();
                    }
                });
            
            }
        }, WAIT_TIME_MS_SUCCESS_DISSAPEAR);
    }
            
    @Override
    public void onError(final Exception error) {
        updateStatusText(getString(R.string.error));
        Log.e("temp", "error", error);
        new ConfirmationDialog(UploadingView.this, getString(R.string.error_dialog_title), getString(R.string.send_report_error),
                getString(R.string.send_report_error_retry), getString(R.string.send_report_error_cancel), null, new ResultCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean result) {
                TrySendReport(toSend);
            }
            
            @Override
            public void onError(Exception error) {
                startActivity(new Intent(UploadingView.this, StartActivity.class));
                finish();
            }
        }).showDialog();
    }
    });
    */
}

From source file:jahirfiquitiva.iconshowcase.activities.ViewerActivity.java

private void showApplyWallpaperDialog(final Activity context, final String wallUrl) {
    ISDialogs.showApplyWallpaperDialog(context, new MaterialDialog.SingleButtonCallback() {
        @Override//from  w ww  . j a  va  2 s . co m
        public void onClick(@NonNull MaterialDialog materialDialog, @NonNull DialogAction dialogAction) {
            if (dialogApply != null) {
                dialogApply.dismiss();
            }

            final ApplyWallpaper[] applyTask = new ApplyWallpaper[1];

            final boolean[] enteredApplyTask = { false };

            dialogApply = new MaterialDialog.Builder(context).content(R.string.downloading_wallpaper)
                    .progress(true, 0).cancelable(false).onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            if (applyTask[0] != null) {
                                applyTask[0].cancel(true);
                            }
                            dialogApply.dismiss();
                        }
                    }).show();

            Glide.with(context).load(wallUrl).asBitmap().diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(final Bitmap resource,
                                GlideAnimation<? super Bitmap> glideAnimation) {
                            if (resource != null && dialogApply.isShowing()) {
                                enteredApplyTask[0] = true;

                                if (dialogApply != null) {
                                    dialogApply.dismiss();
                                }
                                dialogApply = new MaterialDialog.Builder(context)
                                        .content(R.string.setting_wall_title).progress(true, 0)
                                        .cancelable(false).show();

                                applyTask[0] = new ApplyWallpaper(context, dialogApply, resource, false, layout,
                                        toHide1, toHide2);
                                applyTask[0].execute();
                            }
                        }
                    });

            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    runOnUIThread(context, new Runnable() {
                        @Override
                        public void run() {
                            if (!enteredApplyTask[0]) {
                                String newContent = context.getString(R.string.downloading_wallpaper) + "\n"
                                        + context.getString(R.string.download_takes_longer);
                                dialogApply.setContent(newContent);
                                dialogApply.setActionButton(DialogAction.POSITIVE, android.R.string.cancel);
                            }
                        }
                    });
                }
            }, 15000);
        }
    }, new MaterialDialog.SingleButtonCallback() {
        @Override
        public void onClick(@NonNull MaterialDialog materialDialog, @NonNull DialogAction dialogAction) {
            if (!PermissionUtils.canAccessStorage(context)) {
                PermissionUtils.setViewerActivityAction("crop");
                PermissionUtils.requestStoragePermission(context);
            } else {
                cropWallpaper(wallUrl);
            }
        }
    });
}

From source file:jahirfiquitiva.iconshowcase.activities.ViewerActivity.java

private void cropWallpaper(String wallUrl) {
    if (dialogApply != null) {
        dialogApply.dismiss();//from ww w.  j a v  a2s .co m
    }

    final WallpaperToCrop[] cropTask = new WallpaperToCrop[1];

    final boolean[] enteredCropTask = { false };

    dialogApply = new MaterialDialog.Builder(context).content(R.string.downloading_wallpaper).progress(true, 0)
            .cancelable(false).onPositive(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    if (cropTask[0] != null) {
                        cropTask[0].cancel(true);
                    }
                    dialogApply.dismiss();
                }
            }).show();

    Glide.with(context).load(wallUrl).asBitmap().diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(final Bitmap resource,
                        GlideAnimation<? super Bitmap> glideAnimation) {
                    if (resource != null && dialogApply.isShowing()) {
                        enteredCropTask[0] = true;
                        if (dialogApply != null) {
                            dialogApply.dismiss();
                        }
                        dialogApply = new MaterialDialog.Builder(context)
                                .content(context.getString(R.string.preparing_wallpaper)).progress(true, 0)
                                .cancelable(false).onPositive(new MaterialDialog.SingleButtonCallback() {
                                    @Override
                                    public void onClick(@NonNull MaterialDialog dialog,
                                            @NonNull DialogAction which) {
                                        if (cropTask[0] != null) {
                                            cropTask[0].cancel(true);
                                        }
                                        dialogApply.dismiss();
                                    }
                                }).show();
                        cropTask[0] = new WallpaperToCrop(context, dialogApply, resource, layout,
                                item.getWallName(), toHide1, toHide2);
                        cropTask[0].execute();
                        Timer timer = new Timer();
                        timer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                runOnUIThread(context, new Runnable() {
                                    @Override
                                    public void run() {
                                        String content = context.getString(R.string.preparing_wallpaper) + "\n"
                                                + context.getString(R.string.download_takes_longer);

                                        dialogApply.setContent(content);
                                        dialogApply.setActionButton(DialogAction.POSITIVE,
                                                android.R.string.cancel);
                                    }
                                });
                            }
                        }, 7000);
                    }
                }
            });

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            runOnUIThread(context, new Runnable() {
                @Override
                public void run() {
                    if (!enteredCropTask[0]) {
                        String newContent = context.getString(R.string.downloading_wallpaper) + "\n"
                                + context.getString(R.string.download_takes_longer);
                        dialogApply.setContent(newContent);
                        dialogApply.setActionButton(DialogAction.POSITIVE, android.R.string.cancel);
                    }
                }
            });
        }
    }, 15000);
}

From source file:be.ac.ua.comp.scarletnebula.core.Server.java

private void refreshUntilServerHasState(final VmState state, final int attempt) {
    if (getStatus() == state || attempt > 20) {
        return;/*w  ww  .j a  v  a2  s  . com*/
    }

    try {
        refresh();
    } catch (final ServerDisappearedException e) {
        getCloud().unlink(this);
        return;
    } catch (final Exception e) {
        log.error("Something happened while refreshing server " + this, e);
    }

    if (getStatus() == state) {
        return;
    }

    // If the server's state still isn't the one we want it to be, try
    // again, but only after waiting
    // a logarithmic amount of time.
    final double wait = 15.0 * (Math.log10(attempt) + 1.0);

    final java.util.Timer timer = new java.util.Timer();
    timer.schedule(new java.util.TimerTask() {
        @Override
        public void run() {
            refreshUntilServerHasState(state, attempt + 1);
            log.debug("Refreshing state for server " + getFriendlyName()
                    + " because timer fired, waiting for state " + state.toString());
            cancel();
        }
    }, (long) (wait * 1000));

}

From source file:jahirfiquitiva.iconshowcase.activities.WallpaperViewerActivity.java

private void cropWallpaper(String wallUrl) {
    if (dialogApply != null) {
        dialogApply.dismiss();/*from   www.  j  a  va2s .  c o  m*/
    }

    final WallpaperToCrop[] cropTask = new WallpaperToCrop[1];

    final boolean[] enteredCropTask = { false };

    dialogApply = new MaterialDialog.Builder(context).content(R.string.downloading_wallpaper).progress(true, 0)
            .cancelable(false).onPositive(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    if (cropTask[0] != null) {
                        cropTask[0].cancel(true);
                    }
                    dialogApply.dismiss();
                }
            }).show();

    Glide.with(context).load(wallUrl).asBitmap().dontAnimate().diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(final Bitmap resource,
                        GlideAnimation<? super Bitmap> glideAnimation) {
                    if (resource != null && dialogApply.isShowing()) {
                        enteredCropTask[0] = true;
                        if (dialogApply != null) {
                            dialogApply.dismiss();
                        }
                        dialogApply = new MaterialDialog.Builder(context)
                                .content(context.getString(R.string.preparing_wallpaper)).progress(true, 0)
                                .cancelable(false).onPositive(new MaterialDialog.SingleButtonCallback() {
                                    @Override
                                    public void onClick(@NonNull MaterialDialog dialog,
                                            @NonNull DialogAction which) {
                                        if (cropTask[0] != null) {
                                            cropTask[0].cancel(true);
                                        }
                                        dialogApply.dismiss();
                                    }
                                }).show();
                        cropTask[0] = new WallpaperToCrop(context, dialogApply, resource, layout,
                                item.getWallName(), toHide1, toHide2);
                        cropTask[0].execute();
                        Timer timer = new Timer();
                        timer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                runOnUIThread(context, new Runnable() {
                                    @Override
                                    public void run() {
                                        String content = context.getString(R.string.preparing_wallpaper) + "\n"
                                                + context.getString(R.string.download_takes_longer);

                                        dialogApply.setContent(content);
                                        dialogApply.setActionButton(DialogAction.POSITIVE,
                                                android.R.string.cancel);
                                    }
                                });
                            }
                        }, 7000);
                    }
                }
            });

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            runOnUIThread(context, new Runnable() {
                @Override
                public void run() {
                    if (!enteredCropTask[0]) {
                        String newContent = context.getString(R.string.downloading_wallpaper) + "\n"
                                + context.getString(R.string.download_takes_longer);
                        dialogApply.setContent(newContent);
                        dialogApply.setActionButton(DialogAction.POSITIVE, android.R.string.cancel);
                    }
                }
            });
        }
    }, 10000);
}