Aurimas Liutikas | 88c7ff1 | 2023-08-10 12:42:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.backup.internal; |
| 18 | |
| 19 | import static com.android.server.backup.BackupManagerService.TAG; |
| 20 | |
| 21 | import android.content.pm.PackageInfo; |
| 22 | import android.util.Slog; |
| 23 | |
| 24 | import com.android.server.backup.TransportManager; |
| 25 | import com.android.server.backup.UserBackupManagerService; |
| 26 | import com.android.server.backup.transport.BackupTransportClient; |
| 27 | import com.android.server.backup.transport.TransportConnection; |
| 28 | |
| 29 | import java.io.File; |
| 30 | |
| 31 | public class PerformClearTask implements Runnable { |
| 32 | private final UserBackupManagerService mBackupManagerService; |
| 33 | private final TransportManager mTransportManager; |
| 34 | private final TransportConnection mTransportConnection; |
| 35 | private final PackageInfo mPackage; |
| 36 | private final OnTaskFinishedListener mListener; |
| 37 | |
| 38 | PerformClearTask(UserBackupManagerService backupManagerService, |
| 39 | TransportConnection transportConnection, PackageInfo packageInfo, |
| 40 | OnTaskFinishedListener listener) { |
| 41 | mBackupManagerService = backupManagerService; |
| 42 | mTransportManager = backupManagerService.getTransportManager(); |
| 43 | mTransportConnection = transportConnection; |
| 44 | mPackage = packageInfo; |
| 45 | mListener = listener; |
| 46 | } |
| 47 | |
| 48 | public void run() { |
| 49 | String callerLogString = "PerformClearTask.run()"; |
| 50 | BackupTransportClient transport = null; |
| 51 | try { |
| 52 | // Clear the on-device backup state to ensure a full backup next time |
| 53 | String transportDirName = |
| 54 | mTransportManager.getTransportDirName( |
| 55 | mTransportConnection.getTransportComponent()); |
| 56 | File stateDir = new File(mBackupManagerService.getBaseStateDir(), transportDirName); |
| 57 | File stateFile = new File(stateDir, mPackage.packageName); |
| 58 | stateFile.delete(); |
| 59 | |
| 60 | transport = mTransportConnection.connectOrThrow(callerLogString); |
| 61 | // Tell the transport to remove all the persistent storage for the app |
| 62 | // TODO - need to handle failures |
| 63 | transport.clearBackupData(mPackage); |
| 64 | } catch (Exception e) { |
| 65 | Slog.e(TAG, "Transport threw clearing data for " + mPackage + ": " + e.getMessage()); |
| 66 | } finally { |
| 67 | if (transport != null) { |
| 68 | try { |
| 69 | // TODO - need to handle failures |
| 70 | transport.finishBackup(); |
| 71 | } catch (Exception e) { |
| 72 | // Nothing we can do here, alas |
| 73 | Slog.e(TAG, "Unable to mark clear operation finished: " + e.getMessage()); |
| 74 | } |
| 75 | } |
| 76 | mListener.onFinished(callerLogString); |
| 77 | // Last but not least, release the cpu |
| 78 | mBackupManagerService.getWakelock().release(); |
| 79 | } |
| 80 | } |
| 81 | } |