1d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/*
2d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Copyright (C) 2015 The Android Open Source Project
3d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
4d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Licensed under the Apache License, Version 2.0 (the "License");
5d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * you may not use this file except in compliance with the License.
6d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * You may obtain a copy of the License at
7d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
8d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *      http://www.apache.org/licenses/LICENSE-2.0
9d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
10d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Unless required by applicable law or agreed to in writing, software
11d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * distributed under the License is distributed on an "AS IS" BASIS,
12d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * See the License for the specific language governing permissions and
14d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * limitations under the License.
15d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
16d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging.sms;
17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.app.Notification;
19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.app.PendingIntent;
20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.Context;
21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.res.Resources;
22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.support.v4.app.NotificationCompat;
23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.support.v4.app.NotificationManagerCompat;
24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.Factory;
26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.R;
27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.ui.UIIntents;
28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.PendingIntentConstants;
29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.PhoneUtils;
30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/**
32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Class that handles SMS auto delete and notification when storage is low
33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class SmsStorageStatusManager {
35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Handles storage low signal for SMS
37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void handleStorageLow() {
39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (!PhoneUtils.getDefault().isSmsEnabled()) {
40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return;
41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // TODO: Auto-delete messages, when that setting exists and is enabled
44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Notify low storage for SMS
46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        postStorageLowNotification();
47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Handles storage OK signal for SMS
51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void handleStorageOk() {
53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (!PhoneUtils.getDefault().isSmsEnabled()) {
54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return;
55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        cancelStorageLowNotification();
57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Post sms storage low notification
61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static void postStorageLowNotification() {
63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Context context = Factory.get().getApplicationContext();
64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Resources resources = context.getResources();
65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final PendingIntent pendingIntent = UIIntents.get()
66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                .getPendingIntentForLowStorageNotifications(context);
67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        builder.setContentTitle(resources.getString(R.string.sms_storage_low_title))
70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                .setTicker(resources.getString(R.string.sms_storage_low_notification_ticker))
71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                .setSmallIcon(R.drawable.ic_failed_light)
72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                .setPriority(Notification.PRIORITY_DEFAULT)
73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                .setOngoing(true)      // Can't be swiped off
74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                .setAutoCancel(false)  // Don't auto cancel
75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                .setContentIntent(pendingIntent);
76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final NotificationCompat.BigTextStyle bigTextStyle =
78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                new NotificationCompat.BigTextStyle(builder);
79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        bigTextStyle.bigText(resources.getString(R.string.sms_storage_low_text));
80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Notification notification = bigTextStyle.build();
81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final NotificationManagerCompat notificationManager =
83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                NotificationManagerCompat.from(Factory.get().getApplicationContext());
84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        notificationManager.notify(getNotificationTag(),
86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                PendingIntentConstants.SMS_STORAGE_LOW_NOTIFICATION_ID, notification);
87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Cancel the notification
91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void cancelStorageLowNotification() {
93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final NotificationManagerCompat notificationManager =
94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                NotificationManagerCompat.from(Factory.get().getApplicationContext());
95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        notificationManager.cancel(getNotificationTag(),
96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                PendingIntentConstants.SMS_STORAGE_LOW_NOTIFICATION_ID);
97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static String getNotificationTag() {
100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return Factory.get().getApplicationContext().getPackageName() + ":smsstoragelow";
101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd}
103