Android Open Source - hpush D B






From Project

Back to project page hpush.

License

The source code is released under:

MIT License

If you think the Android project hpush 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

package com.hpush.db;
/*w w  w  . ja va  2 s . c  om*/

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

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.hpush.data.Daily;
import com.hpush.data.DailyListItem;
import com.hpush.data.Message;
import com.hpush.data.MessageListItem;
import com.hpush.utils.Prefs;


/**
 * Defines methods that operate on database.
 * <p/>
 * <b>Singleton pattern.</b>
 * <p/>
 * <p/>
 *
 * @author Xinyue Zhao
 */
public final class DB {
  /**
   * {@link android.content.Context}.
   */
  private Context mContext;
  /**
   * Impl singleton pattern.
   */
  private static DB sInstance;
  /**
   * Helper class that create, delete, update tables of database.
   */
  private  DatabaseHelper mDatabaseHelper;
  /**
   * The database object.
   */
  private SQLiteDatabase mDB;

  /**
   * Constructor of {@link DB}. Impl singleton pattern so that it is private.
   *
   * @param cxt
   *     {@link android.content.Context}.
   */
  private DB(Context cxt) {
    mContext = cxt;
  }

  /**
   * Get instance of  {@link  DB} singleton.
   *
   * @param cxt
   *     {@link android.content.Context}.
   *
   * @return The {@link DB} singleton.
   */
  public static DB getInstance(Context cxt) {
    if (sInstance == null) {
      sInstance = new DB(cxt);
    }
    return sInstance;
  }

  /**
   * Open database.
   */
  public synchronized void open() {
    mDatabaseHelper = new  DatabaseHelper(mContext);
    mDB = mDatabaseHelper.getWritableDatabase();
  }

  /**
   * Close database.
   */
  public synchronized void close() {
    mDatabaseHelper.close();
  }


  public synchronized boolean addMessage(Message item) {
    if (mDB == null || !mDB.isOpen()) {
      open();
    }
    boolean success = false;
    try {
      long rowId = -1;
      ContentValues v = new ContentValues();
      v.put(MessagesTbl.BY, item.getBy());
      v.put(MessagesTbl.ID, item.getId());
      v.put(MessagesTbl.SCORE, item.getScore());
      v.put(MessagesTbl.COMMENTS_COUNT, item.getCommentsCount());
      v.put(MessagesTbl.TEXT, item.getText());
      v.put(MessagesTbl.TIME, item.getTime());
      v.put(MessagesTbl.TITLE, item.getTitle());
      v.put(MessagesTbl.URL, item.getUrl());

      rowId = mDB.insert(MessagesTbl.TABLE_NAME, null, v);
      item.setDbId(rowId);
      success = rowId != -1;
    } finally {
      close();
    }
    return success;
  }

  public synchronized boolean addBookmark(Message item) {
    if (mDB == null || !mDB.isOpen()) {
      open();
    }
    boolean success = false;
    try {
      long rowId = -1;
      ContentValues v = new ContentValues();
      v.put(BookmarksTbl.BY, item.getBy());
      v.put(BookmarksTbl.ID, item.getId());
      v.put(BookmarksTbl.SCORE, item.getScore());
      v.put(BookmarksTbl.COMMENTS_COUNT, item.getCommentsCount());
      v.put(BookmarksTbl.TEXT, item.getText());
      v.put(BookmarksTbl.TIME, item.getTime());
      v.put(BookmarksTbl.TITLE, item.getTitle());
      v.put(BookmarksTbl.URL, item.getUrl());

      rowId = mDB.insert(BookmarksTbl.TABLE_NAME, null, v);
      item.setDbId(rowId);
      success = rowId != -1;
    } finally {
      close();
    }
    return success;
  }

  public synchronized boolean updateMessage(Message item) {
    if (mDB == null || !mDB.isOpen()) {
      open();
    }
    boolean success = false;
    try {
      long rowId;
      ContentValues v = new ContentValues();
      v.put(MessagesTbl.BY, item.getBy());
      v.put(MessagesTbl.SCORE, item.getScore());
      v.put(MessagesTbl.COMMENTS_COUNT, item.getCommentsCount());
      v.put(MessagesTbl.TEXT, item.getText());
      v.put(MessagesTbl.TIME, item.getTime());
      v.put(MessagesTbl.TITLE, item.getTitle());
      v.put(MessagesTbl.URL, item.getUrl());
      String[] args = new String[] { item.getId() + "" };
      rowId = mDB.update(MessagesTbl.TABLE_NAME, v, MessagesTbl.ID + " = ?", args);
      success = rowId != -1;
    } finally {
      close();
    }
    return success;
  }


