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