1 /*
2  * Copyright 2021 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 androidx.camera.video;
18 
19 import androidx.core.util.Preconditions;
20 
21 import com.google.auto.value.AutoValue;
22 
23 import org.jspecify.annotations.NonNull;
24 import org.jspecify.annotations.Nullable;
25 
26 import java.io.File;
27 
28 /**
29  * A class providing options for storing the result to a given file.
30  *
31  * <p>The file must be in a path where the application has the write permission.
32  *
33  * <p>To use a {@link android.os.ParcelFileDescriptor} as an output destination instead of a
34  * {@link File}, use {@link FileDescriptorOutputOptions}.
35  */
36 public final class FileOutputOptions extends OutputOptions {
37 
38     private final FileOutputOptionsInternal mFileOutputOptionsInternal;
39 
FileOutputOptions(@onNull FileOutputOptionsInternal fileOutputOptionsInternal)40     FileOutputOptions(@NonNull FileOutputOptionsInternal fileOutputOptionsInternal) {
41         super(fileOutputOptionsInternal);
42         mFileOutputOptionsInternal = fileOutputOptionsInternal;
43     }
44 
45     /** Gets the File instance */
getFile()46     public @NonNull File getFile() {
47         return mFileOutputOptionsInternal.getFile();
48     }
49 
50     @Override
toString()51     public @NonNull String toString() {
52         // Don't use Class.getSimpleName(), class name will be changed by proguard obfuscation.
53         return mFileOutputOptionsInternal.toString().replaceFirst("FileOutputOptionsInternal",
54                 "FileOutputOptions");
55     }
56 
57     @Override
equals(@ullable Object o)58     public boolean equals(@Nullable Object o) {
59         if (this == o) {
60             return true;
61         }
62         if (!(o instanceof FileOutputOptions)) {
63             return false;
64         }
65         return mFileOutputOptionsInternal.equals(
66                 ((FileOutputOptions) o).mFileOutputOptionsInternal);
67     }
68 
69     @Override
hashCode()70     public int hashCode() {
71         return mFileOutputOptionsInternal.hashCode();
72     }
73 
74     /** The builder of the {@link FileOutputOptions} object. */
75     public static final class Builder extends OutputOptions.Builder<FileOutputOptions, Builder> {
76 
77         private final FileOutputOptionsInternal.Builder mInternalBuilder;
78 
79         /**
80          * Creates a builder of the {@link FileOutputOptions} with a file object.
81          *
82          * <p>The file object can be created with a path using the {@link File} APIs. The path
83          * must be seekable and writable.
84          *
85          * @param file the file object.
86          * @see File
87          */
88         @SuppressWarnings("StreamFiles") // FileDescriptor API is in FileDescriptorOutputOptions
Builder(@onNull File file)89         public Builder(@NonNull File file) {
90             super(new AutoValue_FileOutputOptions_FileOutputOptionsInternal.Builder());
91             Preconditions.checkNotNull(file, "File can't be null.");
92             mInternalBuilder = (FileOutputOptionsInternal.Builder) mRootInternalBuilder;
93             mInternalBuilder.setFile(file);
94         }
95 
96         /** Builds the {@link FileOutputOptions} instance. */
97         @Override
build()98         public @NonNull FileOutputOptions build() {
99             return new FileOutputOptions(mInternalBuilder.build());
100         }
101     }
102 
103     @AutoValue
104     abstract static class FileOutputOptionsInternal extends OutputOptions.OutputOptionsInternal {
getFile()105         abstract @NonNull File getFile();
106 
107         @SuppressWarnings("NullableProblems") // Nullable problem in AutoValue generated class
108         @AutoValue.Builder
109         abstract static class Builder extends OutputOptions.OutputOptionsInternal.Builder<Builder> {
setFile(@onNull File file)110             abstract @NonNull Builder setFile(@NonNull File file);
111             @Override
build()112             abstract @NonNull FileOutputOptionsInternal build();
113         }
114     }
115 }
116