Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS module loading for older Androids (fixes #9311) #13812

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,23 @@
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ichi2.utils
package com.ichi2.anki.utils

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.ichi2.utils.AssetHelper
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class AssetHelperTest {
@Test
fun guessMimeTypeTest() {
assertThat(AssetHelper.guessMimeType("test.js"), equalTo("text/javascript"))
assertThat(AssetHelper.guessMimeType("test.mjs"), equalTo("text/javascript"))
assertThat(AssetHelper.guessMimeType("test.json"), equalTo("application/json"))
assertThat(AssetHelper.guessMimeType("test.css"), equalTo("text/css"))
assertThat(AssetHelper.guessMimeType("test.txt"), equalTo("text/plain"))
assertThat(AssetHelper.guessMimeType("test.png"), equalTo("image/png"))
assertThat(AssetHelper.guessMimeType("test.zip"), equalTo("application/zip"))
Expand Down
13 changes: 9 additions & 4 deletions AnkiDroid/src/main/java/com/ichi2/utils/AssetHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
*/
package com.ichi2.utils

import java.net.URLConnection
import android.webkit.MimeTypeMap

/** Clone of RestrictedApi functionality */
object AssetHelper {
/**
* Use [URLConnection.guessContentTypeFromName] to guess MIME type or return the
* Use [MimeTypeMap.getMimeTypeFromExtension] to guess MIME type or return the
* "text/plain" if it can't guess.
*
* Copy of [androidx.webkit.internal.AssetHelper.guessMimeType]
Expand All @@ -49,7 +49,12 @@ object AssetHelper {
* @return MIME type guessed from file extension or "text/plain".
*/
fun guessMimeType(path: String?): String {
val mimeType = URLConnection.guessContentTypeFromName(path)
return mimeType ?: "text/plain"
val extension = MimeTypeMap.getFileExtensionFromUrl(path)
return when (extension) {
"js" -> "text/javascript"
"mjs" -> "text/javascript"
"json" -> "application/json"
else -> MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension) ?: "text/plain"
}
}
}