1/*
2 * Copyright (C) 2008 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.calculator2;
18
19import android.widget.BaseAdapter;
20
21import java.io.DataInput;
22import java.io.DataOutput;
23import java.io.IOException;
24import java.util.Vector;
25
26class History {
27    private static final int VERSION_1 = 1;
28    private static final int MAX_ENTRIES = 100;
29    Vector<HistoryEntry> mEntries = new Vector<HistoryEntry>();
30    int mPos;
31    BaseAdapter mObserver;
32
33    History() {
34        clear();
35    }
36
37    History(int version, DataInput in) throws IOException {
38        if (version >= VERSION_1) {
39            int size = in.readInt();
40            for (int i = 0; i < size; ++i) {
41                mEntries.add(new HistoryEntry(version, in));
42            }
43            mPos = in.readInt();
44        } else {
45            throw new IOException("invalid version " + version);
46        }
47    }
48
49    void setObserver(BaseAdapter observer) {
50        mObserver = observer;
51    }
52
53    private void notifyChanged() {
54        if (mObserver != null) {
55            mObserver.notifyDataSetChanged();
56        }
57    }
58
59    void clear() {
60        mEntries.clear();
61        mEntries.add(new HistoryEntry(""));
62        mPos = 0;
63        notifyChanged();
64    }
65
66    void write(DataOutput out) throws IOException {
67        out.writeInt(mEntries.size());
68        for (HistoryEntry entry : mEntries) {
69            entry.write(out);
70        }
71        out.writeInt(mPos);
72    }
73
74    void update(String text) {
75        current().setEdited(text);
76    }
77
78    boolean moveToPrevious() {
79        if (mPos > 0) {
80            --mPos;
81            return true;
82        }
83        return false;
84    }
85
86    boolean moveToNext() {
87        if (mPos < mEntries.size() - 1) {
88            ++mPos;
89            return true;
90        }
91        return false;
92    }
93
94    void enter(String text) {
95        current().clearEdited();
96        if (mEntries.size() >= MAX_ENTRIES) {
97            mEntries.remove(0);
98        }
99        if (mEntries.size() < 2 ||
100            !text.equals(mEntries.elementAt(mEntries.size() - 2).getBase())) {
101            mEntries.insertElementAt(new HistoryEntry(text), mEntries.size() - 1);
102        }
103        mPos = mEntries.size() - 1;
104        notifyChanged();
105    }
106
107    HistoryEntry current() {
108        return mEntries.elementAt(mPos);
109    }
110
111    String getText() {
112        return current().getEdited();
113    }
114
115    String getBase() {
116        return current().getBase();
117    }
118}
119