47fb491d17a084eef069e0d592cce18116c1c526
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / managers / OCNotificationManager.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android.files.managers;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import com.owncloud.android.R;
25 import com.owncloud.android.utils.DisplayUtils;
26
27 import android.app.Notification;
28 import android.app.NotificationManager;
29 import android.content.Context;
30 import android.widget.RemoteViews;
31
32
33 public class OCNotificationManager {
34
35 enum NotificationType {
36 NOTIFICATION_SIMPLE,
37 NOTIFICATION_PROGRESS
38 }
39
40 static public class NotificationData {
41 private String mText, mSubtitle;
42 private int mPercent;
43 private boolean mOngoing;
44
45 public NotificationData(String text, String subtitle, boolean ongoing) {
46 this(text, subtitle, -1, ongoing);
47 }
48
49 public NotificationData(int percent, boolean ongoing) {
50 this(null, null, percent, ongoing);
51 }
52
53 public NotificationData(String text, int percent, boolean ongoing) {
54 this(text, null, percent, ongoing);
55 }
56
57 public NotificationData(String text, String subtitle, int percent, boolean ongoing) {
58 mText = text;
59 mPercent = percent;
60 mSubtitle = subtitle;
61 mOngoing = ongoing;
62 }
63
64 public String getText() { return mText; }
65 public int getPercent() { return mPercent; }
66 public String getSubtitle() { return mSubtitle; }
67 public boolean getOngoing() { return mOngoing; }
68 }
69
70 static private OCNotificationManager mInstance = null;
71
72 private class NotificationTypePair {
73 public Notification mNotificaiton;
74 public NotificationType mType;
75 public NotificationTypePair(Notification n, NotificationType type) {
76 mNotificaiton = n;
77 mType = type;
78 }
79 }
80
81 private Context mContext;
82 private Map<Integer, NotificationTypePair> mNotificationMap;
83 private int mNotificationCounter;
84 NotificationManager mNM;
85
86 static OCNotificationManager getInstance(Context context) {
87 if (mInstance == null)
88 mInstance = new OCNotificationManager(context);
89 return mInstance;
90 }
91
92 OCNotificationManager(Context context) {
93 mContext = context;
94 mNotificationMap = new HashMap<Integer, NotificationTypePair>();
95 mNM = (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
96 mNotificationCounter = 0;
97 }
98
99 public int postNotification(NotificationType type, NotificationData data) {
100 mNotificationCounter++;
101 Notification notification = null;
102
103 switch (type) {
104 case NOTIFICATION_SIMPLE:
105 notification = new Notification(DisplayUtils.getSeasonalIconId(), data.getText(), System.currentTimeMillis());
106 break;
107 case NOTIFICATION_PROGRESS:
108 notification = new Notification();
109 notification.contentView = new RemoteViews(mContext.getPackageName(), R.layout.progressbar_layout);
110 notification.contentView.setTextViewText(R.id.status_text,
111 data.getText());
112 notification.contentView.setImageViewResource(R.id.status_icon,
113 R.id.icon);
114 notification.contentView.setProgressBar(R.id.status_progress,
115 100,
116 data.getPercent(),
117 false);
118 break;
119 default:
120 return -1;
121 }
122 if (data.getOngoing()) {
123 notification.flags |= notification.flags | Notification.FLAG_ONGOING_EVENT;
124 }
125
126 mNotificationMap.put(mNotificationCounter, new NotificationTypePair(notification, type));
127 return mNotificationCounter;
128 }
129
130 public boolean updateNotification(int notification_id, NotificationData data) {
131 if (!mNotificationMap.containsKey(notification_id)) {
132 return false;
133 }
134 NotificationTypePair pair = mNotificationMap.get(notification_id);
135 switch (pair.mType) {
136 case NOTIFICATION_PROGRESS:
137 pair.mNotificaiton.contentView.setProgressBar(R.id.status_text,
138 100,
139 data.getPercent(),
140 false);
141 return true;
142 case NOTIFICATION_SIMPLE:
143 pair.mNotificaiton = new Notification(DisplayUtils.getSeasonalIconId(),
144 data.getText(), System.currentTimeMillis());
145 mNM.notify(notification_id, pair.mNotificaiton);
146 return true;
147 default:
148 return false;
149 }
150 }
151
152 public void discardNotification(int notification_id) {
153 mNM.cancel(notification_id);
154 mNotificationMap.remove(notification_id);
155 }
156 }