Example usage for android.os CountDownTimer CountDownTimer

List of usage examples for android.os CountDownTimer CountDownTimer

Introduction

In this page you can find the example usage for android.os CountDownTimer CountDownTimer.

Prototype

public CountDownTimer(long millisInFuture, long countDownInterval) 

Source Link

Usage

From source file:com.almalence.plugins.capture.video.VideoCapturePlugin.java

private void updateRecordingTime() {
    if (!isRecording && !onPause) {
        mRecordingTimeView.setText("00:00");
        mRecorded = 0;/*from   w w  w .j  av  a2 s.  c o  m*/
        return;
    }

    if (onPause) {
        mRecorded = timeStringToMillisecond(mRecordingTimeView.getText().toString());

        blinkPause();

        return;
    }

    long now = SystemClock.uptimeMillis();
    long delta = now - mRecordingStartTime + mRecorded;

    // Starting a minute before reaching the max duration
    // limit, we'll countdown the remaining time instead.
    boolean countdownRemainingTime = (mMaxVideoDurationInMs != 0 && delta >= mMaxVideoDurationInMs - 60000);

    long deltaAdjusted = (long) (delta * captureRate / 24);
    // *captureRate/24 needed for time lapse
    if (countdownRemainingTime) {
        deltaAdjusted = Math.max(0, mMaxVideoDurationInMs - deltaAdjusted) + 999;
    }
    String text;

    long targetNextUpdateDelay;
    text = millisecondToTimeString(deltaAdjusted, false);
    targetNextUpdateDelay = 900;

    mRecordingTimeView.setText(text);

    if (mRecordingTimeCountsDown != countdownRemainingTime) {
        // Avoid setting the color on every update, do it only
        // when it needs changing.
        mRecordingTimeCountsDown = countdownRemainingTime;

        int color = ApplicationScreen.getAppResources().getColor(R.color.recording_time_remaining_text);

        mRecordingTimeView.setTextColor(color);
    }

    long actualNextUpdateDelay = targetNextUpdateDelay - (delta % targetNextUpdateDelay);

    new CountDownTimer(actualNextUpdateDelay, actualNextUpdateDelay) {
        public void onTick(long millisUntilFinished) {
        }

        public void onFinish() {
            updateRecordingTime();
        }
    }.start();

    // show recording shutter
    if (showRecording) {
        stopVideoButton.setImageDrawable(
                ApplicationScreen.getAppResources().getDrawable(R.drawable.plugin_capture_video_stop_square));
        showRecording = false;
    } else {
        stopVideoButton.setImageDrawable(ApplicationScreen.getAppResources()
                .getDrawable(R.drawable.plugin_capture_video_stop_square_red));
        showRecording = true;
    }
}

From source file:xj.property.activity.HXBaseActivity.MainActivity.java

private void fetchCommunityCategoryData() {
    RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(Config.NET_BASE).build();
    restAdapter.setLogLevel(RestAdapter.LogLevel.FULL);
    GetUserService isUserExistService = restAdapter.create(GetUserService.class);
    Callback<CommunityCategoryDataRespBean> callback = new Callback<CommunityCategoryDataRespBean>() {
        @Override/*  w ww.  ja v a2s. co m*/
        public void success(CommunityCategoryDataRespBean bean, Response response) {
            if (bean != null && "yes".equals(bean.getStatus())) {
                List<CommunityCategoryDataRespBean.InfoEntity> info = bean.getInfo();
                if (info != null && info.size() > 0) {

                    myCommunityCategoryDialog = new MyCommunityCategoryDialog(getmContext(), info);
                    myCommunityCategoryDialog.setCanceledOnTouchOutside(true);
                    myCommunityCategoryDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            PreferencesUtil.setFirstOpen(getmContext(), false);
                        }
                    });

                    //                        WindowManager windowManager = getWindowManager();
                    //                        Display display = windowManager.getDefaultDisplay();

                    //                        2015/12/18 start

                    //                        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
                    //                        Window window = myCommunityCategoryDialog.getWindow();
                    //                        lp.copyFrom(window.getAttributes());
                    //
                    //                        lp.width = DensityUtil.dip2px(getmContext(),360f);
                    //                        lp.height = DensityUtil.dip2px(getmContext(),641f);
                    //                        window.setAttributes(lp);
                    //                        2015/12/18 end

                    //                        WindowManager manager = (WindowManager) getSystemService(Activity.WINDOW_SERVICE);
                    //                        int width, height;
                    //
                    //                        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) {
                    //                            width = manager.getDefaultDisplay().getWidth();
                    //                            height = manager.getDefaultDisplay().getHeight();
                    //                        } else {
                    //                            Point point = new Point();
                    //                            manager.getDefaultDisplay().getSize(point);
                    //                            width = point.x;
                    //                            height = point.y;
                    //                        }
                    //                        lp.width = width;
                    //                        lp.height = height;

                    //                        WindowManager.LayoutParams lp = myCommunityCategoryDialog.getWindow().getAttributes();
                    //                        lp.width = getmContext().getResources().getDisplayMetrics().widthPixels;
                    //                        myCommunityCategoryDialog.getWindow().setAttributes(lp);

                    myCommunityCategoryDialog.show();
                    homeDialogTimer = new CountDownTimer(DIALOG_SHOW_TIME, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                        }

                        @Override
                        public void onFinish() {
                            myCommunityCategoryDialog.dismiss();
                        }
                    };
                    homeDialogTimer.start();

                }
            }
            PreferencesUtil.setFirstOpen(getmContext(), false);
        }

        @Override
        public void failure(RetrofitError error) {
            PreferencesUtil.setFirstOpen(getmContext(), false);
        }
    };

    long communityId;
    if (PreferencesUtil.getLogin(this)) {
        communityId = PreferencesUtil.getLoginInfo(this).getCommunityId();
    } else {
        communityId = PreferencesUtil.getCommityId(this);
    }
    isUserExistService.getCommunityCategoryData(communityId, callback);
}

