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.net.Uri;
23 import android.provider.Settings;
24 import android.util.Log;
25 
26 /**
27  * Receive new app installed broadcast and notify user new app installed.
28  */
29 public class PackageInstalledReceiver extends BroadcastReceiver {
30     private static final String TAG = PackageInstalledReceiver.class.getSimpleName();
31 
32     private static final boolean DEBUG = false;
33 
34     @Override
onReceive(Context context, Intent intent)35     public void onReceive(Context context, Intent intent) {
36         if (Settings.Global.getInt(context.getContentResolver(),
37                 Settings.Global.SHOW_NEW_APP_INSTALLED_NOTIFICATION_ENABLED, 0) == 0) {
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             // TODO: Make sure the installer information here is accurate
67             String installer =
68                     context.getPackageManager().getInstallerPackageName(packageName);
69             new PackageInstalledNotificationUtils(context, installer,
70                     packageName).postAppInstalledNotification();
71         }
72     }
73 }
74