Ben Gruver | 1ab3d6e | 2017-12-07 13:45:08 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.internal.app; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.DialogInterface; |
| 21 | import android.content.Intent; |
| 22 | import android.content.IntentSender; |
Ben Gruver | c7ffbe6 | 2018-01-19 11:28:04 -0800 | [diff] [blame] | 23 | import android.content.pm.ApplicationInfo; |
Philip P. Moltmann | a6f5c70 | 2018-04-23 09:09:16 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageItemInfo; |
Ben Gruver | c7ffbe6 | 2018-01-19 11:28:04 -0800 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
Ben Gruver | 1ab3d6e | 2017-12-07 13:45:08 -0800 | [diff] [blame] | 26 | import android.os.Bundle; |
| 27 | import android.util.Log; |
Ben Gruver | c7ffbe6 | 2018-01-19 11:28:04 -0800 | [diff] [blame] | 28 | import android.view.View; |
| 29 | import android.widget.TextView; |
Ben Gruver | 1ab3d6e | 2017-12-07 13:45:08 -0800 | [diff] [blame] | 30 | import 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 | */ |
| 37 | public class HarmfulAppWarningActivity extends AlertActivity implements |
| 38 | DialogInterface.OnClickListener { |
Ben Gruver | c7ffbe6 | 2018-01-19 11:28:04 -0800 | [diff] [blame] | 39 | private static final String TAG = HarmfulAppWarningActivity.class.getSimpleName(); |
Ben Gruver | 1ab3d6e | 2017-12-07 13:45:08 -0800 | [diff] [blame] | 40 | |
| 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 Gruver | 1ab3d6e | 2017-12-07 13:45:08 -0800 | [diff] [blame] | 47 | @Override |
| 48 | protected void onCreate(Bundle savedInstanceState) { |
| 49 | super.onCreate(savedInstanceState); |
| 50 | |
Ben Gruver | c7ffbe6 | 2018-01-19 11:28:04 -0800 | [diff] [blame] | 51 | final Intent intent = getIntent(); |
Ben Gruver | 1ab3d6e | 2017-12-07 13:45:08 -0800 | [diff] [blame] | 52 | 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 Gruver | c7ffbe6 | 2018-01-19 11:28:04 -0800 | [diff] [blame] | 61 | 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 Gruver | 1ab3d6e | 2017-12-07 13:45:08 -0800 | [diff] [blame] | 71 | p.mTitle = getString(R.string.harmful_app_warning_title); |
Ben Gruver | c7ffbe6 | 2018-01-19 11:28:04 -0800 | [diff] [blame] | 72 | p.mView = createView(applicationInfo); |
| 73 | |
| 74 | p.mPositiveButtonText = getString(R.string.harmful_app_warning_uninstall); |
Ben Gruver | 1ab3d6e | 2017-12-07 13:45:08 -0800 | [diff] [blame] | 75 | p.mPositiveButtonListener = this; |
Ben Gruver | c7ffbe6 | 2018-01-19 11:28:04 -0800 | [diff] [blame] | 76 | p.mNegativeButtonText = getString(R.string.harmful_app_warning_open_anyway); |
Ben Gruver | 1ab3d6e | 2017-12-07 13:45:08 -0800 | [diff] [blame] | 77 | p.mNegativeButtonListener = this; |
| 78 | |
| 79 | mAlert.installContent(mAlertParams); |
| 80 | } |
| 81 | |
Ben Gruver | c7ffbe6 | 2018-01-19 11:28:04 -0800 | [diff] [blame] | 82 | 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. Moltmann | a6f5c70 | 2018-04-23 09:09:16 -0700 | [diff] [blame] | 86 | .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 Gruver | c7ffbe6 | 2018-01-19 11:28:04 -0800 | [diff] [blame] | 90 | ((TextView) view.findViewById(R.id.message)) |
| 91 | .setText(mHarmfulAppWarning); |
| 92 | return view; |
| 93 | } |
| 94 | |
Ben Gruver | 1ab3d6e | 2017-12-07 13:45:08 -0800 | [diff] [blame] | 95 | @Override |
| 96 | public void onClick(DialogInterface dialog, int which) { |
| 97 | switch (which) { |
| 98 | case DialogInterface.BUTTON_POSITIVE: |
Ben Gruver | c7ffbe6 | 2018-01-19 11:28:04 -0800 | [diff] [blame] | 99 | getPackageManager().deletePackage(mPackageName, null /*observer*/, 0 /*flags*/); |
| 100 | EventLogTags.writeHarmfulAppWarningUninstall(mPackageName); |
Ben Gruver | 1ab3d6e | 2017-12-07 13:45:08 -0800 | [diff] [blame] | 101 | finish(); |
| 102 | break; |
| 103 | case DialogInterface.BUTTON_NEGATIVE: |
Ben Gruver | c7ffbe6 | 2018-01-19 11:28:04 -0800 | [diff] [blame] | 104 | 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 Gruver | 1ab3d6e | 2017-12-07 13:45:08 -0800 | [diff] [blame] | 114 | finish(); |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public static Intent createHarmfulAppWarningIntent(Context context, String targetPackageName, |
| 120 | IntentSender target, CharSequence harmfulAppWarning) { |
Ben Gruver | c7ffbe6 | 2018-01-19 11:28:04 -0800 | [diff] [blame] | 121 | final Intent intent = new Intent(); |
Ben Gruver | 1ab3d6e | 2017-12-07 13:45:08 -0800 | [diff] [blame] | 122 | 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 | } |