aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/anysoftkeyboard/dictionaries/DictionaryFactory.java
blob: 4d9bc7ee8e7c4d1fc8492eef4f033c6f3482f5c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright (c) 2013 Menny Even-Danan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.anysoftkeyboard.dictionaries;

import android.content.Context;

import com.anysoftkeyboard.base.dictionaries.Dictionary;
import com.anysoftkeyboard.base.dictionaries.EditableDictionary;
import com.anysoftkeyboard.dictionaries.content.ContactsDictionary;
import com.anysoftkeyboard.dictionaries.sqlite.AutoDictionary;
import com.anysoftkeyboard.utils.Log;
import com.menny.android.anysoftkeyboard.AnyApplication;

public class DictionaryFactory {

    private static final String TAG = "ASK DictFactory";
    private AutoDictionary mAutoDictionary = null;
    private String mUserDictionaryLocale = null;
    private EditableDictionary mUserDictionary = null;

    public DictionaryFactory() {
    }

    private static boolean equalsString(String a, String b) {
        if (a == null && b == null) return true;
        else if (a == null || b == null) return false;
        else return a.equals(b);
    }

    public synchronized EditableDictionary createUserDictionary(Context context, String locale) {
        if (mUserDictionary != null) {
            if (!mUserDictionary.isClosed() && equalsString(mUserDictionaryLocale, locale)) {
                Log.d(TAG, "Returning cached user-dictionary for locale " + mUserDictionaryLocale);
                return mUserDictionary;
            } else {
                mUserDictionary.close();
            }
        }
        Log.d(TAG, "Creating a new UserDictionary for locale " + locale);
        mUserDictionary = new UserDictionary(context, locale);
        DictionaryASyncLoader loader = new DictionaryASyncLoader(null);
        loader.execute(mUserDictionary);

        mUserDictionaryLocale = locale;
        return mUserDictionary;
    }

    public synchronized Dictionary createContactsDictionary(Context context) {
        return new ContactsDictionary(context.getApplicationContext());
    }

    public synchronized AutoDictionary createAutoDictionary(Context context, String currentAutoDictionaryLocale) {
        if (AnyApplication.getConfig().getAutoDictionaryInsertionThreshold() < 0) return null;

        if (mAutoDictionary != null && !mAutoDictionary.isClosed()) {
            if (equalsString(mAutoDictionary.getLocale(), currentAutoDictionaryLocale)) {
                return mAutoDictionary;
            } else {
                //will create a new one shortly.
                mAutoDictionary.close();
            }
        }

        Log.d(TAG, "Creating AutoDictionary for locale: " + currentAutoDictionaryLocale);

        mAutoDictionary = new AutoDictionary(context, currentAutoDictionaryLocale);

        DictionaryASyncLoader loader = new DictionaryASyncLoader(null);
        loader.execute(mAutoDictionary);

        return mAutoDictionary;
    }
}