1 /*
2  * Copyright (C) 2019 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.settings.security;
18 
19 import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;
20 
21 import android.app.Activity;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.security.Credentials;
25 import android.view.View;
26 import android.widget.Toast;
27 
28 import androidx.annotation.Nullable;
29 
30 import com.android.settings.R;
31 import com.android.settings.SetupWizardUtils;
32 
33 import com.google.android.setupcompat.template.FooterBarMixin;
34 import com.google.android.setupcompat.template.FooterButton;
35 import com.google.android.setupdesign.GlifLayout;
36 import com.google.android.setupdesign.util.ThemeHelper;
37 
38 /**
39  * Creates a warning dialog explaining the consequences of installing a CA certificate
40  * This is displayed before a CA certificate can be installed from Settings.
41  */
42 public class InstallCaCertificateWarning extends Activity {
43 
44     private static final String CERT_INSTALLER_PACKAGE_NAME = "com.android.certinstaller";
45 
46     @Override
onCreate(@ullable Bundle savedInstanceState)47     public void onCreate(@Nullable Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49 
50         setTheme(SetupWizardUtils.getTheme(this, getIntent()));
51         ThemeHelper.trySetDynamicColor(this);
52         setContentView(R.layout.ca_certificate_warning_dialog);
53         getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
54 
55         final GlifLayout layout = findViewById(R.id.setup_wizard_layout);
56         layout.setHeaderText(R.string.ca_certificate_warning_title);
57 
58         final FooterBarMixin mixin = layout.getMixin(FooterBarMixin.class);
59         mixin.setSecondaryButton(
60                 new FooterButton.Builder(this)
61                         .setText(R.string.certificate_warning_install_anyway)
62                         .setListener(installCaCertificate())
63                         .setButtonType(FooterButton.ButtonType.OTHER)
64                         .setTheme(com.google.android.setupdesign.R.style.SudGlifButton_Secondary)
65                         .build()
66         );
67         mixin.getSecondaryButtonView().setFilterTouchesWhenObscured(true);
68 
69         mixin.setPrimaryButton(
70                 new FooterButton.Builder(this)
71                         .setText(R.string.certificate_warning_dont_install)
72                         .setListener(returnToInstallCertificateFromStorage())
73                         .setButtonType(FooterButton.ButtonType.NEXT)
74                         .setTheme(com.google.android.setupdesign.R.style.SudGlifButton_Primary)
75                         .build()
76         );
77         mixin.getPrimaryButtonView().setFilterTouchesWhenObscured(true);
78     }
79 
installCaCertificate()80     private View.OnClickListener installCaCertificate() {
81         return v -> {
82             final Intent intent = new Intent();
83             intent.setAction(Credentials.INSTALL_ACTION);
84             intent.setPackage(CERT_INSTALLER_PACKAGE_NAME);
85             intent.putExtra(Credentials.EXTRA_CERTIFICATE_USAGE, Credentials.CERTIFICATE_USAGE_CA);
86             startActivity(intent);
87             finish();
88         };
89     }
90 
returnToInstallCertificateFromStorage()91     private View.OnClickListener returnToInstallCertificateFromStorage() {
92         return v -> {
93             Toast.makeText(this, R.string.cert_not_installed, Toast.LENGTH_SHORT).show();
94             finish();
95         };
96     }
97 
98 }
99