  public synchronized boolean updateBookmark(Message item) {
    if (mDB == null || !mDB.isOpen()) {
      open();
    }
    boolean success = false;
    try {
      long rowId;
      ContentValues v = new ContentValues();
      v.put(BookmarksTbl.BY, item.getBy());
      v.put(BookmarksTbl.SCORE, item.getScore());
      v.put(BookmarksTbl.COMMENTS_COUNT, item.getCommentsCount());
      v.put(BookmarksTbl.TEXT, item.getText());
      v.put(BookmarksTbl.TIME, item.getTime());
      v.put(BookmarksTbl.TITLE, item.getTitle());
      v.put(BookmarksTbl.URL, item.getUrl());
      String[] args = new String[] { item.getId() + "" };
      rowId = mDB.update(BookmarksTbl.TABLE_NAME, v, BookmarksTbl.ID + " = ?", args);
      success = rowId != -1;
    } finally {
      close();
    }
    return success;
  }




  public synchronized int removeMessage(Message item) {
    if (mDB == null || !mDB.isOpen()) {
      open();
    }
    int rowsRemain = -1;
    boolean success;
    try {
      long rowId;
      if(item != null) {
        String whereClause = MessagesTbl.ID + "=?";
        String[] whereArgs = new String[] { String.valueOf(item.getId()) };
        rowId = mDB.delete(MessagesTbl.TABLE_NAME, whereClause, whereArgs);
      } else {
        rowId = mDB.delete(MessagesTbl.TABLE_NAME, null, null);
      }
      success = rowId > 0;
      if (success) {
        Cursor c = mDB.query(MessagesTbl.TABLE_NAME, new String[] { MessagesTbl.ID }, null, null, null, null, null);
        rowsRemain = c.getCount();
      } else {
        rowsRemain = -1;
      }
    } finally {
      close();
    }
    return rowsRemain;
  }


  public synchronized int removeBookmark(Message item) {
    if (mDB == null || !mDB.isOpen()) {
      open();
    }
    int rowsRemain = -1;
    boolean success;
    try {
      long rowId;
      if(item != null) {
        String whereClause = MessagesTbl.ID + "=?";
        String[] whereArgs = new String[] { String.valueOf(item.getId()) };
        rowId = mDB.delete(BookmarksTbl.TABLE_NAME, whereClause, whereArgs);
      } else {
        rowId = mDB.delete(BookmarksTbl.TABLE_NAME, null, null);
      }
      success = rowId > 0;
      if (success) {
        Cursor c = mDB.query(BookmarksTbl.TABLE_NAME, new String[] { BookmarksTbl.ID }, null, null, null, null, null);
        rowsRemain = c.getCount();
      } else {
        rowsRemain = -1;
      }
    } finally {
      close();
    }
    return rowsRemain;
  }



  /**
   * Sort direction.
   */
  public enum Sort {
    DESC("DESC"), ASC("ASC");
    private String nm;
    Sort(String nm) {
      this.nm = nm;
    }

    @Override
    public String toString() {
      return nm;
    }
  }


  public synchronized List<MessageListItem> getMessages(Sort sort) {
    if (mDB == null || !mDB.isOpen()) {
      open();
    }
    Cursor c = mDB.query(MessagesTbl.TABLE_NAME, null, null, null, null, null,
         getSortBy(mContext) + " " + sort.toString());
    Message item    ;
    List<MessageListItem>  list = new ArrayList<>();
    try {
      while (c.moveToNext()) {
        item = new Message(
            c.getLong(c.getColumnIndex(MessagesTbl.DB_ID)),
            c.getString(c.getColumnIndex(MessagesTbl.BY)),
            c.getLong(c.getColumnIndex(MessagesTbl.ID)),
            c.getLong(c.getColumnIndex(MessagesTbl.SCORE)),
            c.getLong(c.getColumnIndex(MessagesTbl.COMMENTS_COUNT)),
            c.getString(c.getColumnIndex(MessagesTbl.TEXT)),
            c.getLong(c.getColumnIndex(MessagesTbl.TIME)),
            c.getString(c.getColumnIndex(MessagesTbl.TITLE)),
            c.getString(c.getColumnIndex(MessagesTbl.URL)),
            c.getLong(c.getColumnIndex(MessagesTbl.PUSHED_TIME))
        );
        list.add(new MessageListItem(item));
      }
    } finally {
      if (c != null) {
        c.close();
      }
      close();
      return list;
    }
  }


