private void signOut(long providerId, long accountId) {
    try {

      IImConnection conn = mApp.getConnection(providerId);
      if (conn != null) {
        conn.logout();
      } else {
        // Normally, we can always get the connection when user chose to
        // sign out. However, if the application crash unexpectedly, the
        // status will never be updated. Clear the status in this case
        // to make it recoverable from the crash.
        ContentValues values = new ContentValues(2);
        values.put(Imps.AccountStatus.PRESENCE_STATUS, Imps.Presence.OFFLINE);
        values.put(Imps.AccountStatus.CONNECTION_STATUS, Imps.ConnectionStatus.OFFLINE);
        String where = Imps.AccountStatus.ACCOUNT + "=?";
        getContentResolver()
            .update(
                Imps.AccountStatus.CONTENT_URI,
                values,
                where,
                new String[] {Long.toString(accountId)});
      }
    } catch (RemoteException ex) {
      Log.e(ImApp.LOG_TAG, "signout: caught ", ex);
    } finally {
      // finish();

      //   Toast.makeText(this, getString(R.string.signed_out_prompt), Toast.LENGTH_LONG).show();
    }
  }
Пример #2
0
 public void onConnectionCreated(IImConnection conn) throws RemoteException {
   long providerId = conn.getProviderId();
   synchronized (mConnections) {
     if (!mConnections.containsKey(providerId)) {
       mConnections.put(providerId, conn);
       conn.registerConnectionListener(mConnectionListener);
     }
   }
   broadcastConnEvent(EVENT_CONNECTION_CREATED, providerId, null);
 }
Пример #3
0
 public void setConnection(IImConnection conn) {
   mConn = conn;
   try {
     mPresence = conn.getUserPresence();
     mProviderId = conn.getProviderId();
   } catch (RemoteException e) {
     mHandler.showServiceErrorAlert();
   }
   if (mPresence == null) {
     mPresence = new Presence();
   }
   updateView();
 }
Пример #4
0
 public IImConnection getConnectionByAccount(long accountId) {
   synchronized (mConnections) {
     for (IImConnection conn : mConnections.values()) {
       try {
         if (conn.getAccountId() == accountId) {
           return conn;
         }
       } catch (RemoteException e) {
         // No server!
       }
     }
     return null;
   }
 }
Пример #5
0
  void updatePresence(int status, String statusText) {
    if (mPresence == null) {
      // No connection yet. Don't allow to update presence yet.
      return;
    }

    Presence newPresence = new Presence(mPresence);

    if (status != -1) {
      newPresence.setStatus(status);
    }

    if (statusText != null) newPresence.setStatusText(statusText);

    try {
      int res = mConn.updateUserPresence(newPresence);
      if (res != ImErrorInfo.NO_ERROR) {
        mHandler.showAlert(R.string.error, ErrorResUtils.getErrorRes(getResources(), res));
      } else {
        mPresence = newPresence;
        updateView();
      }
    } catch (RemoteException e) {
      mHandler.showServiceErrorAlert();
    }
  }
Пример #6
0
 private void unregisterListeners() {
   try {
     mConn.unregisterConnectionListener(mConnectionListener);
   } catch (RemoteException e) {
     mHandler.showServiceErrorAlert();
   }
 }
  private IContactList getContactList(IImConnection conn) {
    if (conn == null) {
      return null;
    }

    try {
      IContactListManager contactListMgr = conn.getContactListManager();
      String listName = getSelectedListName();
      if (!TextUtils.isEmpty(listName)) {
        return contactListMgr.getContactList(listName);
      } else {
        // Use the default list
        List<IBinder> lists = contactListMgr.getContactLists();
        for (IBinder binder : lists) {
          IContactList list = IContactList.Stub.asInterface(binder);
          if (list.isDefault()) {
            return list;
          }
        }
        // No default list, use the first one as default list
        if (!lists.isEmpty()) {
          return IContactList.Stub.asInterface(lists.get(0));
        }
        return null;
      }
    } catch (RemoteException e) {
      // If the service has died, there is no list for now.
      return null;
    }
  }
