Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mosip 21692 packet status sync job #40

Merged
merged 4 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions client/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<queries android:name="android.permission.QUERY_ALL_PACKAGES" />
<!-- android:allowBackup="true"-->

<application
android:name=".BaseApplication"
android:icon="@mipmap/mosip_logo"
Expand All @@ -15,6 +17,12 @@
android:supportsRtl="true"
android:theme="@style/Theme.registration_client">

<service
android:name="io.mosip.registration.clientmanager.jobservice.PacketStatusSyncJob"
android:label="Packet Sync Status Job"
android:permission="android.permission.BIND_JOB_SERVICE" >
</service>

<activity android:name=".activites.MainActivity"
android:label="@string/app_name" />

Expand Down Expand Up @@ -51,6 +59,12 @@
android:exported="true">
</activity>

<activity android:name=".activites.JobServiceActivity"
android:exported="true"
android:parentActivityName=".activites.MainActivity"
android:windowSoftInputMode="adjustResize">
</activity>

<activity android:name=".activites.PreviewDocumentActivity"
android:windowSoftInputMode="adjustResize"
android:parentActivityName=".activites.ScreenActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dagger.Module;
import dagger.android.ContributesAndroidInjector;
import io.mosip.registration.app.activites.*;
import io.mosip.registration.clientmanager.jobservice.PacketStatusSyncJob;

