• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.adservices.shell;
18 
19 import android.annotation.Nullable;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import java.util.Objects;
24 
25 /**
26  * Represents the response from the runShellCommand API.
27  *
28  * @hide
29  */
30 public class ShellCommandResult implements Parcelable {
31 
32     private static final int RESULT_OK = 0;
33 
34     private final int mResultCode;
35     @Nullable private final String mOut;
36     @Nullable private final String mErr;
37 
ShellCommandResult(int resultCode, @Nullable String out, @Nullable String err)38     private ShellCommandResult(int resultCode, @Nullable String out, @Nullable String err) {
39         mResultCode = resultCode;
40         mOut = out;
41         mErr = err;
42     }
43 
ShellCommandResult(Parcel in)44     private ShellCommandResult(Parcel in) {
45         this(in.readInt(), in.readString(), in.readString());
46     }
47 
ShellCommandResult(Builder builder)48     private ShellCommandResult(Builder builder) {
49         this(builder.mResultCode, builder.mOut, builder.mErr);
50     }
51 
52     public static final Creator<ShellCommandResult> CREATOR =
53             new Parcelable.Creator<>() {
54                 @Override
55                 public ShellCommandResult createFromParcel(Parcel in) {
56                     Objects.requireNonNull(in);
57                     return new ShellCommandResult(in);
58                 }
59 
60                 @Override
61                 public ShellCommandResult[] newArray(int size) {
62                     return new ShellCommandResult[size];
63                 }
64             };
65 
66     @Override
describeContents()67     public int describeContents() {
68         return 0;
69     }
70 
71     @Override
writeToParcel(Parcel dest, int flags)72     public void writeToParcel(Parcel dest, int flags) {
73         Objects.requireNonNull(dest);
74         dest.writeInt(mResultCode);
75         dest.writeString(mOut);
76         dest.writeString(mErr);
77     }
78 
79     /** Returns the command status. */
getResultCode()80     public int getResultCode() {
81         return mResultCode;
82     }
83 
84     /** Returns {@code true} if {@link #getResultCode} is greater than equal to 0. */
isSuccess()85     public boolean isSuccess() {
86         return getResultCode() >= 0;
87     }
88 
89     /** Returns the output of the shell command result. */
90     @Nullable
getOut()91     public String getOut() {
92         return mOut;
93     }
94 
95     /** Returns the error message associated with this response. */
96     @Nullable
getErr()97     public String getErr() {
98         return mErr;
99     }
100 
101     /**
102      * Builder for {@link ShellCommandResult}.
103      *
104      * @hide
105      */
106     public static final class Builder {
107         private int mResultCode = RESULT_OK;
108         @Nullable private String mOut;
109         @Nullable private String mErr;
110 
Builder()111         public Builder() {}
112 
113         /** Sets the Status Code. */
setResultCode(int resultCode)114         public Builder setResultCode(int resultCode) {
115             mResultCode = resultCode;
116             return this;
117         }
118 
119         /** Sets the shell command output in case of success. */
setOut(@ullable String out)120         public Builder setOut(@Nullable String out) {
121             mOut = out;
122             return this;
123         }
124 
125         /** Sets the error message in case of command failure. */
setErr(@ullable String err)126         public Builder setErr(@Nullable String err) {
127             mErr = err;
128             return this;
129         }
130 
131         /** Builds a {@link ShellCommandResult} object. */
build()132         public ShellCommandResult build() {
133             return new ShellCommandResult(this);
134         }
135     }
136 }
137