1 /* 2 * Copyright (C) 2021 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.car.ui.appstyledview; 18 19 import android.content.Context; 20 import android.view.View; 21 22 import androidx.annotation.NonNull; 23 import androidx.annotation.VisibleForTesting; 24 25 import com.android.car.ui.appstyledview.AppStyledViewController.AppStyledDismissListener; 26 import com.android.car.ui.appstyledview.AppStyledViewController.AppStyledVCloseClickListener; 27 import com.android.car.ui.appstyledview.AppStyledViewController.AppStyledViewNavIcon; 28 import com.android.car.ui.sharedlibrarysupport.SharedLibraryFactorySingleton; 29 30 import java.util.Objects; 31 32 /** 33 * Controller to interact with the app styled view UI. 34 */ 35 public final class AppStyledDialogController { 36 37 @NonNull 38 private AppStyledViewController mAppStyledViewController; 39 @NonNull 40 private AppStyledDialog mDialog; 41 AppStyledDialogController(@onNull Context context)42 public AppStyledDialogController(@NonNull Context context) { 43 Objects.requireNonNull(context); 44 mAppStyledViewController = SharedLibraryFactorySingleton.get(context) 45 .createAppStyledView(context); 46 mDialog = new AppStyledDialog(context, mAppStyledViewController); 47 } 48 49 @VisibleForTesting setAppStyledViewController(AppStyledViewController controller, Context context)50 void setAppStyledViewController(AppStyledViewController controller, Context context) { 51 mAppStyledViewController = controller; 52 mDialog = new AppStyledDialog(context, mAppStyledViewController); 53 } 54 55 /** 56 * Sets the content view to de displayed in AppStyledView. 57 */ setContentView(@onNull View contentView)58 public void setContentView(@NonNull View contentView) { 59 Objects.requireNonNull(contentView); 60 61 mDialog.setContent(contentView); 62 mAppStyledViewController.setOnCloseClickListener(mDialog::dismiss); 63 } 64 65 /** 66 * Sets the nav icon to be used. 67 */ setNavIcon(@ppStyledViewNavIcon int navIcon)68 public void setNavIcon(@AppStyledViewNavIcon int navIcon) { 69 mAppStyledViewController.setNavIcon(navIcon); 70 } 71 72 /** 73 * Displays the dialog to the user with the custom view provided by the app. 74 */ show()75 public void show() { 76 mDialog.show(); 77 } 78 79 /** 80 * Sets the {@link AppStyledVCloseClickListener} 81 */ setOnCloseClickListener(@onNull AppStyledVCloseClickListener listener)82 public void setOnCloseClickListener(@NonNull AppStyledVCloseClickListener listener) { 83 mAppStyledViewController.setOnCloseClickListener(() -> { 84 mDialog.dismiss(); 85 listener.onClick(); 86 }); 87 } 88 89 /** 90 * Sets the {@link AppStyledDismissListener} 91 */ setOnDismissListener(@onNull AppStyledDismissListener listener)92 public void setOnDismissListener(@NonNull AppStyledDismissListener listener) { 93 mDialog.setOnDismissListener(listener); 94 } 95 96 /** 97 * Returns the width of the AppStyledView 98 */ getAppStyledViewDialogWidth()99 public int getAppStyledViewDialogWidth() { 100 return mAppStyledViewController.getDialogWindowLayoutParam( 101 mDialog.getWindowLayoutParams()).width; 102 } 103 104 /** 105 * Returns the height of the AppStyledView 106 */ getAppStyledViewDialogHeight()107 public int getAppStyledViewDialogHeight() { 108 return mAppStyledViewController.getDialogWindowLayoutParam( 109 mDialog.getWindowLayoutParams()).height; 110 } 111 112 @VisibleForTesting getAppStyledDialog()113 AppStyledDialog getAppStyledDialog() { 114 return mDialog; 115 } 116 } 117