Skip to content

Commit

Permalink
fix: fixes and improvements to onboarding (now can't be avoided by pr…
Browse files Browse the repository at this point in the history
…essing back, as per Google Play policies)
  • Loading branch information
ErikBjare committed Oct 13, 2023
1 parent 4b9b99b commit 20015bd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
16 changes: 9 additions & 7 deletions mobile/src/main/java/net/activitywatch/android/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)

// Set up alarm to send heartbeats
val usw = UsageStatsWatcher(this)
usw.setupAlarm()

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

// Set up UI
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)

// Set up alarm to send heartbeats
val usw = UsageStatsWatcher(this)
usw.setupAlarm()

binding.navView.setNavigationItemSelectedListener(this)

val ri = RustInterface(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,22 @@ class OnboardingActivity : AppCompatActivity() {
viewPager.adapter = OnboardingAdapter(this)
TabLayoutMediator(tabLayout, viewPager) { _, _ -> }.attach()

val nextButton = findViewById<Button>(R.id.nextButton)
val backButton = findViewById<Button>(R.id.backButton)

// helper function to update texts/visibility of buttons on page change
fun updateButtons() {
nextButton.text = if (viewPager.currentItem == numPages - 1) "Finish" else "Continue"
backButton.visibility = if (viewPager.currentItem > 0) View.VISIBLE else View.GONE
}

// If not users first time, skip to the last page
val prefs = AWPreferences(this)
if (!prefs.isFirstTime()) {
viewPager.currentItem = OnboardingPage.ACCESSIBILITY_PERMISSION.ordinal
updateButtons()
}

val nextButton = findViewById<Button>(R.id.nextButton)
nextButton.setOnClickListener {
val currentItem = viewPager.currentItem
if (currentItem < numPages - 1) {
Expand All @@ -56,20 +65,36 @@ class OnboardingActivity : AppCompatActivity() {
finish()
} else {
// Show a snackbar and don't finish the activity
val snackbar = Snackbar.make(viewPager, "Please grant usage access permission", Snackbar.LENGTH_LONG)
val snackbar = Snackbar.make(viewPager, "Please grant usage access permission, they are necessary for the core function of the app.", Snackbar.LENGTH_LONG)
snackbar.show()
}
}
updateButtons()
}

backButton.setOnClickListener {
val currentItem = viewPager.currentItem
if (currentItem > 0) {
viewPager.currentItem = currentItem - 1
}
updateButtons()
}

// Update the text of the next button based on the current page
viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
// if on last page, disable button until permission is granted
nextButton.text = if (position == numPages - 1) "Finish" else "Continue"
updateButtons()
}
})
}

override fun onBackPressed() {
// If back button is pressed, exit the app,
// since we don't want to allow the user to accidentally skip onboarding.
// (Google Play policy, due to sensitive permissions)
// https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces#back-button
finishAffinity()
}
}

class OnboardingAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class UsageStatsWatcher constructor(val context: Context) {
} else {
Log.w(TAG, "Was not allowed access to UsageStats, enable in settings.")

// Unused, deprecated in favor of OnboardingActivity
/*
Handler(Looper.getMainLooper()).post {
// Create an alert dialog to inform the user
AlertDialog.Builder(context)
Expand All @@ -106,6 +108,7 @@ class UsageStatsWatcher constructor(val context: Context) {
}
.show()
}
*/
null
}
}
Expand Down
14 changes: 13 additions & 1 deletion mobile/src/main/res/layout/activity_onboarding.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/tabLayout"/>

<Button
android:id="@+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:layout_margin="16dp"
android:visibility="gone"
style="?android:attr/borderlessButtonStyle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>

<Button
android:id="@+id/nextButton"
android:layout_width="wrap_content"
Expand All @@ -26,7 +38,7 @@
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="16dp"
app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 20015bd

Please sign in to comment.