Merge "Add TrascodeHelper class"
diff --git a/src/com/android/providers/media/TranscodeHelper.java b/src/com/android/providers/media/TranscodeHelper.java
new file mode 100644
index 0000000..a588ee4
--- /dev/null
+++ b/src/com/android/providers/media/TranscodeHelper.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.providers.media;
+
+import android.annotation.NonNull;
+import android.os.Environment;
+
+import com.android.providers.media.util.FileUtils;
+
+import java.io.File;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class TranscodeHelper {
+    /**
+     * Regex that matches path of transcode file. The regex only
+     * matches emulated volume, for files in other volumes we don't
+     * seamlessly transcode.
+     */
+    private static final Pattern PATTERN_TRANSCODE_PATH = Pattern.compile(
+            "(?i)^/storage/emulated/(?:[0-9]+)/\\.transcode/(?:\\d+)$");
+
+    private static final String DIRECTORY_TRANSCODE = ".transcode";
+
+    /**
+     * @return true if the file path matches transcode file path.
+     */
+    public static boolean isTranscodeFile(@NonNull String path) {
+        final Matcher matcher = PATTERN_TRANSCODE_PATH.matcher(path);
+        return matcher.matches();
+    }
+
+    @NonNull
+    public static File getTranscodeDirectory() {
+        final File transcodeDirectory =
+                FileUtils.buildPath(Environment.getExternalStorageDirectory(), DIRECTORY_TRANSCODE);
+        transcodeDirectory.mkdirs();
+        return transcodeDirectory;
+    }
+
+    /**
+     * @return transcode file's path for given {@code rowId}
+     */
+    @NonNull
+    public static String getTranscodePath(long rowId) {
+        return new File(getTranscodeDirectory(), String.valueOf(rowId)).getAbsolutePath();
+    }
+}