Пример #8
0
 private void fetchActiveConnections() {
   try {
     // register the listener before fetch so that we won't miss any connection.
     mImService.addConnectionCreatedListener(mConnCreationListener);
     synchronized (mConnections) {
       for (IBinder binder : (List<IBinder>) mImService.getActiveConnections()) {
         IImConnection conn = IImConnection.Stub.asInterface(binder);
         long providerId = conn.getProviderId();
         if (!mConnections.containsKey(providerId)) {
           mConnections.put(providerId, conn);
           conn.registerConnectionListener(mConnectionListener);
         }
       }
     }
   } catch (RemoteException e) {
     Log.e(LOG_TAG, "fetching active connections", e);
   }
 }
  private void unregisterListeners() {
    try {
      mConn.unregisterConnectionListener(mConnectionListener);
    } catch (RemoteException e) {

      mHandler.showServiceErrorAlert(e.getLocalizedMessage());
      LogCleaner.error(ImApp.LOG_TAG, "remote error", e);
    }
  }
Пример #10
0
 public void loggingIn(boolean loggingIn) {
   mProgressBar.setVisibility(loggingIn ? View.VISIBLE : View.GONE);
   try {
     Presence newPresence = mConn.getUserPresence();
     if (newPresence != null) mPresence = newPresence;
   } catch (RemoteException e) {
     mHandler.showServiceErrorAlert();
   }
   updateView();
 }
Пример #11
0
    @Override
    public void onSelfPresenceUpdated(IImConnection connection) {
      if (Log.isLoggable(LOG_TAG, Log.DEBUG)) log("onUserPresenceUpdated");

      try {
        long providerId = connection.getProviderId();
        broadcastConnEvent(EVENT_USER_PRESENCE_UPDATED, providerId, null);
      } catch (RemoteException e) {
        Log.e(LOG_TAG, "onUserPresenceUpdated", e);
      }
    }
Пример #12
0
 @Override
 public void onUpdateSelfPresenceError(IImConnection connection, ImErrorInfo error) {
   if (Log.isLoggable(LOG_TAG, Log.DEBUG)) {
     log("onUpdateUserPresenceError(" + error + ")");
   }
   try {
     long providerId = connection.getProviderId();
     broadcastConnEvent(EVENT_UPDATE_USER_PRESENCE_ERROR, providerId, error);
   } catch (RemoteException e) {
     Log.e(LOG_TAG, "onUpdateUserPresenceError", e);
   }
 }
Пример #13
0
  void handleIntent() {
    ContentResolver cr = getContentResolver();
    long providerId = Imps.Provider.getProviderIdForName(cr, mProviderName);
    long accountId;

    mConn = mApp.getConnection(providerId);
    if (mConn == null) {
      Cursor c = DatabaseUtils.queryAccountsForProvider(cr, ACCOUNT_PROJECTION, providerId);
      if (c == null) {
        addAccount(providerId);
      } else {
        accountId = c.getLong(ACCOUNT_ID_COLUMN);
        if (c.isNull(ACCOUNT_PW_COLUMN)) {
          editAccount(accountId);
        } else {
          signInAccount(accountId);
        }
      }
    } else {
      try {
        int state = mConn.getState();
        accountId = mConn.getAccountId();

        if (state < ImConnection.LOGGED_IN) {
          signInAccount(accountId);
        } else if (state == ImConnection.LOGGED_IN || state == ImConnection.SUSPENDED) {
          if (!isValidToAddress()) {
            showContactList(accountId);
          } else {
            openChat(providerId, accountId);
          }
        }
      } catch (RemoteException e) {
        // Ouch!  Service died!  We'll just disappear.
        Log.w("ImUrlActivity", "Connection disappeared!");
      }
    }
    finish();
  }
