/* * 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 * * http://www.rexsee.com/CN/legal/license.html * * 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.communication; import java.util.ArrayList; import rexsee.core.browser.Browser; import rexsee.core.browser.clazz.JavascriptInterface; import android.app.Activity; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.telephony.SmsManager; public class RexseeSMSSender implements JavascriptInterface { private static final String INTERFACE_NAME = "SMSSender"; @Override public String getInterfaceName() { return mBrowser.application.resources.prefix + INTERFACE_NAME; } @Override public JavascriptInterface getInheritInterface(Browser childBrowser) { return this; } @Override public JavascriptInterface getNewInterface(Browser childBrowser) { return new RexseeSMSSender(childBrowser); } public static final String EXTRA_SMS_ID = "smsID"; public static final String EXTRA_SMS_PART = "smsPart"; public static final String EXTRA_SMS_PART_TOTAL = "smsPartTotal"; public static final String ACTION_SMS_SENT = "SENT_SMS_ACTION"; public static final String ACTION_SMS_DELIVERED = "DELIVERED_SMS_ACTION"; public static final String EVENT_ONSMSSENT = "onSmsSent"; public static final String EVENT_ONSMSDELIVERED = "onSmsDelivered"; private final Context mContext; private final Browser mBrowser; private String mId = ""; private int mTotalSize = 0; private int mSentSize = 0; private String getResultString(int resultCode) { switch (resultCode) { case Activity.RESULT_OK : return "OK"; case SmsManager.RESULT_ERROR_GENERIC_FAILURE : return "RESULT_ERROR_GENERIC_FAILURE"; case SmsManager.RESULT_ERROR_NO_SERVICE : return "RESULT_ERROR_NO_SERVICE"; case SmsManager.RESULT_ERROR_NULL_PDU : return "RESULT_ERROR_NULL_PDU"; case SmsManager.RESULT_ERROR_RADIO_OFF : return "RESULT_ERROR_RADIO_OFF"; default : return "OK"; } } private final BroadcastReceiver mSentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { mSentSize++; if (mSentSize == mTotalSize) { context.unregisterReceiver(this); mBrowser.eventList.run(EVENT_ONSMSSENT, new String[]{mId, getResultString(getResultCode())}); } } }; private final BroadcastReceiver mDeliveredReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { context.unregisterReceiver(this); mBrowser.eventList.run(EVENT_ONSMSDELIVERED, new String[]{mId, intent.getStringExtra("pdu")}); } }; public RexseeSMSSender(Browser browser) { mBrowser = browser; mContext = browser.getContext(); browser.eventList.add(EVENT_ONSMSSENT); browser.eventList.add(EVENT_ONSMSDELIVERED); } //JavaScript Interface public void send(String phoneNumber, String message) { send(null, phoneNumber, message); } public void send(String id, String phoneNumber, String message) { SmsManager sms = SmsManager.getDefault(); ArrayList<String> msgs = sms.divideMessage(message); mTotalSize = msgs.size(); mSentSize = 0; mId = (id != null && !id.trim().equals("")) ? id : null; try { mContext.unregisterReceiver(mSentReceiver); mContext.unregisterReceiver(mDeliveredReceiver); } catch (Exception e) { } ArrayList<PendingIntent> sentIntents = null; ArrayList<PendingIntent> deliveryIntents = null; if (mId != null) { sentIntents = new ArrayList<PendingIntent>(); deliveryIntents = new ArrayList<PendingIntent>(); PendingIntent deliverPI = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_SMS_DELIVERED), 0); for (int i = 0; i < mTotalSize; i++) { PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_SMS_SENT + "_" + i), 0); sentIntents.add(sentPI); mContext.registerReceiver(mSentReceiver, new IntentFilter(ACTION_SMS_SENT + "_" + i)); deliveryIntents.add(deliverPI); } mContext.registerReceiver(mDeliveredReceiver, new IntentFilter(ACTION_SMS_DELIVERED)); } sms.sendMultipartTextMessage(phoneNumber, null, msgs, sentIntents, deliveryIntents); } }
僅對Rexsee的源碼和函數事件作了整理,相關的demo或源碼解析能夠在Rexsee社區瞭解,目前Rexsee已提供了近2000個擴展,覆蓋Android原生功能超過90%,且所有開放: http://www.rexsee.com/