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 
17 package com.android.server.healthconnect.migration;
18 
19 import android.annotation.NonNull;
20 import android.health.connect.HealthConnectDataState;
21 import android.util.Slog;
22 
23 import com.android.server.healthconnect.storage.TransactionManager;
24 import com.android.server.healthconnect.storage.datatypehelpers.MigrationEntityHelper;
25 
26 /**
27  * Deletes temporary data after migration is complete.
28  *
29  * @hide
30  */
31 public final class MigrationCleaner {
32 
33     private static final String LOG_TAG = "MigrationCleaner";
34 
35     private final MigrationEntityHelper mMigrationEntityHelper;
36     private final TransactionManager mTransactionManager;
37     private final PriorityMigrationHelper mPriorityMigrationHelper;
38 
MigrationCleaner( @onNull TransactionManager transactionManager, @NonNull PriorityMigrationHelper priorityMigrationHelper)39     public MigrationCleaner(
40             @NonNull TransactionManager transactionManager,
41             @NonNull PriorityMigrationHelper priorityMigrationHelper) {
42         mMigrationEntityHelper = new MigrationEntityHelper();
43         mTransactionManager = transactionManager;
44         mPriorityMigrationHelper = priorityMigrationHelper;
45     }
46 
47     /** Attaches this migration cleaner to the provided {@link MigrationStateManager}. */
attachTo(@onNull MigrationStateManager migrationStateManager)48     public void attachTo(@NonNull MigrationStateManager migrationStateManager) {
49         migrationStateManager.addStateChangedListener(this::onMigrationStateChanged);
50     }
51 
onMigrationStateChanged(@ealthConnectDataState.DataMigrationState int state)52     private void onMigrationStateChanged(@HealthConnectDataState.DataMigrationState int state) {
53         if (state == HealthConnectDataState.MIGRATION_STATE_COMPLETE) {
54             clean();
55         }
56     }
57 
clean()58     private void clean() {
59         try {
60             mMigrationEntityHelper.clearData(mTransactionManager);
61         } catch (RuntimeException e) {
62             Slog.e(LOG_TAG, "Error clearing MigrationEntityHelper", e);
63         }
64 
65         try {
66             mPriorityMigrationHelper.clearData(mTransactionManager);
67         } catch (RuntimeException e) {
68             Slog.e(LOG_TAG, "Error clearing PriorityMigrationHelper", e);
69         }
70     }
71 }
72