Skip to content

Commit

Permalink
[PR-11804] Native email sharing (#4)
Browse files Browse the repository at this point in the history
* [PR-11804] Native email sharing

* Remove recipients, bump version
  • Loading branch information
human-spider authored Jul 29, 2019
1 parent 5808fcd commit 8a24409
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath 'com.android.tools.build:gradle:3.4.2'
classpath 'de.mobilej.unmock:UnMockPlugin:0.6.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'

Expand Down
4 changes: 2 additions & 2 deletions sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
minSdkVersion 16
targetSdkVersion 27
versionCode 38
versionName "0.5.8"
versionName "0.5.9"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down Expand Up @@ -58,5 +58,5 @@ dependencies {
implementation 'com.squareup.okhttp3:logging-interceptor:3.2.0'
implementation 'com.facebook.android:facebook-android-sdk:[4,5)'
implementation 'com.google.code.gson:gson:2.8.+'
implementation 'com.android.support:support-v4:26.1.0'
implementation 'com.android.support:support-v4:27.1.0'
}
35 changes: 26 additions & 9 deletions sdk/src/main/java/com/talkable/sdk/TalkableOfferFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;

public class TalkableOfferFragment extends Fragment {
public static final String OFFER_CODE_PARAM = "offer_code_param";
public static final int SEND_SMS_CODE = 1;
public static final int REQUEST_READ_CONTACTS = 2;
public static final int REQUEST_CODE_SEND_SMS = 1;
public static final int REQUEST_CODE_READ_CONTACTS = 2;
public static final int REQUEST_CODE_SEND_NATIVE_MAIL = 3;

private WebView mWebView;
private WebAppInterface mWebAppInterface;
Expand Down Expand Up @@ -125,9 +128,15 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
FacebookUtils.onActivityResult(requestCode, resultCode, data);
}

if (requestCode == SEND_SMS_CODE && mWebAppInterface != null) { // resultCode always RESULT_CANCELED for Intent.ACTION_SENDTO
if (mWebAppInterface == null) return;

if (requestCode == REQUEST_CODE_SEND_SMS) { // resultCode always RESULT_CANCELED for SMS sharing
shareSucceeded(SharingChannel.SMS.toString());
}

if (requestCode == REQUEST_CODE_SEND_NATIVE_MAIL && resultCode == Activity.RESULT_OK) {
shareSucceeded(SharingChannel.NATIVE_MAIL.toString());
}
}

public boolean onBackPressed() {
Expand Down Expand Up @@ -216,17 +225,25 @@ public void shareSucceeded(String channel) {
// Callbacks |
//-----------+

public void shareOfferViaNativeMail(String subject, String message, String claimUrl) {
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
if (subject != null) {
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
}
if (message != null) {
intent.putExtra(Intent.EXTRA_TEXT, message);
}
startActivityForResult(intent, REQUEST_CODE_SEND_NATIVE_MAIL);
}

public void shareOfferViaSms(String recipients, String message, String claimUrl) {
String uriString = "sms:";
if (recipients != null) {
uriString += recipients;
}
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uriString));
if (message != null && claimUrl != null && !message.contains(claimUrl)) {
message += " " + claimUrl;
}
intent.putExtra("sms_body", message);
startActivityForResult(intent, TalkableOfferFragment.SEND_SMS_CODE);
startActivityForResult(intent, REQUEST_CODE_SEND_SMS);
}

public void importContacts() {
Expand All @@ -236,14 +253,14 @@ public void importContacts() {
== PackageManager.PERMISSION_GRANTED) {
publishImportedContacts();
} else {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_READ_CONTACTS);
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_CODE_READ_CONTACTS);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_READ_CONTACTS:
case REQUEST_CODE_READ_CONTACTS:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
publishImportedContacts();
} else {
Expand Down
7 changes: 7 additions & 0 deletions sdk/src/main/java/com/talkable/sdk/WebAppInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public void publish(String eventName, String data) {
talkableOfferFragment.responsiveIframeHeight(
JsonUtils.getJsonInt(json, "height"));
break;
case "share_offer_via_native_mail":
talkableOfferFragment.shareOfferViaNativeMail(
JsonUtils.getJsonString(json, "subject"),
JsonUtils.getJsonString(json, "message"),
JsonUtils.getJsonString(json, "claim_url")
);
break;
case "share_offer_via_facebook":
talkableOfferFragment.shareOfferViaFacebook(
JsonUtils.getJsonString(json, "claim_url"),
Expand Down
15 changes: 8 additions & 7 deletions sdk/src/main/java/com/talkable/sdk/models/SharingChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import com.google.gson.annotations.SerializedName;

public enum SharingChannel {
@SerializedName("facebook") FACEBOOK("facebook"),
@SerializedName("facebook_message") FACEBOOK_MESSAGE("facebook_message"),
@SerializedName("twitter") TWITTER("twitter"),
@SerializedName("linkedin") LINKEDIN("linkedin"),
@SerializedName("email") EMAIL("email"),
@SerializedName("sms") SMS("sms"),
@SerializedName("other") OTHER("other");
@SerializedName("facebook") FACEBOOK("facebook"),
@SerializedName("facebook_message") FACEBOOK_MESSAGE("facebook_message"),
@SerializedName("twitter") TWITTER("twitter"),
@SerializedName("linkedin") LINKEDIN("linkedin"),
@SerializedName("email") EMAIL("email"),
@SerializedName("sms") SMS("sms"),
@SerializedName("other") OTHER("other"),
@SerializedName("direct_email_native") NATIVE_MAIL("direct_email_native");

private final String identifier;

Expand Down

0 comments on commit 8a24409

Please sign in to comment.