1cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher/*
2cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher * Copyright (C) 2014 The Android Open Source Project
3cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher *
4cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher * Licensed under the Apache License, Version 2.0 (the "License");
5cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher * you may not use this file except in compliance with the License.
6cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher * You may obtain a copy of the License at
7cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher *
8cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher *      http://www.apache.org/licenses/LICENSE-2.0
9cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher *
10cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher * Unless required by applicable law or agreed to in writing, software
11cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher * distributed under the License is distributed on an "AS IS" BASIS,
12cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher * See the License for the specific language governing permissions and
14cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher * limitations under the License.
15cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher */
16cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher
17cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucherpackage com.android.ex.camera2.portability;
18cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher
19cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucherimport android.os.Handler;
20cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucherimport android.os.Looper;
21cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucherimport android.os.Message;
22cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher
23cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucherimport java.util.LinkedList;
24cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher
25cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucherclass HistoryHandler extends Handler {
26cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher    private static final int MAX_HISTORY_SIZE = 400;
27cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher
28cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher    final LinkedList<Integer> mMsgHistory;
29cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher
30cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher    HistoryHandler(Looper looper) {
31cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher        super(looper);
32cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher        mMsgHistory = new LinkedList<Integer>();
33cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher        // We add a -1 at the beginning to mark the very beginning of the
34cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher        // history.
35cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher        mMsgHistory.offerLast(-1);
36cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher    }
37cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher
38733ca8dfa76ac34d1f9caff8798d01a4a8f44002Senpo Hu    Integer getCurrentMessage() {
39733ca8dfa76ac34d1f9caff8798d01a4a8f44002Senpo Hu        return mMsgHistory.peekLast();
40733ca8dfa76ac34d1f9caff8798d01a4a8f44002Senpo Hu    }
41733ca8dfa76ac34d1f9caff8798d01a4a8f44002Senpo Hu
42cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher    String generateHistoryString(int cameraId) {
43cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher        String info = new String("HIST");
44cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher        info += "_ID" + cameraId;
45cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher        for (Integer msg : mMsgHistory) {
46cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher            info = info + '_' + msg.toString();
47cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher        }
48cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher        info += "_HEND";
49cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher        return info;
50cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher    }
51cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher
52cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher    /**
53cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher     * Subclasses' implementations should call this one before doing their work.
54cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher     */
55cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher    @Override
56cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher    public void handleMessage(Message msg) {
57cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher        mMsgHistory.offerLast(msg.what);
58cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher        while (mMsgHistory.size() > MAX_HISTORY_SIZE) {
59cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher            mMsgHistory.pollFirst();
60cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher        }
61cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher    }
62cef46862d6937bc98bf1a6b087c5daa22b5239f3Sol Boucher}
63