1 /*
2  * Copyright (C) 2013 Roger Chen <cxr514033970@gmail.com>
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.mms.ui;
18 
19 import android.app.Notification;
20 import android.app.NotificationManager;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 
25 import com.android.mms.R;
26 
27 public class SmsStorageMonitor extends BroadcastReceiver {
28     private Context mContext;
29     private final int NOTIFICATION_STORAGE_LIMITED_ID = -1;
30     private static NotificationManager mNotificationManager;
31 
32     @Override
onReceive(Context context, Intent intent)33     public void onReceive(Context context, Intent intent) {
34         mContext = context;
35         if (mNotificationManager == null) {
36             mNotificationManager =
37                     (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
38         }
39         if (intent.getAction().equals(Intent.ACTION_DEVICE_STORAGE_FULL)) {
40             notifyReachStorageLimited();
41         } else if (intent.getAction().equals(Intent.ACTION_DEVICE_STORAGE_NOT_FULL)) {
42             cancelStorageLimitedWarning();
43         }
44     }
45 
notifyReachStorageLimited()46     private void notifyReachStorageLimited() {
47         Notification.Builder mBuilder =
48                 new Notification.Builder(mContext)
49                         .setSmallIcon(R.mipmap.ic_launcher_smsmms)
50                         .setContentTitle(mContext.getString(R.string.storage_warning_title))
51                         .setContentText(mContext.getString(R.string.storage_warning_content))
52                         .setOngoing(true);
53         mNotificationManager.notify(NOTIFICATION_STORAGE_LIMITED_ID, mBuilder.build());
54     }
55 
cancelStorageLimitedWarning()56     private void cancelStorageLimitedWarning() {
57         mNotificationManager.cancel(NOTIFICATION_STORAGE_LIMITED_ID);
58     }
59 }
60