-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from Infosys/cron-job
Cron job for auto upload
- Loading branch information
Showing
12 changed files
with
221 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
android/app/src/main/java/io/mosip/registration_client/CronParserUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.mosip.registration_client; | ||
|
||
import com.cronutils.model.Cron; | ||
import com.cronutils.model.CronType; | ||
import com.cronutils.model.definition.CronDefinitionBuilder; | ||
import com.cronutils.model.time.ExecutionTime; | ||
import com.cronutils.parser.CronParser; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.util.Date; | ||
|
||
public class CronParserUtil { | ||
|
||
public static long getNextExecutionTimeInMillis(String cronExpression) { | ||
CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ)); | ||
Cron cron = parser.parse(cronExpression); | ||
|
||
ZonedDateTime now = ZonedDateTime.now(); | ||
ExecutionTime executionTime = ExecutionTime.forCron(cron); | ||
|
||
Date nextExecution = Date.from(executionTime.nextExecution(now).get().toInstant()); | ||
|
||
return nextExecution.getTime(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
android/app/src/main/java/io/mosip/registration_client/UploadBackgroundService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package io.mosip.registration_client; | ||
|
||
import android.app.Notification; | ||
import android.app.NotificationChannel; | ||
import android.app.NotificationManager; | ||
import android.app.Service; | ||
import android.content.Intent; | ||
import android.os.Build; | ||
import android.os.IBinder; | ||
import android.util.Log; | ||
|
||
|
||
public class UploadBackgroundService extends Service { | ||
|
||
private static final int NOTIFICATION_ID = 1; | ||
private static final String CHANNEL_ID = "ForegroundServiceChannel"; | ||
|
||
@Override | ||
public void onCreate() { | ||
super.onCreate(); | ||
} | ||
|
||
@Override | ||
public int onStartCommand(Intent intent, int flags, int startId) { | ||
createNotificationChannel(); | ||
Notification notification = createNotification(); | ||
startForeground(NOTIFICATION_ID, notification); | ||
Log.d(getClass().getSimpleName(), "Upload Packets in background activity"); | ||
Intent broadcastIntent = new Intent("REGISTRATION_PACKET_UPLOAD"); | ||
sendBroadcast(broadcastIntent); | ||
return START_STICKY; | ||
} | ||
|
||
private Notification createNotification() { | ||
// Create a notification for the foreground service | ||
Notification.Builder builder; | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
builder = new Notification.Builder(this, CHANNEL_ID); | ||
} else { | ||
builder = new Notification.Builder(this); | ||
} | ||
|
||
return builder.setContentTitle("Foreground Service") | ||
.setContentText("Running...") | ||
.build(); | ||
} | ||
|
||
private void createNotificationChannel() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
NotificationChannel channel = new NotificationChannel( | ||
CHANNEL_ID, | ||
"Foreground Service Channel", | ||
NotificationManager.IMPORTANCE_DEFAULT | ||
); | ||
NotificationManager manager = getSystemService(NotificationManager.class); | ||
if (manager != null) { | ||
manager.createNotificationChannel(channel); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void onDestroy() { | ||
super.onDestroy(); | ||
} | ||
|
||
@Override | ||
public IBinder onBind(Intent intent) { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.