1 /*
2  * Copyright 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.packageinstaller;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.InstallSourceInfo;
23 import android.content.pm.PackageManager;
24 import android.net.Uri;
25 import android.util.Log;
26 
27 /**
28  * Receive new app installed broadcast and notify user new app installed.
29  */
30 public class PackageInstalledReceiver extends BroadcastReceiver {
31     private static final String TAG = PackageInstalledReceiver.class.getSimpleName();
32 
33     private static final boolean DEBUG = false;
34 
35     @Override
onReceive(Context context, Intent intent)36     public void onReceive(Context context, Intent intent) {
37         if (!context.getPackageManager().shouldShowNewAppInstalledNotification()) {
38             return;
39         }
40 
41         String action = intent.getAction();
42 
43         if (DEBUG) {
44             Log.i(TAG, "Received action: " + action);
45         }
46 
47         if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
48             Uri packageUri = intent.getData();
49             if (packageUri == null) {
50                 return;
51             }
52 
53             String packageName = packageUri.getSchemeSpecificPart();
54             if (packageName == null) {
55                 Log.e(TAG, "No package name");
56                 return;
57             }
58 
59             if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
60                 if (DEBUG) {
61                     Log.i(TAG, "Not new app, skip it: " + packageName);
62                 }
63                 return;
64             }
65 
66             try {
67                 InstallSourceInfo installerInfo =
68                         context.getPackageManager().getInstallSourceInfo(packageName);
69                 String installer = installerInfo.getInstallingPackageName();
70                 if (installer == null) {
71                     Log.e(TAG, "No installer package name for: " + packageName);
72                     return;
73                 }
74                 new PackageInstalledNotificationUtils(context, installer,
75                         packageName).postAppInstalledNotification();
76             } catch (PackageManager.NameNotFoundException e) {
77                 Log.e(TAG, "Cannot get source info for: " + packageName);
78             }
79         }
80     }
81 }
82