• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 package com.android.messaging.sms;
17 
18 import android.app.Notification;
19 import android.app.PendingIntent;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import androidx.core.app.NotificationCompat;
23 import androidx.core.app.NotificationManagerCompat;
24 
25 import com.android.messaging.Factory;
26 import com.android.messaging.R;
27 import com.android.messaging.ui.UIIntents;
28 import com.android.messaging.util.PendingIntentConstants;
29 import com.android.messaging.util.PhoneUtils;
30 
31 /**
32  * Class that handles SMS auto delete and notification when storage is low
33  */
34 public class SmsStorageStatusManager {
35     /**
36      * Handles storage low signal for SMS
37      */
handleStorageLow()38     public static void handleStorageLow() {
39         if (!PhoneUtils.getDefault().isSmsEnabled()) {
40             return;
41         }
42 
43         // TODO: Auto-delete messages, when that setting exists and is enabled
44 
45         // Notify low storage for SMS
46         postStorageLowNotification();
47     }
48 
49     /**
50      * Handles storage OK signal for SMS
51      */
handleStorageOk()52     public static void handleStorageOk() {
53         if (!PhoneUtils.getDefault().isSmsEnabled()) {
54             return;
55         }
56         cancelStorageLowNotification();
57     }
58 
59     /**
60      * Post sms storage low notification
61      */
postStorageLowNotification()62     private static void postStorageLowNotification() {
63         final Context context = Factory.get().getApplicationContext();
64         final Resources resources = context.getResources();
65         final PendingIntent pendingIntent = UIIntents.get()
66                 .getPendingIntentForLowStorageNotifications(context);
67 
68         final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
69         builder.setContentTitle(resources.getString(R.string.sms_storage_low_title))
70                 .setTicker(resources.getString(R.string.sms_storage_low_notification_ticker))
71                 .setSmallIcon(R.drawable.ic_failed_light)
72                 .setPriority(Notification.PRIORITY_DEFAULT)
73                 .setOngoing(true)      // Can't be swiped off
74                 .setAutoCancel(false)  // Don't auto cancel
75                 .setContentIntent(pendingIntent);
76 
77         final NotificationCompat.BigTextStyle bigTextStyle =
78                 new NotificationCompat.BigTextStyle(builder);
79         bigTextStyle.bigText(resources.getString(R.string.sms_storage_low_text));
80         final Notification notification = bigTextStyle.build();
81 
82         final NotificationManagerCompat notificationManager =
83                 NotificationManagerCompat.from(Factory.get().getApplicationContext());
84 
85         notificationManager.notify(getNotificationTag(),
86                 PendingIntentConstants.SMS_STORAGE_LOW_NOTIFICATION_ID, notification);
87     }
88 
89     /**
90      * Cancel the notification
91      */
cancelStorageLowNotification()92     public static void cancelStorageLowNotification() {
93         final NotificationManagerCompat notificationManager =
94                 NotificationManagerCompat.from(Factory.get().getApplicationContext());
95         notificationManager.cancel(getNotificationTag(),
96                 PendingIntentConstants.SMS_STORAGE_LOW_NOTIFICATION_ID);
97     }
98 
getNotificationTag()99     private static String getNotificationTag() {
100         return Factory.get().getApplicationContext().getPackageName() + ":smsstoragelow";
101     }
102 }
103