VideoEditorFactory.java revision 048449ebfa3f42e1431338785f225d25125f294e
1/*
2 * Copyright (C) 2010 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
17package android.media.videoeditor;
18
19import java.io.File;
20import java.io.FileNotFoundException;
21import java.io.IOException;
22import java.lang.reflect.Constructor;
23import java.lang.reflect.InvocationTargetException;
24
25
26/**
27 * The VideoEditorFactory class must be used to instantiate VideoEditor objects
28 * by creating a new project {@link #create(String)} or by loading an
29 * existing project {@link #load(String)}.
30 * {@hide}
31 */
32public class VideoEditorFactory {
33    // VideoEditor implementation classes
34    public static final String TEST_CLASS_IMPLEMENTATION
35            = "android.media.videoeditor.VideoEditorTestImpl";
36    public static final String DEFAULT_CLASS_IMPLEMENTATION
37            = "android.media.videoeditor.VideoEditorImpl";
38
39    /**
40     * This is the factory method for creating a new VideoEditor instance.
41     *
42     * @param projectPath The path where all VideoEditor internal
43     *            files are stored. When a project is deleted the application is
44     *            responsible for deleting the path and its contents.
45     * @param className The implementation class name
46     *
47     * @return The VideoEditor instance
48     *
49     * @throws IOException if path does not exist or if the path can
50     *             not be accessed in read/write mode
51     * @throws IllegalStateException if a previous VideoEditor instance has not
52     *             been released
53     * @throws ClassNotFoundException, NoSuchMethodException,
54     *             InvocationTargetException, IllegalAccessException,
55     *             InstantiationException if the implementation class cannot
56     *             be instantiated.
57     */
58    public static VideoEditor create(String projectPath, String className) throws IOException,
59            ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
60            IllegalAccessException, InstantiationException {
61        // If the project path does not exist create it
62        final File dir = new File(projectPath);
63        if (!dir.exists()) {
64            if (!dir.mkdirs()) {
65                throw new FileNotFoundException("Cannot create project path: " + projectPath);
66            }
67        }
68
69        Class<?> cls = Class.forName(className);
70        Class<?> partypes[] = new Class[1];
71        partypes[0] = String.class;
72        Constructor<?> ct = cls.getConstructor(partypes);
73        Object arglist[] = new Object[1];
74        arglist[0] = projectPath;
75
76        return (VideoEditor)ct.newInstance(arglist);
77    }
78
79    /**
80     * This is the factory method for instantiating a VideoEditor from the
81     * internal state previously saved with the
82     * {@link VideoEditor#save(String)} method.
83     *
84     * @param projectPath The path where all VideoEditor internal files
85     *            are stored. When a project is deleted the application is
86     *            responsible for deleting the path and its contents.
87     * @param generatePreview if set to true the
88     *      {@link MediaEditor#generatePreview()} will be called internally to
89     *      generate any needed transitions.
90     *
91     * @return The VideoEditor instance
92     *
93     * @throws IOException if path does not exist or if the path can
94     *             not be accessed in read/write mode or if one of the resource
95     *             media files cannot be retrieved
96     * @throws IllegalStateException if a previous VideoEditor instance has not
97     *             been released
98     */
99    public static VideoEditor load(String projectPath, boolean generatePreview) throws IOException {
100        final VideoEditorTestImpl videoEditor = new VideoEditorTestImpl(projectPath);
101        if (generatePreview) {
102            videoEditor.generatePreview();
103        }
104        return videoEditor;
105    }
106}
107