summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/example/ti/util/CustomTimer.java')
-rw-r--r--src/com/example/ti/util/CustomTimer.java103
1 files changed, 103 insertions, 0 deletions
diff --git a/src/com/example/ti/util/CustomTimer.java b/src/com/example/ti/util/CustomTimer.java
new file mode 100644
index 0000000..d4f2da5
--- /dev/null
+++ b/src/com/example/ti/util/CustomTimer.java
@@ -0,0 +1,103 @@
1/**************************************************************************************************
2 Filename: CustomTimer.java
3 Revised: $Date: 2013-08-30 12:02:37 +0200 (fr, 30 aug 2013) $
4 Revision: $Revision: 27470 $
5
6 Copyright (c) 2013 - 2014 Texas Instruments Incorporated
7
8 All rights reserved not granted herein.
9 Limited License.
10
11 Texas Instruments Incorporated grants a world-wide, royalty-free,
12 non-exclusive license under copyrights and patents it now or hereafter
13 owns or controls to make, have made, use, import, offer to sell and sell ("Utilize")
14 this software subject to the terms herein. With respect to the foregoing patent
15 license, such license is granted solely to the extent that any such patent is necessary
16 to Utilize the software alone. The patent license shall not apply to any combinations which
17 include this software, other than combinations with devices manufactured by or for TI (TI Devices).
18 No hardware patent is licensed hereunder.
19
20 Redistributions must preserve existing copyright notices and reproduce this license (including the
21 above copyright notice and the disclaimer and (if applicable) source code license limitations below)
22 in the documentation and/or other materials provided with the distribution
23
24 Redistribution and use in binary form, without modification, are permitted provided that the following
25 conditions are met:
26
27 * No reverse engineering, decompilation, or disassembly of this software is permitted with respect to any
28 software provided in binary form.
29 * any redistribution and use are licensed by TI for use only with TI Devices.
30 * Nothing shall obligate TI to provide you with source code for the software licensed and provided to you in object code.
31
32 If software source code is provided to you, modification and redistribution of the source code are permitted
33 provided that the following conditions are met:
34
35 * any redistribution and use of the source code, including any resulting derivative works, are licensed by
36 TI for use only with TI Devices.
37 * any redistribution and use of any object code compiled from the source code and any resulting derivative
38 works, are licensed by TI for use only with TI Devices.
39
40 Neither the name of Texas Instruments Incorporated nor the names of its suppliers may be used to endorse or
41 promote products derived from this software without specific prior written permission.
42
43 DISCLAIMER.
44
45 THIS SOFTWARE IS PROVIDED BY TI AND TIS LICENSORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
46 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 IN NO EVENT SHALL TI AND TIS LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
49 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51 POSSIBILITY OF SUCH DAMAGE.
52
53
54 **************************************************************************************************/
55package com.example.ti.util;
56
57import java.util.Timer;
58import java.util.TimerTask;
59
60import android.widget.ProgressBar;
61
62public class CustomTimer {
63 private Timer mTimer;
64 private CustomTimerCallback mCb = null;
65 private ProgressBar mProgressBar;
66 private int mTimeout;
67
68 public CustomTimer(ProgressBar progressBar, int timeout, CustomTimerCallback cb) {
69 mTimeout = timeout;
70 mProgressBar = progressBar;
71 mTimer = new Timer();
72 ProgressTask t = new ProgressTask();
73 mTimer.schedule(t, 0, 1000); // One second tick
74 mCb = cb;
75 }
76
77 public void stop() {
78 if (mTimer != null) {
79 mTimer.cancel();
80 mTimer = null;
81 }
82 }
83
84 private class ProgressTask extends TimerTask {
85 int i = 0;
86
87 @Override
88 public void run() {
89 i++;
90 if (mProgressBar != null)
91 mProgressBar.setProgress(i);
92 if (i >= mTimeout) {
93 mTimer.cancel();
94 mTimer = null;
95 if (mCb != null)
96 mCb.onTimeout();
97 } else {
98 if (mCb != null)
99 mCb.onTick(i);
100 }
101 }
102 }
103}