Beispiel #1
0
 private void sendMessageLocation(Location location) {
   sendMessageToServer(
       Prefs.getSendMessageJSON(
           userID,
           mItem.getChatGroupID(),
           mItem.getMemberID(),
           Message.Type.CHAT_TYPE_LOCATION,
           location.getLatitude() + "," + location.getLongitude(),
           DateUtils.nowTime(),
           token));
 }
Beispiel #2
0
 private void sendBroadcastReadChat() {
   if (mLocalBroadcastManager != null) {
     Intent intent = new Intent(Actions.UPDATE_READ_CHAT_CONVERSATION);
     intent.putExtra(Extras.ID, mItem.getMemberID());
     mLocalBroadcastManager.sendBroadcast(intent);
   }
 }
Beispiel #3
0
 private void actionPopup(int actionId) {
   if (isMsLief) {
     return;
   }
   switch (actionId) {
     case ACTION_ID_CALL:
       startCall();
       break;
     case ACTION_ID_REMOVE:
       String title =
           String.format(getString(R.string.remove_from_your_lovedones), mItem.getFullName());
       if (UIUtils.ensureNetworkInternet(this, false)) {
         UIUtils.alert(
             this,
             title,
             true,
             R.string.cancel,
             null,
             R.string.yes,
             new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                 removeUser();
               }
             });
       }
       break;
   }
 }
Beispiel #4
0
 private void sendMessageImage(String urlImage) {
   if (TextUtil.isEmpty(urlImage)) {
     UIUtils.alert(this, R.string.url_image_is_null);
     return;
   }
   sendMessageToServer(
       Prefs.getSendMessageJSON(
           userID,
           mItem.getChatGroupID(),
           mItem.getMemberID(),
           Message.Type.CHAT_TYPE_IMAGE,
           urlImage,
           DateUtils.nowTime(),
           token));
   etMessage.setText("");
 }
Beispiel #5
0
 private void sendMessage() {
   String message = etMessage.getText().toString();
   if (message.trim().isEmpty()) {
     UIUtils.alert(this, R.string.message_is_empty);
     return;
   }
   if (isSendSMS) {
     sendMessageToServer(
         Prefs.getSendSmsJSON(
             userID,
             mItem.getChatGroupID() != null ? mItem.getChatGroupID() : "",
             mItem.getMemberID() != null ? mItem.getMemberID() : "",
             mItem.getID(),
             Message.Type.CHAT_TYPE_SEND_SMS,
             message,
             DateUtils.nowTime(),
             token));
   } else {
     sendMessageToServer(
         Prefs.getSendMessageJSON(
             userID,
             mItem.getChatGroupID(),
             mItem.getMemberID(),
             Message.Type.CHAT_TYPE_MESSAGE,
             message,
             DateUtils.nowTime(),
             token));
   }
   etMessage.setText("");
 }
Beispiel #6
0
 private void removeUser() {
   showLoading();
   CallServiceCustomPOST csPost =
       new CallServiceCustomPOST(
           "removeUser",
           userID,
           token,
           deviceId,
           version,
           locale,
           new JsonCallback() {
             @Override
             public void handleData(String mCmd, JSONObject json) throws JSONException {
               if (json != null) {
                 int status = json.getInt("status");
                 if (status == 1) {
                   sendBroadcastUpdateLovedOneAfterRemoved(mItem);
                   finish();
                 } else {
                   try {
                     String errorCode = json.get("errorCode").toString();
                     if (errorCode.equals(CallServiceCustomPOST.ErrorCode.CONNECT_SERVER_FAIL)) {
                       UIUtils.alert(
                           ChatScreen.this,
                           R.string.connect_to_server_failed_plz_wait_a_moment_and_try_again);
                     } else {
                       UIUtils.alert(ChatScreen.this, "Error " + errorCode);
                     }
                   } catch (Exception e) {
                     UIUtils.alert(ChatScreen.this, e.getMessage());
                     Log.d(TAG, json.toString());
                   }
                 }
               }
               hideLoading();
             }
           });
   try {
     String url =
         RemoteMethods.get(
             RemoteMethods.removeUser,
             URLEncoder.encode(userID, "UTF-8"),
             URLEncoder.encode(mItem.getID(), "UTF-8"));
     csPost.execute(url);
   } catch (Exception e) {
     e.printStackTrace();
     hideLoading();
   }
 }
