Skip to content

Commit

Permalink
add a better way to share log
Browse files Browse the repository at this point in the history
  • Loading branch information
kar committed Jan 30, 2020
1 parent e57c586 commit e98b627
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
36 changes: 34 additions & 2 deletions app/src/ui-blokada/kotlin/core/bits/menu/MenuCommon.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package core.bits.menu

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.core.app.ShareCompat
import androidx.core.content.FileProvider
import blocka.blokadaUserAgent
import com.github.salomonbrys.kodein.instance
import core.*
import core.bits.UpdateVB
import core.bits.openWebContent
import gs.environment.ComponentProvider
import gs.presentation.NamedViewBinder
import org.blokada.R
import java.io.File
Expand Down Expand Up @@ -128,7 +131,8 @@ fun createLogMenuItem(ktx: AndroidKontext): NamedViewBinder {
return SimpleMenuItemVB(ktx,
label = R.string.main_log.res(),
icon = R.drawable.ic_bug_report_black_24dp.res(),
action = { shareLog(ktx.ctx) }
action = { shareLog(ktx.ctx) },
longAction = { shareLogAlternative(ktx.ctx) }
)
}

Expand Down Expand Up @@ -178,7 +182,7 @@ fun createPrivacyMenuItem(ktx: AndroidKontext): NamedViewBinder {
)
}

fun shareLog(ctx: Context) {
fun shareLogAlternative(ctx: Context) {
// if (askForExternalStoragePermissionsIfNeeded(activity)) {
val uri = File(ctx.filesDir, "/blokada.log")
val openFileIntent = Intent(Intent.ACTION_SEND)
Expand All @@ -193,6 +197,34 @@ fun shareLog(ctx: Context) {
// }
}

fun shareLog(ctx: Context) {
val uri = File(ctx.filesDir, "/blokada.log")
val actualUri = FileProvider.getUriForFile(ctx, "${ctx.packageName}.files", uri)

val provider = ctx.ktx("share").di().instance<ComponentProvider<Activity>>()
val activity = provider.get()

if (activity != null) {
val intent = ShareCompat.IntentBuilder.from(activity)
.setStream(actualUri)
.setType("text/*")
.intent
.setAction(Intent.ACTION_SEND)
.setDataAndType(actualUri, "text/*")
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
ctx.startActivity(intent)
} else {
val openFileIntent = Intent(Intent.ACTION_SEND)
openFileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
openFileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
openFileIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
openFileIntent.type = "plain/*"
openFileIntent.putExtra(Intent.EXTRA_STREAM, actualUri)
ctx.startActivity(openFileIntent)
}
}

fun createAppDetailsMenuItem(ktx: AndroidKontext): NamedViewBinder {
return SimpleMenuItemVB(ktx,
label = R.string.update_button_appinfo.res(),
Expand Down
7 changes: 7 additions & 0 deletions app/src/ui-blokada/kotlin/core/bits/menu/MenuComponents.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class SimpleMenuItemVB(
val label: Resource,
val icon: Resource,
val action: (ktx: AndroidKontext) -> Unit,
val longAction: ((ktx: AndroidKontext) -> Unit)? = null,
val arrow: Boolean = true,
override val name: Resource = label
): BitVB(), NamedViewBinder {
Expand All @@ -68,6 +69,12 @@ class SimpleMenuItemVB(
view.onTap {
action(ktx)
}
if (longAction != null) {
view.setOnLongClickListener {
longAction.invoke(ktx)
true
}
}
}

override fun detach(view: BitView) {
Expand Down

0 comments on commit e98b627

Please sign in to comment.