Ricky Wai | e740d34 | 2018-09-03 18:10:56 +0100 | [diff] [blame] | 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; |
Ricky Wai | c5ea08b | 2018-09-05 20:55:11 +0100 | [diff] [blame] | 22 | import android.net.Uri; |
Ricky Wai | 13b0abe | 2018-10-23 11:03:02 +0100 | [diff] [blame] | 23 | import android.provider.Settings; |
Ricky Wai | c5ea08b | 2018-09-05 20:55:11 +0100 | [diff] [blame] | 24 | import android.util.Log; |
Ricky Wai | e740d34 | 2018-09-03 18:10:56 +0100 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Receive new app installed broadcast and notify user new app installed. |
| 28 | */ |
| 29 | public class PackageInstalledReceiver extends BroadcastReceiver { |
Ricky Wai | c5ea08b | 2018-09-05 20:55:11 +0100 | [diff] [blame] | 30 | private static final String TAG = PackageInstalledReceiver.class.getSimpleName(); |
Ricky Wai | e740d34 | 2018-09-03 18:10:56 +0100 | [diff] [blame] | 31 | |
Ricky Wai | c5ea08b | 2018-09-05 20:55:11 +0100 | [diff] [blame] | 32 | private static final boolean DEBUG = false; |
Ricky Wai | e740d34 | 2018-09-03 18:10:56 +0100 | [diff] [blame] | 33 | |
| 34 | @Override |
| 35 | public void onReceive(Context context, Intent intent) { |
Ricky Wai | 13b0abe | 2018-10-23 11:03:02 +0100 | [diff] [blame] | 36 | if (Settings.Global.getInt(context.getContentResolver(), |
| 37 | Settings.Global.SHOW_NEW_APP_INSTALLED_NOTIFICATION_ENABLED, 0) == 0) { |
Ricky Wai | c5ea08b | 2018-09-05 20:55:11 +0100 | [diff] [blame] | 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 | } |
Ricky Wai | e740d34 | 2018-09-03 18:10:56 +0100 | [diff] [blame] | 72 | } |
| 73 | } |