Beispiel #7
0
  private void parseMessage(final String msg) {
    try {
      JSONObject json = new JSONObject(msg);
      String sendType = json.getString("sendType");
      try {
        String senderId = json.getString("senderId");
        if (!senderId.equals(userID) && !senderId.equals(mItem.getMemberID())) {
          // ignore
          return;
        }
      } catch (JSONException e1) {
        e1.printStackTrace();
      }

      switch (sendType) {
        case Message.Type.CHAT_TYPE_MESSAGE:
        case Message.Type.CHAT_TYPE_IMAGE:
        case Message.Type.CHAT_TYPE_LOCATION:
        case Message.Type.CHAT_TYPE_SEND_SMS:
        case Message.Type.CHAT_TYPE_SEND_SMS_SUCCESS:
          Message message = Message.parseMassge(json);
          message.setSendout(message.getSenderId().equals(userID));
          Log.d(TAG + ":check", "message send out: " + message.isSendout());
          appendMessage(message);
          break;
        case Message.Type.CHAT_TYPE_SEND_SMS_FAILED_OUT_OF_FREE_SMS:
          sendBroadCastSendSmsFailedOutOfFree();
          break;
        case Message.Type.CHAT_TYPE_SEND_SMS_FAILED_WRONG_NUMBER:
          sendBroadCastSendSmsFailedWrongNumber();
          break;
      }
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }
Beispiel #8
0
 private void startCall() {
   UIUtils.startCall(this, mItem.getFullPhoneNumber());
 }
Beispiel #9
0
  private void requestGetListChat() {
    CallServicePOST cs =
        new CallServicePOST(
            "getListChat",
            new CallServicePOST.JsonCallback() {
              @Override
              public void handleData(String mCmd, JSONObject json) throws JSONException {
                if (json != null) {
                  int status = json.getInt("status");
                  if (status == 1) {
                    JSONArray jArray = json.getJSONArray("data");

                    for (int i = 0; i < jArray.length(); i++) {
                      JSONObject info = jArray.getJSONObject(i);
                      Message message = Message.parseMassge(info);
                      message.setSendout(message.getSenderId().equals(userID));
                      Log.d(TAG + ":check", "message send out: " + message.isSendout());
                      appendMessageToTop(message);
                      lastMessageTime = message.getUnix_timestamp();
                    }
                    if (mAdapter != null) {
                      mAdapter.notifyDataSetChanged();
                      lvMessages.setSelection(mAdapter.getCount() - 1);
                    }
                    if (mItem.isNewChat()) {
                      sendBroadcastReadChat();
                      mItem.setNewChat(false);
                    }

                    //						if (isSendSMS && mAdapter.getCount() > 0) {
                    //							findViewById(R.id.layoutCall).setVisibility(View.VISIBLE);
                    //						}

                    //						if (jArray != null) {
                    //							enableGetMore = (jArray.length() < Values.LIMIT_GET_LIST_TRAVELERS) ?
                    // false : true;
                    //						}
                    hideLayoutNote();
                  } else {
                    try {
                      String errorCode = json.get("errorCode").toString();
                      if (errorCode.equals(CallServicePOST.ErrorCode.CONNECT_SERVER_FAIL)) {
                        UIUtils.alert(
                            ChatScreen.this,
                            R.string.connect_to_server_failed_plz_wait_a_moment_and_try_again);
                      } else {
                        if (listMessages != null && listMessages.size() > 0) {
                          Log.d(TAG + ":handleData", "ERROR " + errorCode + " !!");
                        } else {
                          UIUtils.alert(ChatScreen.this, "ERROR " + errorCode + " !!");
                        }
                      }
                    } catch (Exception e) {
                      Log.d(TAG + ":handleData", e.getMessage());
                    }
                  }
                }
                hideLoading();
                stopSwipeRefresh();
              }
            });

    try {
      String url =
          RemoteMethods.get(
              RemoteMethods.getChatConversation,
              URLEncoder.encode(userID, "UTF-8"),
              URLEncoder.encode(
                  TextUtil.isEmpty(mItem.getChatGroupID()) ? mItem.getID() : mItem.getChatGroupID(),
                  "UTF-8"),
              Values.LIMIT_GET_LIST_CHAT);
      if (lastMessageTime != null) {
        url += "&currentTime=" + lastMessageTime;
      }
      cs.execute(url);
    } catch (Exception e) {
      hideLoading();
    }
  }
Beispiel #10
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chatscreen_layout);
    startWebSocketClient();
    picasso = Picasso.with(this);
    mPrefs = Prefs.get(this);

    try {
      Bundle bundle = getIntent().getExtras();
      mItem = bundle.getParcelable(Extras.ITEM);
    } catch (Exception e) {
      finish();
    }

    userID = mPrefs.getString(Prefs.ID, null);
    token = mPrefs.getString(Prefs.LoginToken, null);
    deviceId = mPrefs.getString(Prefs.DeviceId, null);
    version = mPrefs.getString(Prefs.Version, null);
    locale = mPrefs.getString(Prefs.LanguageLocale, null);

    String fileName = mPrefs.getString(Prefs.FileName, null);
    String domainName = mPrefs.getString(Prefs.DomainName, null);
    userAvatar = domainName + fileName;

    if (mItem.getIsAccepted() != null && mItem.getIsAccepted().equals("1")) {
      isSendSMS = false;
    } else {
      isSendSMS = true;
    }

    isMsLief = mItem.getIsMsLief() != null && mItem.getIsMsLief().equals("1");

    TextView tvTitlebar = (TextView) findViewById(R.id.tvTitleBar);
    lvMessages = (ListView) findViewById(R.id.lvMessages);
    swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.lySwipeRefresh);
    etMessage = (EditText) findViewById(R.id.etMessage);
    tvSend = (TextView) findViewById(R.id.tvSend);
    ImageView ivAvatarNote = (ImageView) findViewById(R.id.ivAvatarNote);
    layoutNote = findViewById(R.id.layoutNote);
    tvNote = (TextView) findViewById(R.id.tvNote);
    tvSmsLimit = (TextView) findViewById(R.id.tvSmsLimit);
    ivBackgroundAlert = (ImageView) findViewById(R.id.ivBackgroundAlert);
    tvContentAlert = (TextView) findViewById(R.id.tvContentAlert);
    tvContentAlert.setText(
        String.format(
            getString(R.string.cannot_send_message_cause_out_of_sms_free),
            mItem.getNameResidence(),
            mItem.getNameResidence()));
    ivBgQuickAction = (ImageView) findViewById(R.id.ivBgQuickAction);

    showAlertSmsLimit(false);

    tvSmsLimit.setVisibility(isSendSMS ? View.VISIBLE : View.GONE);
    findViewById(R.id.ivAction).setVisibility(isSendSMS ? View.INVISIBLE : View.VISIBLE);

    tvTitlebar.setText(TextUtil.isEmpty(mItem.getNameResidence()) ? "" : mItem.getNameResidence());
    picasso
        .load(mItem.getUrlAvatar() != null ? mItem.getUrlAvatar() : "null")
        .resize(100, 100)
        .placeholder(mItem.getResIconRelationship())
        .into(ivAvatarNote);

    if (isMsLief) {
      tvNote.setText(R.string.fell_free_to_ask_lief);
    } else {
      tvNote.setText(
          isSendSMS
              ? String.format(
                  getString(R.string._stil_hasnt_replied_to_your_invitation),
                  mItem.getNameResidence())
              : getString(R.string.break_the_ice_by_chatting_first));
    }
    if (isSendSMS) {
      etMessage.setFilters(new InputFilter[] {new InputFilter.LengthFilter(120)});
      etMessage.addTextChangedListener(
          new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void afterTextChanged(Editable s) {
              tvSmsLimit.setText("" + (120 - s.length()));
            }
          });

      if (!isMsLief) {
        setupPopup();
        findViewById(R.id.ivMore).setVisibility(View.VISIBLE);
      }
    }

    listMessages = new ArrayList<Message>();
    mAdapter =
        new MessagesListAdapter(
            this, listMessages, userAvatar, mItem.getUrlAvatar(), mItem.getResIconRelationship());
    lvMessages.setAdapter(mAdapter);

    Log.d(TAG, "now time: " + DateUtils.nowTime());
    if (UIUtils.isConnectingToInternet(this)) {
      //        	showLoading();
      startSwipeRefresh();
      requestCheckSocket();
      requestGetListChat();
    }

    swipeRefreshLayout.setColorSchemeResources(
        android.R.color.holo_blue_light,
        android.R.color.holo_green_light,
        android.R.color.holo_orange_light,
        android.R.color.holo_red_light);

    swipeRefreshLayout.setOnRefreshListener(this);
    findViewById(R.id.ivBack).setOnClickListener(this);
    findViewById(R.id.tvCall).setOnClickListener(this);
    findViewById(R.id.tvCallNow).setOnClickListener(this);
    findViewById(R.id.ivAction).setOnClickListener(this);
    findViewById(R.id.ivMore).setOnClickListener(this);
    tvSend.setOnClickListener(this);
    ivBackgroundAlert.setOnClickListener(this);
    tvContentAlert.setOnClickListener(this);

    prepareLocationRequest();

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    registerReceiver();
  }