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.google.android.startop.iorap;
18 
19 import android.os.Parcelable;
20 import android.os.Parcel;
21 
22 import android.annotation.IntDef;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 
27 /**
28  * Result data accompanying a request for {@link com.google.android.startop.iorap.ITaskListener}
29  * callbacks.<br /><br />
30  *
31  * Following {@link com.google.android.startop.iorap.IIorap} method invocation,
32  * iorapd will issue in-order callbacks for that corresponding {@link RequestId}.<br /><br />
33  *
34  * State transitions are as follows: <br /><br />
35  *
36  * <pre>
37  *          ┌─────────────────────────────┐
38  *          │                             ▼
39  *        ┌───────┐     ┌─────────┐     ╔═══════════╗
40  *    ──▶ │ BEGAN │ ──▶ │ ONGOING │ ──▶ ║ COMPLETED ║
41  *        └───────┘     └─────────┘     ╚═══════════╝
42  *          │             │
43  *          │             │
44  *          ▼             │
45  *        ╔═══════╗       │
46  *    ──▶ ║ ERROR ║ ◀─────┘
47  *        ╚═══════╝
48  *
49  * </pre> <!-- system/iorap/docs/binder/TaskResult.dot -->
50  *
51  * @hide
52  */
53 public class TaskResult implements Parcelable {
54 
55     public static final int STATE_BEGAN = 0;
56     public static final int STATE_ONGOING = 1;
57     public static final int STATE_COMPLETED = 2;
58     public static final int STATE_ERROR = 3;
59     private static final int STATE_MAX = STATE_ERROR;
60 
61     /** @hide */
62     @IntDef(flag = true, prefix = { "STATE_" }, value = {
63             STATE_BEGAN,
64             STATE_ONGOING,
65             STATE_COMPLETED,
66             STATE_ERROR,
67     })
68     @Retention(RetentionPolicy.SOURCE)
69     public @interface State {}
70 
71     @State public final int state;
72 
73     @Override
toString()74     public String toString() {
75         return String.format("{state: %d}", state);
76     }
77 
78     @Override
equals(Object other)79     public boolean equals(Object other) {
80         if (this == other) {
81             return true;
82         } else if (other instanceof TaskResult) {
83             return equals((TaskResult) other);
84         }
85         return false;
86     }
87 
equals(TaskResult other)88     private boolean equals(TaskResult other) {
89         return state == other.state;
90     }
91 
TaskResult(@tate int state)92     public TaskResult(@State int state) {
93         this.state = state;
94 
95         checkConstructorArguments();
96     }
97 
checkConstructorArguments()98     private void checkConstructorArguments() {
99         CheckHelpers.checkStateInRange(state, STATE_MAX);
100     }
101 
102     //<editor-fold desc="Binder boilerplate">
103     @Override
writeToParcel(Parcel out, int flags)104     public void writeToParcel(Parcel out, int flags) {
105         out.writeInt(state);
106     }
107 
TaskResult(Parcel in)108     private TaskResult(Parcel in) {
109         state = in.readInt();
110 
111         checkConstructorArguments();
112     }
113 
114     @Override
describeContents()115     public int describeContents() {
116         return 0;
117     }
118 
119     public static final Parcelable.Creator<TaskResult> CREATOR
120             = new Parcelable.Creator<TaskResult>() {
121         public TaskResult createFromParcel(Parcel in) {
122             return new TaskResult(in);
123         }
124 
125         public TaskResult[] newArray(int size) {
126             return new TaskResult[size];
127         }
128     };
129     //</editor-fold>
130 }
131