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.provider.DeviceConfig;
21 
22 import com.android.internal.annotations.VisibleForTesting;
23 import com.android.internal.util.IndentingPrintWriter;
24 
25 /**
26  * TextClassifier specific settings.
27  *
28  * <p>Currently, this class does not guarantee co-diverted flags are updated atomically.
29  *
30  * <pre>
31  * adb shell cmd device_config put textclassifier system_textclassifier_enabled true
32  * </pre>
33  *
34  * @see android.provider.DeviceConfig#NAMESPACE_TEXTCLASSIFIER
35  * @hide
36  */
37 // TODO: Rename to TextClassifierSettings.
38 public final class TextClassificationConstants {
39     /**
40      * Whether the smart linkify feature is enabled.
41      */
42     private static final String SMART_LINKIFY_ENABLED = "smart_linkify_enabled";
43     /**
44      * Whether SystemTextClassifier is enabled.
45      */
46     static final String SYSTEM_TEXT_CLASSIFIER_ENABLED = "system_textclassifier_enabled";
47     /**
48      * Whether TextClassifierImpl is enabled.
49      */
50     @VisibleForTesting
51     static final String LOCAL_TEXT_CLASSIFIER_ENABLED = "local_textclassifier_enabled";
52     /**
53      * Enable smart selection without a visible UI changes.
54      */
55     private static final String MODEL_DARK_LAUNCH_ENABLED = "model_dark_launch_enabled";
56     /**
57      * Whether the smart selection feature is enabled.
58      */
59     private static final String SMART_SELECTION_ENABLED = "smart_selection_enabled";
60     /**
61      * Whether the smart text share feature is enabled.
62      */
63     private static final String SMART_TEXT_SHARE_ENABLED = "smart_text_share_enabled";
64     /**
65      * Whether animation for smart selection is enabled.
66      */
67     private static final String SMART_SELECT_ANIMATION_ENABLED =
68             "smart_select_animation_enabled";
69     /**
70      * Max length of text that generateLinks can accept.
71      */
72     @VisibleForTesting
73     static final String GENERATE_LINKS_MAX_TEXT_LENGTH = "generate_links_max_text_length";
74     /**
75      * The TextClassifierService which would like to use. Example of setting the package:
76      * <pre>
77      * adb shell cmd device_config put textclassifier textclassifier_service_package_override \
78      *      com.android.textclassifier
79      * </pre>
80      */
81     @VisibleForTesting
82     static final String TEXT_CLASSIFIER_SERVICE_PACKAGE_OVERRIDE =
83             "textclassifier_service_package_override";
84 
85     /**
86      * The timeout value in seconds used by {@link SystemTextClassifier} for each TextClassifier
87      * API calls.
88      */
89     @VisibleForTesting
90     static final String SYSTEM_TEXT_CLASSIFIER_API_TIMEOUT_IN_SECOND =
91             "system_textclassifier_api_timeout_in_second";
92 
93     /**
94      * The maximum amount of characters before and after the selected text that is passed to the
95      * TextClassifier for the smart selection. e.g. If this value is 100, then 100 characters before
96      * the selection and 100 characters after the selection will be passed to the TextClassifier.
97      */
98     private static final String SMART_SELECTION_TRIM_DELTA = "smart_selection_trim_delta";
99 
100     private static final String DEFAULT_TEXT_CLASSIFIER_SERVICE_PACKAGE_OVERRIDE = null;
101     private static final boolean LOCAL_TEXT_CLASSIFIER_ENABLED_DEFAULT = true;
102     private static final boolean SYSTEM_TEXT_CLASSIFIER_ENABLED_DEFAULT = true;
103     private static final boolean MODEL_DARK_LAUNCH_ENABLED_DEFAULT = false;
104     private static final boolean SMART_SELECTION_ENABLED_DEFAULT = true;
105     private static final boolean SMART_TEXT_SHARE_ENABLED_DEFAULT = true;
106     private static final boolean SMART_LINKIFY_ENABLED_DEFAULT = true;
107     private static final boolean SMART_SELECT_ANIMATION_ENABLED_DEFAULT = true;
108     private static final int GENERATE_LINKS_MAX_TEXT_LENGTH_DEFAULT = 100 * 1000;
109     private static final long SYSTEM_TEXT_CLASSIFIER_API_TIMEOUT_IN_SECOND_DEFAULT = 60;
110     private static final int SMART_SELECTION_TRIM_DELTA_DEFAULT = 120;
111 
112     @Nullable
getTextClassifierServicePackageOverride()113     public String getTextClassifierServicePackageOverride() {
114         return DeviceConfig.getString(DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
115                 TEXT_CLASSIFIER_SERVICE_PACKAGE_OVERRIDE,
116                 DEFAULT_TEXT_CLASSIFIER_SERVICE_PACKAGE_OVERRIDE);
117     }
118 
isLocalTextClassifierEnabled()119     public boolean isLocalTextClassifierEnabled() {
120         return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
121                 LOCAL_TEXT_CLASSIFIER_ENABLED, LOCAL_TEXT_CLASSIFIER_ENABLED_DEFAULT);
122     }
123 
isSystemTextClassifierEnabled()124     public boolean isSystemTextClassifierEnabled() {
125         return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
126                 SYSTEM_TEXT_CLASSIFIER_ENABLED,
127                 SYSTEM_TEXT_CLASSIFIER_ENABLED_DEFAULT);
128     }
129 
isModelDarkLaunchEnabled()130     public boolean isModelDarkLaunchEnabled() {
131         return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
132                 MODEL_DARK_LAUNCH_ENABLED, MODEL_DARK_LAUNCH_ENABLED_DEFAULT);
133     }
134 
isSmartSelectionEnabled()135     public boolean isSmartSelectionEnabled() {
136         return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
137                 SMART_SELECTION_ENABLED, SMART_SELECTION_ENABLED_DEFAULT);
138     }
139 
isSmartTextShareEnabled()140     public boolean isSmartTextShareEnabled() {
141         return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
142                 SMART_TEXT_SHARE_ENABLED, SMART_TEXT_SHARE_ENABLED_DEFAULT);
143     }
144 
isSmartLinkifyEnabled()145     public boolean isSmartLinkifyEnabled() {
146         return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_TEXTCLASSIFIER, SMART_LINKIFY_ENABLED,
147                 SMART_LINKIFY_ENABLED_DEFAULT);
148     }
149 
isSmartSelectionAnimationEnabled()150     public boolean isSmartSelectionAnimationEnabled() {
151         return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
152                 SMART_SELECT_ANIMATION_ENABLED, SMART_SELECT_ANIMATION_ENABLED_DEFAULT);
153     }
154 
getGenerateLinksMaxTextLength()155     public int getGenerateLinksMaxTextLength() {
156         return DeviceConfig.getInt(DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
157                 GENERATE_LINKS_MAX_TEXT_LENGTH, GENERATE_LINKS_MAX_TEXT_LENGTH_DEFAULT);
158     }
159 
getSystemTextClassifierApiTimeoutInSecond()160     public long getSystemTextClassifierApiTimeoutInSecond() {
161         return DeviceConfig.getLong(DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
162                 SYSTEM_TEXT_CLASSIFIER_API_TIMEOUT_IN_SECOND,
163                 SYSTEM_TEXT_CLASSIFIER_API_TIMEOUT_IN_SECOND_DEFAULT);
164     }
165 
getSmartSelectionTrimDelta()166     public int getSmartSelectionTrimDelta() {
167         return DeviceConfig.getInt(DeviceConfig.NAMESPACE_TEXTCLASSIFIER,
168                 SMART_SELECTION_TRIM_DELTA,
169                 SMART_SELECTION_TRIM_DELTA_DEFAULT);
170     }
171 
dump(IndentingPrintWriter pw)172     void dump(IndentingPrintWriter pw) {
173         pw.println("TextClassificationConstants:");
174         pw.increaseIndent();
175         pw.print(GENERATE_LINKS_MAX_TEXT_LENGTH, getGenerateLinksMaxTextLength()).println();
176         pw.print(LOCAL_TEXT_CLASSIFIER_ENABLED, isLocalTextClassifierEnabled()).println();
177         pw.print(MODEL_DARK_LAUNCH_ENABLED, isModelDarkLaunchEnabled()).println();
178         pw.print(SMART_LINKIFY_ENABLED, isSmartLinkifyEnabled()).println();
179         pw.print(SMART_SELECT_ANIMATION_ENABLED, isSmartSelectionAnimationEnabled()).println();
180         pw.print(SMART_SELECTION_ENABLED, isSmartSelectionEnabled()).println();
181         pw.print(SMART_TEXT_SHARE_ENABLED, isSmartTextShareEnabled()).println();
182         pw.print(SYSTEM_TEXT_CLASSIFIER_ENABLED, isSystemTextClassifierEnabled()).println();
183         pw.print(TEXT_CLASSIFIER_SERVICE_PACKAGE_OVERRIDE,
184                 getTextClassifierServicePackageOverride()).println();
185         pw.print(SYSTEM_TEXT_CLASSIFIER_API_TIMEOUT_IN_SECOND,
186                 getSystemTextClassifierApiTimeoutInSecond()).println();
187         pw.print(SMART_SELECTION_TRIM_DELTA, getSmartSelectionTrimDelta()).println();
188         pw.decreaseIndent();
189     }
190 }