androidcallphone-numbercalllog

How to open Call Log when button pressed through OnTouchListener?


I have set OnClickListener, on drawableRight in edittext for phone numbers so, can you help to open Call Log when I press the button, my phone's call log box opens and I select one of the recently used numbers for the edittext box. so, now What to write for the action for onclicklistener?

I have written code in MainActivity.java as:

public class MainActivity extends AppCompatActivity {

   CountryCodePicker ccp;
   TextInputEditText number;

   @SuppressLint({"ResourceType", "ClickableViewAccessibility"})
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       ccp = findViewById(R.id.ccp);
       number = (TextInputEditText) findViewById(R.id.Phone_Number);

       number.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int DRAWABLE_LEFT = 0;
                final int DRAWABLE_TOP = 1;
                final int DRAWABLE_RIGHT = 2;
                final int DRAWABLE_BOTTOM = 3;

                if(event.getAction() == MotionEvent.ACTION_UP) {
                     if(event.getRawX() >= (number.getRight() - number.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                    // your action here
                       CallsProvider callsProvider = new CallsProvider (getApplicationContext());
                       callsProvider.getCalls();
                     }
                }

                return false;
            }
        });

    }

 }

Solution

  • You have to get the permissions as below

    <uses-permission android:name="android.permission.READ_CALL_LOG"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_CALL_LOG"></uses-permission>
    

    Use this code to get call logs

    if (Build.VERSION.SDK_INT >= 23) {
     requestPermissions(new String[] {
      Manifest.permission.READ_CALL_LOG, Manifest.permission.WRITE_CALL_LOG
     }, 200);
    }
    
    Uri allCalls = Uri.parse("content://call_log/calls");
    Cursor c = managedQuery(allCalls, null, null, null, null);
    
    while (c.moveToNext()) {
     String num = c.getString(c.getColumnIndex(CallLog.Calls.NUMBER)); // for  number
     String name = c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME)); // for name
     String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION)); // for duration
     int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));
     Log.e("TEST", num);
    }
    

    If you want to get it in list

    ArrayList < String > list = new ArrayList < > ();
    
    if (Build.VERSION.SDK_INT >= 23) {
     requestPermissions(new String[] {
      Manifest.permission.READ_CALL_LOG, Manifest.permission.WRITE_CALL_LOG
     }, 200);
    }
    
    Uri allCalls = Uri.parse("content://call_log/calls");
    Cursor c = managedQuery(allCalls, null, null, null, null);
    
    while (c.moveToNext()) {
     String num = c.getString(c.getColumnIndex(CallLog.Calls.NUMBER)); // for  number
     String name = c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME)); // for name
     String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION)); // for duration
     int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));
     list.add(num);
    }
    
    final String[] listAccounts = list.toArray(new String[0]);
    final int[] currentApp = {
     0
    };
    
    // Dialog box for multiple card accounts
    final AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);
    //mBuilder.setTitle("Choose an account :");
    mBuilder.setTitle(Html.fromHtml("<font color='#FF7F27'>Num</font>"));
    mBuilder.setCancelable(false);
    mBuilder.setSingleChoiceItems(listAccounts, 0, new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialogInterface, int i) {
      Log.e("Debug_", "Clicked: " + listAccounts[i]);
      currentApp[0] = i;
     }
    });
    mBuilder.setPositiveButton("Select", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialogInterface, int which) {
      Log.e("Debug_", "Name: " + listAccounts[currentApp[0]] + " Index: " + currentApp[0]);
      number.setText(listAccounts[currentApp[0]]);
      dialogInterface.dismiss();
     }
    });
    AlertDialog mDialog = mBuilder.create();
    mDialog.show();