1
1
package com .stardust .scriptdroid .timing ;
2
2
3
3
import android .content .Intent ;
4
+ import android .text .format .Time ;
5
+
6
+ import com .raizlabs .android .dbflow .annotation .Column ;
7
+ import com .raizlabs .android .dbflow .annotation .PrimaryKey ;
8
+ import com .raizlabs .android .dbflow .annotation .Table ;
9
+ import com .stardust .autojs .execution .ExecutionConfig ;
10
+ import com .stardust .scriptdroid .external .ScriptIntents ;
11
+
12
+ import org .joda .time .Instant ;
13
+ import org .joda .time .LocalDate ;
14
+ import org .joda .time .LocalDateTime ;
15
+ import org .joda .time .LocalTime ;
16
+
17
+ import java .util .Calendar ;
18
+ import java .util .concurrent .TimeUnit ;
4
19
5
20
6
21
/**
7
22
* Created by Stardust on 2017/11/27.
8
23
*/
9
-
24
+ @ Table ( database = TimedTaskDatabase . class )
10
25
public class TimedTask {
11
26
27
+ private static final int FLAG_DISPOSABLE = 0 ;
28
+ public final static int FLAG_SUNDAY = 0x1 ;
29
+ public final static int FLAG_MONDAY = 0x2 ;
30
+ public final static int FLAG_TUESDAY = 0x4 ;
31
+ public final static int FLAG_WEDNESDAY = 0x8 ;
32
+ public final static int FLAG_THURSDAY = 0x10 ;
33
+ public final static int FLAG_FRIDAY = 0x20 ;
34
+ public final static int FLAG_SATURDAY = 0x40 ;
35
+ private static final int FLAG_EVERYDAY = 0x7F ;
36
+
37
+ @ PrimaryKey (autoincrement = true )
38
+ @ Column (name = "id" )
12
39
int mId ;
13
40
14
- boolean mDisposable ;
41
+ @ Column (name = "days_of_week" )
42
+ int mDaysOfWeek ;
15
43
44
+ @ Column (name = "scheduled" )
16
45
boolean mScheduled ;
17
46
18
- public boolean isDisposable () {
19
- return mDisposable ;
47
+ @ Column (name = "delay" )
48
+ private long mDelay = 0 ;
49
+
50
+ @ Column (name = "interval" )
51
+ private long mInterval = 0 ;
52
+
53
+ @ Column (name = "loop_times" )
54
+ private int mLoopTimes = 1 ;
55
+
56
+ @ Column (name = "millis" )
57
+ private long mMillis ;
58
+
59
+ @ Column (name = "script_path" )
60
+ private String mScriptPath ;
61
+
62
+ public TimedTask () {
63
+
20
64
}
21
65
22
- public void setDisposable (boolean disposable ) {
23
- mDisposable = disposable ;
66
+ public TimedTask (long millis , int daysOfWeek , String scriptPath , ExecutionConfig config ) {
67
+ mMillis = millis ;
68
+ mDaysOfWeek = daysOfWeek ;
69
+ mScriptPath = scriptPath ;
70
+ mDelay = config .delay ;
71
+ mLoopTimes = config .loopTimes ;
72
+ mInterval = config .interval ;
73
+ }
74
+
75
+ public boolean isDisposable () {
76
+ return mDaysOfWeek == FLAG_DISPOSABLE ;
24
77
}
25
78
26
79
public boolean isScheduled () {
@@ -32,14 +85,37 @@ public void setScheduled(boolean scheduled) {
32
85
}
33
86
34
87
public long getNextTime () {
35
- return 0 ;
88
+ if (isDisposable ()) {
89
+ return mMillis ;
90
+ }
91
+ // TODO: 2017/11/28 day of week
92
+ LocalTime time = LocalTime .fromMillisOfDay (mMillis );
93
+ long nextTimeMillis = time .toDateTimeToday ().getMillis ();
94
+ if (System .currentTimeMillis () > nextTimeMillis ) {
95
+ return nextTimeMillis + TimeUnit .DAYS .toMillis (1 );
96
+ }
97
+ return nextTimeMillis ;
36
98
}
37
99
38
100
public int getId () {
39
101
return mId ;
40
102
}
41
103
42
104
public Intent createIntent () {
43
- return null ;
105
+ return new Intent (TaskReceiver .ACTION_TASK )
106
+ .putExtra (TaskReceiver .EXTRA_TASK_ID , mId )
107
+ .putExtra (ScriptIntents .EXTRA_KEY_PATH , mScriptPath )
108
+ .putExtra (ScriptIntents .EXTRA_KEY_DELAY , mDelay )
109
+ .putExtra (ScriptIntents .EXTRA_KEY_LOOP_TIMES , mLoopTimes )
110
+ .putExtra (ScriptIntents .EXTRA_KEY_LOOP_INTERVAL , mInterval );
44
111
}
112
+
113
+ public static TimedTask dailyTask (LocalTime time , String scriptPath , ExecutionConfig config ) {
114
+ return new TimedTask (time .getMillisOfDay (), FLAG_EVERYDAY , scriptPath , config );
115
+ }
116
+
117
+ public static TimedTask disposableTask (long millis , String scriptPath , ExecutionConfig config ) {
118
+ return new TimedTask (millis , FLAG_EVERYDAY , scriptPath , config );
119
+ }
120
+
45
121
}
0 commit comments