summaryrefslogtreecommitdiff
path: root/packages/PackageInstaller/src/com/android/packageinstaller/PackageInstalledNotificationUtils.java
blob: eea12eca18595eaef908a1f215a28e5271fafd82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.packageinstaller;

import android.annotation.NonNull;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageItemInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;

/**
 * A util class that handle and post new app installed notifications.
 */
class PackageInstalledNotificationUtils {
    private static final String TAG = PackageInstalledNotificationUtils.class.getSimpleName();

    private static final String NEW_APP_INSTALLED_CHANNEL_ID_PREFIX = "INSTALLER:";
    private static final String META_DATA_INSTALLER_NOTIFICATION_SMALL_ICON_KEY =
            "com.android.packageinstaller.notification.smallIcon";
    private static final String META_DATA_INSTALLER_NOTIFICATION_COLOR_KEY =
            "com.android.packageinstaller.notification.color";

    private static final float DEFAULT_MAX_LABEL_SIZE_PX = 500f;

    private final Context mContext;
    private final NotificationManager mNotificationManager;

    private final String mInstallerPackage;
    private final String mInstallerAppLabel;
    private final Icon mInstallerAppSmallIcon;
    private final Integer mInstallerAppColor;

    private final String mInstalledPackage;
    private final String mInstalledAppLabel;
    private final Icon mInstalledAppLargeIcon;

    private final String mChannelId;

