1/*
2 * Copyright (C) 2013 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
17package com.android.settings;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.admin.DevicePolicyManager;
22import android.content.DialogInterface;
23import android.content.DialogInterface.OnClickListener;
24import android.content.DialogInterface.OnDismissListener;
25import android.content.Intent;
26import android.os.Bundle;
27import android.os.UserHandle;
28import android.provider.Settings;
29
30import com.android.settingslib.RestrictedLockUtils;
31
32/**
33 * Activity that shows a dialog explaining that a CA cert is allowing someone to monitor network
34 * traffic. This activity should be launched for the user into which the CA cert is installed
35 * unless Intent.EXTRA_USER_ID is provided.
36 */
37public class MonitoringCertInfoActivity extends Activity implements OnClickListener,
38        OnDismissListener {
39
40    private int mUserId;
41
42    @Override
43    protected void onCreate(Bundle savedStates) {
44        super.onCreate(savedStates);
45
46        mUserId = getIntent().getIntExtra(Intent.EXTRA_USER_ID, UserHandle.myUserId());
47
48        DevicePolicyManager dpm = getSystemService(DevicePolicyManager.class);
49        final int numberOfCertificates = getIntent().getIntExtra(
50                Settings.EXTRA_NUMBER_OF_CERTIFICATES, 1);
51        final int titleId = RestrictedLockUtils.getProfileOrDeviceOwner(this, mUserId) != null
52                ? R.plurals.ssl_ca_cert_settings_button // Check certificate
53                : R.plurals.ssl_ca_cert_dialog_title; // Trust or remove certificate
54        final CharSequence title = getResources().getQuantityText(titleId, numberOfCertificates);
55        setTitle(title);
56
57        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
58        builder.setTitle(title);
59        builder.setCancelable(true);
60        builder.setPositiveButton(getResources().getQuantityText(
61                R.plurals.ssl_ca_cert_settings_button, numberOfCertificates) , this);
62        builder.setNeutralButton(R.string.cancel, null);
63        builder.setOnDismissListener(this);
64
65        if (dpm.getProfileOwnerAsUser(mUserId) != null) {
66            builder.setMessage(getResources().getQuantityString(R.plurals.ssl_ca_cert_info_message,
67                    numberOfCertificates, dpm.getProfileOwnerNameAsUser(mUserId)));
68        } else if (dpm.getDeviceOwnerComponentOnCallingUser() != null) {
69            builder.setMessage(getResources().getQuantityString(
70                    R.plurals.ssl_ca_cert_info_message_device_owner, numberOfCertificates,
71                    dpm.getDeviceOwnerNameOnAnyUser()));
72        } else  {
73            // Consumer case.  Show scary warning.
74            builder.setIcon(android.R.drawable.stat_notify_error);
75            builder.setMessage(R.string.ssl_ca_cert_warning_message);
76        }
77
78        builder.show();
79    }
80
81    @Override
82    public void onClick(DialogInterface dialog, int which) {
83        Intent intent = new Intent(android.provider.Settings.ACTION_TRUSTED_CREDENTIALS_USER);
84        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
85        intent.putExtra(TrustedCredentialsSettings.ARG_SHOW_NEW_FOR_USER, mUserId);
86        startActivity(intent);
87        finish();
88    }
89
90    @Override
91    public void onDismiss(DialogInterface dialogInterface) {
92        finish();
93    }
94}
95