2

I am using the below code to get the call log details which is working very fine for single SIM device, but the problem arises when it comes to the DUAL sim. I am trying to find work around for getting logs from dual sim device.

/**
     * Get All Call Logs details as JSON
     * 
     * @param context
     */
    @SuppressLint("SimpleDateFormat")
    private void getInitialCallDetailsAsJSON() {
        // Print dates of the current week starting on Monday
        DateFormat df = new SimpleDateFormat("dd-MMM-yyyy hh:mm aa",
                Locale.getDefault());

        final Uri contacts = CallLog.Calls.CONTENT_URI;
        final Cursor managedCursor = getContentResolver().query(contacts, null,
                null, null, null);
        final int name = managedCursor
                .getColumnIndex(CallLog.Calls.CACHED_NAME);
        final int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
        final int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
        final int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
        final int durationOfCall = managedCursor
                .getColumnIndex(CallLog.Calls.DURATION);

        final JSONObject allDetailsJsonObject = new JSONObject();
        final JSONArray array = new JSONArray();

        if (managedCursor != null && managedCursor.getCount() > 0) {
            while (managedCursor.moveToNext()) {
                String contactName = managedCursor.getString(name);
                final String phoneNumber = managedCursor.getString(number);
                final String callTypeIndex = managedCursor.getString(type);
                final String callDate = managedCursor.getString(date);
                final String callDurationSeconds = managedCursor
                        .getString(durationOfCall);

                final int totalTime = Integer.parseInt(callDurationSeconds);
                int day = (int) TimeUnit.SECONDS.toDays(totalTime);
                long hours = TimeUnit.SECONDS.toHours(totalTime) - (day * 24);
                long minute = TimeUnit.SECONDS.toMinutes(totalTime)
                        - (TimeUnit.SECONDS.toHours(totalTime) * 60);
                long second = TimeUnit.SECONDS.toSeconds(totalTime)
                        - (TimeUnit.SECONDS.toMinutes(totalTime) * 60);

                String callDurationFormatted = "";
                if (hours < 10) {
                    callDurationFormatted += "0" + hours + "h ";
                } else {
                    callDurationFormatted += hours + "h ";
                }

                if (minute < 10) {
                    callDurationFormatted += "0" + minute + "m ";
                } else {
                    callDurationFormatted += minute + "m ";
                }

                if (second < 10) {
                    callDurationFormatted += "0" + second + "s";
                } else {
                    callDurationFormatted += second + "s";
                }

                String callType = null;
                final int dircode = Integer.parseInt(callTypeIndex);
                switch (dircode) {
                case CallLog.Calls.OUTGOING_TYPE:
                    callType = CallAnalyticsConstant.OUTGOING;
                    break;

                case CallLog.Calls.INCOMING_TYPE:
                    callType = CallAnalyticsConstant.INCOMING;
                    break;

                case CallLog.Calls.MISSED_TYPE:
                    callType = CallAnalyticsConstant.MISSED;
                    break;
                }

                if (contactName == null || contactName.equalsIgnoreCase("")) {
                    contactName = "UNKNOWN";
                }
        }
        managedCursor.close();
    }

If anyone having any idea please kindly help me to get call related details for dual sim phones.

APP which is working fine for dual sim phone as well - CALL LOG MONITOR

Scorpion
  • 6,831
  • 16
  • 75
  • 123
  • what version OS(4.0 , 4.1 ...) of Single sim and what version of dual-sim have you tested ?? – Srinath Ganesh Apr 17 '14 at 03:01
  • For single SIM Android version 4.1.2 - Sony Xperia P. For dual SIM Android version 4.0.4 - Micromax A110 Canvas 2 – Scorpion Apr 17 '14 at 03:03
  • [anything related to security ?](http://android-developers.blogspot.in/2013/02/security-enhancements-in-jelly-bean.html) – Srinath Ganesh Apr 17 '14 at 03:03
  • I don't think its related to security as Call Log Monitor works perfect on both the devices. So its not about security. I think its about some trick or work around which we don't know but Call Log Monitor developer did that. So I am trying to find out that only. – Scorpion Apr 17 '14 at 03:06
  • [cant assure how much correct he is](http://stackoverflow.com/a/12259378/1897935) – Srinath Ganesh Apr 17 '14 at 03:08
  • nothing directly related to your problem . [see](http://stackoverflow.com/a/19474441/1897935) a UN-DOCUMENTED way to programmatically call with 2 sims – Srinath Ganesh Apr 17 '14 at 03:14
  • I checked that answer but all most all apps in market are working with dual sim perfect so its not about SDK or anything. It must be some trick which we have to find. – Scorpion Apr 17 '14 at 03:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/50835/discussion-between-srinath-ganesh-and-scorpion) – Srinath Ganesh Apr 17 '14 at 05:02
  • Can anyone kindly help me for this!!! – Scorpion Apr 22 '14 at 02:36
  • 1
    [.apk](http://apkdownload4free.blogspot.in/2013/06/free-call-log-monitor-15-apk-on-blogger.html) & [decompiler](http://www.decompileandroid.com/) .. hope this helps – Srinath Ganesh Apr 22 '14 at 17:49
  • @SrinathGanesh, Its very helpful and I got the code as well. But its having too much extra code and its clumsy as well so that it will take a lot time to clean it up. But the main thing which I was finding is i think same as my code. Let see. But thanks a lot for the help. – Scorpion Apr 25 '14 at 03:14
  • Search tells me that **android.provider.CallLog** has been used in classes **com.softrelay.calllog.core.DbMonitor** AND **com.softrelay.calllog.core.LogManager** – Srinath Ganesh Apr 29 '14 at 06:03
  • In **com.softrelay.calllog.core.LogManager** , line number **709** , you have `cursor = CallLogApplication.getAppContext().getContentResolver().query(Uri.parse("content://logs/historys"), as, s, null, "date DESC")` .. this looks a good place to check – Srinath Ganesh Apr 29 '14 at 06:21
  • you got ant thing new ? – Srinath Ganesh May 04 '14 at 07:17

2 Answers2

4

You can use "sub_id" constant value to get information about sim card.

Full path to this value CallLog.Calls.SUB_ID = "sub_id", but is not available for public, so just hardcode in API before 21. For >=21 you can use PHONE_ACCOUNT_COMPONENT_NAME.

     /**
     * The subscription ID used to place this call.  This is no longer used and has been
     * replaced with PHONE_ACCOUNT_COMPONENT_NAME/PHONE_ACCOUNT_ID.
     * For ContactsProvider internal use only.
     * <P>Type: INTEGER</P>
     *
     * @Deprecated
     * @hide
     */
    public static final String SUB_ID = "sub_id";

Have a fun :)

Vova K.
  • 678
  • 6
  • 19
  • Can you explain it a bit more? It will be very appreciated if there is a full example or a code snippet. Thanks. – Mohammad Dec 14 '15 at 18:38
0

Here is the method which give you the All Call Logs Detail...

 private void getCallDetails() {

         StringBuffer sb = new StringBuffer();
         Cursor managedCursor = managedQuery( CallLog.Calls.CONTENT_URI,null, null,null, null);
         int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER ); 
         int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
         int date = managedCursor.getColumnIndex( CallLog.Calls.DATE);
         int duration = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
         sb.append( "Call Details :");
         while ( managedCursor.moveToNext() ) {
                 String phNumber = managedCursor.getString( number );
                 String callType = managedCursor.getString( type );
                 String callDate = managedCursor.getString( date );
                 Date callDayTime = new Date(Long.valueOf(callDate));
                 String callDuration = managedCursor.getString( duration );
                 String dir = null;
                 int dircode = Integer.parseInt( callType );
                     switch( dircode ) {
                     case CallLog.Calls.OUTGOING_TYPE:
                     dir = "OUTGOING";
                     break;

                     case CallLog.Calls.INCOMING_TYPE:
                     dir = "INCOMING";
                     break;

                     case CallLog.Calls.MISSED_TYPE:
                     dir = "MISSED";
                     break;
                     }
                 sb.append( "\nPhone Number:--- "+phNumber +" \nCall Type:--- "+dir+" \nCall Date:--- "+callDayTime+" \nCall duration in sec :--- "+callDuration );
                 sb.append("\n----------------------------------");
         }
         managedCursor.close();
         System.out.println(sb.toString());
         }

And Also don't forgot to add these permissions in Manifest.Xml

    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    <uses-permission android:name="android.permission.WRITE_CALL_LOG" />
PankajSharma
  • 1,529
  • 14
  • 27
  • can u highlight the differences ?? (OPs code works in single sim and not in dual sim) – Srinath Ganesh Apr 17 '14 at 17:48
  • @Android Guru, Sorry but its not working. There is only 1 difference in your code and my code that is you have used managedQuery and I have use query. Thanks for your time but unfortunately its not working and also one more thing is that managedQuery is deprecated and not advised to use. Once again thanks, I have to dig into details to find the answer – Scorpion Apr 18 '14 at 02:25
  • @Scorpion This code is working for me..I am using Micro-max Canvas HD for testing..which device you are using for testing? – PankajSharma Apr 18 '14 at 05:08
  • I am having Micromax A110 and its not working in that, also I tested it on Moto G as well but not working. – Scorpion Apr 19 '14 at 09:14
  • @Scorpion come on android coders room we will discuss there – PankajSharma Apr 19 '14 at 09:25
  • @Scorpion have u got the solution?? – PankajSharma Apr 25 '14 at 05:06