/** * @Title: getContacts @Description: get contacts * * @param page page (10 items per page) * @return ArrayList<MyContact> * @throws */ public ArrayList<MyContact> getContacts(int page) { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); String sql = "SELECT DISTINCT name, number FROM logs WHERE record_state_flag = " + RecordState.LOCAL.ordinal() + " ORDER BY date DESC limit " + (page * 10) + ", 10"; Cursor cursor = null; ArrayList<MyContact> contacts = new ArrayList<MyContact>(); try { cursor = db.rawQuery(sql, null); while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex("name")); String number = cursor.getString(cursor.getColumnIndex("number")); long date = getLatestContactDate(number); int count = getContactCount(number); contacts.add(new MyContact(name, number, date, count)); } return contacts; } catch (SQLException e) { e.printStackTrace(); return null; } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } if (db != null && db.isOpen()) { db.close(); } } }
/** * @Title: getIndividualLogs @Description: get individual logs by page * * @param page * @return ArrayList<Log> * @throws */ public ArrayList<Log> getIndividualLogs(String number, int page) { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); String sql = "SELECT * FROM logs WHERE number like '%" + number + "%' AND record_state_flag = " + RecordState.LOCAL.ordinal() + " ORDER BY date DESC limit " + (page * 10) + ", 10"; // System.out.println(sql); Cursor cursor = null; ArrayList<Log> list = new ArrayList<Log>(); try { cursor = db.rawQuery(sql, null); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("id")); String name = cursor.getString(cursor.getColumnIndex("name")); int duration = cursor.getInt(cursor.getColumnIndex("duration")); long date = Long.parseLong(cursor.getString(cursor.getColumnIndex("date"))); CallType callType = CallType.fromInt(cursor.getInt(cursor.getColumnIndex("type")) - 1); String uid = cursor.getString(cursor.getColumnIndex("uid")); RecordFlagState recordFlag = RecordFlagState.fromInt(cursor.getInt(cursor.getColumnIndex("record_flag"))); LogState logState = LogState.NOT_DELETED; RecordState recordState = RecordState.fromInt(cursor.getInt(cursor.getColumnIndex("record_state_flag"))); Timestamp addDate = Timestamp.valueOf(cursor.getString(cursor.getColumnIndex("update_date"))); list.add( new Log( id, name, number, duration, date, callType, uid, recordFlag, logState, recordState, addDate)); } return list; } catch (SQLException e) { e.printStackTrace(); return null; } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } if (db != null && db.isOpen()) { db.close(); } } }
/** * @Title: getContactCount @Description: get count * * @param number number of the contact * @return int count * @throws */ public int getContactCount(String number) { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); String sql = ""; if (number.length() < 5) { sql = "SELECT count(*) as count FROM logs WHERE number = '" + number + "' AND record_state_flag = " + RecordState.LOCAL.ordinal(); } else { sql = "SELECT count(*) as count FROM logs WHERE number like '%" + number + "%' AND record_state_flag = " + RecordState.LOCAL.ordinal(); } Cursor cursor = null; // ArrayList<MyContact> contacts = new ArrayList<MyContact>(); try { cursor = db.rawQuery(sql, null); if (cursor.moveToFirst()) { return cursor.getInt(cursor.getColumnIndex("count")); } } catch (SQLException e) { e.printStackTrace(); return 0; } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } if (db != null && db.isOpen()) { db.close(); } } return 0; }