1 /* 2 * Copyright (C) 2019 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.systemui.screenshot; 18 19 import static android.content.Context.NOTIFICATION_SERVICE; 20 21 import android.app.Notification; 22 import android.app.NotificationManager; 23 import android.app.PendingIntent; 24 import android.app.admin.DevicePolicyManager; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.content.res.Resources; 28 import android.os.UserHandle; 29 import android.util.DisplayMetrics; 30 import android.view.WindowManager; 31 32 import com.android.internal.messages.nano.SystemMessageProto; 33 import com.android.systemui.R; 34 import com.android.systemui.SystemUIApplication; 35 import com.android.systemui.util.NotificationChannels; 36 37 import javax.inject.Inject; 38 39 /** 40 * Convenience class to handle showing and hiding notifications while taking a screenshot. 41 */ 42 public class ScreenshotNotificationsController { 43 private static final String TAG = "ScreenshotNotificationManager"; 44 45 private final Context mContext; 46 private final Resources mResources; 47 private final NotificationManager mNotificationManager; 48 49 @Inject ScreenshotNotificationsController(Context context, WindowManager windowManager)50 ScreenshotNotificationsController(Context context, WindowManager windowManager) { 51 mContext = context; 52 mResources = context.getResources(); 53 mNotificationManager = 54 (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); 55 56 DisplayMetrics displayMetrics = new DisplayMetrics(); 57 windowManager.getDefaultDisplay().getRealMetrics(displayMetrics); 58 } 59 60 /** 61 * Sends a notification that the screenshot capture has failed. 62 */ notifyScreenshotError(int msgResId)63 public void notifyScreenshotError(int msgResId) { 64 Resources res = mContext.getResources(); 65 String errorMsg = res.getString(msgResId); 66 67 // Repurpose the existing notification to notify the user of the error 68 Notification.Builder b = new Notification.Builder(mContext, NotificationChannels.ALERTS) 69 .setTicker(res.getString(R.string.screenshot_failed_title)) 70 .setContentTitle(res.getString(R.string.screenshot_failed_title)) 71 .setContentText(errorMsg) 72 .setSmallIcon(R.drawable.stat_notify_image_error) 73 .setWhen(System.currentTimeMillis()) 74 .setVisibility(Notification.VISIBILITY_PUBLIC) // ok to show outside lockscreen 75 .setCategory(Notification.CATEGORY_ERROR) 76 .setAutoCancel(true) 77 .setColor(mContext.getColor( 78 com.android.internal.R.color.system_notification_accent_color)); 79 final DevicePolicyManager dpm = 80 (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); 81 final Intent intent = 82 dpm.createAdminSupportIntent(DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE); 83 if (intent != null) { 84 final PendingIntent pendingIntent = PendingIntent.getActivityAsUser( 85 mContext, 0, intent, PendingIntent.FLAG_IMMUTABLE, null, UserHandle.CURRENT); 86 b.setContentIntent(pendingIntent); 87 } 88 89 SystemUIApplication.overrideNotificationAppName(mContext, b, true); 90 91 Notification n = new Notification.BigTextStyle(b) 92 .bigText(errorMsg) 93 .build(); 94 mNotificationManager.notify(SystemMessageProto.SystemMessage.NOTE_GLOBAL_SCREENSHOT, n); 95 } 96 } 97