Skip to content

Commit 182a5d2

Browse files
committed
android custom share add
1 parent 8a29d88 commit 182a5d2

File tree

10 files changed

+132
-5
lines changed

10 files changed

+132
-5
lines changed

plugin.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@
6565
<resource-file src="src/android/res/drawable-xhdpi/ic_action_remove.png" target="res/drawable-xhdpi/ic_action_remove.png" />
6666
<resource-file src="src/android/res/drawable-xxhdpi/ic_action_remove.png" target="res/drawable-xxhdpi/ic_action_remove.png" />
6767

68+
69+
<resource-file src="src/android/res/drawable-hdpi/ic_action_share.png" target="res/drawable-hdpi/ic_action_share.png" />
70+
<resource-file src="src/android/res/drawable-mdpi/ic_action_share.png" target="res/drawable-mdpi/ic_action_share.png" />
71+
<resource-file src="src/android/res/drawable-xhdpi/ic_action_share.png" target="res/drawable-xhdpi/ic_action_share.png" />
72+
<resource-file src="src/android/res/drawable-xxhdpi/ic_action_share.png" target="res/drawable-xxhdpi/ic_action_share.png" />
73+
74+
75+
<resource-file src="src/android/res/drawable-hdpi/ic_action_remove_white.png" target="res/drawable-hdpi/ic_action_remove_white.png" />
76+
<resource-file src="src/android/res/drawable-mdpi/ic_action_remove_white.png" target="res/drawable-mdpi/ic_action_remove_white.png" />
77+
<resource-file src="src/android/res/drawable-xhdpi/ic_action_remove_white.png" target="res/drawable-xhdpi/ic_action_remove_white.png" />
78+
<resource-file src="src/android/res/drawable-xxhdpi/ic_action_remove_white.png" target="res/drawable-xxhdpi/ic_action_remove_white.png" />
79+
6880
</platform>
6981

7082
<!-- ios -->

src/android/InAppBrowser.java

Lines changed: 120 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Licensed to the Apache Software Foundation (ASF) under one
2020

2121
import android.annotation.SuppressLint;
2222
import android.annotation.TargetApi;
23+
import android.content.ActivityNotFoundException;
2324
import android.content.ComponentName;
2425
import android.content.Context;
2526
import android.content.Intent;
@@ -106,6 +107,11 @@ public class InAppBrowser extends CordovaPlugin {
106107
private static final String CLEAR_SESSION_CACHE = "clearsessioncache";
107108
private static final String HARDWARE_BACK_BUTTON = "hardwareback";
108109
private static final String MEDIA_PLAYBACK_REQUIRES_USER_ACTION = "mediaPlaybackRequiresUserAction";
110+
private static final String INTENT_PROTOCOL_START = "intent:";
111+
private static final String INTENT_PROTOCOL_INTENT = "#Intent;";
112+
private static final String INTENT_PROTOCOL_END = ";end;";
113+
private static final String GOOGLE_PLAY_STORE_PREFIX = "market://details?id=";
114+
private static final String HOGANGNONO_SCHEME = "hogangnono://";
109115
private static final String SHOULD_PAUSE = "shouldPauseOnSuspend";
110116
private static final Boolean DEFAULT_HARDWARE_BACK = true;
111117
private static final String USER_WIDE_VIEW_PORT = "useWideViewPort";
@@ -785,6 +791,27 @@ public void onClick(View v) {
785791
return _close;
786792
}
787793

794+
private ImageButton createImageButton(int id, String res){
795+
Resources activityRes = cordova.getActivity().getResources();
796+
797+
ImageButton button = new ImageButton(cordova.getActivity());
798+
int resId = activityRes.getIdentifier(res, "drawable", cordova.getActivity().getPackageName());
799+
Drawable resIcon = activityRes.getDrawable(resId);
800+
801+
button.setImageDrawable(resIcon);
802+
button.setScaleType(ImageView.ScaleType.FIT_CENTER);
803+
804+
if (Build.VERSION.SDK_INT >= 16){
805+
button.getAdjustViewBounds();
806+
button.setBackground(null);
807+
}else{
808+
button.setBackgroundDrawable(null);
809+
}
810+
button.setId(Integer.valueOf(id));
811+
812+
return button;
813+
}
814+
788815
@SuppressLint("NewApi")
789816
public void run() {
790817

@@ -890,7 +917,13 @@ public void onClick(View v) {
890917
textLayoutParams.addRule(RelativeLayout.LEFT_OF, 5);
891918
edittext.setLayoutParams(textLayoutParams);
892919
edittext.setId(Integer.valueOf(4));
893-
edittext.setSingleLine(true);
920+
// edittext.setSingleLine(true);
921+
edittext.setEnabled(false);
922+
edittext.setTextSize(15);
923+
edittext.setTextColor(android.graphics.Color.argb(150, 255,255,255));
924+
edittext.setBackgroundColor(android.graphics.Color.argb(0,0,0,0));
925+
edittext.setPadding(0, this.dpToPixels(10), this.dpToPixels(8), this.dpToPixels(10));
926+
894927
edittext.setText(url);
895928
edittext.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
896929
edittext.setImeOptions(EditorInfo.IME_ACTION_GO);
@@ -906,7 +939,7 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
906939
}
907940
});
908941

