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 com.android.textclassifier.common.base.TcLog; 20 import com.android.textclassifier.common.logging.TextClassificationContext; 21 import com.android.textclassifier.common.logging.TextClassifierEvent; 22 import com.android.textclassifier.common.logging.TextClassifierEvent.ConversationActionsEvent; 23 import com.android.textclassifier.common.logging.TextClassifierEvent.LanguageDetectionEvent; 24 import com.android.textclassifier.common.logging.TextClassifierEvent.TextLinkifyEvent; 25 import com.android.textclassifier.common.logging.TextClassifierEvent.TextSelectionEvent; 26 import javax.annotation.Nullable; 27 28 /** 29 * Converts between {@link TextClassifierEvent} and {@link 30 * android.view.textclassifier.TextClassifierEvent}. 31 */ 32 public final class TextClassifierEventConverter { 33 private static final String TAG = "TextClassifierEventConv"; 34 35 /** 36 * Converts a {@link android.view.textclassifier.TextClassifierEvent} object to a {@link 37 * TextClassifierEvent}. Returns {@code null} if conversion fails. 38 */ 39 @Nullable fromPlatform( @ullable android.view.textclassifier.TextClassifierEvent textClassifierEvent)40 public static TextClassifierEvent fromPlatform( 41 @Nullable android.view.textclassifier.TextClassifierEvent textClassifierEvent) { 42 if (textClassifierEvent == null) { 43 return null; 44 } 45 if (textClassifierEvent 46 instanceof android.view.textclassifier.TextClassifierEvent.TextSelectionEvent) { 47 return fromPlatform( 48 (android.view.textclassifier.TextClassifierEvent.TextSelectionEvent) textClassifierEvent); 49 } else if (textClassifierEvent 50 instanceof android.view.textclassifier.TextClassifierEvent.TextLinkifyEvent) { 51 return fromPlatform( 52 (android.view.textclassifier.TextClassifierEvent.TextLinkifyEvent) textClassifierEvent); 53 } else if (textClassifierEvent 54 instanceof android.view.textclassifier.TextClassifierEvent.ConversationActionsEvent) { 55 return fromPlatform( 56 (android.view.textclassifier.TextClassifierEvent.ConversationActionsEvent) 57 textClassifierEvent); 58 } else if (textClassifierEvent 59 instanceof android.view.textclassifier.TextClassifierEvent.LanguageDetectionEvent) { 60 return fromPlatform( 61 (android.view.textclassifier.TextClassifierEvent.LanguageDetectionEvent) 62 textClassifierEvent); 63 } 64 TcLog.w(TAG, "Unexpected event: " + textClassifierEvent); 65 return null; 66 } 67 fromPlatform( android.view.textclassifier.TextClassifierEvent.TextSelectionEvent textSelectionEvent)68 private static TextSelectionEvent fromPlatform( 69 android.view.textclassifier.TextClassifierEvent.TextSelectionEvent textSelectionEvent) { 70 TextSelectionEvent.Builder builder = 71 new TextSelectionEvent.Builder(textSelectionEvent.getEventType()); 72 copyCommonFields(textSelectionEvent, builder); 73 return builder 74 .setRelativeWordStartIndex(textSelectionEvent.getRelativeWordStartIndex()) 75 .setRelativeWordEndIndex(textSelectionEvent.getRelativeWordEndIndex()) 76 .setRelativeSuggestedWordStartIndex(textSelectionEvent.getRelativeSuggestedWordStartIndex()) 77 .setRelativeSuggestedWordEndIndex(textSelectionEvent.getRelativeSuggestedWordEndIndex()) 78 .build(); 79 } 80 fromPlatform( android.view.textclassifier.TextClassifierEvent.TextLinkifyEvent textLinkifyEvent)81 private static TextLinkifyEvent fromPlatform( 82 android.view.textclassifier.TextClassifierEvent.TextLinkifyEvent textLinkifyEvent) { 83 TextLinkifyEvent.Builder builder = 84 new TextLinkifyEvent.Builder(textLinkifyEvent.getEventType()); 85 copyCommonFields(textLinkifyEvent, builder); 86 return builder.build(); 87 } 88 fromPlatform( android.view.textclassifier.TextClassifierEvent.ConversationActionsEvent conversationActionsEvent)89 private static ConversationActionsEvent fromPlatform( 90 android.view.textclassifier.TextClassifierEvent.ConversationActionsEvent 91 conversationActionsEvent) { 92 ConversationActionsEvent.Builder builder = 93 new ConversationActionsEvent.Builder(conversationActionsEvent.getEventType()); 94 copyCommonFields(conversationActionsEvent, builder); 95 return builder.build(); 96 } 97 fromPlatform( android.view.textclassifier.TextClassifierEvent.LanguageDetectionEvent languageDetectionEvent)98 private static LanguageDetectionEvent fromPlatform( 99 android.view.textclassifier.TextClassifierEvent.LanguageDetectionEvent 100 languageDetectionEvent) { 101 LanguageDetectionEvent.Builder builder = 102 new LanguageDetectionEvent.Builder(languageDetectionEvent.getEventType()); 103 copyCommonFields(languageDetectionEvent, builder); 104 return builder.build(); 105 } 106 107 @Nullable fromPlatform( @ullable android.view.textclassifier.TextClassificationContext textClassificationContext)108 private static TextClassificationContext fromPlatform( 109 @Nullable android.view.textclassifier.TextClassificationContext textClassificationContext) { 110 if (textClassificationContext == null) { 111 return null; 112 } 113 return new TextClassificationContext.Builder( 114 textClassificationContext.getPackageName(), textClassificationContext.getWidgetType()) 115 .setWidgetVersion(textClassificationContext.getWidgetVersion()) 116 .build(); 117 } 118 copyCommonFields( android.view.textclassifier.TextClassifierEvent sourceEvent, TextClassifierEvent.Builder<?> destBuilder)119 private static void copyCommonFields( 120 android.view.textclassifier.TextClassifierEvent sourceEvent, 121 TextClassifierEvent.Builder<?> destBuilder) { 122 destBuilder 123 .setActionIndices(sourceEvent.getActionIndices()) 124 .setEventContext(fromPlatform(sourceEvent.getEventContext())) 125 .setEntityTypes(sourceEvent.getEntityTypes()) 126 .setEventIndex(sourceEvent.getEventIndex()) 127 .setExtras(sourceEvent.getExtras()) 128 .setLocale(sourceEvent.getLocale() == null ? null : sourceEvent.getLocale().toLocale()) 129 .setModelName(sourceEvent.getModelName()) 130 .setResultId(sourceEvent.getResultId()) 131 .setScores(sourceEvent.getScores()); 132 } 133 TextClassifierEventConverter()134 private TextClassifierEventConverter() {} 135 } 136