Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.joythis.android.myphonecaller;
- import android.Manifest;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.net.Uri;
- import android.util.Log;
- import android.widget.Toast;
- import androidx.core.content.ContextCompat;
- import java.net.URI;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- public class AmUtil {
- Activity mActivity;
- public AmUtil(Activity pA){
- this.mActivity = pA;
- }//AmUtil
- public void fb(
- String pStrMsg
- ){
- Toast t = Toast.makeText(
- this.mActivity,
- pStrMsg,
- Toast.LENGTH_LONG
- );
- t.show();
- }//fb
- public void phoneTo(
- String pStrPhoneNumber
- ){
- try {
- Uri uriForPhoneNumber = Uri.parse("tel:" + pStrPhoneNumber);
- Intent intentForMakingThePhoneCall =
- new Intent(
- Intent.ACTION_CALL
- );
- intentForMakingThePhoneCall.setData(
- uriForPhoneNumber
- );
- boolean bUserAuthorizedTheAppToMakePhoneCalls =
- ContextCompat.checkSelfPermission(
- mActivity,
- Manifest.permission.CALL_PHONE
- ) == PackageManager.PERMISSION_GRANTED;
- if (bUserAuthorizedTheAppToMakePhoneCalls) {
- this.mActivity.startActivity(
- intentForMakingThePhoneCall
- );
- }
- else{
- boolean bUserAcceptsExplanationsOnWhyThePermissionIsImportant =
- mActivity.shouldShowRequestPermissionRationale(
- Manifest.permission.CALL_PHONE
- );
- if (bUserAcceptsExplanationsOnWhyThePermissionIsImportant){
- fb("Without this permission, the app can NOT make phone calls.");
- }
- }
- }//try
- catch(Exception e){
- Log.e(
- getClass().getName(),
- e.toString()
- );
- }//catch
- }//phoneTo
- public Map<Integer, ArrayList<String>>
- identifyPermissionsGrantedAndDenied(
- String[] paNecessaryPermissions
- )
- {
- Map<Integer, ArrayList<String>> retMap = new HashMap<>();
- ArrayList<String> alGranted = new ArrayList<>();
- ArrayList<String> alDenied = new ArrayList<>();
- for (String permission : paNecessaryPermissions){
- boolean bGranted =
- ContextCompat.checkSelfPermission(
- mActivity,
- permission
- ) == PackageManager.PERMISSION_GRANTED;
- boolean bDenied =
- ContextCompat.checkSelfPermission(
- mActivity,
- permission
- ) == PackageManager.PERMISSION_DENIED;
- if (bGranted) alGranted.add(permission);
- if (bDenied) alDenied.add(permission);
- }//for
- retMap.put(/* Integer 0 */ PackageManager.PERMISSION_GRANTED, alGranted);
- retMap.put(/* Integer -1 */ PackageManager.PERMISSION_DENIED, alDenied);
- return retMap;
- }//identifyPermissionsGrantedAndDenied
- public void requestNecessaryPermissionsNotYetGranted(
- String[] paNecessaryPermissions,
- int piCallBackCodeForWhenTheUserResponds
- ){
- /*
- do not request permissions already granted
- */
- }//requestNecessaryPermissionsNotYetGranted
- }
- //**
- package com.joythis.android.myphonecaller;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
- import androidx.annotation.Nullable;
- import java.util.ArrayList;
- /*
- for the non-volatile representation of "contacts"
- following the framework, regarding inheritance from
- SQLiteOpenHelper
- */
- //1 - assume the inheritance
- public class ContactDB extends SQLiteOpenHelper{
- public final static String DB_NAME = "contacts.db";
- public final static int DB_VERSION = 4;
- //create constants for all table and column names
- public final static String TABLE_CONTACTS = "tContacts";
- public final static String COL_ID = "_id";
- public final static String COL_NAME = "cName";
- public final static String COL_NUMBER = "cNumber";
- /*
- CREATE TABLE IF NOT EXISTS tContacts(
- _id INTEGER PRIMARY KEY AUTOINCREMENT,
- cName TEXT NOT NULL,
- cNumber TEXT NOT NULL
- );
- */
- public final static String CREATE_TABLE_CONTACTS =
- "CREATE TABLE IF NOT EXISTS "+TABLE_CONTACTS+"(\n"+
- COL_ID+" INTEGER PRIMARY KEY NOT NULL, \n"+
- COL_NAME+" TEXT NOT NULL, \n"+
- COL_NUMBER+" TEXT NOT NULL\n"+
- ");";
- /*
- DROP TABLE IF EXISTS tContacts;
- */
- public final static String DROP_TABLE_CONTACTS =
- "DROP TABLE IF EXISTS "+TABLE_CONTACTS+";";
- public ContactDB(
- Context pContext
- ){
- super(
- pContext,
- DB_NAME,
- null,
- DB_VERSION
- );
- }//ContactDB
- public ContactDB(
- @Nullable Context context,
- @Nullable String name,
- @Nullable SQLiteDatabase.CursorFactory factory,
- int version
- ){
- super(context, name, factory, version);
- }//ContactDB
- void installDB(
- SQLiteDatabase pDB
- ){
- if (pDB!=null){
- try{
- pDB.execSQL(
- CREATE_TABLE_CONTACTS
- );
- }//try
- catch (Exception e){
- Log.e(
- getClass().getName(),
- e.toString()
- );
- }//catch
- }//if
- }//installDB
- /*
- will be AUTOMATICALLY called upon the first need to
- use the database; e.g. upon the first "insert", or the
- first "select".
- The developer will NEVER, EVER, call this method directly.
- */
- //2
- @Override
- public void onCreate(SQLiteDatabase db) {
- //perform database installation here
- installDB(db);
- }//onCreate
- /*
- will be AUTOMATICALLY called
- */
- //2
- @Override
- public void onUpgrade(
- SQLiteDatabase db,
- int oldVersion,
- int newVersion
- )
- {
- if (db!=null){
- //upgrade/change the database infrastructure here
- if (newVersion > oldVersion){
- //destroy the current infrastructure7
- //TODO: backup of the previous data
- try {
- db.execSQL(DROP_TABLE_CONTACTS);
- }//try
- catch(Exception e){
- Log.e(
- getClass().getName(),
- e.toString()
- );
- }//catch
- installDB(db); //rebuild using the new version of the infrastructure
- }//if
- }//if
- }//onUpgrade
- //insertContact
- /*
- will return the "line" where the record is inserted
- */
- public final static String KEY_NAME = "KEY_NAME";
- public final static String KEY_NUMBER = "KEY_NUMBER";
- public long insertContact (Contact pC)
- {
- SQLiteDatabase db = this.getWritableDatabase();
- if (db!=null){
- ContentValues pairsKeyValue = new ContentValues();
- pairsKeyValue.put(
- COL_NAME,
- pC.getName()
- ); //contact name
- pairsKeyValue.put(
- COL_NUMBER,
- pC.getNumber()
- ); //contact number
- //-1 on failure
- long iRecordNumber =
- db.insert(
- TABLE_CONTACTS,
- null,
- pairsKeyValue
- );
- db.close();
- return iRecordNumber;
- }//if
- return -1;
- }//insertContact
- /*
- public long insertContact(
- String pStrName, String pStrNumber
- ){
- }//insertContact
- */
- public final static String SELECT_ALL_CONTACTS_BY_NAME_ASC =
- "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NAME+" ASC;";
- public final static String SELECT_ALL_CONTACTS_BY_NAME_DESC =
- "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NAME+" DESC;";
- public final static String SELECT_ALL_CONTACTS_BY_NUMBER_ASC =
- "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NUMBER+" ASC;";
- public final static String SELECT_ALL_CONTACTS_BY_NUMBER_DESC =
- "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_NUMBER+" DESC;";
- public final static String SELECT_ALL_CONTACTS_BY_INSERT_DATE_ASC =
- "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_ID+" ASC;";
- public final static String SELECT_ALL_CONTACTS_BY_INSERT_DATE_DESC =
- "SELECT * FROM "+TABLE_CONTACTS+" ORDER BY "+COL_ID+" DESC;";
- public ArrayList<Contact> selectAllContactsByNameASC(){
- return selectAllByQuery(SELECT_ALL_CONTACTS_BY_NAME_ASC);
- }//selectAllContactsByNameASC
- public ArrayList<Contact> selectAllContactsByNameDESC(){
- return selectAllByQuery(SELECT_ALL_CONTACTS_BY_NAME_DESC);
- }//selectAllContactsByNameASC
- public ArrayList<Contact> selectAllContactsByNumberASC(){
- return selectAllByQuery(SELECT_ALL_CONTACTS_BY_NUMBER_ASC);
- }//selectAllContactsByNumberASC
- public ArrayList<Contact> selectAllContactsByNumberDESC(){
- return selectAllByQuery(SELECT_ALL_CONTACTS_BY_NUMBER_DESC);
- }//selectAllContactsByNumberASC
- public ArrayList<Contact> selectAllContactsByInsertDateASC(){
- return selectAllByQuery(SELECT_ALL_CONTACTS_BY_INSERT_DATE_ASC);
- }//selectAllContactsByNameASC
- public ArrayList<Contact> selectAllContactsByInsertDateDESC(){
- return selectAllByQuery(SELECT_ALL_CONTACTS_BY_INSERT_DATE_DESC);
- }//selectAllContactsByNameASC
- private ArrayList<Contact> selectAllByQuery(
- String pSelectQuery
- ){
- ArrayList<Contact> aRet = new ArrayList<>();
- boolean bCheck = pSelectQuery.length()>0;
- //bCheck = !pSelectQuery.isEmpty();
- if (bCheck){
- try {
- SQLiteDatabase db = getReadableDatabase();
- if (db != null) {
- Cursor cursor =
- db.rawQuery(
- pSelectQuery,
- null
- );
- if (cursor != null) {
- cursor.moveToFirst();
- while (!cursor.isAfterLast()) {
- String strName =
- cursor.getString(
- cursor.getColumnIndex(COL_NAME)
- //which column INDEX?
- );
- String strNumber =
- cursor.getString(
- cursor.getColumnIndex(COL_NUMBER)
- //which column INDEX?
- );
- Contact c = new Contact(strName, strNumber);
- aRet.add(c);
- cursor.moveToNext();
- }//while
- db.close();
- }//if
- }//if can read the database
- }//try
- catch(Exception e){
- Log.e(
- this.getClass().getName(), //log's category
- e.toString() //message logged (will be visible in LogCat)
- );
- }//catch
- }//if got a non empty query
- return aRet;
- }//selectAll
- }//ContactDB
- //**
- package com.joythis.android.myphonecaller;
- import androidx.annotation.NonNull;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Context;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- //data members
- Context mContext;
- ContactDB mDB;
- AmUtil mUtil;
- ArrayList<Contact> mAlContacts; //runtime representation of the contacts managed by the app
- EditText mEtName, mEtNumber;
- Button mBtnInsertContact;
- ListView mLvContacts;
- ArrayAdapter<Contact> mAd;
- ListView.OnItemLongClickListener mLvLongClickHandler =
- new ListView.OnItemLongClickListener() {
- @Override
- public boolean onItemLongClick
- (AdapterView<?> parent, //the ListView
- View view, //the particular item with which the user is interacting with
- int position, //the index of the item in the corresponding data
- long id
- )
- {
- Contact c = mAlContacts.get(position);
- String strNumber = c.getNumber();
- mUtil.fb("Will phone to "+strNumber);
- mUtil.phoneTo(strNumber);
- //return false; //the chain of events will keep going on
- return true; //the event was fully consumed
- }//onItemLongClick
- };//mLvLongClickHandler
- Button.OnClickListener mButtonClickHandler =
- new Button.OnClickListener() {
- @Override
- public void onClick(View v) {
- switch(v.getId()){
- case R.id.idBtnInsertContact:
- actionInsertContact();
- break;
- }//switch
- }//onClick
- };//mButtonClickHandler
- void actionInsertContact(){
- String strName = mEtName.getText().toString().trim();
- String strNumber = mEtNumber.getText().toString().trim();
- boolean bCheck = strName.length()>0 && strNumber.length()>0;
- if (bCheck){
- Contact c = new Contact(strName, strNumber);
- long iWhereInserted = mDB.insertContact(c);
- //mAlContacts.add(c);
- ArrayList<Contact> alAfterInsert = mDB.selectAllContactsByInsertDateDESC();
- syncContactsDataWithTheirPresentation(alAfterInsert);
- }//
- else{
- mUtil.fb("Name and Number can NOT be empty!");
- }
- }//actionInsertContact
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.insert_contact_rl);
- init();
- }//onCreate
- void init(){
- mContext = this;
- mDB = new ContactDB(this);
- mUtil = new AmUtil(this);
- mAlContacts = new ArrayList<>();
- mEtName = findViewById(R.id.idEtName);
- mEtNumber = findViewById(R.id.idEtNumber);
- mBtnInsertContact = findViewById(R.id.idBtnInsertContact);
- mBtnInsertContact.setOnClickListener(
- mButtonClickHandler
- );
- mLvContacts = findViewById(R.id.idLvContacts);
- mAd = new ArrayAdapter<>(
- mContext,
- android.R.layout.simple_list_item_1, //the object for viewing each list item
- mAlContacts //the data (at a specific memory address)
- );
- mLvContacts.setAdapter(mAd);
- mLvContacts.setOnItemLongClickListener(mLvLongClickHandler);
- //TODO:
- /*
- layout
- op layout
- */
- ArrayList<Contact> alSortedByDateDesc = mDB.selectAllContactsByInsertDateDESC();
- syncContactsDataWithTheirPresentation(alSortedByDateDesc);
- }//init
- /*
- this method assigns a "menu resource file" to the
- activity's action bar.
- */
- @Override
- public boolean onCreateOptionsMenu(Menu pMenu) {
- MenuInflater minf = this.getMenuInflater();
- if (minf!=null){
- minf.inflate(
- R.menu.sorting_options_menu,//id the XML for the menu
- pMenu //Java ref to assign to the menu
- );
- }//if
- return super.onCreateOptionsMenu(pMenu);
- }//onCreateOptionsMenu
- /*
- here we code what should happen upon the selection of
- any of the menu options
- */
- @Override
- public boolean onOptionsItemSelected(@NonNull MenuItem pMenuItem) {
- switch(pMenuItem.getItemId()){
- case R.id.idMenuItemSortNameAsc:
- actionSortNameAsc();
- break;
- case R.id.idMenuItemSortNameDesc:
- actionSortNameDesc();
- break;
- case R.id.idMenuItemSortNumberAsc:
- actionSortNumberAsc();
- break;
- case R.id.idMenuItemSortNumberDesc:
- actionSortNumberDesc();
- break;
- case R.id.idMenuItemSortInsertDateAsc:
- actionSortInsertDateAsc();
- break;
- case R.id.idMenuItemSortInsertDateDesc:
- actionSortInsertDateDesc();
- break;
- }//switch
- return super.onOptionsItemSelected(pMenuItem);
- }
- void actionSortNameAsc(){
- ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNameASC();
- syncContactsDataWithTheirPresentation(alAfterUserChoice);
- }
- void actionSortNameDesc(){
- ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNameDESC();
- syncContactsDataWithTheirPresentation(alAfterUserChoice);
- }
- void actionSortNumberAsc(){
- ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNumberASC();
- syncContactsDataWithTheirPresentation(alAfterUserChoice);
- }
- void actionSortNumberDesc(){
- ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNumberDESC();
- syncContactsDataWithTheirPresentation(alAfterUserChoice);
- }
- void actionSortInsertDateAsc(){
- ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByInsertDateASC();
- syncContactsDataWithTheirPresentation(alAfterUserChoice);
- }
- void actionSortInsertDateDesc(){
- ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByInsertDateDESC();
- syncContactsDataWithTheirPresentation(alAfterUserChoice);
- }
- void syncContactsDataWithTheirPresentation(
- ArrayList<Contact> pAlContacts
- ){
- //would be wrong!
- //mAlContacts = pAlContacts; //would change the memory address!
- if (pAlContacts!=null && pAlContacts.size()>0){
- mAlContacts.clear(); //clears the data, but keeps the original memory address to which the Adapter was bound
- for (Contact c: pAlContacts){
- mAlContacts.add(c);
- }//for
- }//if
- mAd.notifyDataSetChanged();
- }//syncContactsDataWithTheirPresentation
- }//MainActivity
Advertisement
Add Comment
Please, Sign In to add comment