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

AC-467 : user details Change detection #428

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+132 −1
Diff settings

Always

Just for now

@@ -180,6 +180,8 @@
<service
android:name=".api.FormListService"
android:enabled="true" />
<service android:name=".services.AuthenticateCheckService"
android:enabled="true"/>

<activity
android:name=".activities.formdisplay.FormDisplayActivity"
@@ -33,6 +33,7 @@
import org.openmrs.mobile.models.Link;
import org.openmrs.mobile.models.Obscreate;
import org.openmrs.mobile.security.SecretKeyGenerator;
import org.openmrs.mobile.services.AuthenticateCheckService;
import org.openmrs.mobile.utilities.ApplicationConstants;

import java.io.File;
@@ -63,8 +64,11 @@ public void onCreate() {

Intent i=new Intent(this,FormListService.class);
startService(i);
Intent intent=new Intent(this,AuthenticateCheckService.class);
startService(intent);
}


protected void initializeDB() {
Configuration.Builder configurationBuilder = new Configuration.Builder(this);
configurationBuilder.addModelClasses(Link.class);
@@ -244,7 +248,7 @@ public void setCurrentUserInformation(Map<String, String> userInformation) {
return infoMap;
}

private void clearCurrentLoggedInUserInfo() {
public void clearCurrentLoggedInUserInfo() {
SharedPreferences prefs = OpenMRS.getInstance().getOpenMRSSharedPreferences();
SharedPreferences.Editor editor = prefs.edit();
editor.remove(ApplicationConstants.UserKeys.USER_PERSON_NAME);
@@ -0,0 +1,125 @@
package org.openmrs.mobile.services;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import org.openmrs.mobile.activities.login.LoginActivity;
import org.openmrs.mobile.api.RestApi;
import org.openmrs.mobile.api.RestServiceBuilder;
import org.openmrs.mobile.application.OpenMRS;
import org.openmrs.mobile.databases.OpenMRSDBOpenHelper;
import org.openmrs.mobile.models.Session;
import org.openmrs.mobile.utilities.ApplicationConstants;
import org.openmrs.mobile.utilities.NetworkUtils;
import org.openmrs.mobile.utilities.ToastUtil;

import java.util.Timer;
import java.util.TimerTask;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
* Created by Chathuranga on 27/07/2018.
*/

This comment has been minimized.

Copy link
@f4ww4z

f4ww4z Nov 19, 2018

Collaborator

Can you remove this comment? And add the OpenMRS license at line 1, like other Java files present in the code.


public class AuthenticateCheckService extends Service {


private IBinder mBinder = new SocketServerBinder();
private Timer mTimer;
private boolean mRunning = false;
private OpenMRS mOpenMRS = OpenMRS.getInstance();

@Override
public void onCreate() {
super.onCreate();
mTimer = new Timer();
mTimer.schedule(new TimerTask() {

@Override
public void run() {

if (mRunning) {
String username = mOpenMRS.getUsername();
String password = mOpenMRS.getPassword();

if ((!username.equals(ApplicationConstants.EMPTY_STRING)) &&
(!password.equals(ApplicationConstants.EMPTY_STRING))) {
Log.e("Service Task ", "Running");
authenticateCheck(username, password);
}

}
}
}, 10000, 10000);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mRunning = true;
return START_NOT_STICKY;
}

@Override
public IBinder onBind(Intent arg0) {
mRunning = true;
return mBinder;
}

@Override
public boolean onUnbind(Intent intent) {
mRunning = false;
return super.onUnbind(intent);
}

private void authenticateCheck(String username, String password) {
if (NetworkUtils.hasNetwork()) {
RestApi restApi = RestServiceBuilder.createService(RestApi.class, username, password);
Call<Session> call = restApi.getSession();
call.enqueue(new Callback<Session>() {
@Override
public void onResponse(Call<Session> call, Response<Session> response) {
if (response.isSuccessful()) {

Session session = response.body();
if (session.isAuthenticated()) {
Log.e("Service Task ", "user authenticated");

} else {
Log.e("Service Task ", "User Credentials Changed");
OpenMRSDBOpenHelper.getInstance().closeDatabases();
mOpenMRS.clearUserPreferencesData();
mOpenMRS.clearCurrentLoggedInUserInfo();
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);

This comment has been minimized.

Copy link
@f4ww4z

f4ww4z Nov 19, 2018

Collaborator

The user should be informed that the login credentials is incorrect anymore, before going to the Login screen. I think the user should be shown a dialog with positive and negative buttons.

This comment has been minimized.

Copy link
@VANKINEENITAWRUN

VANKINEENITAWRUN Mar 11, 2019

Contributor

@f4ww4z can you please mention what would be the action of negative and positive buttons would be? or can we show a Toast or snack bar?

This comment has been minimized.

Copy link
@f4ww4z

f4ww4z Mar 12, 2019

Collaborator

@VANKINEENITAWRUN so when the user's changed credentials from the web app, the user should be shown a dialog first that shows the user's credentials changed, and that he will be logged out. I think that only 1 button (text is 'OK') is needed.

}
} else {
ToastUtil.error("Error in AuthenticateCheckService Response");
}
}

@Override
public void onFailure(Call<Session> call, Throwable t) {
ToastUtil.error("Error in AuthenticateCheckService");
}
});
} else {
Log.e("Service Task ", "No Network");
}
}

public class SocketServerBinder extends Binder {

public AuthenticateCheckService getService() {
return AuthenticateCheckService.this;
}

}

}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.