Commit cfe3cf4f by Kunj Gupta

Added logout api.

parent 22845514
......@@ -8,6 +8,8 @@ import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Query;
/**
......@@ -33,6 +35,9 @@ public interface LoginApi {
@Field(LoginApiResponse.Json.CLIENT_SECRET) String clientSecret,
@Field(LoginApiResponse.Json.REFRESH_TOKEN) String refreshToken);
@PUT(Constants.URL_PUT_LOGOUT)
Call<ResponseBody> logout(@Query(LoginApiResponse.Json.JSON_SYS_ID) String userSysId);
}
package com.vsoft.servicenow.api.listeners.put;
/**
* @since 1.0
* @author Kunj on 17/04/2018
*
*/
public interface PutLogoutApiListener {
void onDoneApiCall();
void onFailApiCall();
}
......@@ -3,6 +3,7 @@ package com.vsoft.servicenow.api.managers;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
......@@ -13,6 +14,7 @@ import com.google.gson.JsonParseException;
import com.vsoft.servicenow.api.RestClient;
import com.vsoft.servicenow.api.interfaces.LoginApi;
import com.vsoft.servicenow.api.listeners.get.GetUserLoginApiListener;
import com.vsoft.servicenow.api.listeners.put.PutLogoutApiListener;
import com.vsoft.servicenow.api.pojos.LoginApiResponse;
import com.vsoft.servicenow.utils.Constants;
import com.vsoft.servicenow.enums.SyncStatus;
......@@ -55,7 +57,7 @@ public class LoginApiManager {
try {
value = json.getAsLong();
} catch (NumberFormatException e) {
CatalogueLog.d("LoginApiManager: getCatalogues: deserialize: long.class: NumberFormatException: ");
CatalogueLog.d("LoginApiManager: logout: deserialize: long.class: NumberFormatException: ");
}
return value;
}
......@@ -147,4 +149,41 @@ public class LoginApiManager {
return SyncStatus.FAIL;
}
}
public static void logout(Context context, PutLogoutApiListener listener) {
CatalogueLog.d("LoginApiManager: logout");
String accessToken = PrefManager.getSharedPref(context, PrefManager.PREFERENCE_ACCESS_TOKEN);
String userSysId = PrefManager.getSharedPref(context, PrefManager.PREFERENCE_USER_SYS_ID);
if (accessToken.isEmpty() || userSysId.isEmpty()) {
listener.onFailApiCall();
return;
}
final Retrofit retrofit = RestClient.getInitializedRestAdapter(accessToken);
Call<ResponseBody> call = retrofit.create(LoginApi.class).logout(userSysId);
try {
Response<ResponseBody> response = call.execute();
if (response.isSuccessful()) {
listener.onDoneApiCall();
} else {
CatalogueLog.d("LoginApiManager: logout: response is not success");
if (response.code() == 401) {
Log.d(Constants.TAG, "-- is 401, try refresh token...");
Log.d(Constants.TAG, "refresh token: " + PrefManager.getSharedPref(context, PrefManager.PREFERENCE_REFRESH_TOKEN));
SyncStatus status = LoginApiManager.refreshLogin(context, PrefManager.getSharedPref(context, PrefManager.PREFERENCE_REFRESH_TOKEN));
if (status == SyncStatus.SUCCESS) {
CatalogueLog.d("refresh token success, retry same...");
logout(context, listener);
} else {
CatalogueLog.d("refresh token failed, return FAIL");
}
} else {
listener.onFailApiCall();
}
}
} catch(IOException e) {
CatalogueLog.e("logout: IOException: ", e);
listener.onFailApiCall();
}
}
}
......@@ -73,5 +73,6 @@ public class LoginApiResponse {
public static final String REFRESH_TOKEN = "refresh_token";
public static final String JSON_ACCESS_TOKEN = "access_token";
public static final String JSON_SYS_ID = "sys_id";
}
}
......@@ -38,6 +38,7 @@ import com.vsoft.servicenow.speechRecognizer.OnDSPermissionsListener;
import com.vsoft.servicenow.utils.CatalogueLog;
import com.vsoft.servicenow.utils.Constants;
import com.vsoft.servicenow.utils.PrefManager;
import com.vsoft.servicenow.utils.Util;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -185,6 +186,8 @@ public class ChatActivity extends HandleNotificationActivity implements OnDSList
mVoiceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Util.hideSoftKeyboard(ChatActivity.this, v);
if (null == mLoggedInUsername) return;
if (!mSocket.connected()) return;
......@@ -254,7 +257,6 @@ public class ChatActivity extends HandleNotificationActivity implements OnDSList
addMessage(mLoggedInUsername, message);
// perform the sending message attempt.
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put(CLIENT_MESSAGE_KEY, message);
......@@ -262,7 +264,6 @@ public class ChatActivity extends HandleNotificationActivity implements OnDSList
} catch (JSONException e) {
e.printStackTrace();
}
CatalogueLog.e("jsonObject: "+jsonObject);
mSocket.emit(NEW_MESSAGE, jsonObject);
}
......
package com.vsoft.servicenow.ui;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.GridView;
import com.google.android.gms.analytics.Tracker;
......@@ -12,10 +15,20 @@ import com.vsoft.servicenow.CatalogueApplication;
import com.vsoft.servicenow.MenuProvider;
import com.vsoft.servicenow.R;
import com.vsoft.servicenow.adapters.HomeScreenAdapter;
import com.vsoft.servicenow.api.interfaces.LoginApi;
import com.vsoft.servicenow.api.listeners.get.GetCatalogueApiListener;
import com.vsoft.servicenow.api.listeners.put.PutLogoutApiListener;
import com.vsoft.servicenow.api.managers.CatalogueApiManager;
import com.vsoft.servicenow.api.managers.LoginApiManager;
import com.vsoft.servicenow.db.managers.CatalogueManager;
import com.vsoft.servicenow.db.models.Catalogue;
import com.vsoft.servicenow.enums.SyncStatus;
import com.vsoft.servicenow.menu.HomeScreenMenuItemData;
import com.vsoft.servicenow.utils.PrefManager;
import com.vsoft.servicenow.utils.Util;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
......@@ -25,8 +38,10 @@ import butterknife.OnItemClick;
* Created by Kunj on 11/8/16.
*/
public class HomeScreen extends HandleNotificationActivity {
@BindView(R.id.tool_bar_view) Toolbar mToolbar;
@BindView(R.id.home_screen_grid_view) GridView mGridView;
@BindView(R.id.tool_bar_view)
Toolbar mToolbar;
@BindView(R.id.home_screen_grid_view)
GridView mGridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -34,8 +49,6 @@ public class HomeScreen extends HandleNotificationActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
CatalogueApplication app = (CatalogueApplication) getApplication();
ButterKnife.bind(this);
CatalogueApplication application = (CatalogueApplication) getApplication();
......@@ -62,17 +75,7 @@ public class HomeScreen extends HandleNotificationActivity {
.setCancelable(false)
.setPositiveButton(R.string.ok_string, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_ACCESS_TOKEN, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_REFRESH_TOKEN, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_USER_LAST_NAME, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_USER_SYS_ID, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_USER_FIRST_NAME, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_USER_ID, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_USER_FULL_NAME, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_USER_EMAIL_ID, "");
Intent loginIntent = new Intent(HomeScreen.this, LoginScreen.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(loginIntent);
new LogoutTask().execute();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
......@@ -83,4 +86,70 @@ public class HomeScreen extends HandleNotificationActivity {
AlertDialog alert = builder.create();
alert.show();
}
class LogoutTask extends AsyncTask<String, Void, SyncStatus> {
private ProgressDialog progressDialog;
private SyncStatus syncStatus = SyncStatus.FAIL;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(HomeScreen.this);
progressDialog.setMessage(getString(R.string.loading_string));
progressDialog.show();
progressDialog.setCancelable(false);
}
@Override
protected SyncStatus doInBackground(String... params) {
LoginApiManager.logout(HomeScreen.this, new PutLogoutApiListener() {
@Override
public void onDoneApiCall() {
syncStatus = SyncStatus.SUCCESS;
}
@Override
public void onFailApiCall() {
syncStatus = SyncStatus.FAIL;
}
});
return syncStatus;
}
@Override
protected void onPostExecute(SyncStatus syncStatus) {
super.onPostExecute(syncStatus);
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
if (syncStatus == SyncStatus.SUCCESS) {
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_ACCESS_TOKEN, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_REFRESH_TOKEN, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_USER_LAST_NAME, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_USER_SYS_ID, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_USER_FIRST_NAME, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_USER_ID, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_USER_FULL_NAME, "");
PrefManager.setSharedPref(HomeScreen.this, PrefManager.PREFERENCE_USER_EMAIL_ID, "");
Intent loginIntent = new Intent(HomeScreen.this, LoginScreen.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(loginIntent);
} else {
showErrorDialog(R.string.failed_to_logout_string);
}
}
}
private void showErrorDialog(int message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message)
.setCancelable(false)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
......@@ -150,6 +150,7 @@ public class Constants {
public static final String URL_REFRESH_LOGIN = URL_POST_LOGIN_ITEM;
public static final String URL_PUT_DEVICE_REGISTERATION = "api/now/table/sys_user/{user_sys_id}";
public static final String URL_GET_USERDETAILS = API_PATH + "sys_user";
public static final String URL_PUT_LOGOUT = "/api/vsng2/uofl_mobile/logout";
/*Catalogue Category API */
public static final String URL_GET_CATALOGUE = DOMAIN + AppConfig.URL_GET_CATALOGUE;
......
......@@ -39,6 +39,7 @@
<string name="failed_to_fetch_user_detail_string">Failed to fetch User Details.</string>
<string name="failed_to_fetch_hr_case_category_string">Failed to fetch HR Case Category.</string>
<string name="failed_to_fetch_hr_case_items_category_string">Failed to fetch HR Case Category Items.</string>
<string name="failed_to_logout_string">Failed to logout.</string>
<!--Login Screen-->
<string name="prompt_relogin_login_expired">Login expired, please login again&#8230;</string>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment