55import java .io .InputStream ;
66import java .io .File ;
77import java .io .IOException ;
8+ import java .lang .reflect .InvocationTargetException ;
89import java .util .Collections ;
910import java .util .Iterator ;
1011import java .util .List ;
3334
3435import android .webkit .WebViewClient ;
3536import android .webkit .WebView ;
37+ import android .net .Uri ;
3638
3739import org .renpy .android .ResourceManager ;
3840
@@ -44,6 +46,7 @@ public class PythonActivity extends Activity {
4446 private static final String TAG = "PythonActivity" ;
4547
4648 public static PythonActivity mActivity = null ;
49+ public static boolean mOpenExternalLinksInBrowser = false ;
4750
4851 /** If shared libraries (e.g. SDL or the native application) could not be loaded. */
4952 public static boolean mBrokenLibraries ;
@@ -162,6 +165,13 @@ public void onClick(DialogInterface dialog,int id) {
162165 mWebView .setWebViewClient (new WebViewClient () {
163166 @ Override
164167 public boolean shouldOverrideUrlLoading (WebView view , String url ) {
168+ if (mOpenExternalLinksInBrowser ) {
169+ if (!(url .startsWith ("file:" ) || url .startsWith ("http://127.0.0.1:" ))) {
170+ Intent i = new Intent (Intent .ACTION_VIEW , Uri .parse (url ));
171+ startActivity (i );
172+ return true ;
173+ }
174+ }
165175 view .loadUrl (url );
166176 return false ;
167177 }
@@ -239,6 +249,16 @@ public void run() {
239249 mActivity .runOnUiThread (new LoadUrl (url ));
240250 }
241251
252+ public static void enableZoom () {
253+ mActivity .runOnUiThread (new Runnable () {
254+ @ Override
255+ public void run () {
256+ mWebView .getSettings ().setBuiltInZoomControls (true );
257+ mWebView .getSettings ().setDisplayZoomControls (false );
258+ }
259+ });
260+ }
261+
242262 public static ViewGroup getLayout () {
243263 return mLayout ;
244264 }
@@ -455,6 +475,73 @@ public static void stop_service() {
455475 public static native void nativeSetenv (String name , String value );
456476 public static native int nativeInit (Object arguments );
457477
478+
479+ /**
480+ * Used by android.permissions p4a module to register a call back after
481+ * requesting runtime permissions
482+ **/
483+ public interface PermissionsCallback {
484+ void onRequestPermissionsResult (int requestCode , String [] permissions , int [] grantResults );
485+ }
486+
487+ private PermissionsCallback permissionCallback ;
488+ private boolean havePermissionsCallback = false ;
489+
490+ public void addPermissionsCallback (PermissionsCallback callback ) {
491+ permissionCallback = callback ;
492+ havePermissionsCallback = true ;
493+ Log .v (TAG , "addPermissionsCallback(): Added callback for onRequestPermissionsResult" );
494+ }
495+
496+ @ Override
497+ public void onRequestPermissionsResult (int requestCode , String [] permissions , int [] grantResults ) {
498+ Log .v (TAG , "onRequestPermissionsResult()" );
499+ if (havePermissionsCallback ) {
500+ Log .v (TAG , "onRequestPermissionsResult passed to callback" );
501+ permissionCallback .onRequestPermissionsResult (requestCode , permissions , grantResults );
502+ }
503+ super .onRequestPermissionsResult (requestCode , permissions , grantResults );
504+ }
505+
506+ /**
507+ * Used by android.permissions p4a module to check a permission
508+ **/
509+ public boolean checkCurrentPermission (String permission ) {
510+ if (android .os .Build .VERSION .SDK_INT < 23 )
511+ return true ;
512+
513+ try {
514+ java .lang .reflect .Method methodCheckPermission =
515+ Activity .class .getMethod ("checkSelfPermission" , String .class );
516+ Object resultObj = methodCheckPermission .invoke (this , permission );
517+ int result = Integer .parseInt (resultObj .toString ());
518+ if (result == PackageManager .PERMISSION_GRANTED )
519+ return true ;
520+ } catch (IllegalAccessException | NoSuchMethodException |
521+ InvocationTargetException e ) {
522+ }
523+ return false ;
524+ }
525+
526+ /**
527+ * Used by android.permissions p4a module to request runtime permissions
528+ **/
529+ public void requestPermissionsWithRequestCode (String [] permissions , int requestCode ) {
530+ if (android .os .Build .VERSION .SDK_INT < 23 )
531+ return ;
532+ try {
533+ java .lang .reflect .Method methodRequestPermission =
534+ Activity .class .getMethod ("requestPermissions" ,
535+ String [].class , int .class );
536+ methodRequestPermission .invoke (this , permissions , requestCode );
537+ } catch (IllegalAccessException | NoSuchMethodException |
538+ InvocationTargetException e ) {
539+ }
540+ }
541+
542+ public void requestPermissions (String [] permissions ) {
543+ requestPermissionsWithRequestCode (permissions , 1 );
544+ }
458545}
459546
460547
0 commit comments