1/**
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16
17package com.android.inputmethod.dictionarypack;
18
19import android.view.View;
20
21import com.android.inputmethod.latin.utils.CollectionUtils;
22
23import java.util.ArrayList;
24import java.util.HashMap;
25
26/**
27 * Helper class to maintain the interface state of word list preferences.
28 *
29 * This is necessary because the views are created on-demand by calling code. There are many
30 * situations where views are renewed with little relation with user interaction. For example,
31 * when scrolling, the view is reused so it doesn't keep its state, which means we need to keep
32 * it separately. Also whenever the underlying dictionary list undergoes a change (for example,
33 * update the metadata, or finish downloading) the whole list has to be thrown out and recreated
34 * in case some dictionaries appeared, disappeared, changed states etc.
35 */
36public class DictionaryListInterfaceState {
37    private static class State {
38        public boolean mOpen = false;
39        public int mStatus = MetadataDbHelper.STATUS_UNKNOWN;
40    }
41
42    private HashMap<String, State> mWordlistToState = CollectionUtils.newHashMap();
43    private ArrayList<View> mViewCache = CollectionUtils.newArrayList();
44
45    public boolean isOpen(final String wordlistId) {
46        final State state = mWordlistToState.get(wordlistId);
47        if (null == state) return false;
48        return state.mOpen;
49    }
50
51    public int getStatus(final String wordlistId) {
52        final State state = mWordlistToState.get(wordlistId);
53        if (null == state) return MetadataDbHelper.STATUS_UNKNOWN;
54        return state.mStatus;
55    }
56
57    public void setOpen(final String wordlistId, final int status) {
58        final State newState;
59        final State state = mWordlistToState.get(wordlistId);
60        newState = null == state ? new State() : state;
61        newState.mOpen = true;
62        newState.mStatus = status;
63        mWordlistToState.put(wordlistId, newState);
64    }
65
66    public void closeAll() {
67        for (final State state : mWordlistToState.values()) {
68            state.mOpen = false;
69        }
70    }
71
72    public View findFirstOrphanedView() {
73        for (final View v : mViewCache) {
74            if (null == v.getParent()) return v;
75        }
76        return null;
77    }
78
79    public View addToCacheAndReturnView(final View view) {
80        mViewCache.add(view);
81        return view;
82    }
83
84    public void removeFromCache(final View view) {
85        mViewCache.remove(view);
86    }
87}
88