• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
23 import android.content.pm.ApplicationInfo;
24 import android.content.pm.PackageItemInfo;
25 import android.content.pm.PackageManager;
26 import android.os.Bundle;
27 import android.util.Log;
28 import android.view.View;
29 import android.widget.TextView;
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 {
39     private static final String TAG = HarmfulAppWarningActivity.class.getSimpleName();
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 
47     @Override
onCreate(Bundle savedInstanceState)48     protected void onCreate(Bundle savedInstanceState) {
49         super.onCreate(savedInstanceState);
50 
51         final Intent intent = getIntent();
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 
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;
71         p.mTitle = getString(R.string.harmful_app_warning_title);
72         p.mView = createView(applicationInfo);
73 
74         p.mPositiveButtonText = getString(R.string.harmful_app_warning_uninstall);
75         p.mPositiveButtonListener = this;
76         p.mNegativeButtonText = getString(R.string.harmful_app_warning_open_anyway);
77         p.mNegativeButtonListener = this;
78 
79         mAlert.installContent(mAlertParams);
80     }
81 
createView(ApplicationInfo applicationInfo)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))
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));
90         ((TextView) view.findViewById(R.id.message))
91                 .setText(mHarmfulAppWarning);
92         return view;
93     }
94 
95     @Override
onClick(DialogInterface dialog, int which)96     public void onClick(DialogInterface dialog, int which) {
97         switch (which) {
98             case DialogInterface.BUTTON_POSITIVE:
99                 getPackageManager().deletePackage(mPackageName, null /*observer*/, 0 /*flags*/);
100                 EventLogTags.writeHarmfulAppWarningUninstall(mPackageName);
101                 finish();
102                 break;
103             case DialogInterface.BUTTON_NEGATIVE:
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);
114                 finish();
115                 break;
116         }
117     }
118 
createHarmfulAppWarningIntent(Context context, String targetPackageName, IntentSender target, CharSequence harmfulAppWarning)119     public static Intent createHarmfulAppWarningIntent(Context context, String targetPackageName,
120             IntentSender target, CharSequence harmfulAppWarning) {
121         final Intent intent = new Intent();
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 }
129