From b2c2b630232094c78f7834c3f938edd0af2fa5df Mon Sep 17 00:00:00 2001 From: Rody Davis Date: Thu, 16 Aug 2018 09:20:28 -0400 Subject: [PATCH] Android SMS/MMS done --- .vscode/launch.json | 7 -- .vscode/settings.json | 3 + android/.classpath | 6 ++ android/.idea/.name | 1 + android/.idea/caches/build_file_checksums.ser | Bin 0 -> 494 bytes android/.idea/codeStyles/Project.xml | 29 ++++++ android/.idea/gradle.xml | 17 ++++ android/.idea/misc.xml | 31 ++++++ android/.idea/modules.xml | 8 ++ android/.idea/runConfigurations.xml | 12 +++ android/.project | 23 +++++ .../org.eclipse.buildship.core.prefs | 2 + android/app/build.gradle | 1 + android/build.gradle | 4 + .../fluttersms/FlutterSmsPlugin.java | 95 +++++++++++++++++- example/android/.project | 17 ++++ .../org.eclipse.buildship.core.prefs | 2 + example/android/app/.classpath | 6 ++ example/android/app/.project | 23 +++++ .../org.eclipse.buildship.core.prefs | 2 + .../android/app/src/main/AndroidManifest.xml | 6 ++ 21 files changed, 286 insertions(+), 9 deletions(-) delete mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 android/.classpath create mode 100644 android/.idea/.name create mode 100644 android/.idea/caches/build_file_checksums.ser create mode 100644 android/.idea/codeStyles/Project.xml create mode 100644 android/.idea/gradle.xml create mode 100644 android/.idea/misc.xml create mode 100644 android/.idea/modules.xml create mode 100644 android/.idea/runConfigurations.xml create mode 100644 android/.project create mode 100644 android/.settings/org.eclipse.buildship.core.prefs create mode 100644 example/android/.project create mode 100644 example/android/.settings/org.eclipse.buildship.core.prefs create mode 100644 example/android/app/.classpath create mode 100644 example/android/app/.project create mode 100644 example/android/app/.settings/org.eclipse.buildship.core.prefs diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 5c7247b..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [] -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e0f15db --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.configuration.updateBuildConfiguration": "automatic" +} \ No newline at end of file diff --git a/android/.classpath b/android/.classpath new file mode 100644 index 0000000..eb19361 --- /dev/null +++ b/android/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/android/.idea/.name b/android/.idea/.name new file mode 100644 index 0000000..f44ac10 --- /dev/null +++ b/android/.idea/.name @@ -0,0 +1 @@ +flutter_sms \ No newline at end of file diff --git a/android/.idea/caches/build_file_checksums.ser b/android/.idea/caches/build_file_checksums.ser new file mode 100644 index 0000000000000000000000000000000000000000..1e5713d61348800c297e6533e37aa3f1c145e830 GIT binary patch literal 494 zcmZ4UmVvdnh`~NNKUXg?FQq6yGexf?KR>5fFEb@IQ7^qHF(oHeub?PDD>b=9F91S2 zm1gFoxMk*~I%lLNXBU^|7Q2L-Ts|(GuF1r}uGBYr_F>vMNC#JY1CYR(Fc`|U8WE7y=Yu=W7eJN?>I{s zwDd!ZQ;UlAi}F({QxeNEi}j%<=p%xsAhoC@Gqt$1kfDx&4P?v}*`AyI44YV9HgL{( z^TVmGgn=gs6l^fd;R+{SX81I7*P7FHyu9xGD~s7m7zA?ilM{15fsSG7vBb``?XR~* l&Te + + + + + + + + + + + + + \ No newline at end of file diff --git a/android/.idea/gradle.xml b/android/.idea/gradle.xml new file mode 100644 index 0000000..47bd81f --- /dev/null +++ b/android/.idea/gradle.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/android/.idea/misc.xml b/android/.idea/misc.xml new file mode 100644 index 0000000..b031b27 --- /dev/null +++ b/android/.idea/misc.xml @@ -0,0 +1,31 @@ + + + + + + + + + + \ No newline at end of file diff --git a/android/.idea/modules.xml b/android/.idea/modules.xml new file mode 100644 index 0000000..3a99f9f --- /dev/null +++ b/android/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/android/.idea/runConfigurations.xml b/android/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/android/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/android/.project b/android/.project new file mode 100644 index 0000000..4a844b2 --- /dev/null +++ b/android/.project @@ -0,0 +1,23 @@ + + + flutter_sms + Project flutter_sms created by Buildship. + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.buildship.core.gradleprojectbuilder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.buildship.core.gradleprojectnature + + diff --git a/android/.settings/org.eclipse.buildship.core.prefs b/android/.settings/org.eclipse.buildship.core.prefs new file mode 100644 index 0000000..e889521 --- /dev/null +++ b/android/.settings/org.eclipse.buildship.core.prefs @@ -0,0 +1,2 @@ +connection.project.dir= +eclipse.preferences.version=1 diff --git a/android/app/build.gradle b/android/app/build.gradle index b945b9b..347481e 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -58,4 +58,5 @@ dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + implementation 'com.android.support:support-v4:27.1.0' } diff --git a/android/build.gradle b/android/build.gradle index 04eabf9..8ee3b98 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -32,3 +32,7 @@ android { disable 'InvalidPackage' } } + +dependencies { + api 'com.android.support:appcompat-v7:27.1.0' +} diff --git a/android/src/main/java/com/appleeducate/fluttersms/FlutterSmsPlugin.java b/android/src/main/java/com/appleeducate/fluttersms/FlutterSmsPlugin.java index 9fc36a6..f94e548 100644 --- a/android/src/main/java/com/appleeducate/fluttersms/FlutterSmsPlugin.java +++ b/android/src/main/java/com/appleeducate/fluttersms/FlutterSmsPlugin.java @@ -5,21 +5,112 @@ import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel.MethodCallHandler; import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.PluginRegistry.Registrar; +import android.app.Activity; +import android.content.Intent; +import android.telephony.SmsManager; + +import android.Manifest; +import android.app.Activity; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.provider.Settings; +import android.support.v4.app.ActivityCompat; +//import android.support.v4.content.ContextCompat; +import android.util.Log; +import android.Manifest; +import android.app.Activity; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.media.MediaScannerConnection; +import android.net.Uri; +import android.provider.MediaStore; +import android.support.annotation.VisibleForTesting; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.FileProvider; +import io.flutter.plugin.common.MethodCall; +import io.flutter.plugin.common.MethodChannel; +import io.flutter.plugin.common.PluginRegistry; +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.UUID; +import java.util.ArrayList; /** FlutterSmsPlugin */ public class FlutterSmsPlugin implements MethodCallHandler { + Activity activity; + /** Plugin registration. */ public static void registerWith(Registrar registrar) { final MethodChannel channel = new MethodChannel(registrar.messenger(), "flutter_sms"); - channel.setMethodCallHandler(new FlutterSmsPlugin()); + channel.setMethodCallHandler(new FlutterSmsPlugin(registrar.activity())); } @Override public void onMethodCall(MethodCall call, Result result) { - if (call.method.equals("getPlatformVersion")) { + if (call.method.equals("sendSMS")) { + String message = call.argument("message"); + ArrayList recipients = call.argument("recipients"); + String smsPermission = "SEND_SMS"; + if (!checkPermission(smsPermission)) { + requestPermission(smsPermission); + if (!checkPermission(smsPermission)) { + openSettings(); + } else { + sendSMS(recipients, message); + } + } else { + sendSMS(recipients, message); + } result.success("Android " + android.os.Build.VERSION.RELEASE); } else { result.notImplemented(); } } + + private FlutterSmsPlugin(Activity activity) { + this.activity = activity; + } + + private void sendSMS(ArrayList phones, String message) { + Intent sendIntent = new Intent(Intent.ACTION_VIEW); + sendIntent.putExtra("sms_body", message); + sendIntent.setData(Uri.parse("sms:" + phones)); + activity.startActivity(sendIntent); + } + + private boolean checkPermission(String permission) { + permission = getManifestPermission(permission); + Log.i("SimplePermission", "Checking permission : " + permission); + return PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(activity, permission); + } + + private void requestPermission(String permission) { + permission = getManifestPermission(permission); + Log.i("SimplePermission", "Requesting permission : " + permission); + String[] perm = { permission }; + ActivityCompat.requestPermissions(activity, perm, 0); + } + + private String getManifestPermission(String permission) { + String res; + switch (permission) { + case "SEND_SMS": + res = Manifest.permission.SEND_SMS; + break; + default: + res = "ERROR"; + break; + } + return res; + } + + private void openSettings() { + Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, + Uri.parse("package:" + activity.getPackageName())); + intent.addCategory(Intent.CATEGORY_DEFAULT); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + activity.startActivity(intent); + } } diff --git a/example/android/.project b/example/android/.project new file mode 100644 index 0000000..3964dd3 --- /dev/null +++ b/example/android/.project @@ -0,0 +1,17 @@ + + + android + Project android created by Buildship. + + + + + org.eclipse.buildship.core.gradleprojectbuilder + + + + + + org.eclipse.buildship.core.gradleprojectnature + + diff --git a/example/android/.settings/org.eclipse.buildship.core.prefs b/example/android/.settings/org.eclipse.buildship.core.prefs new file mode 100644 index 0000000..e889521 --- /dev/null +++ b/example/android/.settings/org.eclipse.buildship.core.prefs @@ -0,0 +1,2 @@ +connection.project.dir= +eclipse.preferences.version=1 diff --git a/example/android/app/.classpath b/example/android/app/.classpath new file mode 100644 index 0000000..eb19361 --- /dev/null +++ b/example/android/app/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/example/android/app/.project b/example/android/app/.project new file mode 100644 index 0000000..ac485d7 --- /dev/null +++ b/example/android/app/.project @@ -0,0 +1,23 @@ + + + app + Project app created by Buildship. + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.buildship.core.gradleprojectbuilder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.buildship.core.gradleprojectnature + + diff --git a/example/android/app/.settings/org.eclipse.buildship.core.prefs b/example/android/app/.settings/org.eclipse.buildship.core.prefs new file mode 100644 index 0000000..b1886ad --- /dev/null +++ b/example/android/app/.settings/org.eclipse.buildship.core.prefs @@ -0,0 +1,2 @@ +connection.project.dir=.. +eclipse.preferences.version=1 diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index 901a681..a1885e9 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -6,6 +6,7 @@ to allow setting breakpoints, to provide hot reload, etc. --> +