1/*
2 * Copyright (C) 2014 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 com.android.camera.one;
18
19import java.io.File;
20import java.text.SimpleDateFormat;
21import java.util.Date;
22import java.util.TimeZone;
23
24/**
25 * A common abstract {@link OneCamera} implementation that contains some utility
26 * functions and plumbing we don't want every sub-class of {@link OneCamera} to
27 * duplicate. Hence all {@link OneCamera} implementations should sub-class this
28 * class instead.
29 */
30public abstract class AbstractOneCamera implements OneCamera {
31    protected CameraErrorListener mCameraErrorListener;
32    protected FocusStateListener mFocusStateListener;
33    protected ReadyStateChangedListener mReadyStateChangedListener;
34
35    /**
36     * Number of characters from the end of the device serial number used to
37     * construct folder names for debugging output.
38     */
39    static final int DEBUG_FOLDER_SERIAL_LENGTH = 4;
40
41    @Override
42    public final void setCameraErrorListener(CameraErrorListener listener) {
43        mCameraErrorListener = listener;
44    }
45
46    @Override
47    public final void setFocusStateListener(FocusStateListener listener) {
48        mFocusStateListener = listener;
49    }
50
51    @Override
52    public void setReadyStateChangedListener(ReadyStateChangedListener listener) {
53        mReadyStateChangedListener = listener;
54    }
55
56    /**
57     * Create a directory we can use to store debugging information during Gcam
58     * captures.
59     * <br />
60     * The directory created is [root]/[folderName]/SSSS_YYYYMMDD_HHMMSS_XXX,
61     * where 'SSSS' are the last 'DEBUG_FOLDER_SERIAL_LENGTH' digits of the
62     * devices serial number, and 'XXX' are milliseconds of the timestamp.
63     *
64     * @param root the root into which we put a session-specific sub-directory.
65     * @param folderName the sub-folder within 'root' where the data should be
66     *            put.
67     * @return The session-specific directory (absolute path) into which to
68     *         store debug information.
69     */
70    protected static String makeDebugDir(File root, String folderName) {
71        if (root == null) {
72            return null;
73        }
74        if (!root.exists() || !root.isDirectory()) {
75            throw new RuntimeException("Gcam debug directory not valid or doesn't exist: "
76                    + root.getAbsolutePath());
77        }
78
79        String serialSubstring = "";
80        String serial = android.os.Build.SERIAL;
81        if (serial != null) {
82            int length = serial.length();
83
84            if (length > DEBUG_FOLDER_SERIAL_LENGTH) {
85                serialSubstring = serial.substring(length - DEBUG_FOLDER_SERIAL_LENGTH, length);
86            } else {
87                serialSubstring = serial;
88            }
89        }
90
91        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS");
92        simpleDateFormat.setTimeZone(TimeZone.getDefault());
93        String currentDateAndTime = simpleDateFormat.format(new Date());
94
95        String burstFolderName = String.format("%s_%s", serialSubstring, currentDateAndTime);
96        File destFolder = new File(new File(root, folderName), burstFolderName);
97        if (!destFolder.mkdirs()) {
98            throw new RuntimeException("Could not create Gcam debug data folder.");
99        }
100        String destFolderPath = destFolder.getAbsolutePath();
101        return destFolderPath;
102    }
103
104    /**
105     * If set, tells the ready state changed listener the new state.
106     */
107    protected void broadcastReadyState(boolean readyForCapture) {
108        if (mReadyStateChangedListener != null) {
109            mReadyStateChangedListener.onReadyStateChanged(readyForCapture);
110        }
111    }
112
113    @Override
114    public float getMaxZoom() {
115        // If not implemented, return 1.0.
116        return 1f;
117    }
118
119    @Override
120    public void setZoom(float zoom) {
121        // If not implemented, no-op.
122    }
123}
124