From bb6dd220847b9645514e25c2fea79fce76f8bd35 Mon Sep 17 00:00:00 2001 From: anshulv1401 Date: Mon, 18 Apr 2022 10:25:12 +0530 Subject: [PATCH 1/4] Packet Status sync job service added --- client/app/src/main/AndroidManifest.xml | 5 ++ .../app/activites/ListingActivity.java | 66 +++++++++++++++++++ .../PacketStatusSyncJobService.java | 62 +++++++++++++++++ .../src/main/res/layout/activity_login.xml | 4 +- .../src/main/res/layout/listing_activity.xml | 56 ++++++++++++---- .../dto/http/PacketStatusUpdateDto.java | 20 ++++++ .../http/PacketStatusUpdateResponseDto.java | 16 +++++ .../service/PacketServiceImpl.java | 36 ++++++++++ .../clientmanager/spi/PacketService.java | 7 ++ .../clientmanager/spi/SyncRestService.java | 5 +- 10 files changed, 259 insertions(+), 18 deletions(-) create mode 100644 client/app/src/main/java/io/mosip/registration/app/jobservice/PacketStatusSyncJobService.java create mode 100644 client/clientmanager/src/main/java/io/mosip/registration/clientmanager/dto/http/PacketStatusUpdateDto.java create mode 100644 client/clientmanager/src/main/java/io/mosip/registration/clientmanager/dto/http/PacketStatusUpdateResponseDto.java diff --git a/client/app/src/main/AndroidManifest.xml b/client/app/src/main/AndroidManifest.xml index 95053e42c..07c000274 100644 --- a/client/app/src/main/AndroidManifest.xml +++ b/client/app/src/main/AndroidManifest.xml @@ -5,8 +5,10 @@ + + + \ No newline at end of file diff --git a/client/app/src/main/java/io/mosip/registration/app/activites/ListingActivity.java b/client/app/src/main/java/io/mosip/registration/app/activites/ListingActivity.java index f078e4824..6fdd79f34 100644 --- a/client/app/src/main/java/io/mosip/registration/app/activites/ListingActivity.java +++ b/client/app/src/main/java/io/mosip/registration/app/activites/ListingActivity.java @@ -1,5 +1,8 @@ package io.mosip.registration.app.activites; +import android.app.job.JobInfo; +import android.app.job.JobScheduler; +import android.content.ComponentName; import android.content.Context; import android.os.Bundle; import android.util.Log; @@ -12,6 +15,7 @@ import androidx.lifecycle.ViewModelProvider; import dagger.android.support.DaggerAppCompatActivity; import io.mosip.registration.app.R; +import io.mosip.registration.app.jobservice.PacketStatusSyncJobService; import io.mosip.registration.app.viewmodel.ListingViewModel; import io.mosip.registration.app.viewmodel.ViewModelFactory; import io.mosip.registration.clientmanager.entity.Registration; @@ -35,6 +39,50 @@ public void onCreate(Bundle savedInstanceState) { ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressbar); progressBar.setVisibility(View.VISIBLE); + final Button syncPacketStatusButton = findViewById(R.id.listing_pkt_status_sync); + final Button jobScheduleButton = findViewById(R.id.listing_job_schedule); + final Button jobCancelButton = findViewById(R.id.listing_job_cancel); + + syncPacketStatusButton.setOnClickListener(v -> { + Toast.makeText(ListingActivity.this,"Starting packet status sync", Toast.LENGTH_SHORT).show(); + try { + packetService.syncAllPacketStatus(); + } catch (Exception e) { + Log.e(TAG, "packet status sync failed", e); + Toast.makeText(ListingActivity.this, "packet status sync failed", Toast.LENGTH_SHORT).show(); + } + }); + + jobScheduleButton.setOnClickListener(v -> { + Toast.makeText(ListingActivity.this,"Setting up job", Toast.LENGTH_SHORT).show(); + try { + int resultCode = scheduleJob(); + + if (resultCode == JobScheduler.RESULT_SUCCESS) { + Log.d(TAG, "Job scheduled"); + Toast.makeText(ListingActivity.this, "Job scheduled", Toast.LENGTH_SHORT).show(); + } else { + Log.d(TAG, "Job scheduling failed"); + Toast.makeText(ListingActivity.this, "Job scheduling failed", Toast.LENGTH_SHORT).show(); + } + } catch (Exception e) { + Log.e(TAG, "Job scheduling failed", e); + Toast.makeText(ListingActivity.this, "Job scheduling failed", Toast.LENGTH_SHORT).show(); + } + }); + + jobCancelButton.setOnClickListener(v -> { + Toast.makeText(ListingActivity.this,"Cancelling Job", Toast.LENGTH_SHORT).show(); + try { + cancelJob(); + Log.d(TAG, "Job cancelled"); + Toast.makeText(ListingActivity.this, "Job cancelled", Toast.LENGTH_SHORT).show(); + } catch (Exception e) { + Log.e(TAG, "Cancelling Job failed", e); + Toast.makeText(ListingActivity.this, "Cancelling Job failed", Toast.LENGTH_SHORT).show(); + } + }); + //to display back button getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setTitle("Registrations"); @@ -117,4 +165,22 @@ public class ViewHolder { Button syncButton; Button uploadButton; } + + private int scheduleJob() { + ComponentName componentName = new ComponentName(this, PacketStatusSyncJobService.class); + JobInfo info = new JobInfo.Builder(123, componentName) + //.setRequiresCharging(false) + //.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED) + .setPersisted(true) + .setPeriodic(15 * 60 * 1000) + .build(); + + JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE); + return scheduler.schedule(info); + } + + private void cancelJob() { + JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE); + scheduler.cancel(123); + } } diff --git a/client/app/src/main/java/io/mosip/registration/app/jobservice/PacketStatusSyncJobService.java b/client/app/src/main/java/io/mosip/registration/app/jobservice/PacketStatusSyncJobService.java new file mode 100644 index 000000000..d9ffef0ec --- /dev/null +++ b/client/app/src/main/java/io/mosip/registration/app/jobservice/PacketStatusSyncJobService.java @@ -0,0 +1,62 @@ +package io.mosip.registration.app.jobservice; + +import android.app.job.JobParameters; +import android.app.job.JobService; +import android.util.Log; +import android.widget.Toast; + +import androidx.work.Configuration; + +import javax.inject.Inject; + +import io.mosip.registration.clientmanager.spi.PacketService; + + +public class PacketStatusSyncJobService extends JobService { + private static final String TAG = "PacketStatusSyncJobService"; + private boolean jobCancelled = false; + private Thread jobThread; + + @Inject + PacketService packetService; + + public PacketStatusSyncJobService(){ + Configuration.Builder builder = new Configuration.Builder(); + builder.setJobSchedulerJobIdRange(0, 1000); + } + @Override + public boolean onStartJob(JobParameters params) { + Log.d(TAG, "Job started"); + doBackgroundWork(params); + + return true; + } + + private void doBackgroundWork(final JobParameters params) { + jobThread = new Thread(() -> { + + Log.d(TAG, "Packet Status Sync Job Service Started"); + + Toast.makeText(getBaseContext(),"Starting packet status sync", Toast.LENGTH_SHORT).show(); + try { + packetService.syncAllPacketStatus(); + } catch (Exception e) { + Log.e(TAG, "packet status sync failed", e); + Toast.makeText(getBaseContext(), "packet status sync failed", Toast.LENGTH_SHORT).show(); + } + + Log.d(TAG, "Packet Status Sync Job Service Completed"); + jobFinished(params, false); + }); + + jobThread.start(); + } + + @Override + public boolean onStopJob(JobParameters params) { + if (jobThread != null && jobThread.isAlive()) { + jobThread.interrupt(); + } + return true; + } +} \ No newline at end of file diff --git a/client/app/src/main/res/layout/activity_login.xml b/client/app/src/main/res/layout/activity_login.xml index f025547b1..1de930178 100644 --- a/client/app/src/main/res/layout/activity_login.xml +++ b/client/app/src/main/res/layout/activity_login.xml @@ -48,7 +48,7 @@ android:layout_marginEnd="24dp" android:drawableRight="@drawable/outline_person" android:hint="@string/prompt_username" - android:text="username" + android:text="110006" android:inputType="textEmailAddress" android:selectAllOnFocus="true" app:layout_constraintEnd_toEndOf="parent" @@ -62,7 +62,7 @@ android:layout_marginStart="24dp" android:layout_marginTop="8dp" android:layout_marginEnd="24dp" - android:text="password" + android:text="mosip" android:drawableRight="@drawable/outline_lock" android:hint="@string/prompt_password" android:imeActionLabel="@string/action_sign_in_short" diff --git a/client/app/src/main/res/layout/listing_activity.xml b/client/app/src/main/res/layout/listing_activity.xml index a60a8e834..d34824cd0 100644 --- a/client/app/src/main/res/layout/listing_activity.xml +++ b/client/app/src/main/res/layout/listing_activity.xml @@ -5,23 +5,51 @@ android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".activites.ListingActivity"> + +