Skip to content

Commit 20015bd

Browse files
committed
fix: fixes and improvements to onboarding (now can't be avoided by pressing back, as per Google Play policies)
1 parent 4b9b99b commit 20015bd

File tree

4 files changed

+54
-12
lines changed

4 files changed

+54
-12
lines changed

mobile/src/main/java/net/activitywatch/android/MainActivity.kt

+9-7
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,6 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
3636

3737
override fun onCreate(savedInstanceState: Bundle?) {
3838
super.onCreate(savedInstanceState)
39-
binding = ActivityMainBinding.inflate(layoutInflater)
40-
val view = binding.root
41-
setContentView(view)
42-
43-
// Set up alarm to send heartbeats
44-
val usw = UsageStatsWatcher(this)
45-
usw.setupAlarm()
4639

4740
// If first time, or usage not allowed, show onboarding activity
4841
val prefs = AWPreferences(this)
@@ -52,6 +45,15 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
5245
startActivity(intent)
5346
}
5447

48+
// Set up UI
49+
binding = ActivityMainBinding.inflate(layoutInflater)
50+
val view = binding.root
51+
setContentView(view)
52+
53+
// Set up alarm to send heartbeats
54+
val usw = UsageStatsWatcher(this)
55+
usw.setupAlarm()
56+
5557
binding.navView.setNavigationItemSelectedListener(this)
5658

5759
val ri = RustInterface(this)

mobile/src/main/java/net/activitywatch/android/OnboardingActivity.kt

+29-4
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,22 @@ class OnboardingActivity : AppCompatActivity() {
3737
viewPager.adapter = OnboardingAdapter(this)
3838
TabLayoutMediator(tabLayout, viewPager) { _, _ -> }.attach()
3939

40+
val nextButton = findViewById<Button>(R.id.nextButton)
41+
val backButton = findViewById<Button>(R.id.backButton)
42+
43+
// helper function to update texts/visibility of buttons on page change
44+
fun updateButtons() {
45+
nextButton.text = if (viewPager.currentItem == numPages - 1) "Finish" else "Continue"
46+
backButton.visibility = if (viewPager.currentItem > 0) View.VISIBLE else View.GONE
47+
}
48+
4049
// If not users first time, skip to the last page
4150
val prefs = AWPreferences(this)
4251
if (!prefs.isFirstTime()) {
4352
viewPager.currentItem = OnboardingPage.ACCESSIBILITY_PERMISSION.ordinal
53+
updateButtons()
4454
}
4555

46-
val nextButton = findViewById<Button>(R.id.nextButton)
4756
nextButton.setOnClickListener {
4857
val currentItem = viewPager.currentItem
4958
if (currentItem < numPages - 1) {
@@ -56,20 +65,36 @@ class OnboardingActivity : AppCompatActivity() {
5665
finish()
5766
} else {
5867
// Show a snackbar and don't finish the activity
59-
val snackbar = Snackbar.make(viewPager, "Please grant usage access permission", Snackbar.LENGTH_LONG)
68+
val snackbar = Snackbar.make(viewPager, "Please grant usage access permission, they are necessary for the core function of the app.", Snackbar.LENGTH_LONG)
6069
snackbar.show()
6170
}
6271
}
72+
updateButtons()
73+
}
74+
75+
backButton.setOnClickListener {
76+
val currentItem = viewPager.currentItem
77+
if (currentItem > 0) {
78+
viewPager.currentItem = currentItem - 1
79+
}
80+
updateButtons()
6381
}
6482

6583
// Update the text of the next button based on the current page
6684
viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
6785
override fun onPageSelected(position: Int) {
68-
// if on last page, disable button until permission is granted
69-
nextButton.text = if (position == numPages - 1) "Finish" else "Continue"
86+
updateButtons()
7087
}
7188
})
7289
}
90+
91+
override fun onBackPressed() {
92+
// If back button is pressed, exit the app,
93+
// since we don't want to allow the user to accidentally skip onboarding.
94+
// (Google Play policy, due to sensitive permissions)
95+
// https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces#back-button
96+
finishAffinity()
97+
}
7398
}
7499

75100
class OnboardingAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {

mobile/src/main/java/net/activitywatch/android/watcher/UsageStatsWatcher.kt

+3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class UsageStatsWatcher constructor(val context: Context) {
9292
} else {
9393
Log.w(TAG, "Was not allowed access to UsageStats, enable in settings.")
9494

95+
// Unused, deprecated in favor of OnboardingActivity
96+
/*
9597
Handler(Looper.getMainLooper()).post {
9698
// Create an alert dialog to inform the user
9799
AlertDialog.Builder(context)
@@ -106,6 +108,7 @@ class UsageStatsWatcher constructor(val context: Context) {
106108
}
107109
.show()
108110
}
111+
*/
109112
null
110113
}
111114
}

mobile/src/main/res/layout/activity_onboarding.xml

+13-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
app:layout_constraintTop_toTopOf="parent"
1313
app:layout_constraintBottom_toTopOf="@id/tabLayout"/>
1414

15+
<Button
16+
android:id="@+id/backButton"
17+
android:layout_width="wrap_content"
18+
android:layout_height="wrap_content"
19+
android:text="Back"
20+
android:layout_margin="16dp"
21+
android:visibility="gone"
22+
style="?android:attr/borderlessButtonStyle"
23+
app:layout_constraintBottom_toBottomOf="parent"
24+
app:layout_constraintStart_toStartOf="parent"
25+
/>
26+
1527
<Button
1628
android:id="@+id/nextButton"
1729
android:layout_width="wrap_content"
@@ -26,7 +38,7 @@
2638
<com.google.android.material.tabs.TabLayout
2739
android:id="@+id/tabLayout"
2840
android:layout_width="match_parent"
29-
android:layout_height="wrap_content"
41+
android:layout_height="16dp"
3042
app:layout_constraintBottom_toBottomOf="parent"/>
3143

3244
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)