1 /*
2  * Copyright (C) 2018 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.keyvalue;
18 
19 import android.app.backup.BackupTransport;
20 
21 import com.android.internal.util.Preconditions;
22 
23 /**
24  * The key-value backup task has failed, no more packages will be processed and we shouldn't attempt
25  * any more backups now. These can be caused by transport failures (as opposed to agent failures).
26  *
27  * @see KeyValueBackupTask
28  * @see AgentException
29  */
30 class TaskException extends BackupException {
31     private static final int DEFAULT_STATUS = BackupTransport.TRANSPORT_ERROR;
32 
stateCompromised()33     static TaskException stateCompromised() {
34         return new TaskException(/* stateCompromised */ true, DEFAULT_STATUS);
35     }
36 
stateCompromised(Exception cause)37     static TaskException stateCompromised(Exception cause) {
38         if (cause instanceof TaskException) {
39             TaskException exception = (TaskException) cause;
40             return new TaskException(cause, /* stateCompromised */ true, exception.getStatus());
41         }
42         return new TaskException(cause, /* stateCompromised */ true, DEFAULT_STATUS);
43     }
44 
forStatus(int status)45     static TaskException forStatus(int status) {
46         Preconditions.checkArgument(
47                 status != BackupTransport.TRANSPORT_OK, "Exception based on TRANSPORT_OK");
48         return new TaskException(/* stateCompromised */ false, status);
49     }
50 
causedBy(Exception cause)51     static TaskException causedBy(Exception cause) {
52         if (cause instanceof TaskException) {
53             return (TaskException) cause;
54         }
55         return new TaskException(cause, /* stateCompromised */ false, DEFAULT_STATUS);
56     }
57 
create()58     static TaskException create() {
59         return new TaskException(/* stateCompromised */ false, DEFAULT_STATUS);
60     }
61 
62     private final boolean mStateCompromised;
63     private final int mStatus;
64 
TaskException(Exception cause, boolean stateCompromised, int status)65     private TaskException(Exception cause, boolean stateCompromised, int status) {
66         super(cause);
67         mStateCompromised = stateCompromised;
68         mStatus = status;
69     }
70 
TaskException(boolean stateCompromised, int status)71     private TaskException(boolean stateCompromised, int status) {
72         mStateCompromised = stateCompromised;
73         mStatus = status;
74     }
75 
isStateCompromised()76     boolean isStateCompromised() {
77         return mStateCompromised;
78     }
79 
getStatus()80     int getStatus() {
81         return mStatus;
82     }
83 }
84