1 /* 2 * Copyright (C) 2020 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 package android.ext.services.autofill; 17 18 import android.app.PendingIntent; 19 import android.content.Context; 20 import android.content.IntentSender; 21 import android.os.Bundle; 22 import android.service.autofill.InlinePresentation; 23 import android.service.autofill.InlineSuggestionRenderService; 24 import android.util.Log; 25 import android.view.View; 26 27 import androidx.annotation.NonNull; 28 import androidx.annotation.Nullable; 29 import androidx.autofill.inline.Renderer; 30 import androidx.core.util.Preconditions; 31 32 public class InlineSuggestionRenderServiceImpl extends InlineSuggestionRenderService { 33 34 private static final String TAG = "InlinePresentationRendererServiceImpl"; 35 36 @NonNull 37 @Override onGetInlineSuggestionsRendererInfo()38 public Bundle onGetInlineSuggestionsRendererInfo() { 39 return Renderer.getSupportedInlineUiVersionsAsBundle(); 40 } 41 42 /** 43 * @hide 44 */ 45 @Nullable 46 @Override onRenderSuggestion(@onNull InlinePresentation presentation, int width, int height)47 public View onRenderSuggestion(@NonNull InlinePresentation presentation, 48 int width, int height) { 49 Preconditions.checkNotNull(presentation, TAG + ": InlinePresentation should not be null"); 50 Log.v(TAG, "onRenderSuggestion: width=" + width + ", height=" + height); 51 52 final Bundle style = presentation.getInlinePresentationSpec().getStyle(); 53 View suggestionView = null; 54 PendingIntent attributionIntent = null; 55 if(style != null && !style.isEmpty()) { 56 suggestionView = Renderer.render(this, presentation.getSlice(), style); 57 attributionIntent = Renderer.getAttributionIntent(presentation.getSlice()); 58 } 59 if (suggestionView != null && attributionIntent != null) { 60 final IntentSender attributionIntentSender = attributionIntent.getIntentSender(); 61 suggestionView.setOnLongClickListener((v) -> { 62 startIntentSender(attributionIntentSender); 63 return true; 64 }); 65 } 66 return suggestionView; 67 } 68 69 @Override attachBaseContext(Context newBase)70 protected void attachBaseContext(Context newBase) { 71 super.attachBaseContext(newBase); 72 } 73 } 74