/
RecentCallers.java
82 lines (65 loc) · 2.46 KB
/
RecentCallers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.plugins.recentCallers;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.CallLog;
import android.util.Log;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Sarath DR on 20/06/2014.
*/
public class RecentCallers extends CordovaPlugin
{
CallbackContext callbackContext;
private Cursor c;
private Context context;
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException
{
this.context = cordova.getActivity().getApplicationContext();
this.callbackContext = callbackContext;
if( action.equals("getRecentCallers")){
Integer limit = args.getInt(0);
this.getRecentCallers(limit);
return true;
}
return false;
}
public void getRecentCallers(Integer limit )
{
ContentResolver resolver = this.context.getContentResolver();
c = resolver.query(CallLog.Calls.CONTENT_URI, null, null, null, null);
JSONArray resultSet = new JSONArray();
int i = 1;
int numberIndex = c.getColumnIndex( CallLog.Calls.NUMBER );
int nameIndex = c.getColumnIndex( CallLog.Calls.CACHED_NAME );
int type = c.getColumnIndex( CallLog.Calls.TYPE );
List<String> names = new ArrayList<String>();
while ( c.moveToNext() && i <= limit ) {
if( c.getInt( type ) == CallLog.Calls.INCOMING_TYPE ){
String name = c.getString( nameIndex );
String number = c.getString( numberIndex );
if(name!=null && ( names.isEmpty() || !names.contains( number ) ) && number.length() > 7 ){
try {
JSONObject contactObject = new JSONObject();
contactObject.put("name", name);
contactObject.put("phone", number);
resultSet.put(contactObject);
names.add(number);
i++;
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
c.close();
callbackContext.success(resultSet);
}
}