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.restore;
18 
19 import java.util.concurrent.atomic.AtomicBoolean;
20 import java.util.concurrent.atomic.AtomicInteger;
21 
22 /**
23  * Restore infrastructure.
24  */
25 public abstract class RestoreEngine {
26 
27     private static final String TAG = "RestoreEngine";
28 
29     public static final int SUCCESS = 0;
30     public static final int TARGET_FAILURE = -2;
31     public static final int TRANSPORT_FAILURE = -3;
32 
33     private final AtomicBoolean mRunning = new AtomicBoolean(false);
34     private final AtomicInteger mResult = new AtomicInteger(SUCCESS);
35 
isRunning()36     public boolean isRunning() {
37         return mRunning.get();
38     }
39 
setRunning(boolean stillRunning)40     public void setRunning(boolean stillRunning) {
41         synchronized (mRunning) {
42             mRunning.set(stillRunning);
43             mRunning.notifyAll();
44         }
45     }
46 
waitForResult()47     public int waitForResult() {
48         synchronized (mRunning) {
49             while (isRunning()) {
50                 try {
51                     mRunning.wait();
52                 } catch (InterruptedException e) {
53                 }
54             }
55         }
56         return getResult();
57     }
58 
getResult()59     public int getResult() {
60         return mResult.get();
61     }
62 
setResult(int result)63     public void setResult(int result) {
64         mResult.set(result);
65     }
66 
67     // TODO: abstract restore state and APIs
68 }
69