1 /*
<lambda>null2  * Copyright (C) 2023 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  *      http://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.data
20 
21 import android.os.Bundle
22 import android.view.WindowManager
23 import androidx.activity.viewModels
24 import androidx.navigation.findNavController
25 import androidx.navigation.fragment.NavHostFragment
26 import com.android.healthconnect.controller.R
27 import com.android.healthconnect.controller.migration.MigrationActivity.Companion.maybeRedirectToMigrationActivity
28 import com.android.healthconnect.controller.migration.MigrationActivity.Companion.maybeShowWhatsNewDialog
29 import com.android.healthconnect.controller.migration.MigrationViewModel
30 import com.android.healthconnect.controller.migration.MigrationViewModel.MigrationFragmentState
31 import com.android.healthconnect.controller.migration.api.MigrationRestoreState.MigrationUiState
32 import com.android.healthconnect.controller.navigation.DestinationChangedListener
33 import com.android.healthconnect.controller.utils.FeatureUtils
34 import com.android.settingslib.collapsingtoolbar.CollapsingToolbarBaseActivity
35 import dagger.hilt.android.AndroidEntryPoint
36 import javax.inject.Inject
37 
38 /** Entry point activity for Health Connect Data Management controllers. */
39 @AndroidEntryPoint(CollapsingToolbarBaseActivity::class)
40 class DataManagementActivity : Hilt_DataManagementActivity() {
41 
42     @Inject lateinit var featureUtils: FeatureUtils
43 
44     private val migrationViewModel: MigrationViewModel by viewModels()
45 
46     override fun onCreate(savedInstanceState: Bundle?) {
47         super.onCreate(savedInstanceState)
48 
49         // This flag ensures a non system app cannot show an overlay on Health Connect. b/313425281
50         window.addSystemFlags(WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS)
51 
52         setContentView(R.layout.activity_data_management)
53         if (savedInstanceState == null && featureUtils.isNewInformationArchitectureEnabled()) {
54             updateNavGraphToNewIA()
55         }
56 
57         val currentMigrationState = migrationViewModel.getCurrentMigrationUiState()
58         if (maybeRedirectToMigrationActivity(this, currentMigrationState)) {
59             return
60         }
61 
62         migrationViewModel.migrationState.observe(this) { migrationState ->
63             when (migrationState) {
64                 is MigrationFragmentState.WithData -> {
65                     if (migrationState.migrationRestoreState.migrationUiState ==
66                         MigrationUiState.COMPLETE) {
67                         maybeShowWhatsNewDialog(this)
68                     }
69                 }
70                 else -> {
71                     // do nothing
72                 }
73             }
74         }
75     }
76 
77     private fun updateNavGraphToNewIA() {
78         val navRes = R.navigation.data_nav_graph_new_ia
79         val finalHost = NavHostFragment.create(navRes)
80         supportFragmentManager
81             .beginTransaction()
82             .replace(R.id.nav_host_fragment, finalHost)
83             .setPrimaryNavigationFragment(finalHost)
84             .commit()
85     }
86 
87     override fun onStart() {
88         super.onStart()
89         findNavController(R.id.nav_host_fragment)
90             .addOnDestinationChangedListener(DestinationChangedListener(this))
91     }
92 
93     override fun onResume() {
94         super.onResume()
95         val currentMigrationState = migrationViewModel.getCurrentMigrationUiState()
96 
97         if (maybeRedirectToMigrationActivity(this, currentMigrationState)) {
98             return
99         }
100     }
101 
102     override fun onBackPressed() {
103         val navController = findNavController(R.id.nav_host_fragment)
104         if (!navController.popBackStack()) {
105             finish()
106         }
107     }
108 
109     override fun onNavigateUp(): Boolean {
110         val navController = findNavController(R.id.nav_host_fragment)
111         if (!navController.popBackStack()) {
112             finish()
113         }
114         return true
115     }
116 }
117