private Boolean login(final LoLin1Account acc) { ChatServer chatServer; chatServer = ChatServer.valueOf(acc.getRealmEnum().name().toUpperCase(Locale.ENGLISH)); try { api = new LoLChat( LoLin1Application.getInstance().getContext(), ConnectivityManager.CONNECTIVITY_ACTION, chatServer, Boolean.FALSE); } catch (IOException e) { Crashlytics.logException(e); e.printStackTrace(System.err); if (!(e instanceof SSLException)) { launchBroadcastLoginFailed(); } return Boolean.FALSE; } Boolean loginSuccess = Boolean.FALSE; try { loginSuccess = api.login(acc.getUsername(), acc.getPassword()); } catch (IOException e) { Crashlytics.logException(e); } if (loginSuccess) { api.reloadRoster(); isConnected = Boolean.TRUE; return Boolean.TRUE; } else { isConnected = Boolean.FALSE; return Boolean.FALSE; } }
private void connect(final LoLin1Account acc) { mSmackAndroid = LoLChat.init(getApplicationContext()); mLoginTask = new AsyncTask<LoLin1Account, Void, Void>() { @Override protected Void doInBackground(LoLin1Account... params) { Boolean loginSuccess = login(params[0]); if (loginSuccess) { launchBroadcastLoginSuccessful(); setUpChatOverviewListener(); } else { launchBroadcastLoginFailed(); } return null; } }; mLoginTask.executeOnExecutor(Executors.newSingleThreadExecutor(), acc); }
private void disconnect() { // All the null checks are necessary because this method is run when an account is added // from out of the app as well try { if (mLoginTask != null) mLoginTask.get(); // Disconnecting in the middle of a login may be troublesome } catch (InterruptedException | ExecutionException e) { Crashlytics.logException(e); } try { if (api != null) { api.disconnect(); api = null; } } catch (SmackException.NotConnectedException e) { Crashlytics.logException(e); } if (mSmackAndroid != null) mSmackAndroid.onDestroy(); isConnected = Boolean.FALSE; }
@SuppressWarnings("unused") public static List<Friend> getOnlineFriends() { return api.getOnlineFriends(); }
private void setUpChatOverviewListener() { api.addFriendListener( new FriendListener() { @Override public void onFriendLeave(Friend friend) { ChatIntentService.this.launchBroadcastChatEvent(); } @Override public void onFriendJoin(Friend friend) { ChatIntentService.this.launchBroadcastChatEvent(); } @Override public void onFriendAvailable(Friend friend) { ChatIntentService.this.launchBroadcastChatEvent(); } @Override public void onFriendAway(Friend friend) { ChatIntentService.this.launchBroadcastChatEvent(); } @Override public void onFriendBusy(Friend friend) { ChatIntentService.this.launchBroadcastChatEvent(); } @Override public void onFriendStatusChange(Friend friend) { ChatIntentService.this.launchBroadcastChatEvent(); } }); api.addChatListener( new ChatListener() { @Override public void onMessage(Friend friend, String message) { final Context context = LoLin1Application.getInstance().getContext(); ChatMessageWrapper messageWrapper = new ChatMessageWrapper(message, System.currentTimeMillis(), friend); ChatHistoryManager.addMessageToFriendChat(messageWrapper, friend); launchBroadcastMessageReceived(message, friend.getName()); new AsyncTask<Object, Void, Boolean>() { private Context mContext; private String mMessage; private Friend mFriend; @Override protected Boolean doInBackground(Object... params) { mContext = (Context) params[0]; mMessage = (String) params[1]; mFriend = (Friend) params[2]; return Utils.isRunningOnForeground(mContext); } @Override protected void onPostExecute(Boolean isOnForeground) { if (!isOnForeground) ChatNotificationManager.createOrUpdateMessageReceivedNotification( mContext, mMessage, mFriend); } }.executeOnExecutor(Executors.newSingleThreadExecutor(), context, message, friend); } }); }