summaryrefslogtreecommitdiff
path: root/apk/src/com/android/healthconnect/controller/migration/ModuleUpdateRequiredFragment.kt
blob: b5f144b5983304dd82248cfaf22802558f37edab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.healthconnect.controller.migration

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.android.healthconnect.controller.R
import com.android.healthconnect.controller.utils.NavigationUtils
import com.android.healthconnect.controller.utils.logging.HealthConnectLogger
import com.android.healthconnect.controller.utils.logging.MigrationElement
import com.android.healthconnect.controller.utils.logging.PageName
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint(Fragment::class)
class ModuleUpdateRequiredFragment : Hilt_ModuleUpdateRequiredFragment() {

    @Inject lateinit var logger: HealthConnectLogger
    @Inject lateinit var navigationUtils: NavigationUtils

    companion object {
        private const val TAG = "ModuleUpdateRequiredFragment"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val onBackPressedCallback =
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    findNavController()
                        .navigate(R.id.action_migrationModuleUpdateNeededFragment_to_homeScreen)
                    requireActivity().finish()
                }
            }
        requireActivity().onBackPressedDispatcher.addCallback(this, onBackPressedCallback)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        logger.setPageId(PageName.MIGRATION_MODULE_UPDATE_NEEDED_PAGE)
        return inflater.inflate(R.layout.migration_module_update_needed, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val updateButton = view.findViewById<Button>(R.id.update_button)
        val cancelButton = view.findViewById<Button>(R.id.cancel_button)
        logger.logImpression(MigrationElement.MIGRATION_UPDATE_NEEDED_UPDATE_BUTTON)
        logger.logImpression(MigrationElement.MIGRATION_UPDATE_NEEDED_CANCEL_BUTTON)

        updateButton.setOnClickListener {
            logger.logInteraction(MigrationElement.MIGRATION_UPDATE_NEEDED_UPDATE_BUTTON)
            try {
                navigationUtils.navigate(
                    this, R.id.action_migrationModuleUpdateNeededFragment_to_systemUpdateActivity)
            } catch (exception: Exception) {
                Log.e(TAG, "System update activity does not exist", exception)
                Toast.makeText(requireContext(), R.string.default_error, Toast.LENGTH_SHORT).show()
            }
        }

        cancelButton.setOnClickListener {
            logger.logInteraction(MigrationElement.MIGRATION_UPDATE_NEEDED_CANCEL_BUTTON)
            val sharedPreferences =
                requireActivity()
                    .getSharedPreferences("USER_ACTIVITY_TRACKER", Context.MODE_PRIVATE)
            val moduleUpdateSeen =
                sharedPreferences.getBoolean(getString(R.string.module_update_needed_seen), false)

            if (!moduleUpdateSeen) {
                sharedPreferences.edit().apply {
                    putBoolean(getString(R.string.module_update_needed_seen), true)
                    apply()
                }
                navigationUtils.navigate(
                    this, R.id.action_migrationModuleUpdateNeededFragment_to_homeScreen)
            }

            requireActivity().finish()
        }
    }

    override fun onResume() {
        super.onResume()
        logger.logPageImpression()
    }
}