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 */
16package com.android.messaging.sms;
17
18import android.app.Notification;
19import android.app.PendingIntent;
20import android.content.Context;
21import android.content.res.Resources;
22import android.support.v4.app.NotificationCompat;
23import android.support.v4.app.NotificationManagerCompat;
24
25import com.android.messaging.Factory;
26import com.android.messaging.R;
27import com.android.messaging.ui.UIIntents;
28import com.android.messaging.util.PendingIntentConstants;
29import com.android.messaging.util.PhoneUtils;
30
31/**
32 * Class that handles SMS auto delete and notification when storage is low
33 */
34public class SmsStorageStatusManager {
35    /**
36     * Handles storage low signal for SMS
37     */
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     */
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     */
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     */
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
99    private static String getNotificationTag() {
100        return Factory.get().getApplicationContext().getPackageName() + ":smsstoragelow";
101    }
102}
103