909-
942+
/*
910943
// Header Close/Done button
911944
int closeButtonId = leftToRight ? 1 : 5;
912945
View close = createCloseButton(closeButtonId);
@@ -930,7 +963,55 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
930963
931964
View footerClose = createCloseButton(7);
932965
footer.addView(footerClose);
966+
*/
933967

968+
// Action Button Container layout
969+
RelativeLayout customButtonContainer = new RelativeLayout(cordova.getActivity());
970+
RelativeLayout.LayoutParams containerLayoutParmas = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
971+
containerLayoutParmas.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
972+
customButtonContainer.setLayoutParams(containerLayoutParmas);
973+
customButtonContainer.setHorizontalGravity(Gravity.RIGHT);
974+
customButtonContainer.setVerticalGravity(Gravity.CENTER_VERTICAL);
975+
customButtonContainer.setId(Integer.valueOf(5));
976+
977+
// Close/Done button
978+
int closeButtonId = 7;
979+
ImageButton closeButton = createImageButton(closeButtonId, "ic_action_remove_white");
980+
closeButton.setContentDescription("Close Button");
981+
closeButton.setPadding(0, this.dpToPixels(10), 0, this.dpToPixels(10));
982+
closeButton.setOnClickListener(new View.OnClickListener(){
983+
public void onClick(View v){
984+
closeDialog();
985+
}
986+
});
987+
988+
// Share button
989+
int shareButtonId = 8;
990+
ImageButton shareButton = createImageButton(shareButtonId, "ic_action_share");
991+
shareButton.setPadding(5, this.dpToPixels(10), 5, this.dpToPixels(10));
992+
shareButton.setContentDescription("Share Button");
993+
shareButton.setOnClickListener(new View.OnClickListener() {
994+
public void onClick(View v) {
995+
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
996+
sharingIntent.setType("text/plain");
997+
String shareBody = inAppWebView.getUrl();
998+
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, inAppWebView.getUrl());
999+
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
1000+
1001+
cordova.getActivity().startActivity(Intent.createChooser(sharingIntent, "URL 공유"));
1002+
}
1003+
});
1004+
1005+
RelativeLayout.LayoutParams closeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
1006+
closeLayoutParams.addRule(RelativeLayout.RIGHT_OF, shareButtonId);
1007+
closeButton.setLayoutParams(closeLayoutParams);
1008+
1009+
RelativeLayout.LayoutParams shareLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
1010+
shareLayoutParams.addRule(RelativeLayout.ALIGN_LEFT);
1011+
shareButton.setLayoutParams(shareLayoutParams);
1012+
1013+
customButtonContainer.addView((View)closeButton);
1014+
customButtonContainer.addView((View)shareButton);
9341015

