-
-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Offer to hide AR quests if streetmeasure fails to install or fails to…
… return a result (fixes #4849)
- Loading branch information
1 parent
2683a20
commit a3c0a5a
Showing
10 changed files
with
136 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
app/src/main/java/de/westnordost/streetcomplete/quests/AbstractArMeasureQuestForm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package de.westnordost.streetcomplete.quests | ||
|
||
import android.content.ActivityNotFoundException | ||
import android.os.Bundle | ||
import androidx.annotation.StringRes | ||
import androidx.appcompat.app.AlertDialog | ||
import de.westnordost.streetcomplete.ApplicationConstants.STREETMEASURE | ||
import de.westnordost.streetcomplete.R | ||
import de.westnordost.streetcomplete.data.meta.LengthUnit | ||
import de.westnordost.streetcomplete.osm.Length | ||
import de.westnordost.streetcomplete.screens.measure.ArQuestsDisabler | ||
import de.westnordost.streetcomplete.screens.measure.MeasureContract | ||
import de.westnordost.streetcomplete.util.ktx.isPackageInstalled | ||
import de.westnordost.streetcomplete.util.ktx.openUri | ||
import org.koin.android.ext.android.inject | ||
|
||
/** Abstract superclass for all forms that let StreetMeasure measure stuff. */ | ||
abstract class AbstractArMeasureQuestForm<T>: AbstractOsmQuestForm<T>() { | ||
private val arQuestsDisabler: ArQuestsDisabler by inject() | ||
|
||
private val launcher = registerForActivityResult(MeasureContract(), ::onMeasuredInternal) | ||
|
||
private var requestedARInstall: Boolean = false | ||
private var requestedARMeasurement: Boolean = false | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
// returned to app without StreetMeasure installed but requested install before | ||
if (requestedARInstall && !requireContext().packageManager.isPackageInstalled(STREETMEASURE)) { | ||
showDisableARMeasureQuestsDialog(R.string.quest_disable_message_not_installed) | ||
} | ||
requestedARInstall = false | ||
requestedARMeasurement = false | ||
} | ||
|
||
override fun onSaveInstanceState(outState: Bundle) { | ||
super.onSaveInstanceState(outState) | ||
outState.putBoolean(AR_INSTALL_REQUESTED, requestedARInstall) | ||
outState.putBoolean(AR_MEASUREMENT_REQUESTED, requestedARMeasurement) | ||
} | ||
|
||
fun takeMeasurement(lengthUnit: LengthUnit, measureVertical: Boolean) { | ||
try { | ||
requestedARMeasurement = true | ||
launcher.launch(MeasureContract.Params(lengthUnit, measureVertical)) | ||
} catch (e: ActivityNotFoundException) { | ||
requestedARInstall = true | ||
requestedARMeasurement = false | ||
context?.openUri("market://details?id=$STREETMEASURE") | ||
} | ||
} | ||
|
||
private fun onMeasuredInternal(length: Length?) { | ||
// returned to app but StreetMeasure did not return a correct result (app crashed?) | ||
if (length == null) { | ||
showDisableARMeasureQuestsDialog(R.string.quest_disable_message_not_working) | ||
return | ||
} | ||
onMeasured(length) | ||
} | ||
|
||
protected abstract fun onMeasured(length: Length) | ||
|
||
private fun showDisableARMeasureQuestsDialog(@StringRes message: Int) { | ||
val ctx = context ?: return | ||
AlertDialog.Builder(ctx) | ||
.setTitle(R.string.quest_disable_title) | ||
.setMessage( | ||
ctx.getString(message) + | ||
"\n\n" + | ||
ctx.getString(R.string.quest_disable_message_tape_measure) | ||
) | ||
.setPositiveButton(R.string.quest_disable_action) { _, _ -> | ||
arQuestsDisabler.hideAllArQuests() | ||
} | ||
.setNegativeButton(android.R.string.cancel, null) | ||
.show() | ||
} | ||
|
||
companion object { | ||
private const val AR_MEASUREMENT_REQUESTED = "ar_measurement_requested" | ||
private const val AR_INSTALL_REQUESTED = "ar_install_requested" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
app/src/main/java/de/westnordost/streetcomplete/screens/measure/ArQuestsDisabler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package de.westnordost.streetcomplete.screens.measure | ||
|
||
import de.westnordost.streetcomplete.data.quest.QuestTypeRegistry | ||
import de.westnordost.streetcomplete.data.visiblequests.VisibleQuestTypeController | ||
|
||
class ArQuestsDisabler( | ||
private val questTypeRegistry: QuestTypeRegistry, | ||
private val visibleQuestTypeController: VisibleQuestTypeController | ||
) { | ||
private val arQuestNames = listOf( | ||
"AddMaxPhysicalHeight", | ||
"AddRoadWidth", | ||
"AddCyclewayWidth" | ||
) | ||
|
||
fun hideAllArQuests() { | ||
val arQuests = arQuestNames.mapNotNull { questTypeRegistry.getByName(it) } | ||
visibleQuestTypeController.setVisibilities(arQuests.associateWith { false }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
app/src/main/java/de/westnordost/streetcomplete/util/ktx/PackageManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package de.westnordost.streetcomplete.util.ktx | ||
|
||
import android.content.pm.PackageManager | ||
|
||
fun PackageManager.isPackageInstalled(packageName: String): Boolean = | ||
try { | ||
getPackageInfo(packageName, 0) != null | ||
} catch (e: PackageManager.NameNotFoundException) { | ||
false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters