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.fragment.app.Fragment
27 import com.android.healthconnect.controller.R
28 import com.android.healthconnect.controller.shared.Constants.INTEGRATION_PAUSED_SEEN_KEY
29 import com.android.healthconnect.controller.shared.Constants.USER_ACTIVITY_TRACKER
30 import com.android.healthconnect.controller.utils.NavigationUtils
31 import com.android.healthconnect.controller.utils.logging.HealthConnectLogger
32 import com.android.healthconnect.controller.utils.logging.MigrationElement
33 import com.android.healthconnect.controller.utils.logging.PageName
34 import dagger.hilt.android.AndroidEntryPoint
35 import javax.inject.Inject
36 
37 @AndroidEntryPoint(Fragment::class)
38 class MigrationPausedFragment : Hilt_MigrationPausedFragment() {
39 
40     @Inject lateinit var logger: HealthConnectLogger
41     @Inject lateinit var navigationUtils: NavigationUtils
42 
43     companion object {
44         private const val TAG = "MigrationPausedFragment"
45     }
46 
onCreateViewnull47     override fun onCreateView(
48         inflater: LayoutInflater,
49         container: ViewGroup?,
50         savedInstanceState: Bundle?
51     ): View? {
52         logger.setPageId(PageName.MIGRATION_PAUSED_PAGE)
53         return inflater.inflate(R.layout.migration_paused, container, false)
54     }
55 
onViewCreatednull56     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
57         super.onViewCreated(view, savedInstanceState)
58 
59         val resumeButton = view.findViewById<Button>(R.id.resume_button)
60         val cancelButton = view.findViewById<Button>(R.id.cancel_button)
61         logger.logImpression(MigrationElement.MIGRATION_PAUSED_CONTINUE_BUTTON)
62         resumeButton.setOnClickListener {
63             logger.logInteraction(MigrationElement.MIGRATION_PAUSED_CONTINUE_BUTTON)
64             try {
65                 navigationUtils.navigate(this, R.id.action_migrationPausedFragment_to_migrationApk)
66             } catch (exception: Exception) {
67                 Log.e(TAG, "Migration APK does not exist", exception)
68                 Toast.makeText(requireContext(), R.string.default_error, Toast.LENGTH_SHORT).show()
69             }
70         }
71 
72         cancelButton.setOnClickListener {
73             logger.logInteraction(MigrationElement.MIGRATION_UPDATE_NEEDED_CANCEL_BUTTON)
74             val sharedPreferences =
75                 requireActivity().getSharedPreferences(USER_ACTIVITY_TRACKER, Context.MODE_PRIVATE)
76             val integrationPausedSeen =
77                 sharedPreferences.getBoolean(INTEGRATION_PAUSED_SEEN_KEY, false)
78             if (!integrationPausedSeen) {
79                 sharedPreferences.edit().apply {
80                     putBoolean(INTEGRATION_PAUSED_SEEN_KEY, true)
81                     apply()
82                 }
83                 navigationUtils.navigate(this, R.id.action_migrationPausedFragment_to_homeScreen)
84             }
85             requireActivity().finish()
86         }
87     }
88 
onResumenull89     override fun onResume() {
90         super.onResume()
91         logger.logPageImpression()
92     }
93 }
94