blob: 74c7b580933706f7273a31fe50aef9ed20638cc5 [file] [log] [blame]
Ricky Waie740d342018-09-03 18:10:56 +01001/*
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
17package com.android.packageinstaller;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
Ricky Waic5ea08b2018-09-05 20:55:11 +010022import android.net.Uri;
Ricky Wai13b0abe2018-10-23 11:03:02 +010023import android.provider.Settings;
Ricky Waic5ea08b2018-09-05 20:55:11 +010024import android.util.Log;
Ricky Waie740d342018-09-03 18:10:56 +010025
26/**
27 * Receive new app installed broadcast and notify user new app installed.
28 */
29public class PackageInstalledReceiver extends BroadcastReceiver {
Ricky Waic5ea08b2018-09-05 20:55:11 +010030 private static final String TAG = PackageInstalledReceiver.class.getSimpleName();
Ricky Waie740d342018-09-03 18:10:56 +010031
Ricky Waic5ea08b2018-09-05 20:55:11 +010032 private static final boolean DEBUG = false;
Ricky Waie740d342018-09-03 18:10:56 +010033
34 @Override
35 public void onReceive(Context context, Intent intent) {
Ricky Wai13b0abe2018-10-23 11:03:02 +010036 if (Settings.Global.getInt(context.getContentResolver(),
37 Settings.Global.SHOW_NEW_APP_INSTALLED_NOTIFICATION_ENABLED, 0) == 0) {
Ricky Waic5ea08b2018-09-05 20:55:11 +010038 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 }
Ricky Waie740d342018-09-03 18:10:56 +010072 }
73}