Chinaunix首页 | 论坛 | 博客
  • 博客访问: 75041
  • 博文数量: 48
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 340
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-30 14:22
文章分类

全部博文(48)

文章存档

2014年(47)

2013年(1)

我的朋友

分类: Android平台

2014-01-27 13:46:47

    分享:通话记录

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

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

  /*

  * Copyright (C) 2011 The Rexsee Open Source Project

  *

  * Licensed under the Rexsee License, Version 1.0 (the "License");

  * you may not use this file except in compliance with the License.

  * You may obtain a copy of the License at

  *

  * 

  *

  * Unless required by applicable law or agreed to in writing, software

  * distributed under the License is distributed on an "AS IS" BASIS,

  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

  * See the License for the specific language governing permissions and

  * limitations under the License.

  */

  package rexsee.content;

  import rexsee.core.browser.JavascriptInterface;

  import rexsee.core.browser.RexseeBrowser;

  import rexsee.core.browser.UrlListener;

  import rexsee.core.lang.RexseeLanguage;

  import rexsee.core.utilities.RexseeUtilities;

  import android.content.ContentResolver;

  import android.content.Context;

  import android.database.Cursor;

  import android.provider.CallLog.Calls;

  public class RexseeCallLog implements JavascriptInterface {

  private static final String INTERFACE_NAME = "CallLog";

  @Override

  public String getInterfaceName() {

  return mBrowser.application.resources.prefix + INTERFACE_NAME;

  }

  @Override

  public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) {

  return this;

  }

  @Override

  public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) {

  return new RexseeCallLog(childBrowser);

  }

  private static final String[] CALLS_COLUMNS = new String[]{

  Calls.NUMBER, //0

  Calls.TYPE, //1

  Calls.DATE, //2

  Calls.DURATION //3

  };

  private final Context mContext;

  private final RexseeBrowser mBrowser;

  public RexseeCallLog(RexseeBrowser browser) {

  mBrowser = browser;

  mContext = browser.getContext();

  browser.urlListeners.add(new UrlListener(mBrowser.application.resources.prefix + ":callLog") {

  @Override

  public void run(final Context context, final RexseeBrowser browser, String url) {

  new Thread() {

  @Override

  public void run() {

  browser.progressDialog.show(RexseeLanguage.PROGRESS_ONGOING);

  String html = "";

  html += "";

  html += "";

  try {

  ContentResolver contentResolver = context.getContentResolver();

  String[] columns = new String[]{

  Calls.NUMBER, //0

  Calls.TYPE, //1

  Calls.DATE, //2

  Calls.DURATION //3

  };

  Cursor cursor = contentResolver.query(Calls.CONTENT_URI, columns, null, null, Calls.DATE + " DESC limit 100");

  if (cursor == null || cursor.getCount() == 0) {

  html += "No recent call found.

  ";

  } else {

  for (int i = 0; i < cursor.getCount(); i++) {

  cursor.moveToPosition(i);

  html += "";

  html += "

  " + cursor.getString(0) + "

  ";

  html += "

  " + getType(cursor.getInt(1)) + "

  ";

  html += "

  " + RexseeUtilities.timeStamp2dateString(cursor.getLong(2)) + "

  ";

  html += "

  " + cursor.getString(3) + " seconds

  ";

  html += "

  ";

  }

  }

  cursor.close();

  } catch (Exception e) {

  html += "Exception: " + e.getLocalizedMessage() + "

  ";

  }

  html += "";

  html += "";

  html += "";

  browser.function.loadHTMLWithoutHistory(html);

  }

  }.start();

  }

  @Override

  public boolean shouldAddToHistory(Context context, RexseeBrowser browser, String url) {

  return true;

  }

  });

  }

  private static String getType(int type) {

  switch (type) {

  case Calls.MISSED_TYPE :

  return "MISSED";

  case Calls.INCOMING_TYPE :

  return "INCOMING";

  case Calls.OUTGOING_TYPE :

  return "OUTGOING";

  default :

  return "unknown";

  }

  }

  private static int getType(String type) {

  if (type == null) return -1;

  else if (type.equalsIgnoreCase("MISSED")) return Calls.MISSED_TYPE;

  else if (type.equalsIgnoreCase("INCOMING")) return Calls.INCOMING_TYPE;

  else if (type.equalsIgnoreCase("OUTGOING")) return Calls.OUTGOING_TYPE;

  else return -1;

  }

  //JavaScript Interface

  public String getContentUri() {

  return Calls.CONTENT_URI.toString();

  }

  public String get(int number) {

  return get(null, number);

  }

  public String get(String type, int number) {

  int t = getType(type);

  String selection = (t > 0) ? Calls.TYPE + "=" + t : null;

  return getData(selection, number);

  }

  public String getData(String selection, int number) {

  ContentResolver contentResolver = mContext.getContentResolver();

  Cursor cursor;

  if (number > 0) {

  cursor = contentResolver.query(Calls.CONTENT_URI, CALLS_COLUMNS, selection, null, Calls.DATE + " DESC limit " + number);

  } else {

  cursor = contentResolver.query(Calls.CONTENT_URI, CALLS_COLUMNS, selection, null, Calls.DATE + " DESC");

  }

  if (cursor == null) return "[]";

  String rtn = "";

  for (int i = 0; i < cursor.getCount(); i++) {

  cursor.moveToPosition(i);

  if (i > 0) rtn += ",";

  rtn += "{";

  rtn += "\"number\":\"" + cursor.getString(0) + "\"";

  rtn += ",\"type\":\"" + getType(cursor.getInt(1)) + "\"";

  rtn += ",\"date\":" + cursor.getString(2);

  rtn += ",\"duration\":" + cursor.getString(3);

  rtn += "}";

  }

  cursor.close();

  return "[" + rtn + "]";

  }

  public String getColumns() {

  return RexseeContent.getContentColumns_(mContext, Calls.CONTENT_URI);

  }

  }

想要了解更多有关android源代码的知识可以查询:天地会。

阅读(283) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~