1/* 2 * Copyright (C) 2009 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.inputmethod.pinyin; 18 19import java.util.Vector; 20 21import android.content.Context; 22 23/** 24 * Class used to cache previously loaded soft keyboard layouts. 25 */ 26public class SkbPool { 27 private static SkbPool mInstance = null; 28 29 private Vector<SkbTemplate> mSkbTemplates = new Vector<SkbTemplate>(); 30 private Vector<SoftKeyboard> mSoftKeyboards = new Vector<SoftKeyboard>(); 31 32 private SkbPool() { 33 } 34 35 public static SkbPool getInstance() { 36 if (null == mInstance) mInstance = new SkbPool(); 37 return mInstance; 38 } 39 40 public void resetCachedSkb() { 41 mSoftKeyboards.clear(); 42 } 43 44 public SkbTemplate getSkbTemplate(int skbTemplateId, Context context) { 45 for (int i = 0; i < mSkbTemplates.size(); i++) { 46 SkbTemplate t = mSkbTemplates.elementAt(i); 47 if (t.getSkbTemplateId() == skbTemplateId) { 48 return t; 49 } 50 } 51 52 if (null != context) { 53 XmlKeyboardLoader xkbl = new XmlKeyboardLoader(context); 54 SkbTemplate t = xkbl.loadSkbTemplate(skbTemplateId); 55 if (null != t) { 56 mSkbTemplates.add(t); 57 return t; 58 } 59 } 60 return null; 61 } 62 63 // Try to find the keyboard in the pool with the cache id. If there is no 64 // keyboard found, try to load it with the given xml id. 65 public SoftKeyboard getSoftKeyboard(int skbCacheId, int skbXmlId, 66 int skbWidth, int skbHeight, Context context) { 67 for (int i = 0; i < mSoftKeyboards.size(); i++) { 68 SoftKeyboard skb = mSoftKeyboards.elementAt(i); 69 if (skb.getCacheId() == skbCacheId && skb.getSkbXmlId() == skbXmlId) { 70 skb.setSkbCoreSize(skbWidth, skbHeight); 71 skb.setNewlyLoadedFlag(false); 72 return skb; 73 } 74 } 75 if (null != context) { 76 XmlKeyboardLoader xkbl = new XmlKeyboardLoader(context); 77 SoftKeyboard skb = xkbl.loadKeyboard(skbXmlId, skbWidth, skbHeight); 78 if (skb != null) { 79 if (skb.getCacheFlag()) { 80 skb.setCacheId(skbCacheId); 81 mSoftKeyboards.add(skb); 82 } 83 } 84 return skb; 85 } 86 return null; 87 } 88} 89