  public synchronized List<MessageListItem> getBookmarks(Sort sort) {
    if (mDB == null || !mDB.isOpen()) {
      open();
    }
    Cursor c = mDB.query(BookmarksTbl.TABLE_NAME, null, null, null, null, null,
         getSortBy(mContext) + " " + sort.toString());
    Message item   ;
    List<MessageListItem>  list = new ArrayList<>();
    try {
      while (c.moveToNext()) {
        item = new Message(
            c.getLong(c.getColumnIndex(BookmarksTbl.DB_ID)),
            c.getString(c.getColumnIndex(BookmarksTbl.BY)),
            c.getLong(c.getColumnIndex(BookmarksTbl.ID)),
            c.getLong(c.getColumnIndex(BookmarksTbl.SCORE)),
            c.getLong(c.getColumnIndex(BookmarksTbl.COMMENTS_COUNT)),
            c.getString(c.getColumnIndex(BookmarksTbl.TEXT)),
            c.getLong(c.getColumnIndex(BookmarksTbl.TIME)),
            c.getString(c.getColumnIndex(BookmarksTbl.TITLE)),
            c.getString(c.getColumnIndex(BookmarksTbl.URL)),
            c.getLong(c.getColumnIndex(BookmarksTbl.PUSHED_TIME))
        );
        list.add(new MessageListItem(item));
      }
    } finally {
      if (c != null) {
        c.close();
      }
      close();
      return list;
    }
  }


  public synchronized  boolean findMessage(Message item) {
    if (mDB == null || !mDB.isOpen()) {
      open();
    }
    boolean success;
    try {
      String whereClause =   MessagesTbl.ID + "=?";
      String[] whereArgs = new String[] {   String.valueOf(item.getId()) };
      Cursor c = mDB.query(MessagesTbl.TABLE_NAME, new String[] { MessagesTbl.ID }, whereClause, whereArgs, null, null, null);
      success = c.getCount() >= 1;
    } finally {
      close();
    }
    return success ;
  }

  public synchronized boolean findBookmark(Message item) {
    if (mDB == null || !mDB.isOpen()) {
      open();
    }
    boolean success;
    try {
      String whereClause =   BookmarksTbl.ID + "=?";
      String[] whereArgs = new String[] {   String.valueOf(item.getId()) };
      Cursor c = mDB.query(BookmarksTbl.TABLE_NAME, new String[] { BookmarksTbl.ID }, whereClause, whereArgs, null, null, null);
      success = c.getCount() >= 1;
    } finally {
      close();
    }
    return success ;
  }


  /**
   * Get column name to sort data.
   * <p/>
   * <code> <p/>
   * <item>Scores</item> <p/>
   * <item>Arrival</item> <p/>
   * <item>Creation</item> <p/>
   * <item>Comments</item> <p/>
   * </code>
   *
   * @param cxt
   *     {@link android.content.Context}.
   *
   * @return Name of column.
   */
  private static String getSortBy(Context cxt) {
    String sortTypeValue = Prefs.getInstance(cxt.getApplicationContext()).getSortTypeValue();
    switch (sortTypeValue) {
    case "0":
      return MessagesTbl.SCORE;
    case "1":
      return MessagesTbl.PUSHED_TIME;
    case "2":
      return MessagesTbl.TIME;
    case "3":
      return MessagesTbl.COMMENTS_COUNT;
    default:
      return MessagesTbl.TIME;
    }
  }

