1 /*
2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  *
17  */
18 
19 package com.android.healthconnect.controller.onboarding
20 
21 import android.app.Activity
22 import android.content.Context
23 import android.content.Intent
24 import android.os.Bundle
25 import android.view.WindowManager
26 import android.widget.Button
27 import androidx.fragment.app.FragmentActivity
28 import com.android.healthconnect.controller.R
29 import com.android.healthconnect.controller.shared.Constants.ONBOARDING_SHOWN_PREF_KEY
30 import com.android.healthconnect.controller.shared.Constants.USER_ACTIVITY_TRACKER
31 import com.android.healthconnect.controller.utils.logging.HealthConnectLogger
32 import com.android.healthconnect.controller.utils.logging.OnboardingElement
33 import com.android.healthconnect.controller.utils.logging.PageName
34 import dagger.hilt.android.AndroidEntryPoint
35 import javax.inject.Inject
36 
37 /** Onboarding activity for Health Connect. */
38 @AndroidEntryPoint(FragmentActivity::class)
39 class OnboardingActivity : Hilt_OnboardingActivity() {
40 
41     /** Companion object for OnboardingActivity. */
42     companion object {
43         private const val TARGET_ACTIVITY_INTENT = "ONBOARDING_TARGET_ACTIVITY_INTENT"
44 
shouldRedirectToOnboardingActivitynull45         fun shouldRedirectToOnboardingActivity(activity: Activity): Boolean {
46             val sharedPreference =
47                 activity.getSharedPreferences(USER_ACTIVITY_TRACKER, Context.MODE_PRIVATE)
48             val previouslyOpened = sharedPreference.getBoolean(ONBOARDING_SHOWN_PREF_KEY, false)
49             if (!previouslyOpened) {
50                 return true
51             }
52             return false
53         }
54 
createIntentnull55         fun createIntent(context: Context, targetIntent: Intent? = null): Intent {
56             val flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NO_ANIMATION
57 
58             return Intent(context, OnboardingActivity::class.java).apply {
59                 addFlags(flags)
60                 if (targetIntent != null) {
61                     putExtra(TARGET_ACTIVITY_INTENT, targetIntent)
62                 }
63             }
64         }
65     }
66 
67     @Inject lateinit var logger: HealthConnectLogger
68 
69     private var targetIntent: Intent? = null
70 
onCreatenull71     override fun onCreate(savedInstanceState: Bundle?) {
72         super.onCreate(savedInstanceState)
73         // This flag ensures a non system app cannot show an overlay on Health Connect. b/313425281
74         window.addSystemFlags(WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS)
75         setContentView(R.layout.onboarding_screen)
76 
77         if (intent.hasExtra(TARGET_ACTIVITY_INTENT)) {
78             targetIntent =
79                 intent.getParcelableExtra(
80                     TARGET_ACTIVITY_INTENT,
81                 )
82         }
83 
84         logger.setPageId(PageName.ONBOARDING_PAGE)
85 
86         val sharedPreference = getSharedPreferences(USER_ACTIVITY_TRACKER, Context.MODE_PRIVATE)
87         val goBackButton = findViewById<Button>(R.id.go_back_button)
88         val getStartedButton = findViewById<Button>(R.id.get_started_button)
89         logger.logImpression(OnboardingElement.ONBOARDING_GO_BACK_BUTTON)
90         logger.logImpression(OnboardingElement.ONBOARDING_COMPLETED_BUTTON)
91 
92         goBackButton.setOnClickListener {
93             logger.logInteraction(OnboardingElement.ONBOARDING_GO_BACK_BUTTON)
94             setResult(Activity.RESULT_CANCELED)
95             finish()
96         }
97         getStartedButton.setOnClickListener {
98             logger.logInteraction(OnboardingElement.ONBOARDING_COMPLETED_BUTTON)
99             val editor = sharedPreference.edit()
100             editor.putBoolean(ONBOARDING_SHOWN_PREF_KEY, true)
101             editor.apply()
102             if (targetIntent == null) {
103                 setResult(Activity.RESULT_OK)
104             } else {
105                 startActivity(targetIntent)
106             }
107             finish()
108         }
109     }
110 
onResumenull111     override fun onResume() {
112         super.onResume()
113         logger.logPageImpression()
114     }
115 }
116