예제 #1
0
 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;
   }
 }
예제 #2
0
 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);
 }
예제 #3
0
 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;
 }
예제 #4
0
 @SuppressWarnings("unused")
 public static List<Friend> getOnlineFriends() {
   return api.getOnlineFriends();
 }
예제 #5
0
  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);
          }
        });
  }