package com.google.appinventor.components.runtime.util;

import android.os.Build;
import android.os.Environment;
import com.google.appinventor.components.common.FileScope;
import com.google.appinventor.components.runtime.Form;

public class RUtil {
  /**
   * Checks whether the given {@code path} requires asking the user for file permissions.
   *
   * @param form the form making the request
   * @param path the path to test
   * @param fileScope the scope in which to test
   * @return true if the file will need read permissions and false otherwise
   */
  public static boolean needsFilePermission(Form form, String path, FileScope fileScope) {
    if (fileScope == null) {
      if (path.startsWith("//")) {
        return false;
      }
      if (!path.startsWith("/") && !path.startsWith("file:")) {
        return false;
      }
      if (path.startsWith("file:")) {
        path = path.substring(5);
      }
      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) {
        return path.startsWith(Environment.getExternalStorageDirectory().getAbsolutePath());
      }
      path = "file:" + path;
      return FileUtil.isExternalStorageUri(form, path)
          && !FileUtil.isAppSpecificExternalUri(form, path);
    }
    switch (fileScope) {
      case App:
        return Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT;
      case Asset:
        return form.isRepl() && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT;
      case Shared:
        return true;
      default:
        return false;
    }
  }
}

@@ -1,8 +1,195 @@
+// -*- mode: java; c-basic-offset: 2; -*-%0A// Copyright 2021-2022 MIT, All rights reserved%0A// Released under the Apache License, Version 2.0%0A// http://www.apache.org/licenses/LICENSE-2.0%0A%0A
 package 
@@ -1012,32 +1012,126 @@
  false;%0A      %7D%0A
+      if (Build.VERSION.SDK_INT %3C Build.VERSION_CODES.FROYO) %7B%0A        String fpath = path;%0A  
       if (path.s
@@ -1156,24 +1156,27 @@
 ) %7B%0A        
+ 
+ f
 path = path.
@@ -1199,72 +1199,11 @@
     
-%7D%0A      if (Build.VERSION.SDK_INT %3C Build.VERSION_CODES.FROYO) %7B
+  %7D
 %0A   
@@ -1214,16 +1214,17 @@
  return 
+f
 path.sta
@@ -1305,37 +1305,8 @@
   %7D%0A
-      path = %22file:%22 + path;%0A
     
@@ -1356,18 +1356,16 @@
 , path)%0A
-  
         

Fix CloudDB image fetching for Android 11+