Skip to content

Commit

Permalink
Adding support for saving PDF using Android's "Open With" functionality.
Browse files Browse the repository at this point in the history
Upping versionCode to 138
  • Loading branch information
Dima-Android committed Feb 26, 2025
1 parent 0cc7fa7 commit 118915a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
11 changes: 11 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<!--Share With-->
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
Expand All @@ -54,6 +55,16 @@
<data android:mimeType="image/*" />
<data android:mimeType="video/*" />
</intent-filter>

<!--Open With-->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="content" />
<data android:scheme="file" />
<data android:mimeType="application/pdf" />
</intent-filter>
</activity>

<activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RootActivity : BaseActivity(), Screen<RootViewState, RootViewEffect> {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

viewModel.init(this.intent.extras)
viewModel.init(this.intent)
viewModel.observeViewChanges(this)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.zotero.android.screens.root

import android.os.Bundle
import android.content.Intent
import dagger.hilt.android.lifecycle.HiltViewModel
import org.zotero.android.architecture.BaseViewModel2
import org.zotero.android.architecture.ViewEffect
Expand All @@ -17,10 +17,10 @@ class RootViewModel @Inject constructor(
RootViewState,
RootViewEffect>(initialState = RootViewState()) {

fun init(extras: Bundle?) {
fun init(intent: Intent) {
if (!sessionController.isLoggedIn) {
triggerEffect(RootViewEffect.NavigateToSignIn)
} else if (shareRawAttachmentLoader.doesBundleContainShareData(extras)) {
} else if (shareRawAttachmentLoader.doesIntentContainShareData(intent)) {
triggerEffect(RootViewEffect.NavigateToShare)
} else {
triggerEffect(RootViewEffect.NavigateToDashboard)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class ShareActivity : BaseActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
shareRawAttachmentLoader.loadAttachment(bundleExtras = intent.extras!!)
shareRawAttachmentLoader.loadFromIntent(intent)
setContent {
CustomTheme {
ShareRootNavigation()
Expand All @@ -32,6 +32,7 @@ internal class ShareActivity : BaseActivity() {
context: Context,
): Intent {
return Intent(context, ShareActivity::class.java).apply {
data = extraIntent.data
putExtras(extraIntent)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,26 @@ import javax.inject.Singleton
class ShareRawAttachmentLoader @Inject constructor() {

private lateinit var loadedAttachment: Result<RawAttachment>

fun loadFromIntent(intent: Intent) {
val bundleExtras = intent.extras
if (bundleExtras != null && bundleExtras.containsKey(Intent.EXTRA_STREAM)) {
loadFromIntentExtras(bundleExtras)
} else {
loadFromIntentData(intent.data)
}
}

fun loadAttachment(bundleExtras: Bundle) {
private fun loadFromIntentData(data: Uri?) {
if (data == null) {
loadedAttachment = Result.Failure(AttachmentState.Error.cantLoadWebData)
} else {
loadedAttachment = Result.Success(RawAttachment.fileUrl(data))
}

}

private fun loadFromIntentExtras(bundleExtras: Bundle) {
val urlPath = bundleExtras.getString(Intent.EXTRA_TEXT)
if (urlPath != null) {
val lastPathSegment = urlPath.toUri().lastPathSegment
Expand Down Expand Up @@ -49,13 +67,12 @@ class ShareRawAttachmentLoader @Inject constructor() {
}
}

fun doesBundleContainShareData(bundleExtras: Bundle?): Boolean {
if (bundleExtras == null) {
return false
}
val urlPath = bundleExtras.getString(Intent.EXTRA_TEXT)
val fileContentUri = bundleExtras.getSupportParcelable(Intent.EXTRA_STREAM, Uri::class.java)
return urlPath != null || fileContentUri != null
fun doesIntentContainShareData(intent: Intent): Boolean {
val bundleExtras = intent.extras
val urlPath = bundleExtras?.getString(Intent.EXTRA_TEXT)
val fileContentUri = bundleExtras?.getSupportParcelable(Intent.EXTRA_STREAM, Uri::class.java)
val dataPath = intent.data
return urlPath != null || fileContentUri != null || dataPath != null
}

}
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/BuildConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ object BuildConfig {
const val compileSdkVersion = 34
const val targetSdk = 34

val versionCode = 137 // Must be updated on every build
val versionCode = 138 // Must be updated on every build
val version = Version(
major = 1,
minor = 0,
Expand Down

0 comments on commit 118915a

Please sign in to comment.