From 23a7d0ea09bed88f980f1ebf6c4856080239028c Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Thu, 6 May 2010 14:05:03 +0200 Subject: [PATCH] Fix SMS sending with different API version --- AndroidManifest.xml | 9 +-- src/com/openwide/android/GroupsDbAdapter.java | 1 - src/com/openwide/android/MultiSmsSender.java | 2 +- src/com/openwide/android/MySMSManager.java | 71 +++++++++++++++---- .../openwide/android/MySMSManagerCupcake.java | 29 -------- .../openwide/android/MySMSManagerOther.java | 30 -------- 6 files changed, 61 insertions(+), 81 deletions(-) delete mode 100644 src/com/openwide/android/MySMSManagerCupcake.java delete mode 100644 src/com/openwide/android/MySMSManagerOther.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index fbfc5ec..3bb38a2 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,7 +1,7 @@ + package="com.openwide.android" android:versionCode="8" + android:versionName="1.1.4"> @@ -22,10 +22,7 @@ - - - + diff --git a/src/com/openwide/android/GroupsDbAdapter.java b/src/com/openwide/android/GroupsDbAdapter.java index 2d06f06..4a72982 100755 --- a/src/com/openwide/android/GroupsDbAdapter.java +++ b/src/com/openwide/android/GroupsDbAdapter.java @@ -23,7 +23,6 @@ import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.provider.Contacts.Phones; -import android.provider.ContactsContract.CommonDataKinds.Phone; import android.util.Log; /** diff --git a/src/com/openwide/android/MultiSmsSender.java b/src/com/openwide/android/MultiSmsSender.java index bfa1873..0631ac7 100644 --- a/src/com/openwide/android/MultiSmsSender.java +++ b/src/com/openwide/android/MultiSmsSender.java @@ -91,7 +91,7 @@ public class MultiSmsSender extends Activity { } public void sendMessage() { - MySMSManager manager = MySMSManager.getInstance(); + MySMSManager manager = new MySMSManager(); String message = mEditor.getText().toString(); if("".equals(message)) { diff --git a/src/com/openwide/android/MySMSManager.java b/src/com/openwide/android/MySMSManager.java index 2133340..c1b34dd 100644 --- a/src/com/openwide/android/MySMSManager.java +++ b/src/com/openwide/android/MySMSManager.java @@ -1,34 +1,77 @@ package com.openwide.android; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.ArrayList; import android.app.PendingIntent; import android.os.Build; +import android.util.Log; -public abstract class MySMSManager { - private static MySMSManager sInstance; +public class MySMSManager { + private static Object manager; + private static Method getDefault; + private static Method divide; + private static Method sendMutlipart; - public static MySMSManager getInstance() { - if(sInstance == null) { + public MySMSManager() { + if(manager == null) { String className; int sdkVersion = Integer.parseInt(Build.VERSION.SDK); - if (sdkVersion < Build.VERSION_CODES.DONUT) { - className = "com.openwide.android.MySMSManagerCupcake"; + if (sdkVersion <= 3) { //CUPCAKE + Log.d("MultiSMSSender", "Cupcake version"); + className = "android.telephony.gsm.SmsManager"; } else { - className = "com.openwide.android.MySMSManagerOther"; + Log.d("MultiSMSSender", "version > Cupcake"); + className = "android.telephony.SmsManager"; } try { - Class clazz = - Class.forName(className).asSubclass(MySMSManager.class); - sInstance = clazz.newInstance(); + Class clazz = + Class.forName(className); + Object[] paramInvoke = new Object[0]; + Class[] signGetDefault = new Class[]{}; + Class[] signDivide = new Class[] {String.class}; + Class[] signSend = new Class[] {String.class, String.class, ArrayList.class, ArrayList.class, ArrayList.class}; + getDefault = clazz.getMethod("getDefault", signGetDefault); + divide = clazz.getMethod("divideMessage", signDivide); + sendMutlipart = clazz.getMethod("sendMultipartTextMessage", signSend); + manager = getDefault.invoke(null, paramInvoke); } catch (Exception e) { - throw new IllegalStateException(e); + e.printStackTrace(); } } - return sInstance; } - public abstract ArrayList divideMessage(String text); - public abstract void sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList parts, ArrayList sentIntents, ArrayList deliveryIntents ); + public ArrayList divideMessage(String text){ + Object[] param = new Object[1]; + param[0] = text; + try { + return (ArrayList)divide.invoke(manager, param); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return null; + } + public void sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList parts, ArrayList sentIntents, ArrayList deliveryIntents ) { + Object[] param = new Object[5]; + param[0] = destinationAddress; + param[1] = scAddress; + param[2] = parts; + param[3] = sentIntents; + param[4] = deliveryIntents; + try { + sendMutlipart.invoke(manager, param); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } } diff --git a/src/com/openwide/android/MySMSManagerCupcake.java b/src/com/openwide/android/MySMSManagerCupcake.java deleted file mode 100644 index 3db1ec8..0000000 --- a/src/com/openwide/android/MySMSManagerCupcake.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.openwide.android; - -import java.util.ArrayList; - -import android.app.PendingIntent; -import android.telephony.gsm.SmsManager; - - -@SuppressWarnings("deprecation") -public class MySMSManagerCupcake extends MySMSManager { - - SmsManager mManager; - public MySMSManagerCupcake() { - mManager = SmsManager.getDefault(); - } - - - @Override - public ArrayList divideMessage(String text) { - - return mManager.divideMessage(text); - } - - public void sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList parts, ArrayList sentIntents, ArrayList deliveryIntents ) { - mManager.sendMultipartTextMessage(destinationAddress, scAddress, parts, sentIntents, deliveryIntents); - } - - -} diff --git a/src/com/openwide/android/MySMSManagerOther.java b/src/com/openwide/android/MySMSManagerOther.java deleted file mode 100644 index da540c7..0000000 --- a/src/com/openwide/android/MySMSManagerOther.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.openwide.android; - -import java.util.ArrayList; - -import android.app.PendingIntent; -import android.telephony.SmsManager; - -public class MySMSManagerOther extends MySMSManager { - - SmsManager mManager; - public MySMSManagerOther() { - mManager = SmsManager.getDefault(); - } - - @Override - public ArrayList divideMessage(String text) { - - return mManager.divideMessage(text); - } - - @Override - public void sendMultipartTextMessage(String destinationAddress, - String scAddress, ArrayList parts, - ArrayList sentIntents, - ArrayList deliveryIntents) { - mManager.sendMultipartTextMessage(destinationAddress, scAddress, parts, sentIntents, deliveryIntents); - - } - -}