public boolean storeChatMessage(
     boolean left,
     long message_id,
     String from_number,
     String to_number,
     String direction,
     String time,
     String text,
     String status,
     String read) {
   datasource.createMessage(
       left, message_id, from_number, to_number, direction, time, text, status, read);
   return true;
 }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);

    buttonSend = (Button) findViewById(R.id.buttonSend);
    listView = (ListView) findViewById(R.id.listView1);
    datasource = new RingToDataSource(this);
    datasource.open();

    chatArrayAdapter =
        new ChatArrayAdapter(this.getApplicationContext(), R.layout.activity_chat_singlemessage);
    listView.setAdapter(chatArrayAdapter);
    listView.setDivider(null);
    chatText = (EditText) findViewById(R.id.chatText);
    chatText.setOnKeyListener(
        new View.OnKeyListener() {
          public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_DOWN)
                && (keyCode == KeyEvent.KEYCODE_ENTER)) {
              return sendChatMessage();
            }

            return false;
          }
        });

    buttonSend.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View arg0) {
            sendChatMessage();
          }
        });

    //        else
    //        {
    //            message_list = new ArrayList<Message>(datasource.getAllMessages());
    //            chatArrayAdapter.addListOfMessages(message_list);
    //        }

    //        FragmentManager managerFragment = getSupportFragmentManager();
    //        app = (RingToAPI) managerFragment.findFragmentByTag(String.valueOf(0));
    //
    //        if(app == null)
    //        {
    //            app = new RingToAPI();
    to_number = getIntent().getExtras().getString("to_number");
    from_number = getIntent().getExtras().getString("from_number");

    //            app.setAPI(this, token);
    //            managerFragment.beginTransaction().add(app, String.valueOf(0)).commit();
    //        }

    listView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
    listView.setAdapter(chatArrayAdapter);

    // to scroll the list view to bottom on data change
    chatArrayAdapter.registerDataSetObserver(
        new DataSetObserver() {
          @Override
          public void onChanged() {
            super.onChanged();
            listView.setSelection(chatArrayAdapter.getCount() - 1);
          }
        });

    chatArrayAdapter.addListOfMessages(datasource.getChatThread(to_number, from_number));

    //        if (savedInstanceState != null)
    //        {
    //            RingToApplication singleton = (RingToApplication) getApplicationContext();
    //            token = singleton.getToken();
    //            app = new RingToAPI();
    //            app.setToken(token);
    //        }
    //
    //        else
    //        {
    //            // Takes the previous saved list of messages on screen and appends them to
    //            // the current message list on screen.
    ////            message_list = savedInstanceState.getParcelableArrayList("here");
    ////            chatArrayAdapter.addListOfMessages(message_list);
    //        }

    //        Toast.makeText(getApplicationContext(), Integer.toString(messages.length()),
    // Toast.LENGTH_LONG);
    //        app.pullMessages(getActivity());

  }
 @Override
 protected void onPause() {
   datasource.close();
   super.onPause();
 }
 @Override
 protected void onResume() {
   datasource.open();
   super.onResume();
 }