Skip to content

Commit 29cb0b7

Browse files
Merge pull request #1 from hogangnono/feature/HGNN-407
[HGNN-407] inappbrowser webview permission
2 parents cd8bb7c + 827aa29 commit 29cb0b7

File tree

5 files changed

+195
-134
lines changed

5 files changed

+195
-134
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cordova-plugin-inappbrowser",
3-
"version": "5.0.4-hogangnono",
3+
"version": "5.0.7-hogangnono",
44
"description": "Cordova InAppBrowser Plugin",
55
"types": "./types/index.d.ts",
66
"cordova": {

plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
2222
id="cordova-plugin-inappbrowser"
23-
version="5.0.4-hogangnono">
23+
version="5.0.7-hogangnono">
2424

2525
<name>InAppBrowser</name>
2626
<description>Cordova InAppBrowser Plugin</description>

src/android/InAppBrowser.java

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one
1818
*/
1919
package org.apache.cordova.inappbrowser;
2020

21+
import android.Manifest;
2122
import android.annotation.SuppressLint;
2223
import android.annotation.TargetApi;
2324
import android.content.ActivityNotFoundException;
@@ -48,6 +49,7 @@ Licensed to the Apache Software Foundation (ASF) under one
4849
import android.webkit.CookieManager;
4950
import android.webkit.HttpAuthHandler;
5051
import android.webkit.JavascriptInterface;
52+
import android.webkit.PermissionRequest;
5153
import android.webkit.SslErrorHandler;
5254
import android.webkit.ValueCallback;
5355
import android.webkit.WebChromeClient;
@@ -70,6 +72,7 @@ Licensed to the Apache Software Foundation (ASF) under one
7072
import org.apache.cordova.CordovaPlugin;
7173
import org.apache.cordova.CordovaWebView;
7274
import org.apache.cordova.LOG;
75+
import org.apache.cordova.PermissionHelper;
7376
import org.apache.cordova.PluginManager;
7477
import org.apache.cordova.PluginResult;
7578
import org.json.JSONException;
@@ -122,6 +125,8 @@ public class InAppBrowser extends CordovaPlugin {
122125
private static final String FOOTER_COLOR = "footercolor";
123126
private static final String BEFORELOAD = "beforeload";
124127
private static final String FULLSCREEN = "fullscreen";
128+
private static final int MY_PERMISSIONS_REQUEST_RECORD_AUDIO = 101;
129+
private static final int MY_PERMISSIONS_REQUEST_CAMERA = 102;
125130

126131
private static final List customizableOptions = Arrays.asList(CLOSE_BUTTON_CAPTION, TOOLBAR_COLOR, NAVIGATION_COLOR, CLOSE_BUTTON_COLOR, FOOTER_COLOR);
127132

@@ -153,6 +158,7 @@ public class InAppBrowser extends CordovaPlugin {
153158
private boolean fullscreen = true;
154159
private String[] allowedSchemes;
155160
private InAppBrowserClient currentClient;
161+
private PermissionRequest myRequest;
156162

157163
/**
158164
* Executes the request and returns PluginResult.
@@ -977,19 +983,19 @@ public void onClick(View v) {
977983
String shareBody = inAppWebView.getUrl();
978984
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, inAppWebView.getUrl());
979985
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
980-
986+
981987
cordova.getActivity().startActivity(Intent.createChooser(sharingIntent, "URL 공유"));
982988
}
983989
});
984-
990+
985991
RelativeLayout.LayoutParams closeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
986992
closeLayoutParams.addRule(RelativeLayout.RIGHT_OF, shareButtonId);
987993
closeButton.setLayoutParams(closeLayoutParams);
988994

989995
RelativeLayout.LayoutParams shareLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
990996
shareLayoutParams.addRule(RelativeLayout.ALIGN_LEFT);
991997
shareButton.setLayoutParams(shareLayoutParams);
992-
998+
993999
customButtonContainer.addView((View)closeButton);
9941000
customButtonContainer.addView((View)shareButton);
9951001

@@ -1017,6 +1023,18 @@ public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePath
10171023
cordova.startActivityForResult(InAppBrowser.this, Intent.createChooser(content, "Select File"), FILECHOOSER_REQUESTCODE);
10181024
return true;
10191025
}
1026+
1027+
@Override
1028+
public void onPermissionRequest(PermissionRequest request) {
1029+
myRequest = request;
1030+
for (String permission : request.getResources()) {
1031+
if (permission.equals("android.webkit.resource.AUDIO_CAPTURE")) {
1032+
askForPermission(Manifest.permission.RECORD_AUDIO, MY_PERMISSIONS_REQUEST_RECORD_AUDIO);
1033+
} else if (permission.equals("android.webkit.resource.VIDEO_CAPTURE")) {
1034+
askForPermission(Manifest.permission.CAMERA, MY_PERMISSIONS_REQUEST_CAMERA);
1035+
}
1036+
}
1037+
}
10201038
});
10211039
currentClient = new InAppBrowserClient(thatWebView, edittext, beforeload);
10221040
inAppWebView.setWebViewClient(currentClient);
@@ -1129,6 +1147,40 @@ public void postMessage(String data) {
11291147
return "";
11301148
}
11311149

1150+
/**
1151+
* WebView에서 권한이 필요할 시에 확인하는 로직
1152+
*
1153+
* @param permission 요청하는 권한
1154+
* @param requestCode 내부적으로 정의한 코드
1155+
*/
1156+
public void askForPermission(String permission, int requestCode) {
1157+
if (!PermissionHelper.hasPermission(this, permission)) {
1158+
PermissionHelper.requestPermission(this, requestCode, permission);
1159+
} else {
1160+
myRequest.grant(myRequest.getResources());
1161+
}
1162+
}
1163+
1164+
/**
1165+
* 권한 요청 결과가 반환되는 콜백
1166+
*
1167+
* @param requestCode 요청시 넣어줬던 코드
1168+
* @param permissions 요청했던 권한
1169+
* @param grantResults 권환 획득 여부
1170+
*/
1171+
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
1172+
for (int r : grantResults) {
1173+
if (r == PackageManager.PERMISSION_DENIED) {
1174+
myRequest.deny();
1175+
return;
1176+
}
1177+
}
1178+
1179+
if (requestCode == MY_PERMISSIONS_REQUEST_RECORD_AUDIO || requestCode == MY_PERMISSIONS_REQUEST_CAMERA) {
1180+
myRequest.grant(myRequest.getResources());
1181+
}
1182+
}
1183+
11321184
/**
11331185
* Create a new plugin success result and send it back to JavaScript
11341186
*
@@ -1339,12 +1391,12 @@ else if (url.startsWith(HOGANGNONO_SCHEME)) {
13391391
else if (url.startsWith(INTENT_PROTOCOL_START)) {
13401392
final int customUrlStartIndex = INTENT_PROTOCOL_START.length();
13411393
final int customUrlEndIndex = url.indexOf(INTENT_PROTOCOL_INTENT);
1342-
1394+
13431395
if (customUrlEndIndex < 0) {
13441396
return false;
13451397
} else {
13461398
final String customUrl = url.substring(customUrlStartIndex, customUrlEndIndex);
1347-
1399+
13481400
try {
13491401
cordova.getActivity().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(customUrl)));
13501402
} catch (ActivityNotFoundException e) {

0 commit comments

Comments
 (0)