1 /* 2 * 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 package com.android.healthconnect.controller.migration 17 18 import android.content.Context 19 import android.os.Bundle 20 import android.util.Log 21 import android.view.LayoutInflater 22 import android.view.View 23 import android.view.ViewGroup 24 import android.widget.Button 25 import android.widget.Toast 26 import androidx.activity.OnBackPressedCallback 27 import androidx.fragment.app.Fragment 28 import androidx.navigation.fragment.findNavController 29 import com.android.healthconnect.controller.R 30 import com.android.healthconnect.controller.shared.Constants.MODULE_UPDATE_NEEDED_SEEN 31 import com.android.healthconnect.controller.shared.Constants.USER_ACTIVITY_TRACKER 32 import com.android.healthconnect.controller.utils.NavigationUtils 33 import com.android.healthconnect.controller.utils.logging.HealthConnectLogger 34 import com.android.healthconnect.controller.utils.logging.MigrationElement 35 import com.android.healthconnect.controller.utils.logging.PageName 36 import dagger.hilt.android.AndroidEntryPoint 37 import javax.inject.Inject 38 39 @AndroidEntryPoint(Fragment::class) 40 class ModuleUpdateRequiredFragment : Hilt_ModuleUpdateRequiredFragment() { 41 42 @Inject lateinit var logger: HealthConnectLogger 43 @Inject lateinit var navigationUtils: NavigationUtils 44 45 companion object { 46 private const val TAG = "ModuleUpdateRequiredFragment" 47 } 48 onCreatenull49 override fun onCreate(savedInstanceState: Bundle?) { 50 super.onCreate(savedInstanceState) 51 52 val onBackPressedCallback = 53 object : OnBackPressedCallback(true) { 54 override fun handleOnBackPressed() { 55 findNavController() 56 .navigate(R.id.action_migrationModuleUpdateNeededFragment_to_homeScreen) 57 requireActivity().finish() 58 } 59 } 60 requireActivity().onBackPressedDispatcher.addCallback(this, onBackPressedCallback) 61 } 62 onCreateViewnull63 override fun onCreateView( 64 inflater: LayoutInflater, 65 container: ViewGroup?, 66 savedInstanceState: Bundle? 67 ): View? { 68 logger.setPageId(PageName.MIGRATION_MODULE_UPDATE_NEEDED_PAGE) 69 return inflater.inflate(R.layout.migration_module_update_needed, container, false) 70 } 71 onViewCreatednull72 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 73 super.onViewCreated(view, savedInstanceState) 74 75 val updateButton = view.findViewById<Button>(R.id.update_button) 76 val cancelButton = view.findViewById<Button>(R.id.cancel_button) 77 logger.logImpression(MigrationElement.MIGRATION_UPDATE_NEEDED_UPDATE_BUTTON) 78 logger.logImpression(MigrationElement.MIGRATION_UPDATE_NEEDED_CANCEL_BUTTON) 79 80 updateButton.setOnClickListener { 81 logger.logInteraction(MigrationElement.MIGRATION_UPDATE_NEEDED_UPDATE_BUTTON) 82 try { 83 navigationUtils.navigate( 84 this, R.id.action_migrationModuleUpdateNeededFragment_to_systemUpdateActivity) 85 } catch (exception: Exception) { 86 Log.e(TAG, "System update activity does not exist", exception) 87 Toast.makeText(requireContext(), R.string.default_error, Toast.LENGTH_SHORT).show() 88 } 89 } 90 91 cancelButton.setOnClickListener { 92 logger.logInteraction(MigrationElement.MIGRATION_UPDATE_NEEDED_CANCEL_BUTTON) 93 val sharedPreferences = 94 requireActivity().getSharedPreferences(USER_ACTIVITY_TRACKER, Context.MODE_PRIVATE) 95 val moduleUpdateSeen = sharedPreferences.getBoolean(MODULE_UPDATE_NEEDED_SEEN, false) 96 97 if (!moduleUpdateSeen) { 98 sharedPreferences.edit().apply { 99 putBoolean(MODULE_UPDATE_NEEDED_SEEN, true) 100 apply() 101 } 102 navigationUtils.navigate( 103 this, R.id.action_migrationModuleUpdateNeededFragment_to_homeScreen) 104 } 105 106 requireActivity().finish() 107 } 108 } 109 onResumenull110 override fun onResume() { 111 super.onResume() 112 logger.logPageImpression() 113 } 114 } 115