HistoryHandler.java revision cef46862d6937bc98bf1a6b087c5daa22b5239f3
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    String generateHistoryString(int cameraId) {
39        String info = new String("HIST");
40        info += "_ID" + cameraId;
41        for (Integer msg : mMsgHistory) {
42            info = info + '_' + msg.toString();
43        }
44        info += "_HEND";
45        return info;
46    }
47
48    /**
49     * Subclasses' implementations should call this one before doing their work.
50     */
51    @Override
52    public void handleMessage(Message msg) {
53        mMsgHistory.offerLast(msg.what);
54        while (mMsgHistory.size() > MAX_HISTORY_SIZE) {
55            mMsgHistory.pollFirst();
56        }
57    }
58}
59