1 /*
2  * Copyright (C) 2023 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.packageinstaller.v2.ui.fragments;
18 
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.os.Bundle;
24 import android.util.Log;
25 import androidx.annotation.NonNull;
26 import androidx.fragment.app.DialogFragment;
27 import com.android.packageinstaller.R;
28 import com.android.packageinstaller.v2.model.InstallStage;
29 import com.android.packageinstaller.v2.model.InstallUserActionRequired;
30 import com.android.packageinstaller.v2.ui.InstallActionListener;
31 
32 /**
33  * Dialog to show when the source of apk can not be identified.
34  */
35 public class AnonymousSourceFragment extends DialogFragment {
36 
37     public static final String LOG_TAG = AnonymousSourceFragment.class.getSimpleName();
38     @NonNull
39     private InstallActionListener mInstallActionListener;
40     @NonNull
41     private AlertDialog mDialog;
42 
43     @Override
onAttach(@onNull Context context)44     public void onAttach(@NonNull Context context) {
45         super.onAttach(context);
46         mInstallActionListener = (InstallActionListener) context;
47     }
48 
49     @NonNull
50     @Override
onCreateDialog(Bundle savedInstanceState)51     public Dialog onCreateDialog(Bundle savedInstanceState) {
52         Log.i(LOG_TAG, "Creating " + LOG_TAG);
53         mDialog = new AlertDialog.Builder(requireContext())
54             .setMessage(R.string.anonymous_source_warning)
55             .setPositiveButton(R.string.anonymous_source_continue,
56                 ((dialog, which) -> mInstallActionListener.onPositiveResponse(
57                     InstallUserActionRequired.USER_ACTION_REASON_ANONYMOUS_SOURCE)))
58             .setNegativeButton(R.string.cancel,
59                 ((dialog, which) -> mInstallActionListener.onNegativeResponse(
60                     InstallStage.STAGE_USER_ACTION_REQUIRED))).create();
61         return mDialog;
62     }
63 
64     @Override
onCancel(@onNull DialogInterface dialog)65     public void onCancel(@NonNull DialogInterface dialog) {
66         super.onCancel(dialog);
67         mInstallActionListener.onNegativeResponse(InstallStage.STAGE_USER_ACTION_REQUIRED);
68     }
69 
70     @Override
onStart()71     public void onStart() {
72         super.onStart();
73         mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setFilterTouchesWhenObscured(true);
74     }
75 
76     @Override
onPause()77     public void onPause() {
78         super.onPause();
79         // This prevents tapjacking since an overlay activity started in front of Pia will
80         // cause Pia to be paused.
81         mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
82     }
83 
84     @Override
onResume()85     public void onResume() {
86         super.onResume();
87         mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(true);
88     }
89 }
90