Android Open Source - android-textlater Message D A O






From Project

Back to project page android-textlater.

License

The source code is released under:

Apache License

If you think the Android project android-textlater listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2013 The Android Open Source Project
 *//from  w w w . j  av  a2 s  .  co m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.michael.feng.textlater;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class MessageDAO {
  // Database fields
  private SQLiteDatabase database;
  private SQLiteHelper dbHelper;
  private String[] allColumns = { SQLiteHelper.COL_ID, SQLiteHelper.COL_CONTACT,
      SQLiteHelper.COL_NUMBER, SQLiteHelper.COL_WHEN, SQLiteHelper.COL_CONTENT,
      SQLiteHelper.COL_HASSENT };

  public MessageDAO(Context context) {
    dbHelper = new SQLiteHelper(context);
  }

  public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
  }

  public void close() {
    dbHelper.close();
  }

  public Message insertMessage(Message message) {
    ContentValues values = new ContentValues();
    values.put(SQLiteHelper.COL_CONTACT, message.getTextContact());
    values.put(SQLiteHelper.COL_NUMBER, message.getTextNumber());
    values.put(SQLiteHelper.COL_WHEN, message.getTextWhen());
    values.put(SQLiteHelper.COL_CONTENT, message.getTextContent());
    long insertId = database.insert(SQLiteHelper.TB_NAME, null, values);
    Cursor cursor = database.query(SQLiteHelper.TB_NAME, allColumns, SQLiteHelper.COL_ID
        + " = " + insertId, null, null, null, null);
    cursor.moveToFirst();
    Message newMessage = cursorToMessage(cursor);
    cursor.close();
    return newMessage;
  }

  public void deleteMessageByContact(String textContact) {
    Log.d("Message deleted with textContact: ", textContact + "");
    database.delete(SQLiteHelper.TB_NAME,
        SQLiteHelper.COL_CONTACT + " = '" + textContact + "'", null);
  }

  public void updateMessageStatus(String textContact, String textWhen, String status) {
    ContentValues cv = new ContentValues();
    cv.put("hasSent", status);
    database.update(SQLiteHelper.TB_NAME, cv, SQLiteHelper.COL_CONTACT + " = '" + textContact
        + "' and " + SQLiteHelper.COL_WHEN + " = '" + textWhen + "'", null);
  }

  public List<Message> getMessagesDistinctNumber() {
    List<Message> messageList = new ArrayList<Message>();
    String querySql = "select a.* from messages a inner join (select distinct textNumber, max(_id) as _id from messages group by textNumber ) as b on a.textNumber = b.textNumber and a._id = b._id ORDER BY a._id DESC";
    Cursor cursor = database.rawQuery(querySql, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      Message message = cursorToMessage(cursor);
      messageList.add(message);
      cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return messageList;
  }

  public List<Message> getMessagesByContact(String textContact) {
    List<Message> messageList = new ArrayList<Message>();
    String querySql = "select _id, textContact, textNumber, textWhen, textContent, hasSent from messages where textContact = '"
        + textContact + "' order by _id desc";
    Cursor cursor = database.rawQuery(querySql, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      Message message = cursorToMessage(cursor);
      messageList.add(message);
      cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return messageList;
  }
  
  public List<Message> getMessagesByContactNumber(String textNumber) {
    List<Message> messageList = new ArrayList<Message>();
    String querySql = "select _id, textContact, textNumber, textWhen, textContent, hasSent from messages where textNumber = '"
        + textNumber + "' order by _id desc";
    Cursor cursor = database.rawQuery(querySql, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      Message message = cursorToMessage(cursor);
      messageList.add(message);
      cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return messageList;
  }

  private Message cursorToMessage(Cursor cursor) {
    Message message = new Message();
    message.setId(cursor.getInt(0));
    message.setTextContact(cursor.getString(1));
    message.setTextNumber(cursor.getString(2));
    message.setTextWhen(cursor.getString(3));
    message.setTextContent(cursor.getString(4));
    message.setHasSent(cursor.getString(5));
    return message;
  }
}




Java Source Code List

com.michael.feng.textlater.AlarmReceiver.java
com.michael.feng.textlater.ContactsActivity.java
com.michael.feng.textlater.DetailActivity.java
com.michael.feng.textlater.MainActivity.java
com.michael.feng.textlater.MessageDAO.java
com.michael.feng.textlater.Message.java
com.michael.feng.textlater.NewActivity.java
com.michael.feng.textlater.SQLiteHelper.java
com.michael.feng.textlater.SendActivity.java