am_dot_com

DDM 20201210

Dec 10th, 2020 (edited)
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 28.24 KB | None | 0 0
  1. package com.joythis.android.myphonecaller;
  2.  
  3. import android.Manifest;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.content.pm.PackageManager;
  7. import android.net.Uri;
  8. import android.util.Log;
  9. import android.widget.Toast;
  10.  
  11. import androidx.core.content.ContextCompat;
  12.  
  13. import java.net.URI;
  14. import java.util.ArrayList;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17.  
  18. public class AmUtil {
  19.     Activity mActivity;
  20.  
  21.     public AmUtil(Activity pA){
  22.         this.mActivity = pA;
  23.     }//AmUtil
  24.  
  25.     public void fb(
  26.         String pStrMsg
  27.     ){
  28.         Toast t = Toast.makeText(
  29.             this.mActivity,
  30.             pStrMsg,
  31.             Toast.LENGTH_LONG
  32.         );
  33.         t.show();
  34.     }//fb
  35.  
  36.     public void phoneTo(
  37.         String pStrPhoneNumber
  38.     ){
  39.         try {
  40.             Uri uriForPhoneNumber = Uri.parse("tel:" + pStrPhoneNumber);
  41.  
  42.             Intent intentForMakingThePhoneCall =
  43.                 new Intent(
  44.                     Intent.ACTION_CALL
  45.                 );
  46.  
  47.             intentForMakingThePhoneCall.setData(
  48.                 uriForPhoneNumber
  49.             );
  50.  
  51.             boolean bUserAuthorizedTheAppToMakePhoneCalls =
  52.                 ContextCompat.checkSelfPermission(
  53.                     mActivity,
  54.                     Manifest.permission.CALL_PHONE
  55.                 ) == PackageManager.PERMISSION_GRANTED;
  56.  
  57.             if (bUserAuthorizedTheAppToMakePhoneCalls) {
  58.                 this.mActivity.startActivity(
  59.                     intentForMakingThePhoneCall
  60.                 );
  61.             }
  62.             else{
  63.                 boolean bUserAcceptsExplanationsOnWhyThePermissionIsImportant =
  64.                     mActivity.shouldShowRequestPermissionRationale(
  65.                         Manifest.permission.CALL_PHONE
  66.                     );
  67.                 if (bUserAcceptsExplanationsOnWhyThePermissionIsImportant){
  68.                     fb("Without this permission, the app can NOT make phone calls.");
  69.                 }
  70.             }
  71.         }//try
  72.         catch(Exception e){
  73.             Log.e(
  74.                getClass().getName(),
  75.                e.toString()
  76.             );
  77.         }//catch
  78.     }//phoneTo
  79.  
  80.     public Map<Integer, ArrayList<String>>
  81.         identifyPermissionsGrantedAndDenied(
  82.             String[] paNecessaryPermissions
  83.     )
  84.     {
  85.         Map<Integer, ArrayList<String>> retMap = new HashMap<>();
  86.         ArrayList<String> alGranted = new ArrayList<>();
  87.         ArrayList<String> alDenied = new ArrayList<>();
  88.  
  89.         for (String permission : paNecessaryPermissions){
  90.             boolean bGranted =
  91.                 ContextCompat.checkSelfPermission(
  92.                     mActivity,
  93.                     permission
  94.                 ) == PackageManager.PERMISSION_GRANTED;
  95.  
  96.             boolean bDenied =
  97.                     ContextCompat.checkSelfPermission(
  98.                             mActivity,
  99.                             permission
  100.                     ) == PackageManager.PERMISSION_DENIED;
  101.  
  102.             if (bGranted) alGranted.add(permission);
  103.             if (bDenied) alDenied.add(permission);
  104.         }//for
  105.  
  106.         retMap.put(/* Integer 0 */ PackageManager.PERMISSION_GRANTED, alGranted);
  107.         retMap.put(/* Integer -1 */ PackageManager.PERMISSION_DENIED, alDenied);
  108.  
  109.         return retMap;
  110.     }//identifyPermissionsGrantedAndDenied
  111.  
  112.     public void requestNecessaryPermissionsNotYetGranted(
  113.         String[] paNecessaryPermissions,
  114.         int piCallBackCodeForWhenTheUserResponds
  115.     ){
  116.         /*
  117.         do not request permissions already granted
  118.          */
  119.         Map<Integer, ArrayList<String>> map =
  120.                 this.identifyPermissionsGrantedAndDenied
  121.                     (paNecessaryPermissions);
  122.  
  123.         ArrayList<String> alDenied = map.get(PackageManager.PERMISSION_DENIED);
  124.  
  125.         if(alDenied.size()>0){
  126.             //convert from ArrayList<String> to String[]
  127.             String[] aDenied = new String[alDenied.size()];
  128.             alDenied.toArray(aDenied);
  129.             this.mActivity.requestPermissions(
  130.                 //alDenied, //invalid syntax , because String[] is expected
  131.                 aDenied,
  132.                 piCallBackCodeForWhenTheUserResponds
  133.             );
  134.         }//if
  135.  
  136.     }//requestNecessaryPermissionsNotYetGranted
  137.  
  138.     /*
  139.     receives a list of necessary permissions
  140.     returns a String which states the status of each necessary permission
  141.      */
  142.     public String permissionsStatusToString(
  143.         String[] paNecessaryPermissions
  144.     ){
  145.        String strRet = "";
  146.  
  147.        Map<Integer, ArrayList<String>> map =
  148.         this.identifyPermissionsGrantedAndDenied(paNecessaryPermissions);
  149.  
  150.        ArrayList<String> alGranted =
  151.                map.get(PackageManager.PERMISSION_GRANTED);
  152.        ArrayList<String> alDenied =
  153.                map.get(PackageManager.PERMISSION_DENIED);
  154.  
  155.        strRet+="GRANTED:\n";
  156.        for(String spg : alGranted) strRet+=spg+"\n";
  157.  
  158.        strRet+="\n";
  159.        strRet+="DENIED:\n";
  160.        for(String spd : alDenied) strRet+=spd+"\n";
  161.  
  162.        return strRet;
  163.     }//permissionsStatusToString
  164.  
  165. }//AmUtil
  166.  
  167. //**
  168.  
  169. package com.joythis.android.myphonecaller;
  170.  
  171. import androidx.annotation.NonNull;
  172. import androidx.appcompat.app.AppCompatActivity;
  173.  
  174. import android.Manifest;
  175. import android.content.Context;
  176. import android.content.pm.PackageManager;
  177. import android.os.Bundle;
  178. import android.view.Menu;
  179. import android.view.MenuInflater;
  180. import android.view.MenuItem;
  181. import android.view.View;
  182. import android.widget.AdapterView;
  183. import android.widget.ArrayAdapter;
  184. import android.widget.Button;
  185. import android.widget.EditText;
  186. import android.widget.ListView;
  187.  
  188. import java.util.ArrayList;
  189. import java.util.Map;
  190.  
  191. public class MainActivity extends AppCompatActivity {
  192.     //const to represent the necessary runtime permissions
  193.     public final static String[] NECESSARY_PERMISSIONS =
  194.     {
  195.         Manifest.permission.CALL_PHONE
  196.     };
  197.  
  198.     public final int
  199.         CALL_ME_ON_THIS_CODE_WHEN_THE_USER_REPLIES_TO_THE_REQ = 321;
  200.  
  201.     //data members
  202.     Context mContext;
  203.     ContactDB mDB;
  204.     AmUtil mUtil;
  205.     ArrayList<Contact> mAlContacts; //runtime representation of the contacts managed by the app
  206.  
  207.     EditText mEtName, mEtNumber;
  208.     Button mBtnInsertContact;
  209.     ListView mLvContacts;
  210.     ArrayAdapter<Contact> mAd;
  211.  
  212.     ListView.OnItemLongClickListener mLvLongClickHandler =
  213.     new ListView.OnItemLongClickListener() {
  214.         @Override
  215.         public boolean onItemLongClick
  216.         (AdapterView<?> parent, //the ListView
  217.          View view, //the particular item with which the user is interacting with
  218.          int position, //the index of the item in the corresponding data
  219.          long id
  220.         )
  221.         {
  222.             Contact c = mAlContacts.get(position);
  223.             String strNumber = c.getNumber();
  224.  
  225.             mUtil.fb("Will phone to "+strNumber);
  226.             mUtil.phoneTo(strNumber);
  227.  
  228.             //return false; //the chain of events will keep going on
  229.             return true; //the event was fully consumed
  230.         }//onItemLongClick
  231.     };//mLvLongClickHandler
  232.  
  233.     Button.OnClickListener mButtonClickHandler =
  234.         new Button.OnClickListener() {
  235.         @Override
  236.         public void onClick(View v) {
  237.             switch(v.getId()){
  238.                 case R.id.idBtnInsertContact:
  239.                     actionInsertContact();
  240.                     break;
  241.             }//switch
  242.         }//onClick
  243.     };//mButtonClickHandler
  244.  
  245.     void actionInsertContact(){
  246.         String strName = mEtName.getText().toString().trim();
  247.         String strNumber = mEtNumber.getText().toString().trim();
  248.  
  249.         boolean bCheck = strName.length()>0 && strNumber.length()>0;
  250.         if (bCheck){
  251.             Contact c = new Contact(strName, strNumber);
  252.             long iWhereInserted = mDB.insertContact(c);
  253.             //mAlContacts.add(c);
  254.             ArrayList<Contact> alAfterInsert = mDB.selectAllContactsByInsertDateDESC();
  255.             syncContactsDataWithTheirPresentation(alAfterInsert);
  256.         }//
  257.         else{
  258.             mUtil.fb("Name and Number can NOT be empty!");
  259.         }
  260.     }//actionInsertContact
  261.  
  262.     @Override
  263.     protected void onCreate(Bundle savedInstanceState) {
  264.         super.onCreate(savedInstanceState);
  265.         setContentView(R.layout.insert_contact_rl);
  266.  
  267.         init();
  268.     }//onCreate
  269.  
  270.     void init(){
  271.         mContext = this;
  272.         mDB = new ContactDB(this);
  273.         mUtil = new AmUtil(this);
  274.         mAlContacts = new ArrayList<>();
  275.  
  276.         mEtName = findViewById(R.id.idEtName);
  277.         mEtNumber = findViewById(R.id.idEtNumber);
  278.         mBtnInsertContact = findViewById(R.id.idBtnInsertContact);
  279.         mBtnInsertContact.setOnClickListener(
  280.             mButtonClickHandler
  281.         );
  282.  
  283.         mLvContacts = findViewById(R.id.idLvContacts);
  284.  
  285.         mAd = new ArrayAdapter<>(
  286.             mContext,
  287.             android.R.layout.simple_list_item_1, //the object for viewing each list item
  288.             mAlContacts //the data (at a specific memory address)
  289.         );
  290.  
  291.         mLvContacts.setAdapter(mAd);
  292.         mLvContacts.setOnItemLongClickListener
  293.                 (mLvLongClickHandler);
  294.  
  295.         //TODO:
  296.         /*
  297.         layout
  298.         op layout
  299.          */
  300.  
  301.         ArrayList<Contact> alSortedByDateDesc = mDB.selectAllContactsByInsertDateDESC();
  302.         syncContactsDataWithTheirPresentation(alSortedByDateDesc);
  303.  
  304.         mUtil.requestNecessaryPermissionsNotYetGranted(
  305.             NECESSARY_PERMISSIONS,
  306.             CALL_ME_ON_THIS_CODE_WHEN_THE_USER_REPLIES_TO_THE_REQ
  307.         );
  308.     }//init
  309.  
  310.     /*
  311.     method that will automaticall called when the
  312.     user completes his/her answers regarding the permission(s)
  313.     that were requested
  314.      */
  315.     @Override
  316.     public void onRequestPermissionsResult(
  317.         int requestCode, //identified to whom the answer is destined to
  318.         //paralell arrays
  319.         @NonNull String[] permissions, //e.g. {p1, p2}
  320.         @NonNull int[] grantResults //e.g. {GRANTED, DENIED}
  321.     )
  322.     {
  323.         for (int idx=0; idx<permissions.length; idx++){
  324.             String strCurrentPermission = permissions[idx];
  325.             int iStatus = grantResults[idx];
  326.             boolean bDenied =
  327.                 iStatus == PackageManager.PERMISSION_DENIED;
  328.             if (bDenied){
  329.                 if (this.shouldShowRequestPermissionRationale(
  330.                     strCurrentPermission
  331.                 )){
  332.                     //explain why it is important NOT to deny the permission
  333.                     mUtil.fb(
  334.                         "without the"+
  335.                         strCurrentPermission +
  336.                         " the app will not work as it should."
  337.                     );
  338.                 }
  339.             }
  340.         }
  341.  
  342.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  343.     }//onRequestPermissionsResult
  344.  
  345.     /*
  346.         this method assigns a "menu resource file" to the
  347.         activity's action bar.
  348.          */
  349.     @Override
  350.     public boolean onCreateOptionsMenu(Menu pMenu) {
  351.         MenuInflater minf = this.getMenuInflater();
  352.         if (minf!=null){
  353.             minf.inflate(
  354.                 R.menu.sorting_options_menu,//id the XML for the menu
  355.                 pMenu //Java ref to assign to the menu
  356.             );
  357.         }//if
  358.  
  359.         return super.onCreateOptionsMenu(pMenu);
  360.     }//onCreateOptionsMenu
  361.  
  362.     /*
  363.     here we code what should happen upon the selection of
  364.     any of the menu options
  365.      */
  366.     @Override
  367.     public boolean onOptionsItemSelected(@NonNull MenuItem pMenuItem) {
  368.         switch(pMenuItem.getItemId()){
  369.             case R.id.idMenuItemSortNameAsc:
  370.                 actionSortNameAsc();
  371.                 break;
  372.             case R.id.idMenuItemSortNameDesc:
  373.                 actionSortNameDesc();
  374.                 break;
  375.             case R.id.idMenuItemSortNumberAsc:
  376.                 actionSortNumberAsc();
  377.                 break;
  378.             case R.id.idMenuItemSortNumberDesc:
  379.                 actionSortNumberDesc();
  380.                 break;
  381.             case R.id.idMenuItemSortInsertDateAsc:
  382.                 actionSortInsertDateAsc();
  383.                 break;
  384.             case R.id.idMenuItemSortInsertDateDesc:
  385.                 actionSortInsertDateDesc();
  386.                 break;
  387.             case R.id.idMenuItemCheckPermissions:
  388.                 actionCheckPermissions();
  389.                 break;
  390.         }//switch
  391.         return super.onOptionsItemSelected(pMenuItem);
  392.     }
  393.  
  394.     void actionCheckPermissions(){
  395.         String strCurrentPermissionsStatus =
  396.         mUtil.permissionsStatusToString(NECESSARY_PERMISSIONS);
  397.  
  398.         mUtil.fb(strCurrentPermissionsStatus);
  399.     }//actionCheckPermissions
  400.  
  401.     void actionSortNameAsc(){
  402.         ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNameASC();
  403.         syncContactsDataWithTheirPresentation(alAfterUserChoice);
  404.     }
  405.  
  406.     void actionSortNameDesc(){
  407.         ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNameDESC();
  408.         syncContactsDataWithTheirPresentation(alAfterUserChoice);
  409.     }
  410.  
  411.     void actionSortNumberAsc(){
  412.         ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNumberASC();
  413.         syncContactsDataWithTheirPresentation(alAfterUserChoice);
  414.     }
  415.  
  416.     void actionSortNumberDesc(){
  417.         ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNumberDESC();
  418.         syncContactsDataWithTheirPresentation(alAfterUserChoice);
  419.     }
  420.  
  421.     void actionSortInsertDateAsc(){
  422.         ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByInsertDateASC();
  423.         syncContactsDataWithTheirPresentation(alAfterUserChoice);
  424.     }
  425.  
  426.     void actionSortInsertDateDesc(){
  427.         ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByInsertDateDESC();
  428.         syncContactsDataWithTheirPresentation(alAfterUserChoice);
  429.     }
  430.  
  431.     void syncContactsDataWithTheirPresentation(
  432.         ArrayList<Contact> pAlContacts
  433.     ){
  434.         //would be wrong!
  435.         //mAlContacts = pAlContacts; //would change the memory address!
  436.  
  437.         if (pAlContacts!=null && pAlContacts.size()>0){
  438.             mAlContacts.clear(); //clears the data, but keeps the original memory address to which the Adapter was bound
  439.             for (Contact c: pAlContacts){
  440.                 mAlContacts.add(c);
  441.             }//for
  442.         }//if
  443.  
  444.         mAd.notifyDataSetChanged();
  445.     }//syncContactsDataWithTheirPresentation
  446. }//MainActivity
  447.  
  448. //
  449.  
  450. <?xml version="1.0" encoding="utf-8"?>
  451. <LinearLayout
  452.     xmlns:android="http://schemas.android.com/apk/res/android"
  453.     android:orientation="horizontal"
  454.     android:layout_width="match_parent"
  455.     android:layout_height="match_parent">
  456.  
  457.     <TextView
  458.         android:layout_weight="1"
  459.         android:id="@+id/idTvName"
  460.         android:layout_width="match_parent"
  461.         android:layout_height="wrap_content"/>
  462.  
  463.     <Button
  464.         android:layout_weight="1"
  465.         android:id="@+id/idBtnCall"
  466.         android:drawableLeft="@drawable/ic_baseline_local_phone_24"
  467.         android:drawableRight="@drawable/ic_baseline_local_phone_24"
  468.         android:layout_width="match_parent"
  469.         android:layout_height="wrap_content"/>
  470.  
  471. </LinearLayout>
  472.  
  473. //**
  474.  
  475. package com.joythis.android.myphonecaller;
  476.  
  477. import android.content.Context;
  478. import android.view.View;
  479. import android.widget.ArrayAdapter;
  480. import android.widget.Button;
  481.  
  482. import androidx.annotation.NonNull;
  483.  
  484. import java.util.ArrayList;
  485. import java.util.List;
  486.  
  487. public class ContactAdapter extends ArrayAdapter<Contact> {
  488.     Context mContext;
  489.     int mLayoutResource;
  490.     ArrayList<Contact> mContacts;
  491.     Button.OnClickListener mButtonClickHandler;
  492.  
  493.     public ContactAdapter(
  494.         @NonNull Context context,
  495.         int resource,
  496.         @NonNull ArrayList<Contact> objects,
  497.         Button.OnClickListener pButtonClickHandler
  498.     )
  499.     {
  500.         super(context, resource, objects);
  501.  
  502.         this.mContext = context;
  503.         this.mLayoutResource = resource;
  504.         this.mContacts = objects;
  505.         this.mButtonClickHandler = pButtonClickHandler;
  506.     }//ContactAdapter
  507. }//ContactAdapter
  508.  
  509. //
  510.  
  511. package com.joythis.android.myphonecaller;
  512.  
  513. import android.content.Context;
  514. import android.view.LayoutInflater;
  515. import android.view.View;
  516. import android.view.ViewGroup;
  517. import android.widget.ArrayAdapter;
  518. import android.widget.Button;
  519. import android.widget.TextView;
  520.  
  521. import androidx.annotation.NonNull;
  522. import androidx.annotation.Nullable;
  523.  
  524. import java.util.ArrayList;
  525. import java.util.List;
  526.  
  527. public class ContactAdapter extends ArrayAdapter<Contact> {
  528.     Context mContext;
  529.     int mLayoutResource;
  530.     ArrayList<Contact> mContacts;
  531.     Button.OnClickListener mButtonClickHandler;
  532.  
  533.     public ContactAdapter(
  534.         @NonNull Context context,
  535.         int resource,
  536.         @NonNull ArrayList<Contact> objects,
  537.         Button.OnClickListener pButtonClickHandler
  538.     )
  539.     {
  540.         super(context, resource, objects);
  541.  
  542.         this.mContext = context;
  543.         this.mLayoutResource = resource;
  544.         this.mContacts = objects;
  545.         this.mButtonClickHandler = pButtonClickHandler;
  546.     }//ContactAdapter
  547.  
  548.     @NonNull
  549.     @Override
  550.     public View getView
  551.     (
  552.     int position, //index in mContacts
  553.      @Nullable View convertView, //View that will use the custom layout and that must be returned
  554.      @NonNull ViewGroup parent //container for the View
  555.     )
  556.     {
  557.         LayoutInflater linf =
  558.         (LayoutInflater)
  559.         this.mContext.getSystemService(
  560.             mContext.LAYOUT_INFLATER_SERVICE
  561.         );
  562.  
  563.         if (linf!=null){
  564.             convertView =
  565.                 linf.inflate(
  566.                         this.mLayoutResource,
  567.                         parent,
  568.                         false
  569.                 );
  570.  
  571.             if (convertView!=null){
  572.                 TextView tv = convertView.findViewById(R.id.idTvName);
  573.                 Button btn = convertView.findViewById(R.id.idBtnCall);
  574.                 Contact  c = mContacts.get(position);
  575.                 String sName = c.getName();
  576.                 String sNumber = c.getNumber();
  577.                 tv.setText(sName);
  578.                 btn.setText(sNumber);
  579.                 btn.setOnClickListener(this.mButtonClickHandler);
  580.                 return convertView;
  581.             }
  582.         }
  583.  
  584.  
  585.         return super.getView(position, convertView, parent);
  586.     }//getView
  587. }//ContactAdapter
  588.  
  589. //
  590.  
  591. package com.joythis.android.myphonecaller;
  592.  
  593. import androidx.annotation.NonNull;
  594. import androidx.appcompat.app.AppCompatActivity;
  595.  
  596. import android.Manifest;
  597. import android.content.Context;
  598. import android.content.pm.PackageManager;
  599. import android.os.Bundle;
  600. import android.view.Menu;
  601. import android.view.MenuInflater;
  602. import android.view.MenuItem;
  603. import android.view.View;
  604. import android.widget.AdapterView;
  605. import android.widget.ArrayAdapter;
  606. import android.widget.Button;
  607. import android.widget.EditText;
  608. import android.widget.ListView;
  609.  
  610. import java.util.ArrayList;
  611. import java.util.Map;
  612.  
  613. public class MainActivity extends AppCompatActivity {
  614.     //const to represent the necessary runtime permissions
  615.     public final static String[] NECESSARY_PERMISSIONS =
  616.     {
  617.         Manifest.permission.CALL_PHONE
  618.     };
  619.  
  620.     public final int
  621.         CALL_ME_ON_THIS_CODE_WHEN_THE_USER_REPLIES_TO_THE_REQ = 321;
  622.  
  623.     //data members
  624.     Context mContext;
  625.     ContactDB mDB;
  626.     AmUtil mUtil;
  627.     ArrayList<Contact> mAlContacts; //runtime representation of the contacts managed by the app
  628.  
  629.     EditText mEtName, mEtNumber;
  630.     Button mBtnInsertContact;
  631.     ListView mLvContacts;
  632.     ArrayAdapter<Contact> mAd; //"default" adapter, in use until 2020-12-10
  633.     ContactAdapter mAd2; //2020-12-10, custom adapter
  634.  
  635.     ListView.OnItemLongClickListener mLvLongClickHandler =
  636.     new ListView.OnItemLongClickListener() {
  637.         @Override
  638.         public boolean onItemLongClick
  639.         (AdapterView<?> parent, //the ListView
  640.          View view, //the particular item with which the user is interacting with
  641.          int position, //the index of the item in the corresponding data
  642.          long id
  643.         )
  644.         {
  645.             Contact c = mAlContacts.get(position);
  646.             String strNumber = c.getNumber();
  647.  
  648.             mUtil.fb("Will phone to "+strNumber);
  649.             mUtil.phoneTo(strNumber);
  650.  
  651.             //return false; //the chain of events will keep going on
  652.             return true; //the event was fully consumed
  653.         }//onItemLongClick
  654.     };//mLvLongClickHandler
  655.  
  656.     Button.OnClickListener mButtonClickHandler =
  657.         new Button.OnClickListener() {
  658.         @Override
  659.         public void onClick(View v) {
  660.             switch(v.getId()){
  661.                 case R.id.idBtnInsertContact:
  662.                     actionInsertContact();
  663.                     break;
  664.             }//switch
  665.         }//onClick
  666.     };//mButtonClickHandler
  667.  
  668.     void actionInsertContact(){
  669.         String strName = mEtName.getText().toString().trim();
  670.         String strNumber = mEtNumber.getText().toString().trim();
  671.  
  672.         boolean bCheck = strName.length()>0 && strNumber.length()>0;
  673.         if (bCheck){
  674.             Contact c = new Contact(strName, strNumber);
  675.             long iWhereInserted = mDB.insertContact(c);
  676.             //mAlContacts.add(c);
  677.             ArrayList<Contact> alAfterInsert = mDB.selectAllContactsByInsertDateDESC();
  678.             syncContactsDataWithTheirPresentation(alAfterInsert);
  679.         }//
  680.         else{
  681.             mUtil.fb("Name and Number can NOT be empty!");
  682.         }
  683.     }//actionInsertContact
  684.  
  685.     @Override
  686.     protected void onCreate(Bundle savedInstanceState) {
  687.         super.onCreate(savedInstanceState);
  688.         setContentView(R.layout.insert_contact_rl);
  689.  
  690.         init();
  691.     }//onCreate
  692.  
  693.     void init(){
  694.         mContext = this;
  695.         mDB = new ContactDB(this);
  696.         mUtil = new AmUtil(this);
  697.         mAlContacts = new ArrayList<>();
  698.  
  699.         mEtName = findViewById(R.id.idEtName);
  700.         mEtNumber = findViewById(R.id.idEtNumber);
  701.         mBtnInsertContact = findViewById(R.id.idBtnInsertContact);
  702.         mBtnInsertContact.setOnClickListener(
  703.             mButtonClickHandler
  704.         );
  705.  
  706.         mLvContacts = findViewById(R.id.idLvContacts);
  707.  
  708.         mAd = new ArrayAdapter<>(
  709.             mContext,
  710.             android.R.layout.simple_list_item_1, //the object for viewing each list item
  711.             mAlContacts //the data (at a specific memory address)
  712.         );
  713.  
  714.         //speedy
  715.         mAd2 = new ContactAdapter(
  716.             mContext,
  717.             R.layout.contact_ll,
  718.             mAlContacts,
  719.             mButtonClickHandler
  720.         );
  721.  
  722.         //mLvContacts.setAdapter(mAd);
  723.         mLvContacts.setAdapter(mAd2);
  724.         mLvContacts.setOnItemLongClickListener
  725.                 (mLvLongClickHandler);
  726.  
  727.         //TODO:
  728.         /*
  729.         layout
  730.         op layout
  731.          */
  732.  
  733.         ArrayList<Contact> alSortedByDateDesc = mDB.selectAllContactsByInsertDateDESC();
  734.         syncContactsDataWithTheirPresentation(alSortedByDateDesc);
  735.  
  736.         mUtil.requestNecessaryPermissionsNotYetGranted(
  737.             NECESSARY_PERMISSIONS,
  738.             CALL_ME_ON_THIS_CODE_WHEN_THE_USER_REPLIES_TO_THE_REQ
  739.         );
  740.     }//init
  741.  
  742.     /*
  743.     method that will automaticall called when the
  744.     user completes his/her answers regarding the permission(s)
  745.     that were requested
  746.      */
  747.     @Override
  748.     public void onRequestPermissionsResult(
  749.         int requestCode, //identified to whom the answer is destined to
  750.         //paralell arrays
  751.         @NonNull String[] permissions, //e.g. {p1, p2}
  752.         @NonNull int[] grantResults //e.g. {GRANTED, DENIED}
  753.     )
  754.     {
  755.         for (int idx=0; idx<permissions.length; idx++){
  756.             String strCurrentPermission = permissions[idx];
  757.             int iStatus = grantResults[idx];
  758.             boolean bDenied =
  759.                 iStatus == PackageManager.PERMISSION_DENIED;
  760.             if (bDenied){
  761.                 if (this.shouldShowRequestPermissionRationale(
  762.                     strCurrentPermission
  763.                 )){
  764.                     //explain why it is important NOT to deny the permission
  765.                     mUtil.fb(
  766.                         "without the"+
  767.                         strCurrentPermission +
  768.                         " the app will not work as it should."
  769.                     );
  770.                 }
  771.             }
  772.         }
  773.  
  774.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  775.     }//onRequestPermissionsResult
  776.  
  777.     /*
  778.         this method assigns a "menu resource file" to the
  779.         activity's action bar.
  780.          */
  781.     @Override
  782.     public boolean onCreateOptionsMenu(Menu pMenu) {
  783.         MenuInflater minf = this.getMenuInflater();
  784.         if (minf!=null){
  785.             minf.inflate(
  786.                 R.menu.sorting_options_menu,//id the XML for the menu
  787.                 pMenu //Java ref to assign to the menu
  788.             );
  789.         }//if
  790.  
  791.         return super.onCreateOptionsMenu(pMenu);
  792.     }//onCreateOptionsMenu
  793.  
  794.     /*
  795.     here we code what should happen upon the selection of
  796.     any of the menu options
  797.      */
  798.     @Override
  799.     public boolean onOptionsItemSelected(@NonNull MenuItem pMenuItem) {
  800.         switch(pMenuItem.getItemId()){
  801.             case R.id.idMenuItemRequestDeniedPerms:
  802.                 mUtil.requestNecessaryPermissionsNotYetGranted(
  803.                     NECESSARY_PERMISSIONS,
  804.                     CALL_ME_ON_THIS_CODE_WHEN_THE_USER_REPLIES_TO_THE_REQ
  805.                 );
  806.                 break;
  807.  
  808.             case R.id.idMenuItemSortNameAsc:
  809.                 actionSortNameAsc();
  810.                 break;
  811.             case R.id.idMenuItemSortNameDesc:
  812.                 actionSortNameDesc();
  813.                 break;
  814.             case R.id.idMenuItemSortNumberAsc:
  815.                 actionSortNumberAsc();
  816.                 break;
  817.             case R.id.idMenuItemSortNumberDesc:
  818.                 actionSortNumberDesc();
  819.                 break;
  820.             case R.id.idMenuItemSortInsertDateAsc:
  821.                 actionSortInsertDateAsc();
  822.                 break;
  823.             case R.id.idMenuItemSortInsertDateDesc:
  824.                 actionSortInsertDateDesc();
  825.                 break;
  826.             case R.id.idMenuItemCheckPermissions:
  827.                 actionCheckPermissions();
  828.                 break;
  829.         }//switch
  830.         return super.onOptionsItemSelected(pMenuItem);
  831.     }
  832.  
  833.     void actionCheckPermissions(){
  834.         String strCurrentPermissionsStatus =
  835.         mUtil.permissionsStatusToString(NECESSARY_PERMISSIONS);
  836.  
  837.         mUtil.fb(strCurrentPermissionsStatus);
  838.     }//actionCheckPermissions
  839.  
  840.     void actionSortNameAsc(){
  841.         ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNameASC();
  842.         syncContactsDataWithTheirPresentation(alAfterUserChoice);
  843.     }
  844.  
  845.     void actionSortNameDesc(){
  846.         ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNameDESC();
  847.         syncContactsDataWithTheirPresentation(alAfterUserChoice);
  848.     }
  849.  
  850.     void actionSortNumberAsc(){
  851.         ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNumberASC();
  852.         syncContactsDataWithTheirPresentation(alAfterUserChoice);
  853.     }
  854.  
  855.     void actionSortNumberDesc(){
  856.         ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByNumberDESC();
  857.         syncContactsDataWithTheirPresentation(alAfterUserChoice);
  858.     }
  859.  
  860.     void actionSortInsertDateAsc(){
  861.         ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByInsertDateASC();
  862.         syncContactsDataWithTheirPresentation(alAfterUserChoice);
  863.     }
  864.  
  865.     void actionSortInsertDateDesc(){
  866.         ArrayList<Contact> alAfterUserChoice = mDB.selectAllContactsByInsertDateDESC();
  867.         syncContactsDataWithTheirPresentation(alAfterUserChoice);
  868.     }
  869.  
  870.     void syncContactsDataWithTheirPresentation(
  871.         ArrayList<Contact> pAlContacts
  872.     ){
  873.         //would be wrong!
  874.         //mAlContacts = pAlContacts; //would change the memory address!
  875.  
  876.         if (pAlContacts!=null && pAlContacts.size()>0){
  877.             mAlContacts.clear(); //clears the data, but keeps the original memory address to which the Adapter was bound
  878.             for (Contact c: pAlContacts){
  879.                 mAlContacts.add(c);
  880.             }//for
  881.         }//if
  882.  
  883.         mAd.notifyDataSetChanged();
  884.     }//syncContactsDataWithTheirPresentation
  885. }//MainActivity
Advertisement
Add Comment
Please, Sign In to add comment