Ejemplo n.º 1
0
 @Override
 protected void onDestroy() {
   super.onDestroy();
   if (localstoragehandler != null) {
     localstoragehandler.close();
   }
   if (dbCursor != null) {
     dbCursor.close();
   }
 }
Ejemplo n.º 2
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.messaging_screen); // messaging_screen);

    messageHistoryText = (EditText) findViewById(R.id.messageHistory);

    messageText = (EditText) findViewById(R.id.message);

    messageText.requestFocus();
    focusOnButtons();

    sendMessageButton = (Button) findViewById(R.id.sendMessageButton);

    Bundle extras = getIntent().getExtras();
    Bundle params = getIntent().getExtras();

    friend.userName = extras.getString(FriendInfo.USERNAME);
    friend.ip = extras.getString(FriendInfo.IP);
    friend.port = extras.getString(FriendInfo.PORT);
    String msg = extras.getString(MessageInfo.MESSAGETEXT);
    // System.out.println("lkjhgfd biba "+friend.userName+" biba "+bib);
    Log.i("MESSAGING lOG", "uporabnik " + friend.userName + " " + msg + "");

    setTitle("Pogovor z osebo " + friend.userName);

    //	EditText friendUserName = (EditText) findViewById(R.id.friendUserName);
    //	friendUserName.setText(friend.userName);

    localstoragehandler = new LocalStorageHandler(this);
    dbCursor = localstoragehandler.get(friend.userName, IMService.USERNAME);

    if (dbCursor.getCount() > 0) {
      int noOfScorer = 0;
      dbCursor.moveToFirst();
      while ((!dbCursor.isAfterLast()) && noOfScorer < dbCursor.getCount()) {
        noOfScorer++;

        this.appendToMessageHistory(dbCursor.getString(2), dbCursor.getString(3));
        dbCursor.moveToNext();
      }
    }
    localstoragehandler.close();

    if (msg != null) {
      // this.appendToMessageHistory(friend.userName , msg);
      ((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
          .cancel((friend.userName + msg).hashCode());
    }

    sendMessageButton.setOnClickListener(
        new OnClickListener() {
          CharSequence message;
          Handler handler = new Handler();

          @Override
          public void onClick(View arg0) {
            message = messageText.getText();
            if (message.length() > 0) {
              appendToMessageHistory(imService.getUsername(), message.toString());

              localstoragehandler.insert(
                  imService.getUsername(), friend.userName, message.toString());

              messageText.setText("");
              Thread thread =
                  new Thread() {
                    @Override
                    public void run() {
                      try {
                        if (imService.sendMessage(
                                imService.getUsername(), friend.userName, message.toString())
                            == null) {

                          handler.post(
                              new Runnable() {

                                @Override
                                public void run() {

                                  Toast.makeText(
                                          getApplicationContext(),
                                          R.string.message_cannot_be_sent,
                                          Toast.LENGTH_LONG)
                                      .show();

                                  // showDialog(MESSAGE_CANNOT_BE_SENT);
                                }
                              });
                        }
                      } catch (UnsupportedEncodingException e) {
                        Toast.makeText(
                                getApplicationContext(),
                                R.string.message_cannot_be_sent,
                                Toast.LENGTH_LONG)
                            .show();

                        e.printStackTrace();
                      }
                    }
                  };
              thread.start();
            }
          }
        });

    messageText.setOnKeyListener(
        new OnKeyListener() {
          @Override
          public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == 66) {
              sendMessageButton.performClick();
              return true;
            }
            return false;
          }
        });
  }