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 17 package com.android.inputmethod.dictionarypack; 18 19 import android.view.View; 20 21 import java.util.ArrayList; 22 import java.util.HashMap; 23 24 /** 25 * Helper class to maintain the interface state of word list preferences. 26 * 27 * This is necessary because the views are created on-demand by calling code. There are many 28 * situations where views are renewed with little relation with user interaction. For example, 29 * when scrolling, the view is reused so it doesn't keep its state, which means we need to keep 30 * it separately. Also whenever the underlying dictionary list undergoes a change (for example, 31 * update the metadata, or finish downloading) the whole list has to be thrown out and recreated 32 * in case some dictionaries appeared, disappeared, changed states etc. 33 */ 34 public class DictionaryListInterfaceState { 35 static class State { 36 public boolean mOpen = false; 37 public int mStatus = MetadataDbHelper.STATUS_UNKNOWN; 38 } 39 40 private HashMap<String, State> mWordlistToState = new HashMap<>(); 41 private ArrayList<View> mViewCache = new ArrayList<>(); 42 isOpen(final String wordlistId)43 public boolean isOpen(final String wordlistId) { 44 final State state = mWordlistToState.get(wordlistId); 45 if (null == state) return false; 46 return state.mOpen; 47 } 48 getStatus(final String wordlistId)49 public int getStatus(final String wordlistId) { 50 final State state = mWordlistToState.get(wordlistId); 51 if (null == state) return MetadataDbHelper.STATUS_UNKNOWN; 52 return state.mStatus; 53 } 54 setOpen(final String wordlistId, final int status)55 public void setOpen(final String wordlistId, final int status) { 56 final State newState; 57 final State state = mWordlistToState.get(wordlistId); 58 newState = null == state ? new State() : state; 59 newState.mOpen = true; 60 newState.mStatus = status; 61 mWordlistToState.put(wordlistId, newState); 62 } 63 closeAll()64 public void closeAll() { 65 for (final State state : mWordlistToState.values()) { 66 state.mOpen = false; 67 } 68 } 69 findFirstOrphanedView()70 public View findFirstOrphanedView() { 71 for (final View v : mViewCache) { 72 if (null == v.getParent()) return v; 73 } 74 return null; 75 } 76 addToCacheAndReturnView(final View view)77 public View addToCacheAndReturnView(final View view) { 78 mViewCache.add(view); 79 return view; 80 } 81 removeFromCache(final View view)82 public void removeFromCache(final View view) { 83 mViewCache.remove(view); 84 } 85 } 86