Android源码分享:通话记录(转载)

原文地址:http://social.microsoft.com/Forums/fr-CH/vbasiczhchs/thread/762e8127-06b8-4dd6-86bd-6b4013817f7a

看了别人做的一个通话记录的程序,觉得写的太粗陋了。网上找到了Rexsee的源码,功能很不错,可实现自定义数量的最近通话记录,自定义查询最近通话,读取记录数据库表的URI地址……因为是开源的,所以就转过来了。。

个人还是觉得很强大的。通过js实现调用,可以用HTML进行开发,几乎Android的原生功能都覆盖到了。

  1 /* 
2 * Copyright (C) 2011 The Rexsee Open Source Project
3 *
4 * Licensed under the Rexsee License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.rexsee.com/CN/legal/license.html
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package rexsee.content;
18
19 import rexsee.core.browser.JavascriptInterface;
20 import rexsee.core.browser.RexseeBrowser;
21 import rexsee.core.browser.UrlListener;
22 import rexsee.core.lang.RexseeLanguage;
23 import rexsee.core.utilities.RexseeUtilities;
24 import android.content.ContentResolver;
25 import android.content.Context;
26 import android.database.Cursor;
27 import android.provider.CallLog.Calls;
28
29 public class RexseeCallLog implements JavascriptInterface {
30
31 private static final String INTERFACE_NAME = "CallLog";
32 @Override
33 public String getInterfaceName() {
34 return mBrowser.application.resources.prefix + INTERFACE_NAME;
35 }
36 @Override
37 public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) {
38 return this;
39 }
40 @Override
41 public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) {
42 return new RexseeCallLog(childBrowser);
43 }
44
45 private static final String[] CALLS_COLUMNS = new String[]{
46 Calls.NUMBER, //0
47 Calls.TYPE, //1
48 Calls.DATE, //2
49 Calls.DURATION //3
50 };
51
52 private final Context mContext;
53 private final RexseeBrowser mBrowser;
54
55 public RexseeCallLog(RexseeBrowser browser) {
56 mBrowser = browser;
57 mContext = browser.getContext();
58 browser.urlListeners.add(new UrlListener(mBrowser.application.resources.prefix + ":callLog") {
59 @Override
60 public void run(final Context context, final RexseeBrowser browser, String url) {
61 new Thread() {
62 @Override
63 public void run() {
64 browser.progressDialog.show(RexseeLanguage.PROGRESS_ONGOING);
65 String html = "<HTML><HEAD><TITLE>" + Calls.CONTENT_URI.toString() + "</TITLE></HEAD>";
66 html += "<BODY style='margin:0px;background-color:black;color:white;'>";
67 html += "<table width=100% cellspacing=0 style='color:white;font-size:14px;'>";
68 try {
69 ContentResolver contentResolver = context.getContentResolver();
70 String[] columns = new String[]{
71 Calls.NUMBER, //0
72 Calls.TYPE, //1
73 Calls.DATE, //2
74 Calls.DURATION //3
75 };
76 Cursor cursor = contentResolver.query(Calls.CONTENT_URI, columns, null, null, Calls.DATE + " DESC limit 100");
77 if (cursor == null || cursor.getCount() == 0) {
78 html += "<tr><td style='padding:10px;'>No recent call found.</td></tr>";
79 } else {
80 for (int i = 0; i < cursor.getCount(); i++) {
81 cursor.moveToPosition(i);
82 html += "<tr><td style='border-bottom:1px solid #222222; padding:10 5 10 5;'>";
83 html += "<div style='font-size:16px;font-weight:bold;'>" + cursor.getString(0) + "</div>";
84 html += "<div>" + getType(cursor.getInt(1)) + "</div>";
85 html += "<div>" + RexseeUtilities.timeStamp2dateString(cursor.getLong(2)) + "</div>";
86 html += "<div>" + cursor.getString(3) + " seconds</div>";
87 html += "</td></tr>";
88 }
89 }
90 cursor.close();
91 } catch (Exception e) {
92 html += "<tr><td style='padding:10px;'>Exception: " + e.getLocalizedMessage() + "</td></tr>";
93 }
94 html += "</table>";
95 html += "</BODY>";
96 html += "</HTML>";
97 browser.function.loadHTMLWithoutHistory(html);
98 }
99 }.start();
100 }
101 @Override
102 public boolean shouldAddToHistory(Context context, RexseeBrowser browser, String url) {
103 return true;
104 }
105 });
106 }
107
108 private static String getType(int type) {
109 switch (type) {
110 case Calls.MISSED_TYPE :
111 return "MISSED";
112 case Calls.INCOMING_TYPE :
113 return "INCOMING";
114 case Calls.OUTGOING_TYPE :
115 return "OUTGOING";
116 default :
117 return "unknown";
118 }
119 }
120 private static int getType(String type) {
121 if (type == null) return -1;
122 else if (type.equalsIgnoreCase("MISSED")) return Calls.MISSED_TYPE;
123 else if (type.equalsIgnoreCase("INCOMING")) return Calls.INCOMING_TYPE;
124 else if (type.equalsIgnoreCase("OUTGOING")) return Calls.OUTGOING_TYPE;
125 else return -1;
126 }
127
128 //JavaScript Interface
129
130 public String getContentUri() {
131 return Calls.CONTENT_URI.toString();
132 }
133 public String get(int number) {
134 return get(null, number);
135 }
136 public String get(String type, int number) {
137 int t = getType(type);
138 String selection = (t > 0) ? Calls.TYPE + "=" + t : null;
139 return getData(selection, number);
140 }
141 public String getData(String selection, int number) {
142 ContentResolver contentResolver = mContext.getContentResolver();
143 Cursor cursor;
144 if (number > 0) {
145 cursor = contentResolver.query(Calls.CONTENT_URI, CALLS_COLUMNS, selection, null, Calls.DATE + " DESC limit " + number);
146 } else {
147 cursor = contentResolver.query(Calls.CONTENT_URI, CALLS_COLUMNS, selection, null, Calls.DATE + " DESC");
148 }
149 if (cursor == null) return "[]";
150 String rtn = "";
151 for (int i = 0; i < cursor.getCount(); i++) {
152 cursor.moveToPosition(i);
153 if (i > 0) rtn += ",";
154 rtn += "{";
155 rtn += "\"number\":\"" + cursor.getString(0) + "\"";
156 rtn += ",\"type\":\"" + getType(cursor.getInt(1)) + "\"";
157 rtn += ",\"date\":" + cursor.getString(2);
158 rtn += ",\"duration\":" + cursor.getString(3);
159 rtn += "}";
160 }
161 cursor.close();
162 return "[" + rtn + "]";
163 }
164 public String getColumns() {
165 return RexseeContent.getContentColumns_(mContext, Calls.CONTENT_URI);
166 }
167
168 }

 

posted @ 2012-03-26 14:47  K.Leigh  阅读(602)  评论(0编辑  收藏  举报