From source file:me.ububble.speakall.fragment.ConversationChatFragment.java

public void startCounterToDelete(long timer, final Message msj) {

    CountDownTimer aCounter = new CountDownTimer(timer, 1000) {

        public void onTick(long millisUntilFinished) {
            long days, hours, minutes, seconds;
            long secondsTotal = millisUntilFinished / 1000;
            days = (Math.round(secondsTotal) / 86400);
            hours = (Math.round(secondsTotal) / 3600) - (days * 24);
            minutes = (Math.round(secondsTotal) / 60) - (days * 1440) - (hours * 60);
            seconds = Math.round(secondsTotal) % 60;
        }//  w w w  . j  av  a 2s  .c  om

        public void onFinish() {
            if (ConversationChatFragment.this.isVisible()) {
                new Delete().from(Message.class).where("mensajeId = ?", msj.mensajeId).execute();
                View v = messagesList.findViewWithTag(msj.mensajeId);
                if (v != null)
                    messagesList.removeView(v);

            } else {
                ActiveAndroid.initialize(SpeakSocket.instance);
                new Delete().from(Message.class).where("mensajeId = ?", msj.mensajeId).execute();

            }
            Message message = null;
            if (msj.emisor.equals(u.id)) {
                message = new Select().from(Message.class)
                        .where("emisor = ? or receptor = ?", msj.receptor, msj.receptor)
                        .orderBy("emitedAt DESC").executeSingle();
            } else {
                message = new Select().from(Message.class)
                        .where("emisor = ? or receptor = ?", msj.emisor, msj.emisor).orderBy("emitedAt DESC")
                        .executeSingle();
            }
            if (message != null) {
                Chats chat = null;
                if (msj.emisor.equals(u.id)) {
                    chat = new Select().from(Chats.class).where("idContacto = ?", msj.receptor).executeSingle();
                } else {
                    chat = new Select().from(Chats.class).where("idContacto = ?", msj.emisor).executeSingle();
                }
                if (chat != null) {
                    chat.mensajeId = message.mensajeId;
                    chat.lastStatus = message.status;
                    chat.emitedAt = message.emitedAt;
                    chat.lang = message.emisorLang;
                    chat.notRead = 0;
                    if (chat.idContacto.equals(message.emisor))
                        chat.lastMessage = message.mensajeTrad;
                    else
                        chat.lastMessage = message.mensaje;
                    chat.save();
                }
            } else {
                if (msj.emisor.equals(u.id)) {
                    new Delete().from(Chats.class).where("idContacto = ?", msj.receptor).execute();
                } else {
                    new Delete().from(Chats.class).where("idContacto = ?", msj.emisor).execute();
                }
            }
        }
    };

    aCounter.start();
}

From source file:me.ububble.speakall.fragment.ConversationChatFragment.java

public void startCounterStatusWriting(long timer) {

    writingCounter = new CountDownTimer(timer, 1000) {

        public void onTick(long millisUntilFinished) {
            long days, hours, minutes, seconds;
            long secondsTotal = millisUntilFinished / 1000;
            days = (Math.round(secondsTotal) / 86400);
            hours = (Math.round(secondsTotal) / 3600) - (days * 24);
            minutes = (Math.round(secondsTotal) / 60) - (days * 1440) - (hours * 60);
            seconds = Math.round(secondsTotal) % 60;
            millisToWriting = millisUntilFinished;
        }/*  w  ww.ja  va2s  .co  m*/

        public void onFinish() {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {

                }
            });
            JSONObject dataLastSeen = new JSONObject();
            try {
                dataLastSeen.put("source", u.id);
                dataLastSeen.put("target", contact.idContacto);
                if (SpeakSocket.mSocket != null)
                    if (SpeakSocket.mSocket.connected())
                        SpeakSocket.mSocket.emit("contact-lastseen", dataLastSeen);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            writingCounter = null;
        }
    };

    writingCounter.start();
}

From source file:me.ububble.speakall.fragment.ConversationGroupFragment.java

public void startCounterToDelete(long timer, final MsgGroups msj) {

    CountDownTimer aCounter = new CountDownTimer(timer, 1000) {

        public void onTick(long millisUntilFinished) {
            long days, hours, minutes, seconds;
            long secondsTotal = millisUntilFinished / 1000;
            days = (Math.round(secondsTotal) / 86400);
            hours = (Math.round(secondsTotal) / 3600) - (days * 24);
            minutes = (Math.round(secondsTotal) / 60) - (days * 1440) - (hours * 60);
            seconds = Math.round(secondsTotal) % 60;
        }/*from w  w  w. jav  a  2  s  .  c om*/

        public void onFinish() {
            if (ConversationGroupFragment.this.isVisible()) {
                new Delete().from(MsgGroups.class).where("mensajeId = ?", msj.mensajeId).execute();
                View v = messagesList.findViewWithTag(msj.mensajeId);
                if (v != null)
                    messagesList.removeView(v);
            } else {
                ActiveAndroid.initialize(SpeakSocket.instance);
                new Delete().from(MsgGroups.class).where("mensajeId = ?", msj.mensajeId).execute();
            }
        }
    };

    aCounter.start();
}