Home | History | Annotate | Download | only in app
      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.PackageManager;
     25 import android.os.Bundle;
     26 import android.util.Log;
     27 import android.view.View;
     28 import android.widget.TextView;
     29 import com.android.internal.R;
     30 
     31 /**
     32  * This dialog is shown to the user before an activity in a harmful app is launched.
     33  *
     34  * See {@code PackageManager.setHarmfulAppInfo} for more info.
     35  */
     36 public class HarmfulAppWarningActivity extends AlertActivity implements
     37         DialogInterface.OnClickListener {
     38     private static final String TAG = HarmfulAppWarningActivity.class.getSimpleName();
     39 
     40     private static final String EXTRA_HARMFUL_APP_WARNING = "harmful_app_warning";
     41 
     42     private String mPackageName;
     43     private String mHarmfulAppWarning;
     44     private IntentSender mTarget;
     45 
     46     @Override
     47     protected void onCreate(Bundle savedInstanceState) {
     48         super.onCreate(savedInstanceState);
     49 
     50         final Intent intent = getIntent();
     51         mPackageName = intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME);
     52         mTarget = intent.getParcelableExtra(Intent.EXTRA_INTENT);
     53         mHarmfulAppWarning = intent.getStringExtra(EXTRA_HARMFUL_APP_WARNING);
     54 
     55         if (mPackageName == null || mTarget == null || mHarmfulAppWarning == null) {
     56             Log.wtf(TAG, "Invalid intent: " + intent.toString());
     57             finish();
     58         }
     59 
     60         final ApplicationInfo applicationInfo;
     61         try {
     62             applicationInfo = getPackageManager().getApplicationInfo(mPackageName, 0 /*flags*/);
     63         } catch (PackageManager.NameNotFoundException e) {
     64             Log.e(TAG, "Could not show warning because package does not exist ", e);
     65             finish();
     66             return;
     67         }
     68 
     69         final AlertController.AlertParams p = mAlertParams;
     70         p.mTitle = getString(R.string.harmful_app_warning_title);
     71         p.mView = createView(applicationInfo);
     72 
     73         p.mPositiveButtonText = getString(R.string.harmful_app_warning_uninstall);
     74         p.mPositiveButtonListener = this;
     75         p.mNegativeButtonText = getString(R.string.harmful_app_warning_open_anyway);
     76         p.mNegativeButtonListener = this;
     77 
     78         mAlert.installContent(mAlertParams);
     79     }
     80 
     81     private View createView(ApplicationInfo applicationInfo) {
     82         final View view = getLayoutInflater().inflate(R.layout.harmful_app_warning_dialog,
     83                 null /*root*/);
     84         ((TextView) view.findViewById(R.id.app_name_text))
     85                 .setText(applicationInfo.loadSafeLabel(getPackageManager()));
     86         ((TextView) view.findViewById(R.id.message))
     87                 .setText(mHarmfulAppWarning);
     88         return view;
     89     }
     90 
     91     @Override
     92     public void onClick(DialogInterface dialog, int which) {
     93         switch (which) {
     94             case DialogInterface.BUTTON_POSITIVE:
     95                 getPackageManager().deletePackage(mPackageName, null /*observer*/, 0 /*flags*/);
     96                 EventLogTags.writeHarmfulAppWarningUninstall(mPackageName);
     97                 finish();
     98                 break;
     99             case DialogInterface.BUTTON_NEGATIVE:
    100                 getPackageManager().setHarmfulAppWarning(mPackageName, null /*warning*/);
    101 
    102                 final IntentSender target = getIntent().getParcelableExtra(Intent.EXTRA_INTENT);
    103                 try {
    104                     startIntentSenderForResult(target, -1 /*requestCode*/, null /*fillInIntent*/,
    105                             0 /*flagsMask*/, 0 /*flagsValue*/, 0 /*extraFlags*/);
    106                 } catch (IntentSender.SendIntentException e) {
    107                     Log.e(TAG, "Error while starting intent sender", e);
    108                 }
    109                 EventLogTags.writeHarmfulAppWarningLaunchAnyway(mPackageName);
    110                 finish();
    111                 break;
    112         }
    113     }
    114 
    115     public static Intent createHarmfulAppWarningIntent(Context context, String targetPackageName,
    116             IntentSender target, CharSequence harmfulAppWarning) {
    117         final Intent intent = new Intent();
    118         intent.setClass(context, HarmfulAppWarningActivity.class);
    119         intent.putExtra(Intent.EXTRA_PACKAGE_NAME, targetPackageName);
    120         intent.putExtra(Intent.EXTRA_INTENT, target);
    121         intent.putExtra(EXTRA_HARMFUL_APP_WARNING, harmfulAppWarning);
    122         return intent;
    123     }
    124 }
    125