1 /* 2 * Copyright (C) 2022 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.odpclient; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.ondevicepersonalization.OnDevicePersonalizationManager; 22 import android.ondevicepersonalization.SlotResultHandle; 23 import android.os.Bundle; 24 import android.os.Handler; 25 import android.os.Looper; 26 import android.os.OutcomeReceiver; 27 import android.os.PersistableBundle; 28 import android.util.Log; 29 import android.view.SurfaceControlViewHost.SurfacePackage; 30 import android.view.SurfaceView; 31 import android.view.View; 32 import android.widget.Button; 33 import android.widget.EditText; 34 import android.widget.Toast; 35 36 import java.util.List; 37 import java.util.concurrent.CountDownLatch; 38 import java.util.concurrent.Executors; 39 import java.util.concurrent.atomic.AtomicReference; 40 41 public class MainActivity extends Activity { 42 private static final String TAG = "OdpClient"; 43 private OnDevicePersonalizationManager mOdpManager = null; 44 45 private EditText mTextBox; 46 private Button mGetAdButton; 47 private SurfaceView mRenderedView; 48 49 private Context mContext; 50 51 @Override onCreate(Bundle savedInstanceState)52 public void onCreate(Bundle savedInstanceState) { 53 super.onCreate(savedInstanceState); 54 setContentView(R.layout.activity_main); 55 mContext = getApplicationContext(); 56 if (mOdpManager == null) { 57 mOdpManager = mContext.getSystemService(OnDevicePersonalizationManager.class); 58 } 59 mRenderedView = findViewById(R.id.rendered_view); 60 mRenderedView.setVisibility(View.INVISIBLE); 61 mGetAdButton = findViewById(R.id.get_ad_button); 62 mTextBox = findViewById(R.id.text_box); 63 registerGetAdButton(); 64 } 65 registerGetAdButton()66 private void registerGetAdButton() { 67 mGetAdButton.setOnClickListener( 68 v -> makeRequest()); 69 } 70 makeRequest()71 private void makeRequest() { 72 try { 73 if (mOdpManager == null) { 74 makeToast("OnDevicePersonalizationManager is null"); 75 return; 76 } 77 CountDownLatch latch = new CountDownLatch(1); 78 Log.i(TAG, "Starting execute()"); 79 AtomicReference<SlotResultHandle> slotResultHandle = new AtomicReference<>(); 80 PersistableBundle appParams = new PersistableBundle(); 81 appParams.putString("keyword", mTextBox.getText().toString()); 82 mOdpManager.execute( 83 "com.android.odpsamplenetwork", 84 appParams, 85 Executors.newSingleThreadExecutor(), 86 new OutcomeReceiver<List<SlotResultHandle>, Exception>() { 87 @Override 88 public void onResult(List<SlotResultHandle> result) { 89 makeToast("execute() success: " + result.size()); 90 if (result.size() > 0) { 91 slotResultHandle.set(result.get(0)); 92 } else { 93 Log.e(TAG, "No results!"); 94 } 95 latch.countDown(); 96 } 97 98 @Override 99 public void onError(Exception e) { 100 makeToast("execute() error: " + e.toString()); 101 latch.countDown(); 102 } 103 }); 104 latch.await(); 105 Log.d(TAG, "wait success"); 106 mOdpManager.requestSurfacePackage( 107 slotResultHandle.get(), 108 mRenderedView.getHostToken(), 109 getDisplay().getDisplayId(), 110 mRenderedView.getWidth(), 111 mRenderedView.getHeight(), 112 Executors.newSingleThreadExecutor(), 113 new OutcomeReceiver<SurfacePackage, Exception>() { 114 @Override 115 public void onResult(SurfacePackage surfacePackage) { 116 makeToast( 117 "requestSurfacePackage() success: " 118 + surfacePackage.toString()); 119 new Handler(Looper.getMainLooper()).post(() -> { 120 if (surfacePackage != null) { 121 mRenderedView.setChildSurfacePackage( 122 surfacePackage); 123 } 124 mRenderedView.setZOrderOnTop(true); 125 mRenderedView.setVisibility(View.VISIBLE); 126 }); 127 } 128 129 @Override 130 public void onError(Exception e) { 131 makeToast("requestSurfacePackage() error: " + e.toString()); 132 } 133 }); 134 } catch (Exception e) { 135 Log.e(TAG, "Error", e); 136 } 137 } 138 makeToast(String message)139 private void makeToast(String message) { 140 Log.i(TAG, message); 141 runOnUiThread(() -> Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show()); 142 } 143 } 144