Skip to content

Commit 569e14e

Browse files
tweak feedback
Signed-off-by: androidacy-user <opensource@androidacy.com>
1 parent 7dfc74d commit 569e14e

2 files changed

Lines changed: 92 additions & 91 deletions

File tree

app/src/main/kotlin/com/fox2code/mmm/MainActivity.kt

Lines changed: 87 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton
6565
import com.google.android.material.progressindicator.LinearProgressIndicator
6666
import com.google.android.material.textfield.TextInputEditText
6767
import ly.count.android.sdk.Countly
68-
import ly.count.android.sdk.ModuleFeedback.FeedbackCallback
68+
import ly.count.android.sdk.ModuleFeedback
6969
import timber.log.Timber
7070
import java.sql.Timestamp
7171

@@ -118,7 +118,7 @@ class MainActivity : AppCompatActivity(), OnRefreshListener, OverScrollHelper {
118118
onMainActivityCreate(this)
119119
super.onCreate(savedInstanceState)
120120
INSTANCE = this
121-
121+
122122
// hide this behind a buildconfig flag for now, but crash the app if it's not an official build and not debug
123123
if (BuildConfig.ENABLE_PROTECTION && !MainApplication.o && !BuildConfig.DEBUG) {
124124
throw RuntimeException("This is not an official build of AMM")
@@ -146,8 +146,10 @@ class MainActivity : AppCompatActivity(), OnRefreshListener, OverScrollHelper {
146146
// use countly to track enabled repos
147147
val repoMap = HashMap<String, String>()
148148
repoMap["repos"] = enabledRepos.toString()
149-
Countly.sharedInstance().events().recordEvent("enabled_repos",
150-
repoMap as Map<String, Any>?, 1)
149+
Countly.sharedInstance().events().recordEvent(
150+
"enabled_repos",
151+
repoMap as Map<String, Any>?, 1
152+
)
151153
}
152154
}.start()
153155
val ts = Timestamp(System.currentTimeMillis() - 30L * 24 * 60 * 60 * 1000)
@@ -238,9 +240,10 @@ class MainActivity : AppCompatActivity(), OnRefreshListener, OverScrollHelper {
238240
// filter the appropriate list based on visibility
239241
if (initMode) return
240242
val query = s.toString()
241-
Countly.sharedInstance().events().recordEvent("search", HashMap<String, String>().apply {
242-
put("query", query)
243-
} as Map<String, Any>?, 1)
243+
Countly.sharedInstance().events()
244+
.recordEvent("search", HashMap<String, String>().apply {
245+
put("query", query)
246+
} as Map<String, Any>?, 1)
244247
Thread {
245248
if (moduleViewListBuilder.setQueryChange(query)) {
246249
Timber.i("Query submit: %s on offline list", query)
@@ -266,9 +269,10 @@ class MainActivity : AppCompatActivity(), OnRefreshListener, OverScrollHelper {
266269
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
267270
// filter the appropriate list based on visibility
268271
val query = textInputEditText.text.toString()
269-
Countly.sharedInstance().events().recordEvent("search", HashMap<String, String>().apply {
270-
put("query", query)
271-
} as Map<String, Any>?, 1)
272+
Countly.sharedInstance().events()
273+
.recordEvent("search", HashMap<String, String>().apply {
274+
put("query", query)
275+
} as Map<String, Any>?, 1)
272276
Thread {
273277
if (moduleViewListBuilder.setQueryChange(query)) {
274278
Timber.i("Query submit: %s on offline list", query)
@@ -325,30 +329,30 @@ class MainActivity : AppCompatActivity(), OnRefreshListener, OverScrollHelper {
325329
// show reboot dialog with options to reboot, reboot to recovery, bootloader, or edl, and use RuntimeUtils to reboot
326330
val rebootDialog =
327331
MaterialAlertDialogBuilder(this@MainActivity).setTitle(R.string.reboot).setItems(
328-
arrayOf(
329-
getString(R.string.reboot),
330-
getString(R.string.reboot_recovery),
331-
getString(R.string.reboot_bootloader),
332-
getString(R.string.reboot_edl)
332+
arrayOf(
333+
getString(R.string.reboot),
334+
getString(R.string.reboot_recovery),
335+
getString(R.string.reboot_bootloader),
336+
getString(R.string.reboot_edl)
337+
)
338+
) { _: DialogInterface?, which: Int ->
339+
when (which) {
340+
0 -> RuntimeUtils.reboot(
341+
this@MainActivity,
342+
RuntimeUtils.RebootMode.REBOOT
333343
)
334-
) { _: DialogInterface?, which: Int ->
335-
when (which) {
336-
0 -> RuntimeUtils.reboot(
337-
this@MainActivity,
338-
RuntimeUtils.RebootMode.REBOOT
339-
)
340344

341-
1 -> RuntimeUtils.reboot(
342-
this@MainActivity, RuntimeUtils.RebootMode.RECOVERY
343-
)
345+
1 -> RuntimeUtils.reboot(
346+
this@MainActivity, RuntimeUtils.RebootMode.RECOVERY
347+
)
344348

345-
2 -> RuntimeUtils.reboot(
346-
this@MainActivity, RuntimeUtils.RebootMode.BOOTLOADER
347-
)
349+
2 -> RuntimeUtils.reboot(
350+
this@MainActivity, RuntimeUtils.RebootMode.BOOTLOADER
351+
)
348352

349-
3 -> RuntimeUtils.reboot(this@MainActivity, RuntimeUtils.RebootMode.EDL)
350-
}
351-
}.setNegativeButton(R.string.cancel, null).create()
353+
3 -> RuntimeUtils.reboot(this@MainActivity, RuntimeUtils.RebootMode.EDL)
354+
}
355+
}.setNegativeButton(R.string.cancel, null).create()
352356
rebootDialog.show()
353357
}
354358
// get background color and elevation of reboot fab
@@ -651,6 +655,59 @@ class MainActivity : AppCompatActivity(), OnRefreshListener, OverScrollHelper {
651655
}
652656
ExternalHelper.INSTANCE.refreshHelper(this)
653657
initMode = false
658+
if (MainApplication.shouldShowFeedback()) {
659+
// wait a bit before showing feedback
660+
Handler(Looper.getMainLooper()).postDelayed({
661+
showFeedback()
662+
}, 5000)
663+
Timber.i("Should show feedback")
664+
} else {
665+
Timber.i("Should not show feedback")
666+
}
667+
}
668+
669+
private fun showFeedback() {
670+
Countly.sharedInstance().feedback()
671+
.getAvailableFeedbackWidgets { retrievedWidgets, error ->
672+
Timber.i("Got feedback widgets: %s", retrievedWidgets.size)
673+
if (error == null) {
674+
if (retrievedWidgets.size > 0) {
675+
val feedbackWidget = retrievedWidgets[0]
676+
Countly.sharedInstance().feedback().presentFeedbackWidget(
677+
feedbackWidget,
678+
this@MainActivity,
679+
"Close",
680+
object : ModuleFeedback.FeedbackCallback {
681+
override fun onClosed() {
682+
}
683+
684+
// maybe show a toast when the widget is closed
685+
override fun onFinished(error: String?) {
686+
// error handling here
687+
if (!error.isNullOrEmpty()) {
688+
Toast.makeText(
689+
this@MainActivity,
690+
"Error: $error",
691+
Toast.LENGTH_LONG
692+
).show()
693+
Timber.e(error, "Feedback error")
694+
} else {
695+
Toast.makeText(
696+
this@MainActivity,
697+
"Feedback sent",
698+
Toast.LENGTH_LONG
699+
).show()
700+
}
701+
}
702+
})
703+
// update last feedback time
704+
MainApplication.getSharedPreferences("mmm")?.edit()
705+
?.putLong("last_feedback", System.currentTimeMillis())?.apply()
706+
}
707+
} else {
708+
Timber.e(error, "Failed to get feedback widgets")
709+
}
710+
}
654711
}
655712

656713
private fun updateBlurState() {
@@ -747,44 +804,6 @@ class MainActivity : AppCompatActivity(), OnRefreshListener, OverScrollHelper {
747804
moduleViewListBuilder.applyTo(moduleList!!, moduleViewAdapter!!)
748805
moduleViewListBuilderOnline.applyTo(moduleListOnline!!, moduleViewAdapterOnline!!)
749806
}, "Repo update thread").start()
750-
if (MainApplication.shouldShowFeedback()) {
751-
Countly.sharedInstance().feedback()
752-
.getAvailableFeedbackWidgets { retrievedWidgets, error ->
753-
if (error == null) {
754-
if (retrievedWidgets.size > 0) {
755-
val feedbackWidget = retrievedWidgets[0]
756-
Countly.sharedInstance().feedback().presentFeedbackWidget(
757-
feedbackWidget,
758-
this@MainActivity,
759-
"Close",
760-
object : FeedbackCallback {
761-
override fun onClosed() {
762-
}
763-
764-
// maybe show a toast when the widget is closed
765-
override fun onFinished(error: String) {
766-
// error handling here
767-
if (error.isNotEmpty()) {
768-
Toast.makeText(
769-
this@MainActivity,
770-
"Error: $error",
771-
Toast.LENGTH_LONG
772-
).show()
773-
} else {
774-
Toast.makeText(
775-
this@MainActivity,
776-
"Feedback sent",
777-
Toast.LENGTH_LONG
778-
).show()
779-
}
780-
}
781-
})
782-
}
783-
} else {
784-
Timber.e(error)
785-
}
786-
}
787-
}
788807
}
789808

790809
fun maybeShowUpgrade() {

app/src/main/kotlin/com/fox2code/mmm/MainApplication.kt

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -680,29 +680,11 @@ class MainApplication : Application(), Configuration.Provider, ActivityLifecycle
680680
}
681681

682682
fun shouldShowFeedback(): Boolean {
683-
// should not have been shown in 30 days and only 1 in 5 chance
684-
return if (getSharedPreferences("mmm")!!.getBoolean("pref_feedback_shown", false)) {
685-
false
686-
} else {
687-
val random = Random()
688-
val chance = random.nextInt(5)
689-
if (chance == 0) {
690-
val lastFeedback = getSharedPreferences("mmm")!!.getLong(
691-
"pref_last_feedback", 0
692-
)
693-
val now = System.currentTimeMillis()
694-
if (now - lastFeedback > 2592000000L) {
695-
val editor = getSharedPreferences("mmm")!!.edit()
696-
editor.putLong("pref_last_feedback", now)
697-
editor.apply()
698-
true
699-
} else {
700-
false
701-
}
702-
} else {
703-
false
704-
}
705-
}
683+
// should not have been shown in 14 days and only 1 in 5 chance
684+
val randChance = Random().nextInt(5)
685+
val lastShown = getSharedPreferences("mmm")!!.getLong("last_feedback", 0)
686+
if (BuildConfig.DEBUG) Timber.d("Last feedback shown: %d, randChance: %d", lastShown, randChance)
687+
return System.currentTimeMillis() - lastShown > 1209600000 && randChance == 0
706688
}
707689
}
708690

0 commit comments

Comments
 (0)