Skip to content

Commit

Permalink
added abs vs rel path, permissions and more
Browse files Browse the repository at this point in the history
  • Loading branch information
deeppp15 committed Dec 7, 2024
1 parent 88701bb commit ded23ea
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 142 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.REQUEST_WRITE_PERMISSION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
Expand Down
188 changes: 100 additions & 88 deletions app/src/main/java/org/ole/planet/myplanet/base/BaseContainerFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -152,88 +152,88 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
}
}

private fun checkFileExtension(items: RealmMyLibrary) {
val mimetype = Utilities.getMimeType(items.resourceLocalAddress)
val userId = "${model?.id}"
Log.d("FileExtension", "Mimetype: ${items.resourceLocalAddress}")

val extension = items.resourceLocalAddress
?.substringAfterLast('.', "")
?.lowercase()

val existingAction = mRealm.where(RealmUserChallengeActions::class.java)
.equalTo("userId", userId)
.equalTo("resourceId", items.resourceId)
.findFirst()

if (mimetype != null) {
if (mimetype.contains("image")) {
openIntent(items, ImageViewerActivity::class.java)
if (existingAction == null) {
createAction(mRealm, userId, items.resourceId, "resourceOpen")
}
} else if (mimetype.contains("pdf")) {
openPdf(items)
if (existingAction == null) {
createAction(mRealm, userId, items.resourceId, "resourceOpen")
}
} else if (mimetype.contains("audio")) {
openIntent(items, AudioPlayerActivity::class.java)
if (existingAction == null) {
createAction(mRealm, userId, items.resourceId, "resourceOpen")
}
} else {
checkMoreFileExtensions(extension, items)
}
}
}

private fun checkMoreFileExtensions(extension: String?, items: RealmMyLibrary) {
val userId = "${model?.id}"
val existingAction = mRealm.where(RealmUserChallengeActions::class.java)
.equalTo("userId", userId)
.equalTo("resourceId", items.resourceId)
.findFirst()

when (extension) {
"txt" -> {
if (existingAction == null) {
createAction(mRealm, userId, items.resourceId, "resourceOpen")
}
openIntent(items, TextFileViewerActivity::class.java)
}
"html", "htm" -> {
if (existingAction == null) {
createAction(mRealm, userId, items.resourceId, "resourceOpen")
}
openHtmlResource(items)
}
"md" -> {
if (items.resourceLocalAddress?.contains("README.md", ignoreCase = true) == true) {
Log.d("FileExtension", "Detected README.md file")
return
}

if (existingAction == null) {
createAction(mRealm, userId, items.resourceId, "resourceOpen")
}
openIntent(items, MarkdownViewerActivity::class.java)
}
"csv" -> {
if (existingAction == null) {
createAction(mRealm, userId, items.resourceId, "resourceOpen")
}
openIntent(items, CSVViewerActivity::class.java)
}
"apk" -> {
if (existingAction == null) {
createAction(mRealm, userId, items.resourceId, "resourceOpen")
}
installApk(items)
}
else -> Toast.makeText(activity, getString(R.string.this_file_type_is_currently_unsupported), Toast.LENGTH_LONG).show()
}
}
// private fun checkFileExtension(items: RealmMyLibrary) {
// val mimetype = Utilities.getMimeType(items.resourceLocalAddress)
// val userId = "${model?.id}"
// Log.d("FileExtension", "Mimetype: ${items.resourceLocalAddress}")
//
// val extension = items.resourceLocalAddress
// ?.substringAfterLast('.', "")
// ?.lowercase()
//
// val existingAction = mRealm.where(RealmUserChallengeActions::class.java)
// .equalTo("userId", userId)
// .equalTo("resourceId", items.resourceId)
// .findFirst()
//
// if (mimetype != null) {
// if (mimetype.contains("image")) {
// openIntent(items, ImageViewerActivity::class.java)
// if (existingAction == null) {
// createAction(mRealm, userId, items.resourceId, "resourceOpen")
// }
// } else if (mimetype.contains("pdf")) {
// openPdf(items)
// if (existingAction == null) {
// createAction(mRealm, userId, items.resourceId, "resourceOpen")
// }
// } else if (mimetype.contains("audio")) {
// openIntent(items, AudioPlayerActivity::class.java)
// if (existingAction == null) {
// createAction(mRealm, userId, items.resourceId, "resourceOpen")
// }
// } else {
// checkMoreFileExtensions(extension, items)
// }
// }
// }
//
// private fun checkMoreFileExtensions(extension: String?, items: RealmMyLibrary) {
// val userId = "${model?.id}"
// val existingAction = mRealm.where(RealmUserChallengeActions::class.java)
// .equalTo("userId", userId)
// .equalTo("resourceId", items.resourceId)
// .findFirst()
//
// when (extension) {
// "txt" -> {
// if (existingAction == null) {
// createAction(mRealm, userId, items.resourceId, "resourceOpen")
// }
// openIntent(items, TextFileViewerActivity::class.java)
// }
// "html", "htm" -> {
// if (existingAction == null) {
// createAction(mRealm, userId, items.resourceId, "resourceOpen")
// }
// openHtmlResource(items)
// }
// "md" -> {
// if (items.resourceLocalAddress?.contains("README.md", ignoreCase = true) == true) {
// Log.d("FileExtension", "Detected README.md file")
// return
// }
//
// if (existingAction == null) {
// createAction(mRealm, userId, items.resourceId, "resourceOpen")
// }
// openIntent(items, MarkdownViewerActivity::class.java)
// }
// "csv" -> {
// if (existingAction == null) {
// createAction(mRealm, userId, items.resourceId, "resourceOpen")
// }
// openIntent(items, CSVViewerActivity::class.java)
// }
// "apk" -> {
// if (existingAction == null) {
// createAction(mRealm, userId, items.resourceId, "resourceOpen")
// }
// installApk(items)
// }
// else -> Toast.makeText(activity, getString(R.string.this_file_type_is_currently_unsupported), Toast.LENGTH_LONG).show()
// }
// }

private fun installApk(items: RealmMyLibrary) {
currentLibrary = items
Expand Down Expand Up @@ -285,7 +285,9 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
if (mimetype.startsWith("video")) {
playVideo(videoType, items)
} else {
checkFileExtension(items)
// createAction(mRealm, , items.resourceId, "resourceOpen")
openHtmlResource(items)
//checkFileExtension(items)
}
}

Expand Down Expand Up @@ -368,14 +370,24 @@ abstract class BaseContainerFragment : BaseResourceFragment() {
}

private fun openHtmlResource(items: RealmMyLibrary) {
val intent = Intent(activity, WebViewActivity::class.java).apply {
putExtra("TOUCHED_FILE", "${items.id}/${items.resourceLocalAddress}")
putExtra("title", items.title)
putExtra("isLocalFile", true)
//correct path found in emulator is : /storage/self/primary/Android/data/org.ole.planet.myplanet/files/ole/folder/index.html
val resourcePath = File(MainApplication.context.getExternalFilesDir(null), "ole/${items.id}/${items.resourceLocalAddress}")
if (resourcePath.exists()) {
Log.d("webview","THE FILE PATH IS:"+resourcePath.toURI().toString())
val intent = Intent(activity, WebViewActivity::class.java).apply {
putExtra("TOUCHED_FILE", resourcePath.toURI().toString()) // Use corrected path
putExtra("title", items.title)
putExtra("isLocalFile", true)
}
startActivity(intent)
} else {
Log.e("BaseContainerFragment", "File not found: $resourcePath")
Toast.makeText(requireContext(), "NO QUERO FILE", Toast.LENGTH_LONG).show()
}
startActivity(intent)
}



open fun handleBackPressed() {
val fragmentManager = parentFragmentManager
fragmentManager.popBackStack()
Expand Down
Loading

0 comments on commit ded23ea

Please sign in to comment.