Skip to content

Commit dbc377a

Browse files
authored
Add Whatsapp sharing and fix WebView load bug (#7)
1 parent aad7faf commit dbc377a

File tree

5 files changed

+50
-12
lines changed

5 files changed

+50
-12
lines changed

sdk/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ android {
1010
defaultConfig {
1111
minSdkVersion 16
1212
targetSdkVersion 27
13-
versionCode 39
14-
versionName "0.5.10"
13+
versionCode 40
14+
versionName "0.5.11"
1515
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1616
}
1717
buildTypes {

sdk/src/main/java/com/talkable/sdk/TalkableOfferFragment.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class TalkableOfferFragment extends Fragment {
4747
public static final int REQUEST_CODE_SEND_SMS = 1;
4848
public static final int REQUEST_CODE_READ_CONTACTS = 2;
4949
public static final int REQUEST_CODE_SEND_NATIVE_MAIL = 3;
50+
public static final int REQUEST_CODE_SEND_WHATSAPP = 4;
5051

5152
private WebView mWebView;
5253
private WebAppInterface mWebAppInterface;
@@ -112,13 +113,13 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
112113
}
113114

114115
@Override
115-
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
116+
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
116117
super.onViewCreated(view, savedInstanceState);
117118

118119
initializeWebView();
119120

120121
OfferWebData offerWebData = TalkablePreferencesStore.getOfferWebData(mOfferCode);
121-
mWebView.loadDataWithBaseURL(offerWebData.getOriginUrl(), offerWebData.getHtml(), "text/html", "utf-8", null);
122+
mWebView.loadDataWithBaseURL(Talkable.getServer(), offerWebData.getHtml(), "text/html", "utf-8", offerWebData.getOriginUrl());
122123
}
123124

124125
@Override
@@ -138,6 +139,10 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
138139
if (requestCode == REQUEST_CODE_SEND_NATIVE_MAIL && resultCode == Activity.RESULT_OK) {
139140
shareSucceeded(SharingChannel.NATIVE_MAIL.toString());
140141
}
142+
143+
if (requestCode == REQUEST_CODE_SEND_WHATSAPP && resultCode == Activity.RESULT_OK) {
144+
shareSucceeded(SharingChannel.WHATSAPP.toString());
145+
}
141146
}
142147

143148
public boolean onBackPressed() {
@@ -323,12 +328,28 @@ public void shareOfferViaFacebookMessage(String claimUrl) {
323328
FacebookUtils.shareViaMessenger(this, claimUrl);
324329
}
325330

331+
public void shareOfferViaWhatsApp(String message) {
332+
if (!NativeFeatures.isAvailable(Feature.SHARE_VIA_WHATSAPP)) {
333+
Log.d(Talkable.TAG, "Native feature called when not available: share_via_whatsapp");
334+
return;
335+
}
336+
Intent intent = new Intent(Intent.ACTION_SEND);
337+
intent.setType("text/plain");
338+
intent.setPackage("com.whatsapp");
339+
if (message != null) {
340+
intent.putExtra(Intent.EXTRA_TEXT, message);
341+
}
342+
startActivityForResult(intent, REQUEST_CODE_SEND_WHATSAPP);
343+
}
344+
326345
public void copyToClipboard(String string) {
327346
if (getActivity() == null) return;
328347

329348
ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
330349
ClipData clip = ClipData.newPlainText(string, string);
331-
clipboard.setPrimaryClip(clip);
350+
if (clipboard != null) {
351+
clipboard.setPrimaryClip(clip);
352+
}
332353
}
333354

334355
public void popupOpened() {

sdk/src/main/java/com/talkable/sdk/WebAppInterface.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void publish(String eventName, String data) {
2121
try {
2222
JsonObject json = new JsonObject();
2323
if (data.length() > 0) {
24-
json = new JsonParser().parse(data).getAsJsonObject();
24+
json = JsonParser.parseString(data).getAsJsonObject();
2525
}
2626
switch (eventName) {
2727
case "put_to_clipboard":
@@ -71,6 +71,10 @@ public void publish(String eventName, String data) {
7171
talkableOfferFragment.shareOfferViaTwitter(
7272
JsonUtils.getJsonString(json, "message"));
7373
break;
74+
case "share_offer_via_whatsapp":
75+
talkableOfferFragment.shareOfferViaWhatsApp(
76+
JsonUtils.getJsonString(json, "message"));
77+
break;
7478
case "share_offer_via_sms":
7579
talkableOfferFragment.shareOfferViaSms(
7680
JsonUtils.getJsonString(json, "recipients"),

sdk/src/main/java/com/talkable/sdk/models/SharingChannel.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public enum SharingChannel {
66
@SerializedName("facebook") FACEBOOK("facebook"),
77
@SerializedName("facebook_message") FACEBOOK_MESSAGE("facebook_message"),
88
@SerializedName("twitter") TWITTER("twitter"),
9+
@SerializedName("whatsapp") WHATSAPP("whatsapp"),
910
@SerializedName("linkedin") LINKEDIN("linkedin"),
1011
@SerializedName("email") EMAIL("email"),
1112
@SerializedName("sms") SMS("sms"),

sdk/src/main/java/com/talkable/sdk/utils/NativeFeatures.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public enum Feature {
2424
SHARE_VIA_NATIVE_EMAIL("share_via_native_mail"),
2525
SHARE_VIA_TWITTER("share_via_twitter"),
2626
SHARE_VIA_FACEBOOK("share_via_facebook"),
27-
SHARE_VIA_FACEBOOK_MESSENGER("share_via_facebook_messenger");
27+
SHARE_VIA_FACEBOOK_MESSENGER("share_via_facebook_messenger"),
28+
SHARE_VIA_WHATSAPP("share_via_whatsapp");
2829

2930
private final String identifier;
3031

@@ -42,9 +43,10 @@ public String toString() {
4243
}
4344

4445
public static void initialize(Context context) {
45-
Boolean isSmsAvailable = false;
46-
Boolean isMessengerInstalled = false;
47-
Boolean isMailAvailable = false;
46+
boolean isSmsAvailable = false;
47+
boolean isMessengerInstalled = false;
48+
boolean isMailAvailable = false;
49+
boolean isWhatsAppAvailable = false;
4850

4951
if (context != null) {
5052
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY) &&
@@ -53,13 +55,14 @@ public static void initialize(Context context) {
5355
}
5456

5557
isMessengerInstalled = MessengerUtils.hasMessengerInstalled(context);
58+
isWhatsAppAvailable = isPackageInstalled("com.whatsapp", context);
5659

5760
Intent sendNativeMailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
58-
List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentActivities(sendNativeMailIntent, 0);// .isEmpty();
61+
List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentActivities(sendNativeMailIntent, 0);
5962
if (!resolveInfos.isEmpty()) {
6063
// for some android emulators there is always "com.android.fallback/.Fallback" intent
6164
// https://stackoverflow.com/a/31052350
62-
List<ResolveInfo> filtered = new ArrayList<ResolveInfo>();
65+
List<ResolveInfo> filtered = new ArrayList<>();
6366
for (ResolveInfo info : resolveInfos) {
6467
String packageName = info.activityInfo.packageName;
6568
if (!packageName.toLowerCase().contains("fallback")) {
@@ -77,6 +80,7 @@ public static void initialize(Context context) {
7780
json.addProperty(Feature.SHARE_VIA_FACEBOOK_MESSENGER.toString(), FacebookSdk.isInitialized() && isMessengerInstalled);
7881
json.addProperty(Feature.SHARE_VIA_TWITTER.toString(), false);
7982
json.addProperty(Feature.SHARE_VIA_NATIVE_EMAIL.toString(), isMailAvailable);
83+
json.addProperty(Feature.SHARE_VIA_WHATSAPP.toString(), isWhatsAppAvailable);
8084
json.addProperty("sdk_version", BuildConfig.VERSION_NAME);
8185
json.addProperty("sdk_build", BuildConfig.VERSION_CODE);
8286

@@ -90,4 +94,12 @@ public static String getFeatures() {
9094
public static boolean isAvailable(Feature feature) {
9195
return JsonUtils.getJsonBoolean(features, feature.toString());
9296
}
97+
98+
private static boolean isPackageInstalled(String packageName, Context context) {
99+
try {
100+
return context.getPackageManager().getApplicationInfo(packageName, 0).enabled;
101+
} catch (PackageManager.NameNotFoundException e) {
102+
return false;
103+
}
104+
}
93105
}

0 commit comments

Comments
 (0)