9351016
// WebView
9361017
inAppWebView = new WebView(cordova.getActivity());
@@ -1055,6 +1136,7 @@ public void postMessage(String data) {
10551136
// Add the views to our toolbar if they haven't been disabled
10561137
if (!hideNavigationButtons) toolbar.addView(actionButtonContainer);
10571138
if (!hideUrlBar) toolbar.addView(edittext);
1139+
toolbar.addView(customButtonContainer);
10581140

10591141
// Don't add the toolbar if its been disabled
10601142
if (getShowLocationBar()) {
@@ -1068,9 +1150,9 @@ public void postMessage(String data) {
10681150
main.addView(webViewLayout);
10691151

10701152
// Don't add the footer unless it's been enabled
1071-
if (showFooter) {
1072-
webViewLayout.addView(footer);
1073-
}
1153+
// if (showFooter) {
1154+
// webViewLayout.addView(footer);
1155+
// }
10741156

10751157
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
10761158
lp.copyFrom(dialog.getWindow().getAttributes());
@@ -1264,6 +1346,7 @@ public boolean shouldOverrideUrlLoading(String url, String method) {
12641346
} catch (android.content.ActivityNotFoundException e) {
12651347
LOG.e(LOG_TAG, "Error dialing " + url + ": " + e.toString());
12661348
}
1349+
// [hogangnono] Make to install when use doesn't have app
12671350
} else if (url.startsWith("geo:") || url.startsWith(WebView.SCHEME_MAILTO) || url.startsWith("market:") || url.startsWith("intent:")) {
12681351
try {
12691352
Intent intent = new Intent(Intent.ACTION_VIEW);
@@ -1305,6 +1388,38 @@ else if (url.startsWith("sms:")) {
13051388
LOG.e(LOG_TAG, "Error sending sms " + url + ":" + e.toString());
13061389
}
13071390
}
1391+
// Support hogangnono scheme
1392+
else if (url.startsWith(HOGANGNONO_SCHEME)) {
1393+
final String customUrl = url.substring(HOGANGNONO_SCHEME.length(), url.length());
1394+
1395+
try {
1396+
cordova.getActivity().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(customUrl)));
1397+
} catch (ActivityNotFoundException e) {
1398+
cordova.getActivity().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(GOOGLE_PLAY_STORE_PREFIX + "com.hogangnono.hogangnono")));
1399+
}
1400+
return true;
1401+
}
1402+
// Supports Intent:// scheme. It usually used on above 4 version.
1403+
else if (url.startsWith(INTENT_PROTOCOL_START)) {
1404+
final int customUrlStartIndex = INTENT_PROTOCOL_START.length();
1405+
final int customUrlEndIndex = url.indexOf(INTENT_PROTOCOL_INTENT);
1406+
1407+
if (customUrlEndIndex < 0) {
1408+
return false;
1409+
} else {
1410+
final String customUrl = url.substring(customUrlStartIndex, customUrlEndIndex);
1411+
1412+
try {
1413+
cordova.getActivity().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(customUrl)));
1414+
} catch (ActivityNotFoundException e) {
1415+
final int packageStartIndex = customUrlEndIndex + INTENT_PROTOCOL_INTENT.length();
1416+
final int packageEndIndex = url.indexOf(INTENT_PROTOCOL_END);
1417+
final String packageName = url.substring(packageStartIndex, packageEndIndex < 0 ? url.length() : packageEndIndex).replace("package=", "");
1418+
cordova.getActivity().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(GOOGLE_PLAY_STORE_PREFIX + packageName)));
1419+
}
1420+
return true;
1421+
}
1422+
}
13081423
// Test for whitelisted custom scheme names like mycoolapp:// or twitteroauthresponse:// (Twitter Oauth Response)
13091424
else if (!url.startsWith("http:") && !url.startsWith("https:") && url.matches("^[A-Za-z0-9+.-]*://.*?$")) {
13101425
if (allowedSchemes == null) {
1.71 KB
Loading
1.72 KB
Loading
1.55 KB
Loading
1.93 KB
Loading
1.86 KB
Loading
2.21 KB
Loading
2.03 KB
Loading
1.96 KB
Loading

0 commit comments

Comments
 (0)