Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | package android.view.textclassifier; |
| 18 | |
| 19 | import android.annotation.Nullable; |
| 20 | import android.util.KeyValueListParser; |
| 21 | import android.util.Slog; |
| 22 | |
Jan Althaus | 0aacdb6 | 2018-02-19 11:44:37 +0100 | [diff] [blame] | 23 | import java.util.Arrays; |
| 24 | import java.util.Collections; |
| 25 | import java.util.List; |
| 26 | import java.util.StringJoiner; |
| 27 | |
Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 28 | /** |
| 29 | * TextClassifier specific settings. |
| 30 | * This is encoded as a key=value list, separated by commas. Ex: |
| 31 | * |
| 32 | * <pre> |
| 33 | * smart_selection_dark_launch (boolean) |
| 34 | * smart_selection_enabled_for_edit_text (boolean) |
Jan Althaus | 0aacdb6 | 2018-02-19 11:44:37 +0100 | [diff] [blame] | 35 | * suggest_selection_max_range_length (int) |
| 36 | * classify_text_max_range_length (int) |
| 37 | * generate_links_max_text_length (int) |
| 38 | * entity_list_default (String[]) |
| 39 | * entity_list_not_editable (String[]) |
| 40 | * entity_list_editable (String[]) |
Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 41 | * </pre> |
| 42 | * |
| 43 | * <p> |
| 44 | * Type: string |
| 45 | * see also android.provider.Settings.Global.TEXT_CLASSIFIER_CONSTANTS |
| 46 | * |
| 47 | * Example of setting the values for testing. |
Jan Althaus | 0aacdb6 | 2018-02-19 11:44:37 +0100 | [diff] [blame] | 48 | * adb shell settings put global text_classifier_constants \ |
| 49 | * smart_selection_dark_launch=true,smart_selection_enabled_for_edit_text=true,\ |
| 50 | * entity_list_default=phone:address |
Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 51 | * @hide |
| 52 | */ |
| 53 | public final class TextClassifierConstants { |
| 54 | |
| 55 | private static final String LOG_TAG = "TextClassifierConstants"; |
| 56 | |
| 57 | private static final String SMART_SELECTION_DARK_LAUNCH = |
| 58 | "smart_selection_dark_launch"; |
| 59 | private static final String SMART_SELECTION_ENABLED_FOR_EDIT_TEXT = |
| 60 | "smart_selection_enabled_for_edit_text"; |
Richard Ledley | 9cfa606 | 2018-01-15 13:13:29 +0000 | [diff] [blame] | 61 | private static final String SMART_LINKIFY_ENABLED = |
| 62 | "smart_linkify_enabled"; |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 63 | private static final String SUGGEST_SELECTION_MAX_RANGE_LENGTH = |
| 64 | "suggest_selection_max_range_length"; |
| 65 | private static final String CLASSIFY_TEXT_MAX_RANGE_LENGTH = |
| 66 | "classify_text_max_range_length"; |
| 67 | private static final String GENERATE_LINKS_MAX_TEXT_LENGTH = |
| 68 | "generate_links_max_text_length"; |
Jan Althaus | 31efdc3 | 2018-02-19 22:23:13 +0100 | [diff] [blame] | 69 | private static final String GENERATE_LINKS_LOG_SAMPLE_RATE = |
| 70 | "generate_links_log_sample_rate"; |
Jan Althaus | 0aacdb6 | 2018-02-19 11:44:37 +0100 | [diff] [blame] | 71 | private static final String ENTITY_LIST_DEFAULT = |
| 72 | "entity_list_default"; |
| 73 | private static final String ENTITY_LIST_NOT_EDITABLE = |
| 74 | "entity_list_not_editable"; |
| 75 | private static final String ENTITY_LIST_EDITABLE = |
| 76 | "entity_list_editable"; |
Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 77 | |
| 78 | private static final boolean SMART_SELECTION_DARK_LAUNCH_DEFAULT = false; |
| 79 | private static final boolean SMART_SELECTION_ENABLED_FOR_EDIT_TEXT_DEFAULT = true; |
Richard Ledley | 9cfa606 | 2018-01-15 13:13:29 +0000 | [diff] [blame] | 80 | private static final boolean SMART_LINKIFY_ENABLED_DEFAULT = true; |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 81 | private static final int SUGGEST_SELECTION_MAX_RANGE_LENGTH_DEFAULT = 10 * 1000; |
| 82 | private static final int CLASSIFY_TEXT_MAX_RANGE_LENGTH_DEFAULT = 10 * 1000; |
| 83 | private static final int GENERATE_LINKS_MAX_TEXT_LENGTH_DEFAULT = 100 * 1000; |
Jan Althaus | 31efdc3 | 2018-02-19 22:23:13 +0100 | [diff] [blame] | 84 | private static final int GENERATE_LINKS_LOG_SAMPLE_RATE_DEFAULT = 100; |
Jan Althaus | 0aacdb6 | 2018-02-19 11:44:37 +0100 | [diff] [blame] | 85 | private static final String ENTITY_LIST_DELIMITER = ":"; |
| 86 | private static final String ENTITY_LIST_DEFAULT_VALUE = new StringJoiner(ENTITY_LIST_DELIMITER) |
| 87 | .add(TextClassifier.TYPE_ADDRESS) |
| 88 | .add(TextClassifier.TYPE_EMAIL) |
| 89 | .add(TextClassifier.TYPE_PHONE) |
| 90 | .add(TextClassifier.TYPE_URL) |
| 91 | .add(TextClassifier.TYPE_DATE) |
| 92 | .add(TextClassifier.TYPE_DATE_TIME) |
| 93 | .add(TextClassifier.TYPE_FLIGHT_NUMBER).toString(); |
Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 94 | |
| 95 | /** Default settings. */ |
| 96 | static final TextClassifierConstants DEFAULT = new TextClassifierConstants(); |
| 97 | |
| 98 | private final boolean mDarkLaunch; |
| 99 | private final boolean mSuggestSelectionEnabledForEditableText; |
Richard Ledley | 9cfa606 | 2018-01-15 13:13:29 +0000 | [diff] [blame] | 100 | private final boolean mSmartLinkifyEnabled; |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 101 | private final int mSuggestSelectionMaxRangeLength; |
| 102 | private final int mClassifyTextMaxRangeLength; |
| 103 | private final int mGenerateLinksMaxTextLength; |
Jan Althaus | 31efdc3 | 2018-02-19 22:23:13 +0100 | [diff] [blame] | 104 | private final int mGenerateLinksLogSampleRate; |
Jan Althaus | 0aacdb6 | 2018-02-19 11:44:37 +0100 | [diff] [blame] | 105 | private final List<String> mEntityListDefault; |
| 106 | private final List<String> mEntityListNotEditable; |
| 107 | private final List<String> mEntityListEditable; |
Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 108 | |
| 109 | private TextClassifierConstants() { |
| 110 | mDarkLaunch = SMART_SELECTION_DARK_LAUNCH_DEFAULT; |
| 111 | mSuggestSelectionEnabledForEditableText = SMART_SELECTION_ENABLED_FOR_EDIT_TEXT_DEFAULT; |
Richard Ledley | 9cfa606 | 2018-01-15 13:13:29 +0000 | [diff] [blame] | 112 | mSmartLinkifyEnabled = SMART_LINKIFY_ENABLED_DEFAULT; |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 113 | mSuggestSelectionMaxRangeLength = SUGGEST_SELECTION_MAX_RANGE_LENGTH_DEFAULT; |
| 114 | mClassifyTextMaxRangeLength = CLASSIFY_TEXT_MAX_RANGE_LENGTH_DEFAULT; |
| 115 | mGenerateLinksMaxTextLength = GENERATE_LINKS_MAX_TEXT_LENGTH_DEFAULT; |
Jan Althaus | 31efdc3 | 2018-02-19 22:23:13 +0100 | [diff] [blame] | 116 | mGenerateLinksLogSampleRate = GENERATE_LINKS_LOG_SAMPLE_RATE_DEFAULT; |
Jan Althaus | 0aacdb6 | 2018-02-19 11:44:37 +0100 | [diff] [blame] | 117 | mEntityListDefault = parseEntityList(ENTITY_LIST_DEFAULT_VALUE); |
| 118 | mEntityListNotEditable = mEntityListDefault; |
| 119 | mEntityListEditable = mEntityListDefault; |
Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | private TextClassifierConstants(@Nullable String settings) { |
| 123 | final KeyValueListParser parser = new KeyValueListParser(','); |
| 124 | try { |
| 125 | parser.setString(settings); |
| 126 | } catch (IllegalArgumentException e) { |
| 127 | // Failed to parse the settings string, log this and move on with defaults. |
| 128 | Slog.e(LOG_TAG, "Bad TextClassifier settings: " + settings); |
| 129 | } |
| 130 | mDarkLaunch = parser.getBoolean( |
| 131 | SMART_SELECTION_DARK_LAUNCH, |
| 132 | SMART_SELECTION_DARK_LAUNCH_DEFAULT); |
| 133 | mSuggestSelectionEnabledForEditableText = parser.getBoolean( |
| 134 | SMART_SELECTION_ENABLED_FOR_EDIT_TEXT, |
| 135 | SMART_SELECTION_ENABLED_FOR_EDIT_TEXT_DEFAULT); |
Richard Ledley | 9cfa606 | 2018-01-15 13:13:29 +0000 | [diff] [blame] | 136 | mSmartLinkifyEnabled = parser.getBoolean( |
| 137 | SMART_LINKIFY_ENABLED, |
| 138 | SMART_LINKIFY_ENABLED_DEFAULT); |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 139 | mSuggestSelectionMaxRangeLength = parser.getInt( |
| 140 | SUGGEST_SELECTION_MAX_RANGE_LENGTH, |
| 141 | SUGGEST_SELECTION_MAX_RANGE_LENGTH_DEFAULT); |
| 142 | mClassifyTextMaxRangeLength = parser.getInt( |
| 143 | CLASSIFY_TEXT_MAX_RANGE_LENGTH, |
| 144 | CLASSIFY_TEXT_MAX_RANGE_LENGTH_DEFAULT); |
| 145 | mGenerateLinksMaxTextLength = parser.getInt( |
| 146 | GENERATE_LINKS_MAX_TEXT_LENGTH, |
| 147 | GENERATE_LINKS_MAX_TEXT_LENGTH_DEFAULT); |
Jan Althaus | 31efdc3 | 2018-02-19 22:23:13 +0100 | [diff] [blame] | 148 | mGenerateLinksLogSampleRate = parser.getInt( |
| 149 | GENERATE_LINKS_LOG_SAMPLE_RATE, |
| 150 | GENERATE_LINKS_LOG_SAMPLE_RATE_DEFAULT); |
Jan Althaus | 0aacdb6 | 2018-02-19 11:44:37 +0100 | [diff] [blame] | 151 | mEntityListDefault = parseEntityList(parser.getString( |
| 152 | ENTITY_LIST_DEFAULT, |
| 153 | ENTITY_LIST_DEFAULT_VALUE)); |
| 154 | mEntityListNotEditable = parseEntityList(parser.getString( |
| 155 | ENTITY_LIST_NOT_EDITABLE, |
| 156 | ENTITY_LIST_DEFAULT_VALUE)); |
| 157 | mEntityListEditable = parseEntityList(parser.getString( |
| 158 | ENTITY_LIST_EDITABLE, |
| 159 | ENTITY_LIST_DEFAULT_VALUE)); |
Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Jan Althaus | 0aacdb6 | 2018-02-19 11:44:37 +0100 | [diff] [blame] | 162 | /** Load from a settings string. */ |
| 163 | public static TextClassifierConstants loadFromString(String settings) { |
Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 164 | return new TextClassifierConstants(settings); |
| 165 | } |
| 166 | |
| 167 | public boolean isDarkLaunch() { |
| 168 | return mDarkLaunch; |
| 169 | } |
| 170 | |
| 171 | public boolean isSuggestSelectionEnabledForEditableText() { |
| 172 | return mSuggestSelectionEnabledForEditableText; |
| 173 | } |
Richard Ledley | 9cfa606 | 2018-01-15 13:13:29 +0000 | [diff] [blame] | 174 | |
| 175 | public boolean isSmartLinkifyEnabled() { |
| 176 | return mSmartLinkifyEnabled; |
| 177 | } |
Jan Althaus | 108aad3 | 2018-01-30 15:26:55 +0100 | [diff] [blame] | 178 | |
| 179 | public int getSuggestSelectionMaxRangeLength() { |
| 180 | return mSuggestSelectionMaxRangeLength; |
| 181 | } |
| 182 | |
| 183 | public int getClassifyTextMaxRangeLength() { |
| 184 | return mClassifyTextMaxRangeLength; |
| 185 | } |
| 186 | |
| 187 | public int getGenerateLinksMaxTextLength() { |
| 188 | return mGenerateLinksMaxTextLength; |
| 189 | } |
Jan Althaus | 31efdc3 | 2018-02-19 22:23:13 +0100 | [diff] [blame] | 190 | |
| 191 | public int getGenerateLinksLogSampleRate() { |
| 192 | return mGenerateLinksLogSampleRate; |
| 193 | } |
Jan Althaus | 0aacdb6 | 2018-02-19 11:44:37 +0100 | [diff] [blame] | 194 | |
| 195 | public List<String> getEntityListDefault() { |
| 196 | return mEntityListDefault; |
| 197 | } |
| 198 | |
| 199 | public List<String> getEntityListNotEditable() { |
| 200 | return mEntityListNotEditable; |
| 201 | } |
| 202 | |
| 203 | public List<String> getEntityListEditable() { |
| 204 | return mEntityListEditable; |
| 205 | } |
| 206 | |
| 207 | private static List<String> parseEntityList(String listStr) { |
| 208 | return Collections.unmodifiableList(Arrays.asList(listStr.split(ENTITY_LIST_DELIMITER))); |
| 209 | } |
Abodunrinwa Toki | 0e6b43e | 2017-09-19 23:18:40 +0100 | [diff] [blame] | 210 | } |