blob: ce2d229d41b328ddd5a6b1d691b70fdd53a07ee9 [file] [log] [blame]
Ben Gruver1ab3d6e2017-12-07 13:45:08 -08001/*
2 * Copyright (C) 2018 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.internal.app;
18
19import android.content.Context;
20import android.content.DialogInterface;
21import android.content.Intent;
22import android.content.IntentSender;
Ben Gruverc7ffbe62018-01-19 11:28:04 -080023import android.content.pm.ApplicationInfo;
Philip P. Moltmanna6f5c702018-04-23 09:09:16 -070024import android.content.pm.PackageItemInfo;
Ben Gruverc7ffbe62018-01-19 11:28:04 -080025import android.content.pm.PackageManager;
Ben Gruver1ab3d6e2017-12-07 13:45:08 -080026import android.os.Bundle;
27import android.util.Log;
Ben Gruverc7ffbe62018-01-19 11:28:04 -080028import android.view.View;
29import android.widget.TextView;
Ben Gruver1ab3d6e2017-12-07 13:45:08 -080030import com.android.internal.R;
31
32/**
33 * This dialog is shown to the user before an activity in a harmful app is launched.
34 *
35 * See {@code PackageManager.setHarmfulAppInfo} for more info.
36 */
37public class HarmfulAppWarningActivity extends AlertActivity implements
38 DialogInterface.OnClickListener {
Ben Gruverc7ffbe62018-01-19 11:28:04 -080039 private static final String TAG = HarmfulAppWarningActivity.class.getSimpleName();
Ben Gruver1ab3d6e2017-12-07 13:45:08 -080040
41 private static final String EXTRA_HARMFUL_APP_WARNING = "harmful_app_warning";
42
43 private String mPackageName;
44 private String mHarmfulAppWarning;
45 private IntentSender mTarget;
46
Ben Gruver1ab3d6e2017-12-07 13:45:08 -080047 @Override
48 protected void onCreate(Bundle savedInstanceState) {
49 super.onCreate(savedInstanceState);
50
Ben Gruverc7ffbe62018-01-19 11:28:04 -080051 final Intent intent = getIntent();
Ben Gruver1ab3d6e2017-12-07 13:45:08 -080052 mPackageName = intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME);
53 mTarget = intent.getParcelableExtra(Intent.EXTRA_INTENT);
54 mHarmfulAppWarning = intent.getStringExtra(EXTRA_HARMFUL_APP_WARNING);
55
56 if (mPackageName == null || mTarget == null || mHarmfulAppWarning == null) {
57 Log.wtf(TAG, "Invalid intent: " + intent.toString());
58 finish();
59 }
60
Ben Gruverc7ffbe62018-01-19 11:28:04 -080061 final ApplicationInfo applicationInfo;
62 try {
63 applicationInfo = getPackageManager().getApplicationInfo(mPackageName, 0 /*flags*/);
64 } catch (PackageManager.NameNotFoundException e) {
65 Log.e(TAG, "Could not show warning because package does not exist ", e);
66 finish();
67 return;
68 }
69
70 final AlertController.AlertParams p = mAlertParams;
Ben Gruver1ab3d6e2017-12-07 13:45:08 -080071 p.mTitle = getString(R.string.harmful_app_warning_title);
Ben Gruverc7ffbe62018-01-19 11:28:04 -080072 p.mView = createView(applicationInfo);
73
74 p.mPositiveButtonText = getString(R.string.harmful_app_warning_uninstall);
Ben Gruver1ab3d6e2017-12-07 13:45:08 -080075 p.mPositiveButtonListener = this;
Ben Gruverc7ffbe62018-01-19 11:28:04 -080076 p.mNegativeButtonText = getString(R.string.harmful_app_warning_open_anyway);
Ben Gruver1ab3d6e2017-12-07 13:45:08 -080077 p.mNegativeButtonListener = this;
78
79 mAlert.installContent(mAlertParams);
80 }
81
Ben Gruverc7ffbe62018-01-19 11:28:04 -080082 private View createView(ApplicationInfo applicationInfo) {
83 final View view = getLayoutInflater().inflate(R.layout.harmful_app_warning_dialog,
84 null /*root*/);
85 ((TextView) view.findViewById(R.id.app_name_text))
Philip P. Moltmanna6f5c702018-04-23 09:09:16 -070086 .setText(applicationInfo.loadSafeLabel(getPackageManager(),
87 PackageItemInfo.DEFAULT_MAX_LABEL_SIZE_PX,
88 PackageItemInfo.SAFE_LABEL_FLAG_FIRST_LINE
89 | PackageItemInfo.SAFE_LABEL_FLAG_TRIM));
Ben Gruverc7ffbe62018-01-19 11:28:04 -080090 ((TextView) view.findViewById(R.id.message))
91 .setText(mHarmfulAppWarning);
92 return view;
93 }
94
Ben Gruver1ab3d6e2017-12-07 13:45:08 -080095 @Override
96 public void onClick(DialogInterface dialog, int which) {
97 switch (which) {
98 case DialogInterface.BUTTON_POSITIVE:
Ben Gruverc7ffbe62018-01-19 11:28:04 -080099 getPackageManager().deletePackage(mPackageName, null /*observer*/, 0 /*flags*/);
100 EventLogTags.writeHarmfulAppWarningUninstall(mPackageName);
Ben Gruver1ab3d6e2017-12-07 13:45:08 -0800101 finish();
102 break;
103 case DialogInterface.BUTTON_NEGATIVE:
Ben Gruverc7ffbe62018-01-19 11:28:04 -0800104 getPackageManager().setHarmfulAppWarning(mPackageName, null /*warning*/);
105
106 final IntentSender target = getIntent().getParcelableExtra(Intent.EXTRA_INTENT);
107 try {
108 startIntentSenderForResult(target, -1 /*requestCode*/, null /*fillInIntent*/,
109 0 /*flagsMask*/, 0 /*flagsValue*/, 0 /*extraFlags*/);
110 } catch (IntentSender.SendIntentException e) {
111 Log.e(TAG, "Error while starting intent sender", e);
112 }
113 EventLogTags.writeHarmfulAppWarningLaunchAnyway(mPackageName);
Ben Gruver1ab3d6e2017-12-07 13:45:08 -0800114 finish();
115 break;
116 }
117 }
118
119 public static Intent createHarmfulAppWarningIntent(Context context, String targetPackageName,
120 IntentSender target, CharSequence harmfulAppWarning) {
Ben Gruverc7ffbe62018-01-19 11:28:04 -0800121 final Intent intent = new Intent();
Ben Gruver1ab3d6e2017-12-07 13:45:08 -0800122 intent.setClass(context, HarmfulAppWarningActivity.class);
123 intent.putExtra(Intent.EXTRA_PACKAGE_NAME, targetPackageName);
124 intent.putExtra(Intent.EXTRA_INTENT, target);
125 intent.putExtra(EXTRA_HARMFUL_APP_WARNING, harmfulAppWarning);
126 return intent;
127 }
128}