Skip to content

Commit

Permalink
implementation of SMS receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
babariviere authored and babariviere committed Mar 8, 2018
1 parent 827649f commit cdd48bb
Show file tree
Hide file tree
Showing 81 changed files with 2,033 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## [0.0.1] - TODO: Add release date.

* TODO: Describe initial release.
22 changes: 1 addition & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1 @@
MIT License

Copyright (c) 2018 Bastien Rivière

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
TODO: Add your license here.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
# flutter-sms
SMS library for flutter
# sms_plugin

SMS in flutter.

## Getting Started

For help getting started with Flutter, view our online
[documentation](https://flutter.io/).

For help on editing plugin code, view the [documentation](https://flutter.io/platform-plugins/#edit-code).
8 changes: 8 additions & 0 deletions android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
34 changes: 34 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
group 'com.babariviere.smsplugin'
version '1.0-SNAPSHOT'

buildscript {
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
}
}

rootProject.allprojects {
repositories {
google()
jcenter()
}
}

apply plugin: 'com.android.library'

android {
compileSdkVersion 27

defaultConfig {
minSdkVersion 16
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
lintOptions {
disable 'InvalidPackage'
}
}
1 change: 1 addition & 0 deletions android/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.jvmargs=-Xmx1536M
1 change: 1 addition & 0 deletions android/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'sms_plugin'
7 changes: 7 additions & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.babariviere.smsplugin">
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-sdk android:minSdkVersion="4"/>
</manifest>
67 changes: 67 additions & 0 deletions android/src/main/java/com/babariviere/smsplugin/SmsPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.babariviere.smsplugin;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.SmsMessage;
import android.util.Log;

import org.json.JSONObject;

import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.EventChannel.EventSink;
import io.flutter.plugin.common.EventChannel.StreamHandler;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/**
* SmsPlugin
*/
public class SmsPlugin {
private Activity activity;

private static final String CHANNEL_REC = "plugins.babariviere.com/recvSms";

private int RECEIVE_SMS_ID_REQ = 0;

/**
* Plugin registration.
*/
public static void registerWith(Registrar registrar) {
final SmsPlugin plugin = new SmsPlugin(registrar);
plugin.checkAndRequestPermission(Manifest.permission.RECEIVE_SMS);

// SMS receiver
final SmsReceiver receiver = new SmsReceiver(registrar);
final EventChannel getSmsChannel = new EventChannel(registrar.messenger(),
CHANNEL_REC);
getSmsChannel.setStreamHandler(receiver);
}

private boolean hasPermission(String permission) {
return (Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
activity.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED);
}

private void checkAndRequestPermission(String permission) {
if (!hasPermission(permission)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
activity.requestPermissions(new String[] {permission}, RECEIVE_SMS_ID_REQ);
}
}
}

SmsPlugin(Registrar registrar) {
this.activity = registrar.activity();
}
}
85 changes: 85 additions & 0 deletions android/src/main/java/com/babariviere/smsplugin/SmsReceiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.babariviere.smsplugin;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.SmsMessage;

import org.json.JSONObject;

import io.flutter.plugin.common.EventChannel.*;
import io.flutter.plugin.common.PluginRegistry;

/**
* Created by babariviere on 08/03/18.
*/

public class SmsReceiver implements StreamHandler {
private PluginRegistry.Registrar registrar;
private BroadcastReceiver receiver;

SmsReceiver(PluginRegistry.Registrar registrar) {
this.registrar = registrar;
}

@Override
public void onListen(Object arguments, EventSink events) {
receiver = createSmsReceiver(events);
registrar.context().registerReceiver(receiver, new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION));
}

@Override
public void onCancel(Object o) {
registrar.context().unregisterReceiver(receiver);
receiver = null;
}

@TargetApi(Build.VERSION_CODES.DONUT)
private SmsMessage[] readMessages(Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
SmsMessage[] msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent);
return msgs;
}
Bundle bundle = intent.getExtras();

if (bundle == null || !bundle.containsKey("pdus")) {
return null;
}
final Object pdus[] = (Object[]) bundle.get("pdus");
SmsMessage[] msgs = new SmsMessage[pdus.length];
int idx = 0;
for (Object pdu: pdus) {
msgs[idx] = SmsMessage.createFromPdu((byte[]) pdu);
idx++;
}
return msgs;
}

private BroadcastReceiver createSmsReceiver(final EventSink events) {
return new BroadcastReceiver() {
@TargetApi(Build.VERSION_CODES.DONUT)
@Override
public void onReceive(Context context, Intent intent) {
try {
SmsMessage[] msgs = readMessages(intent);
if (msgs == null) {
return;
}
for (SmsMessage msg: msgs) {
JSONObject obj = new JSONObject();
obj.put("sender", msg.getOriginatingAddress());
obj.put("body", msg.getMessageBody());
events.success(obj.toString());
}
} catch (Exception e) {}
}
};
}
}
10 changes: 10 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.DS_Store
.atom/
.idea
.vscode/
.packages
.pub/
build/
ios/.generated/
packages
.flutter-plugins
8 changes: 8 additions & 0 deletions example/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: 3ea4d06340a97a1e9d7cae97567c64e0569dcaa2
channel: beta
8 changes: 8 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# sms_plugin_example

Demonstrates how to use the sms_plugin plugin.

## Getting Started

For help getting started with Flutter, view our online
[documentation](https://flutter.io/).
12 changes: 12 additions & 0 deletions example/android.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$/android">
<sourceFolder url="file://$MODULE_DIR$/android/app/src/main/java" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="Android API 25 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Flutter for Android" level="project" />
</component>
</module>
10 changes: 10 additions & 0 deletions example/android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.iml
*.class
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
GeneratedPluginRegistrant.java
51 changes: 51 additions & 0 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 27

lintOptions {
disable 'InvalidPackage'
}

defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.babariviere.smspluginexample"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
}

flutter {
source '../..'
}

dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
39 changes: 39 additions & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.babariviere.smspluginexample">

<!-- The INTERNET permission is required for development. Specifically,
flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>

<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name="io.flutter.app.FlutterApplication"
android:label="sms_plugin_example"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- This keeps the window background of the activity showing
until Flutter renders its first frame. It can be removed if
there is no splash screen (such as the default splash screen
defined in @style/LaunchTheme). -->
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Loading

0 comments on commit cdd48bb

Please sign in to comment.