    PackageInstalledNotificationUtils(@NonNull Context context, @NonNull String installerPackage,
            @NonNull String installedPackage) {
        mContext = context;
        mNotificationManager = context.getSystemService(NotificationManager.class);
        ApplicationInfo installerAppInfo;
        ApplicationInfo installedAppInfo;

        try {
            installerAppInfo = context.getPackageManager().getApplicationInfo(installerPackage,
                    PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            // Should not happen
            throw new IllegalStateException("Unable to get application info: " + installerPackage);
        }
        try {
            installedAppInfo = context.getPackageManager().getApplicationInfo(installedPackage,
                    PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            // Should not happen
            throw new IllegalStateException("Unable to get application info: " + installedPackage);
        }
        mInstallerPackage = installerPackage;
        mInstallerAppLabel = getAppLabel(context, installerAppInfo, installerPackage);
        mInstallerAppSmallIcon = getAppNotificationIcon(context, installerAppInfo);
        mInstallerAppColor = getAppNotificationColor(context, installerAppInfo);

        mInstalledPackage = installedPackage;
        mInstalledAppLabel = getAppLabel(context, installedAppInfo, installerPackage);
        mInstalledAppLargeIcon = getAppLargeIcon(installedAppInfo);

        mChannelId = NEW_APP_INSTALLED_CHANNEL_ID_PREFIX + installerPackage;
    }

    /**
     * Get app label from app's manifest.
     *
     * @param context     A context of the current app
     * @param appInfo     Application info of targeted app
     * @param packageName Package name of targeted app
     * @return The label of targeted application, or package name if label is not found
     */
    private static String getAppLabel(@NonNull Context context, @NonNull ApplicationInfo appInfo,
            @NonNull String packageName) {
        CharSequence label = appInfo.loadSafeLabel(context.getPackageManager(),
                DEFAULT_MAX_LABEL_SIZE_PX,
                PackageItemInfo.SAFE_LABEL_FLAG_TRIM
                        | PackageItemInfo.SAFE_LABEL_FLAG_FIRST_LINE).toString();
        if (label != null) {
            return label.toString();
        }
        return packageName;
    }

    /**
     * The app icon from app's manifest.
     *
     * @param appInfo Application info of targeted app
     * @return App icon of targeted app, or Android default app icon if icon is not found
     */
    private static Icon getAppLargeIcon(@NonNull ApplicationInfo appInfo) {
        if (appInfo.icon != 0) {
            return Icon.createWithResource(appInfo.packageName, appInfo.icon);
        } else {
            return Icon.createWithResource("android", android.R.drawable.sym_def_app_icon);
        }
    }

    /**
     * Get notification icon from installer's manifest meta-data.
     *
     * @param context A context of the current app
     * @param appInfo Installer application info
     * @return Notification icon that listed in installer's manifest meta-data.
     * If icon is not found in meta-data, then it returns Android default download icon.
     */
    private static Icon getAppNotificationIcon(@NonNull Context context,
            @NonNull ApplicationInfo appInfo) {
        if (appInfo.metaData == null) {
            return Icon.createWithResource(context, R.drawable.ic_file_download);
        }

        int iconResId = appInfo.metaData.getInt(
                META_DATA_INSTALLER_NOTIFICATION_SMALL_ICON_KEY, 0);
        if (iconResId != 0) {
            return Icon.createWithResource(appInfo.packageName, iconResId);
        }
        return Icon.createWithResource(context, R.drawable.ic_file_download);
    }

    /**
     * Get notification color from installer's manifest meta-data.
     *
     * @param context A context of the current app
     * @param appInfo Installer application info
     * @return Notification color that listed in installer's manifest meta-data, or null if
     * meta-data is not found.
     */
    private static Integer getAppNotificationColor(@NonNull Context context,
            @NonNull ApplicationInfo appInfo) {
        if (appInfo.metaData == null) {
            return null;
        }

        int colorResId = appInfo.metaData.getInt(
                META_DATA_INSTALLER_NOTIFICATION_COLOR_KEY, 0);
        if (colorResId != 0) {
            try {
                PackageManager pm = context.getPackageManager();
                Resources resources = pm.getResourcesForApplication(appInfo.packageName);
                return resources.getColor(colorResId, context.getTheme());
            } catch (PackageManager.NameNotFoundException e) {
                Log.e(TAG, "Error while loading notification color: " + colorResId + " for "
                        + appInfo.packageName);
            }
        }
        return null;
    }

    private static Intent getAppDetailIntent(@NonNull String packageName) {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.fromParts("package", packageName, null));
        return intent;
    }

    private static Intent resolveIntent(@NonNull Context context, @NonNull Intent i) {
        ResolveInfo result = context.getPackageManager().resolveActivity(i, 0);
        if (result == null) {
            return null;
        }
        return new Intent(i.getAction()).setClassName(result.activityInfo.packageName,
                result.activityInfo.name);
    }

    private static Intent getAppStoreLink(@NonNull Context context,
            @NonNull String installerPackageName, @NonNull String packageName) {
        Intent intent = new Intent(Intent.ACTION_SHOW_APP_INFO)
                .setPackage(installerPackageName);

        Intent result = resolveIntent(context, intent);
        if (result != null) {
            result.putExtra(Intent.EXTRA_PACKAGE_NAME, packageName);
            return result;
        }
        return null;
    }

    /**
     * Create notification channel for showing apps installed notifications.
     */
    private void createChannel() {
        NotificationChannel channel = new NotificationChannel(mChannelId, mInstallerAppLabel,
                NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription(
                mContext.getString(R.string.app_installed_notification_channel_description));
        channel.enableVibration(false);
        channel.setSound(null, null);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        channel.setBlockable(true);

        mNotificationManager.createNotificationChannel(channel);
    }

    /**
     * Returns a pending intent when user clicks on apps installed notification.
     * It should launch the app if possible, otherwise it will return app store's app page.
     * If app store's app page is not available, it will return Android app details page.
     */
    private PendingIntent getInstalledAppLaunchIntent() {
        Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(mInstalledPackage);

        // If installed app does not have a launch intent, bring user to app store page
        if (intent == null) {
            intent = getAppStoreLink(mContext, mInstallerPackage, mInstalledPackage);
        }

        // If app store cannot handle this, bring user to app settings page
        if (intent == null) {
            intent = getAppDetailIntent(mInstalledPackage);
        }

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return PendingIntent.getActivity(mContext, 0 /* request code */, intent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    }

    /**
     * Returns a pending intent that starts installer's launch intent.
     * If it doesn't have a launch intent, it will return installer's Android app details page.
     */
    private PendingIntent getInstallerEntranceIntent() {
        Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(mInstallerPackage);

        // If installer does not have a launch intent, bring user to app settings page
        if (intent == null) {
            intent = getAppDetailIntent(mInstallerPackage);
        }

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return PendingIntent.getActivity(mContext, 0 /* request code */, intent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    }

    /**
     * Returns a notification builder for grouped notifications.
     */
    private Notification.Builder getGroupNotificationBuilder() {
        PendingIntent contentIntent = getInstallerEntranceIntent();

        Bundle extras = new Bundle();
        extras.putString(Notification.EXTRA_SUBSTITUTE_APP_NAME, mInstallerAppLabel);

        Notification.Builder builder =
                new Notification.Builder(mContext, mChannelId)
                        .setSmallIcon(mInstallerAppSmallIcon)
                        .setGroup(mChannelId)
                        .setExtras(extras)
                        .setLocalOnly(true)
                        .setCategory(Notification.CATEGORY_STATUS)
                        .setContentIntent(contentIntent)
                        .setGroupSummary(true);

        if (mInstallerAppColor != null) {
            builder.setColor(mInstallerAppColor);
        }
        return builder;
    }

    /**
     * Returns notification build for individual installed applications.
     */
    private Notification.Builder getAppInstalledNotificationBuilder() {
        PendingIntent contentIntent = getInstalledAppLaunchIntent();

        Bundle extras = new Bundle();
        extras.putString(Notification.EXTRA_SUBSTITUTE_APP_NAME, mInstallerAppLabel);

        String tickerText = String.format(
                mContext.getString(R.string.notification_installation_success_status),
                mInstalledAppLabel);

        Notification.Builder builder =
                new Notification.Builder(mContext, mChannelId)
                        .setAutoCancel(true)
                        .setSmallIcon(mInstallerAppSmallIcon)
                        .setContentTitle(mInstalledAppLabel)
                        .setContentText(mContext.getString(
                                R.string.notification_installation_success_message))
                        .setContentIntent(contentIntent)
                        .setTicker(tickerText)
                        .setCategory(Notification.CATEGORY_STATUS)
                        .setShowWhen(true)
                        .setWhen(System.currentTimeMillis())
                        .setLocalOnly(true)
                        .setGroup(mChannelId)
                        .addExtras(extras)
                        .setStyle(new Notification.BigTextStyle());

        if (mInstalledAppLargeIcon != null) {
            builder.setLargeIcon(mInstalledAppLargeIcon);
        }
        if (mInstallerAppColor != null) {
            builder.setColor(mInstallerAppColor);
        }
        return builder;
    }

    /**
     * Post new app installed notification.
     */
    void postAppInstalledNotification() {
        createChannel();

        // Post app installed notification
        Notification.Builder appNotificationBuilder = getAppInstalledNotificationBuilder();
        mNotificationManager.notify(mInstalledPackage, mInstalledPackage.hashCode(),
                appNotificationBuilder.build());

        // Post installer group notification
        Notification.Builder groupNotificationBuilder = getGroupNotificationBuilder();
        mNotificationManager.notify(mInstallerPackage, mInstallerPackage.hashCode(),
                groupNotificationBuilder.build());
    }
}