1/*
2 * Copyright (C) 2015 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.service.media;
18
19import android.app.Service;
20import android.content.Intent;
21import android.os.Handler;
22import android.os.IBinder;
23import android.os.Message;
24import android.os.Messenger;
25
26/**
27 * Extend this class to implement a camera prewarm service. See
28 * {@link android.provider.MediaStore#META_DATA_STILL_IMAGE_CAMERA_PREWARM_SERVICE}.
29 */
30public abstract class CameraPrewarmService extends Service {
31
32    /**
33     * Intent action to bind the service as a prewarm service.
34     * @hide
35     */
36    public static final String ACTION_PREWARM =
37            "android.service.media.CameraPrewarmService.ACTION_PREWARM";
38
39    /**
40     * Message sent by the client indicating that the camera intent has been fired.
41     * @hide
42     */
43    public static final int MSG_CAMERA_FIRED = 1;
44
45    private final Handler mHandler = new Handler() {
46
47        @Override
48        public void handleMessage(Message msg) {
49            switch (msg.what) {
50                case MSG_CAMERA_FIRED:
51                    mCameraIntentFired = true;
52                    break;
53                default:
54                    super.handleMessage(msg);
55            }
56        }
57    };
58    private boolean mCameraIntentFired;
59
60    @Override
61    public IBinder onBind(Intent intent) {
62        if (ACTION_PREWARM.equals(intent.getAction())) {
63            onPrewarm();
64            return new Messenger(mHandler).getBinder();
65        } else {
66            return null;
67        }
68    }
69
70    @Override
71    public boolean onUnbind(Intent intent) {
72        if (ACTION_PREWARM.equals(intent.getAction())) {
73            onCooldown(mCameraIntentFired);
74        }
75        return false;
76    }
77
78    /**
79     * Called when the camera should be prewarmed.
80     */
81    public abstract void onPrewarm();
82
83    /**
84     * Called when prewarm phase is done, either because the camera launch intent has been fired
85     * at this point or prewarm is no longer needed. A client should close the camera
86     * immediately in the latter case.
87     * <p>
88     * In case the camera launch intent has been fired, there is no guarantee about the ordering
89     * of these two events. Cooldown might happen either before or after the activity has been
90     * created that handles the camera intent.
91     *
92     * @param cameraIntentFired Indicates whether the intent to launch the camera has been
93     *                          fired.
94     */
95    public abstract void onCooldown(boolean cameraIntentFired);
96}
97