Commit b4ea5dd4 by Kunj Gupta

Added authorized user for chat, but is not done.

parent 064686d9
......@@ -28,12 +28,16 @@ import android.widget.Toast;
import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.Socket;
import com.google.gson.JsonObject;
import com.vsoft.servicenow.CatalogueApplication;
import com.vsoft.servicenow.R;
import com.vsoft.servicenow.api.managers.LoginApiManager;
import com.vsoft.servicenow.enums.SyncStatus;
import com.vsoft.servicenow.speechRecognizer.DroidSpeech;
import com.vsoft.servicenow.speechRecognizer.OnDSListener;
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 org.json.JSONException;
......@@ -50,16 +54,23 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
private static final String TAG = "ChatActivity";
private static String NEW_MESSAGE = "new message";
private static String AUTHENTICATED = "authenticated";
private static String UNAUTHORIZED = "unauthorized";
private static String ADD_USER = "add user";
private static String NEW_MESSAGE_USER_NAME = "username";
private static String NEW_MESSAGE_MESSAGE = "message";
private static final int REQUEST_LOGIN = 0;
private static final int TYPING_TIMER_LENGTH = 600;
private static final int CHECK_CODE = 102;
private RecyclerView mMessagesView;
private EditText mInputMessageView;
private List<Message> mMessages = new ArrayList<Message>();
private List<Message> mMessages = new ArrayList<>();
private RecyclerView.Adapter mAdapter;
private boolean mTyping = false;
private Handler mTypingHandler = new Handler();
private String mUsername;
private Socket mSocket;
......@@ -107,7 +118,9 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect);
mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
mSocket.on("new message", onNewMessage);
mSocket.on(NEW_MESSAGE, onNewMessage);
mSocket.on(AUTHENTICATED, authenticated);
mSocket.on(UNAUTHORIZED, unAuthorized);
mSocket.connect();
......@@ -119,7 +132,7 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
mMessagesView.setLayoutManager(new LinearLayoutManager(this));
mMessagesView.setAdapter(mAdapter);
mUsername = getIntent().getStringExtra("username");
mUsername = getIntent().getStringExtra(Constants.DATA_KEY_CHAT_USER_NAME);
mInputMessageView = (EditText) findViewById(R.id.message_input);
mInputMessageView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
......@@ -134,27 +147,6 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
}
});
mInputMessageView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (null == mUsername) return;
if (!mSocket.connected()) return;
if (!mTyping) {
mTyping = true;
mSocket.emit("typing");
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
droidSpeech = new DroidSpeech(this, getSupportFragmentManager());
droidSpeech.setOnDroidSpeechListener(this);
......@@ -189,6 +181,9 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
mMessagesView.addOnItemTouchListener(mOnItemTouchListener);
}
/**
* this message calls when we will add send and receive message in chat screen.
*/
private void addMessage(String username, String message) {
if(username.isEmpty() || message.isEmpty())
return;
......@@ -206,6 +201,9 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
scrollToBottom();
}
/**
* This method calls when we will send message to server.
*/
private void attemptSend() {
if (null == mUsername) return;
if (!mSocket.connected()) return;
......@@ -215,8 +213,6 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
speaker.stop();
}
mTyping = false;
String message = mInputMessageView.getText().toString().trim();
if (TextUtils.isEmpty(message)) {
mInputMessageView.requestFocus();
......@@ -227,38 +223,38 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
addMessage(mUsername, message);
// perform the sending message attempt.
mSocket.emit("new message", message);
mSocket.emit(NEW_MESSAGE, message);
}
private void scrollToBottom() {
mMessagesView.scrollToPosition(mAdapter.getItemCount() - 1);
}
/**
* Chat server calls this if we will connect with server.
*/
private Emitter.Listener onConnect = new Emitter.Listener() {
@Override
public void call(Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (!isConnected) {
if (null != mUsername)
mSocket.emit("add user", mUsername);
Toast.makeText(getApplicationContext(),
R.string.connect, Toast.LENGTH_LONG).show();
isConnected = true;
}
userAuthenticateTask();
}
});
}
};
/**
* Chat server calls this if we will disconnect from server.
*/
private Emitter.Listener onDisconnect = new Emitter.Listener() {
@Override
public void call(Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i(TAG, "diconnected");
Log.e(TAG, "diconnected");
isConnected = false;
Toast.makeText(getApplicationContext(),
R.string.disconnect, Toast.LENGTH_LONG).show();
......@@ -267,6 +263,9 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
}
};
/**
* Chat server calls this if we will get any error in connection.
*/
private Emitter.Listener onConnectError = new Emitter.Listener() {
@Override
public void call(Object... args) {
......@@ -281,6 +280,9 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
}
};
/**
* Chat server calls this if we will receive new message.
*/
private Emitter.Listener onNewMessage = new Emitter.Listener() {
@Override
public void call(final Object... args) {
......@@ -291,8 +293,8 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
String username;
String message;
try {
username = data.getString("username");
message = data.getString("message");
username = data.getString(NEW_MESSAGE_USER_NAME);
message = data.getString(NEW_MESSAGE_MESSAGE);
} catch (JSONException e) {
Log.e(TAG, e.getMessage());
return;
......@@ -304,7 +306,85 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
}
};
private final int CHECK_CODE = 102;
/**
* Chat server calling this if user has successfully authenticated
*/
private Emitter.Listener authenticated = new Emitter.Listener() {
@Override
public void call(final Object... args) {
Log.e(TAG, "authorized user: "+args[0]);
runOnUiThread(new Runnable() {
@Override
public void run() {
if (!isConnected) {
if (null != mUsername)
mSocket.emit(ADD_USER, mUsername);
Toast.makeText(getApplicationContext(),
R.string.connect, Toast.LENGTH_LONG).show();
isConnected = true;
}
}
});
}
};
/**
* Chat server calls this method if user is unauthorized
*/
private Emitter.Listener unAuthorized = new Emitter.Listener() {
@Override
public void call(final Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
//TODO : need to check if access token is valid or not.
Log.e(TAG, "unauthorized user: "+args[0]);
Toast.makeText(getApplicationContext(),
R.string.unauthorized_user, Toast.LENGTH_LONG).show();
}
});
}
};
private void refreshTokenApiTask() {
Log.d(Constants.TAG, "-- is 401, try refresh token...");
Log.d(Constants.TAG, "refresh token: " + PrefManager.getSharedPref(this, PrefManager.PREFERENCE_REFRESH_TOKEN));
SyncStatus status = LoginApiManager.refreshLogin(this, PrefManager.getSharedPref(this, PrefManager.PREFERENCE_REFRESH_TOKEN));
if (status == SyncStatus.SUCCESS) {
CatalogueLog.d("refresh token success, retry same...");
userAuthenticateTask();
} else {
CatalogueLog.d("refresh token failed, return FAIL");
}
}
private void userAuthenticateTask() {
String token = "token";
String url = "url";
String authentication = "authentication";
Log.e(TAG, "USER IS TRYING TO MAKING AUTHORIZED CONNECTION");
// TODO : pass the user auth token to chat server
//sending token to server
String accessToken = PrefManager.getSharedPref(ChatActivity.this, PrefManager.PREFERENCE_ACCESS_TOKEN);
String loginUserName = PrefManager.getSharedPref(ChatActivity.this, PrefManager.PREFERENCE_LOGIN_USER_NAME);
if(!TextUtils.isEmpty(accessToken) && !TextUtils.isEmpty(loginUserName)) {
String userDetailURL = Constants.CHAT_USER_API_URL + "?" + Constants.URL_PARAM_SYSPRM_USERNAME + "=" + loginUserName;
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put(token, "Bearer " + accessToken);
jsonObject.put(url, userDetailURL);
CatalogueLog.e(jsonObject.toString());
} catch(JSONException e) {
CatalogueLog.e("userAuthenticateTask: JSONException: "+e.toString());
}
mSocket.emit(authentication, jsonObject);
} else {
Log.e(TAG, "Access Token is empty.");
Toast.makeText(getApplicationContext(),
R.string.error_connect, Toast.LENGTH_LONG).show();
}
}
// This callback is invoked when the Speech Recognizer returns.
// This is where you process the intent and extract the speech text from the intent.
......@@ -439,7 +519,9 @@ public class ChatActivity extends AppCompatActivity implements OnDSListener, OnD
mSocket.off(Socket.EVENT_DISCONNECT, onDisconnect);
mSocket.off(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.off(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
mSocket.off("new message", onNewMessage);
mSocket.off(NEW_MESSAGE, onNewMessage);
mSocket.off(AUTHENTICATED, authenticated);
mSocket.off(UNAUTHORIZED, unAuthorized);
speaker.destroy();
}
......
......@@ -13,6 +13,7 @@ import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.Socket;
import com.google.android.gms.analytics.Tracker;
import com.vsoft.servicenow.chat.ChatActivity;
import com.vsoft.servicenow.utils.Constants;
import com.vsoft.servicenow.utils.Util;
import com.vsoft.servicenow.CatalogueApplication;
import com.vsoft.servicenow.R;
......@@ -70,7 +71,7 @@ public class HomeScreen extends AppCompatActivity {
// perform the user login attempt.
String userFirstName = PrefManager.getSharedPref(HomeScreen.this, PrefManager.PREFERENCE_FIRST_NAME);
Intent intent = new Intent(HomeScreen.this, ChatActivity.class);
intent.putExtra("username", userFirstName);
intent.putExtra(Constants.DATA_KEY_CHAT_USER_NAME, userFirstName);
startActivity(intent);
}
}
......
......@@ -9,6 +9,7 @@
<string name="connect">Connected</string>
<string name="disconnect">Disconnected, Please check your internet connection</string>
<string name="error_connect">Failed to connect</string>
<string name="unauthorized_user">Unauthorized User</string>
<!-- messages -->
<string name="message_welcome">Chat with HR Bot</string>
......
......@@ -73,6 +73,7 @@ public class CatalogueApplication extends Application {
PrefManager.setSharedPref(CatalogueApplication.this, PrefManager.PREFERENCE_USER_ID, "");
PrefManager.setSharedPref(CatalogueApplication.this, PrefManager.PREFERENCE_USER_FULL_NAME, "");
PrefManager.setSharedPref(CatalogueApplication.this, PrefManager.PREFERENCE_USER_EMAIL_ID, "");
PrefManager.setSharedPref(CatalogueApplication.this, PrefManager.PREFERENCE_LOGIN_USER_NAME, "");
Intent loginIntent = new Intent(CatalogueApplication.this, LoginScreen.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(loginIntent);
......
......@@ -135,6 +135,7 @@ public class LoginApiManager {
CatalogueLog.d("---- 401, unSetting access and refresh token, prompt user to login again");
PrefManager.setSharedPref(context, PrefManager.PREFERENCE_ACCESS_TOKEN, "");
PrefManager.setSharedPref(context, PrefManager.PREFERENCE_REFRESH_TOKEN, "");
PrefManager.setSharedPref(context, PrefManager.PREFERENCE_LOGIN_USER_NAME, "");
Intent intent = new Intent(Constants.APPLICATION_BROADCAST_INTENT);
intent.putExtra(Constants.APPLICATION_BROADCAST_DATA_ACTION, Constants.ACTION_PROMPT_LOGIN);
......
......@@ -144,6 +144,7 @@ public class LoginScreen extends Activity {
/*Save access token in Preference*/
PrefManager.setSharedPref(LoginScreen.this, PrefManager.PREFERENCE_ACCESS_TOKEN, loginApiResponse.getAccessToken());
PrefManager.setSharedPref(LoginScreen.this, PrefManager.PREFERENCE_REFRESH_TOKEN, loginApiResponse.getRefreshToken());
PrefManager.setSharedPref(LoginScreen.this, PrefManager.PREFERENCE_LOGIN_USER_NAME, userName);
apiStatus = API_SUCCESS_USER_LOGIN;
publishProgress(USER_DETAIL);
......
......@@ -29,6 +29,7 @@ public class Constants {
public static final String DATA_KEY_CATALOGUE_TITLE = "catalogue_title";
public static final String DATA_KEY_CATALOGUE_ITEM_DESCRIPTION = "catalogue_item_des";
public static final String DATA_KEY_CATALOGUE_ITEM_SHORT_DESCRIPTION = "catalogue_item_short_des";
public static final String DATA_KEY_CHAT_USER_NAME = "username";
/**
* Broadcast custom intent
......@@ -162,4 +163,7 @@ public class Constants {
/*Incident API */
public static final String URL_GET_INCIDENTS = API_PATH + "incident";
public static final String URL_POST_INCIDENT = API_PATH + "incident";
/*Chat Activity*/
public static final String CHAT_USER_API_URL = DOMAIN + URL_GET_USERDETAILS.substring(1);
}
......@@ -21,6 +21,7 @@ public class PrefManager {
public static final String PREFERENCE_LOGIN_VALUES_KEY = "LoginPrefs";
public static final String PREFERENCE_ACCESS_TOKEN = "access_token";
public static final String PREFERENCE_REFRESH_TOKEN = "refresh_token";
public static final String PREFERENCE_LOGIN_USER_NAME = "login_user_name";
private static final String SHARED_PREFERENCE_NAME = PrefManager.class.getSimpleName();
......
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