am_dot_com

DDM 20201124

Nov 24th, 2020 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.36 KB | None | 0 0
  1. package com.joythis.android.myphonecaller;
  2.  
  3. /*
  4. a "natural" representation of what we understand
  5. by "contact"
  6.  */
  7. public class Contact {
  8.     protected String mContactName;
  9.     protected String mContactNumber;
  10.  
  11.     public Contact(
  12.         String pName,
  13.         String pNumber
  14.     ){
  15.         this.mContactName = pName;
  16.         this.mContactNumber = pNumber;
  17.     }//Contact
  18.  
  19.     @Override
  20.     public String toString(){
  21.         String strRet = String.format(
  22.             "%s : %s",
  23.             this.mContactName,
  24.             this.mContactNumber
  25.         );
  26.         return strRet;
  27.     }//toString
  28.  
  29.     public String getName(){
  30.         return this.mContactName;
  31.     }//getName
  32.  
  33.     public String getNumber(){
  34.         return this.mContactNumber;
  35.     }//getNumber
  36. }//Contact
  37.  
  38. //*
  39.  
  40. package com.joythis.android.myphonecaller;
  41.  
  42. import android.content.ContentValues;
  43. import android.content.Context;
  44. import android.database.Cursor;
  45. import android.database.sqlite.SQLiteDatabase;
  46. import android.database.sqlite.SQLiteOpenHelper;
  47. import android.util.Log;
  48.  
  49. import androidx.annotation.Nullable;
  50.  
  51. import java.util.ArrayList;
  52.  
  53. /*
  54. for the non-volatile representation of "contacts"
  55. following the framework, regarding inheritance from
  56. SQLiteOpenHelper
  57.  */
  58. public class ContactDB extends SQLiteOpenHelper{
  59.  
  60.     public final static String DB_NAME = "contacts.db";
  61.     public final static int DB_VERSION = 1;
  62.  
  63.     //create constants for all table and column names
  64.     public final static String TABLE_CONTACTS = "tContacts";
  65.     public final static String COL_ID = "_id";
  66.     public final static String COL_NAME = "cName";
  67.     public final static String COL_NUMBER = "cNumber";
  68.  
  69.     /*
  70.     CREATE TABLE IF NOT EXISTS tContacts(
  71.     _id INTEGER PRIMARY KEY AUTOINCREMENT,
  72.     cName TEXT NOT NULL,
  73.     cNumber TEXT NOT NULL
  74.     );
  75.      */
  76.     public final static String CREATE_TABLE_CONTACTS =
  77.     "CREATE TABLE IF NOT EXISTS "+TABLE_CONTACTS+"(\n"+
  78.     COL_ID+" INTEGER PRIMARY KEY NOT NULL, \n"+
  79.     COL_NAME+" TEXT NOT NULL, \n"+
  80.     COL_NUMBER+" TEXT NOT NULL\n"+
  81.     ");";
  82.  
  83.     /*
  84.     DROP TABLE IF EXISTS tContacts;
  85.      */
  86.     public final static String DROP_TABLE_CONTACTS =
  87.     "DROP TABLE IF EXISTS "+TABLE_CONTACTS+";";
  88.  
  89.     public ContactDB(
  90.         Context pContext
  91.     ){
  92.         super(
  93.             pContext,
  94.             DB_NAME,
  95.             null,
  96.             DB_VERSION
  97.         );
  98.     }//ContactDB
  99.  
  100.     public ContactDB(
  101.         @Nullable Context context,
  102.         @Nullable String name,
  103.         @Nullable SQLiteDatabase.CursorFactory factory,
  104.         int version
  105.     ){
  106.         super(context, name, factory, version);
  107.     }//ContactDB
  108.  
  109.     void installDB(
  110.         SQLiteDatabase pDB
  111.     ){
  112.         if (pDB!=null){
  113.             try{
  114.                 pDB.execSQL(
  115.                         CREATE_TABLE_CONTACTS
  116.                 );
  117.             }//try
  118.             catch (Exception e){
  119.                 Log.e(
  120.                   getClass().getName(),
  121.                   e.toString()
  122.                 );
  123.             }//catch
  124.         }//if
  125.     }//installDB
  126.  
  127.     /*
  128.     will be AUTOMATICALLY called upon the first need to
  129.     use the database; e.g. upon the first "insert", or the
  130.     first "select".
  131.     The developer will NEVER, EVER, call this method directly.
  132.      */
  133.     @Override
  134.     public void onCreate(SQLiteDatabase db) {
  135.         //perform database installation here
  136.         installDB(db);
  137.     }//onCreate
  138.  
  139.     /*
  140.     will be AUTOMATICALLY called
  141.      */
  142.     @Override
  143.     public void onUpgrade(
  144.         SQLiteDatabase db,
  145.         int oldVersion,
  146.         int newVersion
  147.     )
  148.     {
  149.         if (db!=null){
  150.             //upgrade/change the database infrastructure here
  151.             if (newVersion > oldVersion){
  152.                 //destroy the current infrastructure7
  153.                 //TODO: backup of the previous data
  154.                 try {
  155.                     db.execSQL(DROP_TABLE_CONTACTS);
  156.                 }//try
  157.                 catch(Exception e){
  158.                     Log.e(
  159.                         getClass().getName(),
  160.                         e.toString()
  161.                     );
  162.                 }//catch
  163.                 installDB(db);
  164.             }//if
  165.         }//if
  166.     }//onUpgrade
  167.  
  168.     //insertContact
  169.     /*
  170.     will return the "line" where the record is inserted
  171.      */
  172.     public final static String KEY_NAME = "KEY_NAME";
  173.     public final static String KEY_NUMBER = "KEY_NUMBER";
  174.     public long insertContact (Contact pC)
  175.     {
  176.         SQLiteDatabase db = this.getWritableDatabase();
  177.         if (db!=null){
  178.             ContentValues pairsKeyValue = new ContentValues();
  179.  
  180.             pairsKeyValue.put(
  181.                 KEY_NAME,
  182.                 pC.getName()
  183.             ); //contact name
  184.             pairsKeyValue.put(
  185.                 KEY_NUMBER,
  186.                 pC.getNumber()
  187.             ); //contact number
  188.  
  189.             //-1 on failure
  190.             long iRecordNumber =
  191.                 db.insert(
  192.                     TABLE_CONTACTS,
  193.                     null,
  194.                     pairsKeyValue
  195.                 );
  196.  
  197.             return iRecordNumber;
  198.         }//if
  199.  
  200.         return -1;
  201.     }//insertContact
  202.  
  203.     /*
  204.     public long insertContact(
  205.         String pStrName, String pStrNumber
  206.     ){
  207.  
  208.     }//insertContact
  209.  
  210.      */
  211.  
  212.     public final static String SELECT_ALL_CONTACTS_BY_NAME_ASC =
  213.         "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NAME+" ASC;";
  214.     public final static String SELECT_ALL_CONTACTS_BY_NAME_DESC =
  215.             "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NAME+" DESC;";
  216.  
  217.     public final static String SELECT_ALL_CONTACTS_BY_NUMBER_ASC =
  218.         "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NUMBER+" ASC;";
  219.     public final static String SELECT_ALL_CONTACTS_BY_NUMBER_DESC =
  220.             "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NUMBER+" DESC;";
  221.  
  222.     public final static String SELECT_ALL_CONTACTS_BY_INSERT_DATE_ASC =
  223.         "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_ID+" ASC;";
  224.     public final static String SELECT_ALL_CONTACTS_BY_INSERT_DATE_DESC =
  225.         "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_ID+" DESC;";
  226.  
  227.     public ArrayList<Contact> selectAllContactsByNameASC(){
  228.         return selectAllByQuery(SELECT_ALL_CONTACTS_BY_NAME_ASC);
  229.     }//selectAllContactsByNameASC
  230.  
  231.     public ArrayList<Contact> selectAllContactsByNameDESC(){
  232.         return selectAllByQuery(SELECT_ALL_CONTACTS_BY_NAME_DESC);
  233.     }//selectAllContactsByNameASC
  234.  
  235.     public ArrayList<Contact> selectAllContactsByNumberASC(){
  236.         return selectAllByQuery(SELECT_ALL_CONTACTS_BY_NUMBER_ASC);
  237.     }//selectAllContactsByNumberASC
  238.  
  239.     public ArrayList<Contact> selectAllContactsByNumberDESC(){
  240.         return selectAllByQuery(SELECT_ALL_CONTACTS_BY_NUMBER_DESC);
  241.     }//selectAllContactsByNumberASC
  242.  
  243.     public ArrayList<Contact> selectAllContactsByInsertDateASC(){
  244.         return selectAllByQuery(SELECT_ALL_CONTACTS_BY_INSERT_DATE_ASC);
  245.     }//selectAllContactsByNameASC
  246.  
  247.     public ArrayList<Contact> selectAllContactsByInsertDateDESC(){
  248.         return selectAllByQuery(SELECT_ALL_CONTACTS_BY_INSERT_DATE_DESC);
  249.     }//selectAllContactsByNameASC
  250.  
  251.     private ArrayList<Contact> selectAllByQuery(
  252.         String pSelectQuery
  253.     ){
  254.         ArrayList<Contact> aRet = new ArrayList<>();
  255.  
  256.         boolean bCheck = pSelectQuery.length()>0;
  257.         if (bCheck){
  258.             SQLiteDatabase db = getReadableDatabase();
  259.             if (db!=null){
  260.                 Cursor cursor =
  261.                     db.rawQuery(
  262.                         pSelectQuery,
  263.                         null
  264.                     );
  265.  
  266.                 if (cursor!=null){
  267.                     cursor.moveToFirst();
  268.                     while(!cursor.isAfterLast()){
  269.                         String strName =
  270.                             cursor.getString(
  271.                                 cursor.getColumnIndex(COL_NAME)
  272.                                //which column INDEX?
  273.                             );
  274.                         String strNumber =
  275.                             cursor.getString(
  276.                                 cursor.getColumnIndex(COL_NUMBER)
  277.                                 //which column INDEX?
  278.                             );
  279.  
  280.                         Contact c = new Contact(strName, strNumber);
  281.                         aRet.add(c);
  282.  
  283.                         cursor.moveToNext();
  284.                     }//while
  285.                 }//if
  286.             }//if can read the database
  287.         }//if got a non empty query
  288.  
  289.         return aRet;
  290.     }//selectAll
  291. }//ContactDB
  292.  
  293. //**
  294.  
  295. package com.joythis.android.myphonecaller;
  296.  
  297. import androidx.appcompat.app.AppCompatActivity;
  298.  
  299. import android.content.Context;
  300. import android.os.Bundle;
  301.  
  302. import java.util.ArrayList;
  303.  
  304. public class MainActivity extends AppCompatActivity {
  305.  
  306.     Context mContext;
  307.     ContactDB mDB;
  308.  
  309.     @Override
  310.     protected void onCreate(Bundle savedInstanceState) {
  311.         super.onCreate(savedInstanceState);
  312.         setContentView(R.layout.activity_main);
  313.  
  314.         init();
  315.     }//onCreate
  316.  
  317.     void init(){
  318.         mContext = this;
  319.  
  320.         //TODO:
  321.         /*
  322.         layout
  323.         op layout
  324.          */
  325.  
  326.         mDB = new ContactDB(this);
  327.         Contact c = new Contact("Artur", "123");
  328.         mDB.insertContact(c);
  329.  
  330.         ArrayList<Contact> all = mDB.selectAllContactsByNumberDESC();
  331.     }
  332. }
Advertisement
Add Comment
Please, Sign In to add comment