  public synchronized boolean addDaily(String id) {
    if (mDB == null || !mDB.isOpen()) {
      open();
    }
    boolean success = false;
    try {
      long rowId = -1;
      ContentValues v = new ContentValues();
      v.put(DailyTbl.ID, Long.valueOf(id));
      v.put(DailyTbl.EDIT_TIME, System.currentTimeMillis());
      rowId = mDB.insert(DailyTbl.TABLE_NAME, null, v);
      success = rowId != -1;
    } finally {
      close();
    }
    return success;
  }


  public synchronized boolean updateDaily(String id) {
    if (mDB == null || !mDB.isOpen()) {
      open();
    }
    boolean success = false;
    try {
      long rowId;
      ContentValues v = new ContentValues();
      v.put(DailyTbl.EDIT_TIME, System.currentTimeMillis());
      String[] args = new String[] { id    };
      rowId = mDB.update(DailyTbl.TABLE_NAME, v, DailyTbl.ID + " = ?", args);
      success = rowId != -1;
    } finally {
      close();
    }
    return success;
  }

  public synchronized void clearDailies() {
    if (mDB == null || !mDB.isOpen()) {
      open();
    }
    try {
      mDB.delete(DailyTbl.TABLE_NAME, null, null);
    } finally {
      close();
    }
  }

  public synchronized boolean findDaily(String id) {
    if (mDB == null || !mDB.isOpen()) {
      open();
    }
    boolean success;
    try {
      String whereClause =   DailyTbl.ID + "=?";
      String[] whereArgs = new String[] {   id    };
      Cursor c = mDB.query(DailyTbl.TABLE_NAME, new String[] { DailyTbl.ID }, whereClause, whereArgs, null, null, null);
      success = c.getCount() >= 1;
    } finally {
      close();
    }
    return success ;
  }

  public synchronized List<DailyListItem> getDailies(Sort sort) {
    if (mDB == null || !mDB.isOpen()) {
      open();
    }
    Cursor c = mDB.query(DailyTbl.TABLE_NAME, null, null, null, null, null,
        DailyTbl.EDIT_TIME + " " + sort.toString());
    List<DailyListItem>  list = new ArrayList<>();
    try {
      long id;
      Message msg;
      while (c.moveToNext()) {
        id = c.getLong(c.getColumnIndex(DailyTbl.ID));
        msg = getBookmark(id);
        if(msg != null) {
          list.add(new DailyListItem(new Daily(msg, true)));
        }
        else {
          msg = getMessage(id);
          if(msg != null) {
            list.add(new DailyListItem(new Daily(msg, false)));
          }
        }
      }
    } finally {
      if (c != null) {
        c.close();
      }
      close();
    }
    return list;
  }

  public synchronized  Message getMessage(long  id) {
    if (mDB == null || !mDB.isOpen()) {
      open();
    }
    Message msg = null;
    try {
      String whereClause =   MessagesTbl.ID + "=?";
      String[] whereArgs = new String[] {   String.valueOf(id) };
      Cursor c = mDB.query(MessagesTbl.TABLE_NAME, null, whereClause, whereArgs, null, null, null);
      while (c.moveToNext()) {
        msg = new Message(
            c.getLong(c.getColumnIndex(MessagesTbl.DB_ID)),
            c.getString(c.getColumnIndex(MessagesTbl.BY)),
            c.getLong(c.getColumnIndex(MessagesTbl.ID)),
            c.getLong(c.getColumnIndex(MessagesTbl.SCORE)),
            c.getLong(c.getColumnIndex(MessagesTbl.COMMENTS_COUNT)),
            c.getString(c.getColumnIndex(MessagesTbl.TEXT)),
            c.getLong(c.getColumnIndex(MessagesTbl.TIME)),
            c.getString(c.getColumnIndex(MessagesTbl.TITLE)),
            c.getString(c.getColumnIndex(MessagesTbl.URL)),
            c.getLong(c.getColumnIndex(MessagesTbl.PUSHED_TIME))
        );
      }
    } finally {
      close();
    }
    return msg ;
  }

