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.APP_UPDATE_NEEDED_SEEN 31 import com.android.healthconnect.controller.shared.Constants.USER_ACTIVITY_TRACKER 32 import com.android.healthconnect.controller.utils.AppStoreUtils 33 import com.android.healthconnect.controller.utils.NavigationUtils 34 import com.android.healthconnect.controller.utils.logging.HealthConnectLogger 35 import com.android.healthconnect.controller.utils.logging.MigrationElement 36 import com.android.healthconnect.controller.utils.logging.PageName 37 import dagger.hilt.android.AndroidEntryPoint 38 import javax.inject.Inject 39 40 @AndroidEntryPoint(Fragment::class) 41 class AppUpdateRequiredFragment : Hilt_AppUpdateRequiredFragment() { 42 43 @Inject lateinit var logger: HealthConnectLogger 44 @Inject lateinit var appStoreUtils: AppStoreUtils 45 @Inject lateinit var navigationUtils: NavigationUtils 46 47 companion object { 48 private const val TAG = "AppUpdateFragment" 49 const val HC_PACKAGE_NAME_CONFIG_NAME = 50 "android:string/config_healthConnectMigratorPackageName" 51 } 52 53 private lateinit var onBackPressedCallback: OnBackPressedCallback 54 onCreatenull55 override fun onCreate(savedInstanceState: Bundle?) { 56 super.onCreate(savedInstanceState) 57 58 onBackPressedCallback = 59 object : OnBackPressedCallback(true) { 60 override fun handleOnBackPressed() { 61 findNavController() 62 .navigate(R.id.action_migrationAppUpdateNeededFragment_to_homeScreen) 63 requireActivity().finish() 64 } 65 } 66 requireActivity().onBackPressedDispatcher.addCallback(this, onBackPressedCallback) 67 } 68 onCreateViewnull69 override fun onCreateView( 70 inflater: LayoutInflater, 71 container: ViewGroup?, 72 savedInstanceState: Bundle? 73 ): View? { 74 logger.setPageId(PageName.MIGRATION_APP_UPDATE_NEEDED_PAGE) 75 return inflater.inflate(R.layout.migration_app_update_needed, container, false) 76 } 77 onViewCreatednull78 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 79 super.onViewCreated(view, savedInstanceState) 80 81 val updateButton = view.findViewById<Button>(R.id.update_button) 82 val cancelButton = view.findViewById<Button>(R.id.cancel_button) 83 logger.logImpression(MigrationElement.MIGRATION_UPDATE_NEEDED_UPDATE_BUTTON) 84 logger.logImpression(MigrationElement.MIGRATION_UPDATE_NEEDED_CANCEL_BUTTON) 85 86 updateButton.setOnClickListener { 87 logger.logInteraction(MigrationElement.MIGRATION_UPDATE_NEEDED_UPDATE_BUTTON) 88 try { 89 val packageName = 90 getString(resources.getIdentifier(HC_PACKAGE_NAME_CONFIG_NAME, null, null)) 91 val intent = appStoreUtils.getAppStoreLink(packageName) 92 navigationUtils.startActivity(this, intent!!) 93 } catch (exception: Exception) { 94 Log.e(TAG, "App store activity does not exist", exception) 95 Toast.makeText(requireContext(), R.string.default_error, Toast.LENGTH_SHORT).show() 96 } 97 } 98 99 cancelButton.setOnClickListener { 100 logger.logInteraction(MigrationElement.MIGRATION_UPDATE_NEEDED_CANCEL_BUTTON) 101 val sharedPreferences = 102 requireActivity().getSharedPreferences(USER_ACTIVITY_TRACKER, Context.MODE_PRIVATE) 103 val appUpdateSeen = sharedPreferences.getBoolean(APP_UPDATE_NEEDED_SEEN, false) 104 if (!appUpdateSeen) { 105 sharedPreferences.edit().apply { 106 putBoolean(APP_UPDATE_NEEDED_SEEN, true) 107 apply() 108 } 109 navigationUtils.navigate( 110 this, R.id.action_migrationAppUpdateNeededFragment_to_homeScreen) 111 } 112 requireActivity().finish() 113 } 114 } 115 onResumenull116 override fun onResume() { 117 super.onResume() 118 logger.logPageImpression() 119 } 120 } 121