public int onMessageSent(String to, String message) {
    getChatStorage().deleteDraft(to);
    if (messageListFragment != null && messageListFragment.isVisible()) {
      ((ChatListFragment) messageListFragment).refresh();
    }

    return getChatStorage().saveTextMessage("", to, message, System.currentTimeMillis());
  }
  public void displayChat(String sipUri) {
    if (getResources().getBoolean(R.bool.disable_chat)) {
      return;
    }

    LinphoneAddress lAddress;
    try {
      lAddress = LinphoneCoreFactory.instance().createLinphoneAddress(sipUri);
    } catch (LinphoneCoreException e) {
      Log.e("Cannot display chat", e);
      return;
    }
    Contact contact =
        ContactsManager.getInstance().findContactWithAddress(getContentResolver(), lAddress);
    String displayName = contact != null ? contact.getName() : null;

    String pictureUri = null;
    String thumbnailUri = null;
    if (contact != null && contact.getPhotoUri() != null) {
      pictureUri = contact.getPhotoUri().toString();
      thumbnailUri = contact.getThumbnailUri().toString();
    }

    if (isTablet()) {
      if (currentFragment == FragmentsAvailable.CHATLIST
          || currentFragment == FragmentsAvailable.CHAT) {
        Fragment fragment2 = getSupportFragmentManager().findFragmentById(R.id.fragmentContainer2);
        if (fragment2 != null
            && fragment2.isVisible()
            && currentFragment == FragmentsAvailable.CHAT) {
          ChatFragment chatFragment = (ChatFragment) fragment2;
          chatFragment.changeDisplayedChat(sipUri, displayName, pictureUri);
        } else {
          Bundle extras = new Bundle();
          extras.putString("SipUri", sipUri);
          if (contact != null) {
            extras.putString("DisplayName", displayName);
            extras.putString("PictureUri", pictureUri);
            extras.putString("ThumbnailUri", thumbnailUri);
          }
          changeCurrentFragment(FragmentsAvailable.CHAT, extras);
        }
      } else {
        changeCurrentFragment(FragmentsAvailable.CHATLIST, null);
        displayChat(sipUri);
      }
      if (messageListFragment != null && messageListFragment.isVisible()) {
        ((ChatListFragment) messageListFragment).refresh();
      }
    } else {
      Intent intent = new Intent(this, ChatActivity.class);
      intent.putExtra("SipUri", sipUri);
      if (contact != null) {
        intent.putExtra("DisplayName", contact.getName());
        intent.putExtra("PictureUri", pictureUri);
        intent.putExtra("ThumbnailUri", thumbnailUri);
      }
      startOrientationSensor();
      startActivityForResult(intent, CHAT_ACTIVITY);
    }

    LinphoneService.instance().resetMessageNotifCount();
    LinphoneService.instance().removeMessageNotification();
    displayMissedChats(getChatStorage().getUnreadMessageCount());
  }
  @Override
  public void onResume() {
    super.onResume();

    // Check if the is the first time we show the chat view since we use liblinphone chat storage
    useLinphoneStorage = getResources().getBoolean(R.bool.use_linphone_chat_storage);
    final SharedPreferences prefs =
        PreferenceManager.getDefaultSharedPreferences(LinphoneActivity.instance());
    boolean updateNeeded =
        prefs.getBoolean(getString(R.string.pref_first_time_linphone_chat_storage), true);
    updateNeeded = updateNeeded && !isVersionUsingNewChatStorage();
    if (useLinphoneStorage && updateNeeded) {
      AsyncTask<Void, Void, Void> task =
          new AsyncTask<Void, Void, Void>() {
            private ProgressDialog pd;

            @Override
            protected void onPreExecute() {
              pd = new ProgressDialog(LinphoneActivity.instance());
              pd.setTitle(getString(R.string.wait));
              pd.setMessage(getString(R.string.importing_messages));
              pd.setCancelable(false);
              pd.setIndeterminate(true);
              pd.show();
            }

            @Override
            protected Void doInBackground(Void... arg0) {
              try {
                if (importAndroidStoredMessagedIntoLibLinphoneStorage()) {
                  prefs
                      .edit()
                      .putBoolean(getString(R.string.pref_first_time_linphone_chat_storage), false)
                      .commit();
                  LinphoneActivity.instance().getChatStorage().restartChatStorage();
                }
              } catch (Exception e) {
                e.printStackTrace();
              }
              return null;
            }

            @Override
            protected void onPostExecute(Void result) {
              pd.dismiss();
            }
          };
      task.execute((Void[]) null);
    }

    if (LinphoneActivity.isInstanciated()) {
      LinphoneActivity.instance().selectMenu(FragmentsAvailable.CHATLIST);
      LinphoneActivity.instance().updateChatListFragment(this);

      if (getResources().getBoolean(R.bool.show_statusbar_only_on_dialer)) {
        LinphoneActivity.instance().hideStatusBar();
      }
    }

    refresh();
  }