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.tv.dialog;
18 
19 import android.annotation.TargetApi;
20 import android.app.Dialog;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.os.Build;
24 import android.os.Bundle;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.view.ViewGroup.LayoutParams;
29 import android.widget.Button;
30 import android.widget.TextView;
31 import com.android.tv.R;
32 import com.android.tv.common.SoftPreconditions;
33 
34 import java.util.function.Function;
35 
36 import dagger.android.AndroidInjection;
37 
38 @TargetApi(Build.VERSION_CODES.TIRAMISU)
39 public class InteractiveAppDialogFragment extends SafeDismissDialogFragment {
40     private static final boolean DEBUG = false;
41 
42     public static final String DIALOG_TAG = InteractiveAppDialogFragment.class.getName();
43     private static final String TRACKER_LABEL = "Interactive App Dialog";
44     private static final String TV_IAPP_NAME = "tv_iapp_name";
45     private boolean mIsChoseOK;
46     private String mIAppName;
47     private Function mUpdateAitInfo;
48 
create(String iappName)49     public static InteractiveAppDialogFragment create(String iappName) {
50         InteractiveAppDialogFragment fragment = new InteractiveAppDialogFragment();
51         Bundle args = new Bundle();
52         args.putString(TV_IAPP_NAME, iappName);
53         fragment.setArguments(args);
54         return fragment;
55     }
56 
57     @Override
onAttach(Context context)58     public void onAttach(Context context) {
59         AndroidInjection.inject(this);
60         super.onAttach(context);
61     }
62 
63     @Override
onCreate(Bundle savedInstanceState)64     public void onCreate(Bundle savedInstanceState) {
65         super.onCreate(savedInstanceState);
66         mIAppName = getArguments().getString(TV_IAPP_NAME);
67         setStyle(STYLE_NO_TITLE, 0);
68     }
69 
70     @Override
onCreateDialog(Bundle savedInstanceState)71     public Dialog onCreateDialog(Bundle savedInstanceState) {
72         Dialog dlg = super.onCreateDialog(savedInstanceState);
73         dlg.getWindow().getAttributes().windowAnimations = R.style.pin_dialog_animation;
74         mIsChoseOK = false;
75         return dlg;
76     }
77 
78     @Override
getTrackerLabel()79     public String getTrackerLabel() {
80         return TRACKER_LABEL;
81     }
82 
83     @Override
onStart()84     public void onStart() {
85         super.onStart();
86         // Dialog size is determined by its windows size, not inflated view size.
87         // So apply view size to window after the DialogFragment.onStart() where dialog is shown.
88         Dialog dlg = getDialog();
89         if (dlg != null) {
90             dlg.getWindow()
91                     .setLayout(
92                             getResources().getDimensionPixelSize(R.dimen.pin_dialog_width),
93                             LayoutParams.WRAP_CONTENT);
94         }
95     }
96 
97     @Override
onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)98     public View onCreateView(
99             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
100         final View v = inflater.inflate(R.layout.tv_app_dialog, container, false);
101         TextView mTitleView = (TextView) v.findViewById(R.id.title);
102         mTitleView.setText(getString(R.string.tv_app_dialog_title, mIAppName));
103         Button okButton = v.findViewById(R.id.ok);
104         okButton.setOnClickListener(new View.OnClickListener() {
105             @Override
106             public void onClick(View v) {
107                 exit(true);
108             }
109         });
110         Button cancelButton = v.findViewById(R.id.cancel);
111         cancelButton.setOnClickListener(new View.OnClickListener() {
112             @Override
113             public void onClick(View v) {
114                 exit(false);
115             }
116         });
117         return v;
118     }
119 
exit(boolean isokclick)120     private void exit(boolean isokclick) {
121         mIsChoseOK = isokclick;
122         dismiss();
123     }
124 
125     @Override
onDismiss(DialogInterface dialog)126     public void onDismiss(DialogInterface dialog) {
127         super.onDismiss(dialog);
128         SoftPreconditions.checkState(getActivity() instanceof OnInteractiveAppCheckedListener);
129         if (getActivity() instanceof OnInteractiveAppCheckedListener) {
130             ((OnInteractiveAppCheckedListener) getActivity())
131                     .onInteractiveAppChecked(mIsChoseOK);
132         }
133     }
134 
135     public interface OnInteractiveAppCheckedListener {
onInteractiveAppChecked(boolean checked)136         void onInteractiveAppChecked(boolean checked);
137     }
138 }
139