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.chassis.car.ui.plugin.appstyleview;
18 
19 import android.content.Context;
20 import android.view.Gravity;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.WindowManager.LayoutParams;
24 import android.widget.ImageView;
25 import android.widget.ScrollView;
26 
27 import com.android.car.ui.plugin.oemapis.appstyledview.AppStyledViewControllerOEMV1;
28 
29 import com.chassis.car.ui.plugin.R;
30 
31 /**
32  * The OEM implementation for {@link AppStyledViewControllerOEMV1} for a AppStyledView.
33  */
34 public class AppStyleViewControllerImpl implements AppStyledViewControllerOEMV1 {
35 
36     private final Context mPluginContext;
37     private final View mAppStyleView;
38 
AppStyleViewControllerImpl(Context pluginContext)39     public AppStyleViewControllerImpl(Context pluginContext) {
40         mPluginContext = pluginContext;
41         LayoutInflater inflater = LayoutInflater.from(mPluginContext);
42         mAppStyleView = inflater.inflate(R.layout.app_styled_view, null, false);
43     }
44 
45     @Override
getView()46     public View getView() {
47         return mAppStyleView;
48     }
49 
50     @Override
setContent(View content)51     public void setContent(View content) {
52         ScrollView scrollview = mAppStyleView.requireViewById(R.id.app_styled_content);
53         scrollview.removeAllViews();
54         scrollview.addView(content);
55     }
56 
57     @Override
setOnBackClickListener(Runnable listener)58     public void setOnBackClickListener(Runnable listener) {
59         ImageView navIconView = mAppStyleView.requireViewById(R.id.app_styled_view_icon_close);
60         if (listener == null) {
61             navIconView.setOnClickListener(null);
62             navIconView.setClickable(false);
63         } else {
64             navIconView.setOnClickListener(v -> listener.run());
65         }
66     }
67 
68     @Override
setNavIcon(int navIcon)69     public void setNavIcon(int navIcon) {
70         ImageView navIconView = mAppStyleView.requireViewById(R.id.app_styled_view_icon_close);
71         navIconView.setVisibility(navIcon == AppStyledViewControllerOEMV1.NAV_ICON_DISABLED
72                 ? View.INVISIBLE : View.VISIBLE);
73         if (navIcon == AppStyledViewControllerOEMV1.NAV_ICON_BACK) {
74             navIconView.setImageResource(R.drawable.icon_back);
75         } else if (navIcon == AppStyledViewControllerOEMV1.NAV_ICON_CLOSE) {
76             navIconView.setImageResource(R.drawable.icon_close);
77         }
78     }
79 
80     @Override
getDialogWindowLayoutParam(LayoutParams params)81     public LayoutParams getDialogWindowLayoutParam(LayoutParams params) {
82         params.width = Math.round(
83                 mPluginContext.getResources().getDimension(R.dimen.app_styled_dialog_width));
84         params.height = Math.round(
85                 mPluginContext.getResources().getDimension(R.dimen.app_styled_dialog_height));
86         params.gravity = Gravity.TOP | Gravity.START;
87         params.x = Math.round(
88                 mPluginContext.getResources().getDimension(R.dimen.app_styled_dialog_position_x));
89         params.y = Math.round(
90                 mPluginContext.getResources().getDimension(R.dimen.app_styled_dialog_position_y));
91         return params;
92     }
93 }
94