@Module
public abstract class ActivityBuildersModule {
Expand All @@ -28,6 +29,12 @@ public abstract class ActivityBuildersModule {
@ContributesAndroidInjector
abstract AboutActivity contributeAboutActivity();

@ContributesAndroidInjector
abstract JobServiceActivity jobServiceActivity();

@ContributesAndroidInjector
abstract PacketStatusSyncJob providePacketStatusSyncJob();

@ContributesAndroidInjector
abstract PreviewDocumentActivity contributePreviewDocumentActivity();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,12 @@ public MasterDataService provideMasterDataService(SyncRestService syncRestServic
GlobalParamRepository globalParamRepository,
IdentitySchemaRepository identitySchemaRepository,
BlocklistedWordRepository blocklistedWordRepository,
SyncJobDefRepository syncJobDefRepository,
UserDetailRepository userDetailRepository) {
return new MasterDataServiceImpl(appContext, syncRestService, clientCryptoManagerService,
machineRepository, registrationCenterRepository, documentTypeRepository, applicantValidDocRepository,
templateRepository, dynamicFieldRepository, keyStoreRepository, locationRepository,
globalParamRepository, identitySchemaRepository, blocklistedWordRepository, userDetailRepository);
globalParamRepository, identitySchemaRepository, blocklistedWordRepository, syncJobDefRepository, userDetailRepository);
}

@Singleton
Expand Down Expand Up @@ -211,10 +212,10 @@ UserInterfaceHelperService provideUserInterfaceHelperService() {

@Provides
@Singleton
PacketService providePacketService(RegistrationRepository registrationRepository,
PacketService providePacketService(RegistrationRepository registrationRepository, SyncJobDefRepository syncJobDefRepository,
IPacketCryptoService packetCryptoService, SyncRestService syncRestService,
MasterDataService masterDataService) {
return new PacketServiceImpl(appContext, registrationRepository, packetCryptoService, syncRestService,
return new PacketServiceImpl(appContext, registrationRepository, syncJobDefRepository, packetCryptoService, syncRestService,
masterDataService);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package io.mosip.registration.app.activites;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import android.app.job.JobScheduler;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import javax.inject.Inject;

import dagger.android.support.DaggerAppCompatActivity;
import io.mosip.registration.app.R;
import io.mosip.registration.app.util.JobServiceHelper;
import io.mosip.registration.app.viewmodel.JobServiceViewModel;
import io.mosip.registration.app.viewmodel.ViewModelFactory;
import io.mosip.registration.app.viewmodel.model.JobServiceModel;
import io.mosip.registration.clientmanager.spi.PacketService;

public class JobServiceActivity extends DaggerAppCompatActivity {

private static final String TAG = JobServiceActivity.class.getSimpleName();

@Inject
PacketService packetService;

JobServiceHelper jobServiceHelper;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_job_service);
ListView listView = (ListView) findViewById(R.id.jobList);
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressbar);
progressBar.setVisibility(View.VISIBLE);

jobServiceHelper = new JobServiceHelper(this, (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE), packetService);

//to display back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Job Service");

ViewModelFactory viewModelFactory = new ViewModelFactory(new JobServiceViewModel(jobServiceHelper, packetService));
JobServiceViewModel model = new ViewModelProvider(this, viewModelFactory).get(JobServiceViewModel.class);
model.getList().observe(this, list -> {
// update UI
ArrayAdapter<JobServiceModel> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, android.R.id.text2, list);
// Assign adapter to ListView
listView.setAdapter(new CustomListViewAdapter(this, R.layout.custom_list_view_job, list));
progressBar.setVisibility(View.GONE);
});
}

private class CustomListViewAdapter extends ArrayAdapter<JobServiceModel> {

private List<JobServiceModel> mObjects;
private int layout;

public CustomListViewAdapter(@NonNull Context context, int resource, @NonNull List<JobServiceModel> objects) {
super(context, resource, objects);
mObjects = objects;
layout = resource;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

JobServiceModel jobServiceModel = this.mObjects.get(position);
ViewHolder mainViewHolder = null;
if (convertView == null) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
convertView = layoutInflater.inflate(layout, parent, false);

ViewHolder viewHolder = new ViewHolder();
viewHolder.jobName = convertView.findViewById(R.id.jobName);
viewHolder.triggerJobButton = convertView.findViewById(R.id.triggerJob);
viewHolder.toggleActiveButton = convertView.findViewById(R.id.toggleActive);
viewHolder.toggleActiveButton.setChecked(jobServiceModel.getEnabled());

if (jobServiceModel.getActive() && jobServiceModel.getImplemented())
viewHolder.jobName.setText(jobServiceModel.getName());
else {
String name = "";
if (!jobServiceModel.getActive())
name = jobServiceModel.getName() + " (Not Active)";
else
name = jobServiceModel.getName() + " (Not implemented)";
viewHolder.jobName.setText(name);
viewHolder.toggleActiveButton.setEnabled(false);
viewHolder.triggerJobButton.setEnabled(false);
}

convertView.setTag(viewHolder);
}

mainViewHolder = (ViewHolder) convertView.getTag();

mainViewHolder.triggerJobButton.setOnClickListener(v -> {
Toast.makeText(JobServiceActivity.this, "Starting Job " + jobServiceModel.getName(), Toast.LENGTH_SHORT).show();
try {
boolean triggered = jobServiceHelper.triggerJobService(jobServiceModel.getId());
if(!triggered)
Toast.makeText(JobServiceActivity.this, jobServiceModel.getName() + " job failed. Cannot trigger disabled job.", Toast.LENGTH_SHORT).show();

} catch (Exception e) {
Log.e(TAG, jobServiceModel.getApiName() + " job failed", e);
Toast.makeText(JobServiceActivity.this, jobServiceModel.getName() + " job failed", Toast.LENGTH_SHORT).show();
}
});

mainViewHolder.toggleActiveButton.setOnCheckedChangeListener((compoundButton, isChecked) -> {
Toast.makeText(JobServiceActivity.this, "Setting up job", Toast.LENGTH_SHORT).show();
if (isChecked) {
try {
int resultCode = jobServiceHelper.scheduleJob(jobServiceModel.getId(), jobServiceModel.getApiName());

if (resultCode == JobScheduler.RESULT_SUCCESS) {
Log.d(TAG, "Job scheduled");
Toast.makeText(JobServiceActivity.this, "Job scheduled", Toast.LENGTH_SHORT).show();
} else {
Log.d(TAG, "Job scheduling failed");
Toast.makeText(JobServiceActivity.this, "Job scheduling failed", Toast.LENGTH_SHORT).show();
}
} catch (ClassNotFoundException e) {
Log.e(TAG, "Job scheduling failed : service " + jobServiceModel.getApiName() + " not implemented", e);
Toast.makeText(JobServiceActivity.this, "Job scheduling failed : service " + jobServiceModel.getApiName() + " not implemented", Toast.LENGTH_SHORT).show();
compoundButton.setChecked(false);
} catch (Exception e) {
Log.e(TAG, "Job scheduling failed", e);
Toast.makeText(JobServiceActivity.this, "Job scheduling failed : " + e.getMessage(), Toast.LENGTH_SHORT).show();
compoundButton.setChecked(false);
}
} else {
Toast.makeText(JobServiceActivity.this, "Cancelling Job", Toast.LENGTH_SHORT).show();
try {
jobServiceHelper.cancelJob(jobServiceModel.getId());
Log.d(TAG, "Job cancelled");
Toast.makeText(JobServiceActivity.this, "Job cancelled", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e(TAG, "Cancelling Job failed", e);
Toast.makeText(JobServiceActivity.this, "Cancelling Job failed", Toast.LENGTH_SHORT).show();
}
}
});

return convertView;
}
}

public class ViewHolder {
TextView jobName;
Button triggerJobButton;
Switch toggleActiveButton;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import androidx.lifecycle.ViewModelProvider;
import dagger.android.support.DaggerAppCompatActivity;
import io.mosip.registration.app.R;
import io.mosip.registration.app.viewmodel.ListingViewModel;
import io.mosip.registration.app.viewmodel.RegistrationPacketViewModel;
import io.mosip.registration.app.viewmodel.ViewModelFactory;
import io.mosip.registration.clientmanager.entity.Registration;
import io.mosip.registration.clientmanager.spi.PacketService;
Expand Down Expand Up @@ -40,9 +40,9 @@ public void onCreate(Bundle savedInstanceState) {
getSupportActionBar().setTitle("Registrations");
getSupportActionBar().setSubtitle("Note : Packets are auto approved");

ViewModelFactory viewModelFactory = new ViewModelFactory(new ListingViewModel(packetService));
ListingViewModel model = new ViewModelProvider(this, viewModelFactory).get(ListingViewModel.class);
model.getRegistrationList().observe(this, list -> {
ViewModelFactory viewModelFactory = new ViewModelFactory(new RegistrationPacketViewModel(packetService));
RegistrationPacketViewModel model = new ViewModelProvider(this, viewModelFactory).get(RegistrationPacketViewModel.class);
model.getList().observe(this, list -> {
// update UI
ArrayAdapter<Registration> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, android.R.id.text2, list);
Expand Down Expand Up @@ -118,3 +118,4 @@ public class ViewHolder {
Button uploadButton;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,8 @@ public void click_list_packets(View view) {
startActivity(intent);
}


public void click_list_JobService(View view) {
Intent intent = new Intent(this, JobServiceActivity.class);
startActivity(intent);
}
}
Loading