• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 com.android.textclassifier.common.statsd;
18 
19 import android.view.textclassifier.SelectionEvent;
20 import android.view.textclassifier.TextClassificationContext;
21 import android.view.textclassifier.TextClassifierEvent;
22 import javax.annotation.Nullable;
23 
24 /** Helper class to convert a {@link SelectionEvent} to a {@link TextClassifierEvent}. */
25 public final class SelectionEventConverter {
26 
27   /** Converts a {@link SelectionEvent} to a {@link TextClassifierEvent}. */
28   @Nullable
toTextClassifierEvent(SelectionEvent selectionEvent)29   public static TextClassifierEvent toTextClassifierEvent(SelectionEvent selectionEvent) {
30     TextClassificationContext textClassificationContext = null;
31     if (selectionEvent.getPackageName() != null && selectionEvent.getWidgetType() != null) {
32       textClassificationContext =
33           new TextClassificationContext.Builder(
34                   selectionEvent.getPackageName(), selectionEvent.getWidgetType())
35               .setWidgetVersion(selectionEvent.getWidgetVersion())
36               .build();
37     }
38     if (selectionEvent.getInvocationMethod() == SelectionEvent.INVOCATION_LINK) {
39       return new TextClassifierEvent.TextLinkifyEvent.Builder(
40               convertEventType(selectionEvent.getEventType()))
41           .setEventContext(textClassificationContext)
42           .setResultId(selectionEvent.getResultId())
43           .setEventIndex(selectionEvent.getEventIndex())
44           .setEntityTypes(selectionEvent.getEntityType())
45           .build();
46     }
47     if (selectionEvent.getInvocationMethod() == SelectionEvent.INVOCATION_MANUAL) {
48       return new TextClassifierEvent.TextSelectionEvent.Builder(
49               convertEventType(selectionEvent.getEventType()))
50           .setEventContext(textClassificationContext)
51           .setResultId(selectionEvent.getResultId())
52           .setEventIndex(selectionEvent.getEventIndex())
53           .setEntityTypes(selectionEvent.getEntityType())
54           .setRelativeWordStartIndex(selectionEvent.getStart())
55           .setRelativeWordEndIndex(selectionEvent.getEnd())
56           .setRelativeSuggestedWordStartIndex(selectionEvent.getSmartStart())
57           .setRelativeSuggestedWordEndIndex(selectionEvent.getSmartEnd())
58           .build();
59     }
60     return null;
61   }
62 
convertEventType(int eventType)63   private static int convertEventType(int eventType) {
64     switch (eventType) {
65       case SelectionEvent.EVENT_SELECTION_STARTED:
66         return TextClassifierEvent.TYPE_SELECTION_STARTED;
67       case SelectionEvent.EVENT_SELECTION_MODIFIED:
68         return TextClassifierEvent.TYPE_SELECTION_MODIFIED;
69       case SelectionEvent.EVENT_SMART_SELECTION_SINGLE:
70         return SelectionEvent.EVENT_SMART_SELECTION_SINGLE;
71       case SelectionEvent.EVENT_SMART_SELECTION_MULTI:
72         return SelectionEvent.EVENT_SMART_SELECTION_MULTI;
73       case SelectionEvent.EVENT_AUTO_SELECTION:
74         return SelectionEvent.EVENT_AUTO_SELECTION;
75       case SelectionEvent.ACTION_OVERTYPE:
76         return TextClassifierEvent.TYPE_OVERTYPE;
77       case SelectionEvent.ACTION_COPY:
78         return TextClassifierEvent.TYPE_COPY_ACTION;
79       case SelectionEvent.ACTION_PASTE:
80         return TextClassifierEvent.TYPE_PASTE_ACTION;
81       case SelectionEvent.ACTION_CUT:
82         return TextClassifierEvent.TYPE_CUT_ACTION;
83       case SelectionEvent.ACTION_SHARE:
84         return TextClassifierEvent.TYPE_SHARE_ACTION;
85       case SelectionEvent.ACTION_SMART_SHARE:
86         return TextClassifierEvent.TYPE_SMART_ACTION;
87       case SelectionEvent.ACTION_DRAG:
88         return TextClassifierEvent.TYPE_SELECTION_DRAG;
89       case SelectionEvent.ACTION_ABANDON:
90         return TextClassifierEvent.TYPE_SELECTION_DESTROYED;
91       case SelectionEvent.ACTION_OTHER:
92         return TextClassifierEvent.TYPE_OTHER_ACTION;
93       case SelectionEvent.ACTION_SELECT_ALL:
94         return TextClassifierEvent.TYPE_SELECT_ALL;
95       case SelectionEvent.ACTION_RESET:
96         return TextClassifierEvent.TYPE_SELECTION_RESET;
97       default:
98         return 0;
99     }
100   }
101 
SelectionEventConverter()102   private SelectionEventConverter() {}
103 }
104