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.ex.camera2.portability;
18
19import android.os.Handler;
20import android.os.Looper;
21import android.os.Message;
22
23import java.util.LinkedList;
24
25class HistoryHandler extends Handler {
26    private static final int MAX_HISTORY_SIZE = 400;
27
28    final LinkedList<Integer> mMsgHistory;
29
30    HistoryHandler(Looper looper) {
31        super(looper);
32        mMsgHistory = new LinkedList<Integer>();
33        // We add a -1 at the beginning to mark the very beginning of the
34        // history.
35        mMsgHistory.offerLast(-1);
36    }
37
38    Integer getCurrentMessage() {
39        return mMsgHistory.peekLast();
40    }
41
42    String generateHistoryString(int cameraId) {
43        String info = new String("HIST");
44        info += "_ID" + cameraId;
45        for (Integer msg : mMsgHistory) {
46            info = info + '_' + msg.toString();
47        }
48        info += "_HEND";
49        return info;
50    }
51
52    /**
53     * Subclasses' implementations should call this one before doing their work.
54     */
55    @Override
56    public void handleMessage(Message msg) {
57        mMsgHistory.offerLast(msg.what);
58        while (mMsgHistory.size() > MAX_HISTORY_SIZE) {
59            mMsgHistory.pollFirst();
60        }
61    }
62}
63