1 /*
2  * Copyright (C) 2013 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 
17 #ifndef LATINIME_SUGGEST_OPTIONS_H
18 #define LATINIME_SUGGEST_OPTIONS_H
19 
20 #include "defines.h"
21 
22 namespace latinime {
23 
24 class SuggestOptions{
25  public:
SuggestOptions(const int * const options,const int length)26     SuggestOptions(const int *const options, const int length)
27             : mOptions(options), mLength(length) {}
28 
isGesture()29     AK_FORCE_INLINE bool isGesture() const {
30         return getBoolOption(IS_GESTURE);
31     }
32 
useFullEditDistance()33     AK_FORCE_INLINE bool useFullEditDistance() const {
34         return getBoolOption(USE_FULL_EDIT_DISTANCE);
35     }
36 
blockOffensiveWords()37     AK_FORCE_INLINE bool blockOffensiveWords() const {
38         return getBoolOption(BLOCK_OFFENSIVE_WORDS);
39     }
40 
enableSpaceAwareGesture()41     AK_FORCE_INLINE bool enableSpaceAwareGesture() const {
42         return getBoolOption(SPACE_AWARE_GESTURE_ENABLED);
43     }
44 
weightForLocale()45     AK_FORCE_INLINE float weightForLocale() const {
46         // The weight is in thousands and we want the real value, so we divide by 1000.
47         // NativeSuggestOptions#setWeightForLocale does the opposite processing in Java.
48         return static_cast<float>(getIntOption(WEIGHT_FOR_LOCALE_IN_THOUSANDS)) / 1000.0f;
49     }
50 
getAdditionalFeaturesBoolOption(const int key)51     AK_FORCE_INLINE bool getAdditionalFeaturesBoolOption(const int key) const {
52         return getBoolOption(key + ADDITIONAL_FEATURES_OPTIONS);
53     }
54 
55  private:
56     DISALLOW_IMPLICIT_CONSTRUCTORS(SuggestOptions);
57 
58     // Need to update com.android.inputmethod.latin.NativeSuggestOptions when you add, remove or
59     // reorder options.
60     static const int IS_GESTURE = 0;
61     static const int USE_FULL_EDIT_DISTANCE = 1;
62     static const int BLOCK_OFFENSIVE_WORDS = 2;
63     static const int SPACE_AWARE_GESTURE_ENABLED = 3;
64     static const int WEIGHT_FOR_LOCALE_IN_THOUSANDS = 4;
65     // Additional features options are stored after the other options and used as setting values of
66     // experimental features.
67     static const int ADDITIONAL_FEATURES_OPTIONS = 5;
68 
69     const int *const mOptions;
70     const int mLength;
71 
isValidKey(const int key)72     AK_FORCE_INLINE bool isValidKey(const int key) const {
73         return 0 <= key && key < mLength;
74     }
75 
getBoolOption(const int key)76     AK_FORCE_INLINE bool getBoolOption(const int key) const {
77         if (isValidKey(key)) {
78             return mOptions[key] != 0;
79         }
80         return false;
81     }
82 
getIntOption(const int key)83     AK_FORCE_INLINE int getIntOption(const int key) const {
84         if (isValidKey(key)) {
85             return mOptions[key];
86         }
87         return 0;
88     }
89 };
90 } // namespace latinime
91 #endif // LATINIME_SUGGEST_OPTIONS_H
92