  public synchronized Message getBookmark(long id) {
    if (mDB == null || !mDB.isOpen()) {
      open();
    }
    Message msg = null;
    try {
      String whereClause =   BookmarksTbl.ID + "=?";
      String[] whereArgs = new String[] {   String.valueOf(id) };
      Cursor c = mDB.query(BookmarksTbl.TABLE_NAME, null, whereClause, whereArgs, null, null, null);
      while (c.moveToNext()) {
        msg = new Message(
            c.getLong(c.getColumnIndex(BookmarksTbl.DB_ID)),
            c.getString(c.getColumnIndex(BookmarksTbl.BY)),
            c.getLong(c.getColumnIndex(BookmarksTbl.ID)),
            c.getLong(c.getColumnIndex(BookmarksTbl.SCORE)),
            c.getLong(c.getColumnIndex(BookmarksTbl.COMMENTS_COUNT)),
            c.getString(c.getColumnIndex(BookmarksTbl.TEXT)),
            c.getLong(c.getColumnIndex(BookmarksTbl.TIME)),
            c.getString(c.getColumnIndex(BookmarksTbl.TITLE)),
            c.getString(c.getColumnIndex(BookmarksTbl.URL)),
            c.getLong(c.getColumnIndex(BookmarksTbl.PUSHED_TIME))
        );
      }
    } finally {
      close();
    }
    return msg ;
  }
}




Java Source Code List

com.hpush.app.App.java
com.hpush.app.activities.BasicActivity.java
com.hpush.app.activities.DailiesActivity.java
com.hpush.app.activities.MainActivity.java
com.hpush.app.activities.SettingActivity.java
com.hpush.app.activities.WebViewActivity.java
com.hpush.app.adapters.DailiesListAdapter.java
com.hpush.app.adapters.MainViewPagerAdapter.java
com.hpush.app.adapters.MessagesListAdapter.java
com.hpush.app.fragments.AboutDialogFragment.java
com.hpush.app.fragments.AdFragment.java
com.hpush.app.fragments.AppListImpFragment.java
com.hpush.app.fragments.BookmarksListFragment.java
com.hpush.app.fragments.DailiesLstFragment.java
com.hpush.app.fragments.GPlusFragment.java
com.hpush.app.fragments.MessagesListFragment.java
com.hpush.bus.BookmarkAllEvent.java
com.hpush.bus.BookmarkMessageEvent.java
com.hpush.bus.BookmarkedEvent.java
com.hpush.bus.ClickMessageCommentsEvent.java
com.hpush.bus.ClickMessageEvent.java
com.hpush.bus.ClickMessageLinkEvent.java
com.hpush.bus.DeleteAccountEvent.java
com.hpush.bus.EULAConfirmedEvent.java
com.hpush.bus.EULARejectEvent.java
com.hpush.bus.EditSettingsEvent.java
com.hpush.bus.GCMRegistedEvent.java
com.hpush.bus.InsertAccountEvent.java
com.hpush.bus.LoadAllEvent.java
com.hpush.bus.LoginedGPlusEvent.java
com.hpush.bus.LogoutGPlusEvent.java
com.hpush.bus.RemoveAllEvent.java
com.hpush.bus.SelectMessageEvent.java
com.hpush.bus.ShareMessageEvent.java
com.hpush.bus.ShowActionBar.java
com.hpush.bus.SortAllEvent.java
com.hpush.bus.UpdateCurrentTotalMessagesEvent.java
com.hpush.data.DailyListItem.java
com.hpush.data.Daily.java
com.hpush.data.FunctionType.java
com.hpush.data.MessageListItem.java
com.hpush.data.Message.java
com.hpush.data.Status.java
com.hpush.data.SyncList.java
com.hpush.db.BookmarksTbl.java
com.hpush.db.DB.java
com.hpush.db.DailyTbl.java
com.hpush.db.DatabaseHelper.java
com.hpush.db.MessagesTbl.java
com.hpush.gcm.ChangeSettingsTask.java
com.hpush.gcm.GcmBroadcastReceiver.java
com.hpush.gcm.GcmIntentService.java
com.hpush.gcm.RegGCMTask.java
com.hpush.gcm.SyncTask.java
com.hpush.gcm.UnregGCMTask.java
com.hpush.utils.Prefs.java
com.hpush.utils.Utils.java
com.hpush.views.FontTextView.java
com.hpush.views.OnViewAnimatedClickedListener2.java
com.hpush.views.OnViewAnimatedClickedListener3.java
com.hpush.views.OnViewAnimatedClickedListener.java
com.hpush.views.SortActionViewProvider.java
com.hpush.views.WebViewEx.java