Пример #14
0
    @Override
    public void onConnectionStateChange(IImConnection conn, int state, ImErrorInfo error) {
      if (Log.isLoggable(LOG_TAG, Log.DEBUG)) {
        log("onConnectionStateChange(" + state + ", " + error + ")");
      }

      try {
        int what = -1;
        long providerId = conn.getProviderId();
        switch (state) {
          case ImConnection.LOGGED_IN:
            what = EVENT_CONNECTION_LOGGED_IN;
            break;

          case ImConnection.LOGGING_IN:
            what = EVENT_CONNECTION_LOGGING_IN;
            break;

          case ImConnection.LOGGING_OUT:
            what = EVENT_CONNECTION_LOGGING_OUT;
            // MIRON - remove only if disconnected!
            //                    synchronized (mConnections) {
            //                        mConnections.remove(providerId);
            //                    }
            break;

          case ImConnection.DISCONNECTED:
            what = EVENT_CONNECTION_DISCONNECTED;
            synchronized (mConnections) {
              mConnections.remove(providerId);
            }
            // stop the service if there isn't an active connection anymore.
            stopImServiceIfInactive();
            break;

          case ImConnection.SUSPENDED:
            what = EVENT_CONNECTION_SUSPENDED;
            break;
        }
        if (what != -1) {
          broadcastConnEvent(what, providerId, error);
        }
      } catch (RemoteException e) {
        Log.e(LOG_TAG, "onConnectionStateChange", e);
      }
    }
Пример #15
0
  private void openChat(long provider, long account) {
    try {
      IChatSessionManager manager = mConn.getChatSessionManager();
      IChatSession session = manager.getChatSession(mToAddress);
      if (session == null) {
        session = manager.createChatSession(mToAddress);
      }

      Uri data = ContentUris.withAppendedId(Imps.Chats.CONTENT_URI, session.getId());
      Intent intent = new Intent(Intent.ACTION_VIEW, data);
      intent.putExtra(ImServiceConstants.EXTRA_INTENT_FROM_ADDRESS, mToAddress);
      intent.putExtra(ImServiceConstants.EXTRA_INTENT_PROVIDER_ID, provider);
      intent.putExtra(ImServiceConstants.EXTRA_INTENT_ACCOUNT_ID, account);
      intent.addCategory(ImApp.IMPS_CATEGORY);
      startActivity(intent);
    } catch (RemoteException e) {
      // Ouch!  Service died!  We'll just disappear.
      Log.w("ImUrlActivity", "Connection disappeared!");
    }
  }
Пример #16
0
  private StatusIconAdapter getStatusAdapter() {
    try {
      mStatusItems.clear();
      int[] supportedStatus = mConn.getSupportedPresenceStatus();
      for (int i = 0; i < supportedStatus.length; i++) {
        int s = PresenceUtils.convertStatus(supportedStatus[i]);
        if (s == Imps.Presence.OFFLINE) {
          s = Imps.Presence.INVISIBLE;
        }
        ImApp app = ImApp.getApplication((Activity) mContext);
        BrandingResources brandingRes = app.getBrandingResource(mProviderId);
        Drawable icon = brandingRes.getDrawable(PresenceUtils.getStatusIconId(s));
        String text = brandingRes.getString(PresenceUtils.getStatusStringRes(s));
        mStatusItems.add(new StatusItem(supportedStatus[i], icon, text));
      }
    } catch (RemoteException e) {
      mHandler.showServiceErrorAlert();
    }

    return new StatusIconAdapter(mContext, mStatusItems);
  }
  /** @param value */
  protected void setContactNickname(String aAddress, String aNickname, IImConnection conn) {
    try {

      IContactListManager listManager = conn.getContactListManager();
      int result = listManager.setContactName(aAddress, aNickname);
      if (result != ImErrorInfo.NO_ERROR) {
        Toast.makeText(
                mContext, mContext.getString(R.string.error_prefix) + result, Toast.LENGTH_LONG)
            .show(); // TODO -LS error handling
      }
    } catch (Exception e) {
      Toast.makeText(
              mContext,
              mContext.getString(R.string.error_prefix) + e.getMessage(),
              Toast.LENGTH_LONG)
          .show(); // TODO -LS error handling
    }
    mFilterList.invalidate();
    final InputMethodManager imm =
        (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getWindowToken(), 0);
  }