Merge branch 'develop' into refactor_remote_operation_to_rename_folder
[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
26 import android.app.Notification;
27 import android.app.NotificationManager;
28 import android.content.Context;
29 import android.widget.RemoteViews;
30
31
32 public class OCNotificationManager {
33
34 enum NotificationType {
35 NOTIFICATION_SIMPLE,
36 NOTIFICATION_PROGRESS
37 }
38
39 static public class NotificationData {
40 private String mText, mSubtitle;
41 private int mPercent;
42 private boolean mOngoing;
43
44 public NotificationData(String text, String subtitle, boolean ongoing) {
45 this(text, subtitle, -1, ongoing);
46 }
47
48 public NotificationData(int percent, boolean ongoing) {
49 this(null, null, percent, ongoing);
50 }
51
52 public NotificationData(String text, int percent, boolean ongoing) {
53 this(text, null, percent, ongoing);
54 }
55
56 public NotificationData(String text, String subtitle, int percent, boolean ongoing) {
57 mText = text;
58 mPercent = percent;
59 mSubtitle = subtitle;
60 mOngoing = ongoing;
61 }
62
63 public String getText() { return mText; }
64 public int getPercent() { return mPercent; }
65 public String getSubtitle() { return mSubtitle; }
66 public boolean getOngoing() { return mOngoing; }
67 }
68
69 static private OCNotificationManager mInstance = null;
70
71 private class NotificationTypePair {
72 public Notification mNotificaiton;
73 public NotificationType mType;
74 public NotificationTypePair(Notification n, NotificationType type) {
75 mNotificaiton = n;
76 mType = type;
77 }
78 }
79
80 private Context mContext;
81 private Map<Integer, NotificationTypePair> mNotificationMap;
82 private int mNotificationCounter;
83 NotificationManager mNM;
84
85 static OCNotificationManager getInstance(Context context) {
86 if (mInstance == null)
87 mInstance = new OCNotificationManager(context);
88 return mInstance;
89 }
90
91 OCNotificationManager(Context context) {
92 mContext = context;
93 mNotificationMap = new HashMap<Integer, NotificationTypePair>();
94 mNM = (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
95 mNotificationCounter = 0;
96 }
97
98 public int postNotification(NotificationType type, NotificationData data) {
99 mNotificationCounter++;
100 Notification notification = null;
101
102 switch (type) {
103 case NOTIFICATION_SIMPLE:
104 notification = new Notification(R.drawable.icon, data.getText(), System.currentTimeMillis());
105 break;
106 case NOTIFICATION_PROGRESS:
107 notification = new Notification();
108 notification.contentView = new RemoteViews(mContext.getPackageName(), R.layout.progressbar_layout);
109 notification.contentView.setTextViewText(R.id.status_text,
110 data.getText());
111 notification.contentView.setImageViewResource(R.id.status_icon,
112 R.id.icon);
113 notification.contentView.setProgressBar(R.id.status_progress,
114 100,
115 data.getPercent(),
116 false);
117 break;
118 default:
119 return -1;
120 }
121 if (data.getOngoing()) {
122 notification.flags |= notification.flags | Notification.FLAG_ONGOING_EVENT;
123 }
124
125 mNotificationMap.put(mNotificationCounter, new NotificationTypePair(notification, type));
126 return mNotificationCounter;
127 }
128
129 public boolean updateNotification(int notification_id, NotificationData data) {
130 if (!mNotificationMap.containsKey(notification_id)) {
131 return false;
132 }
133 NotificationTypePair pair = mNotificationMap.get(notification_id);
134 switch (pair.mType) {
135 case NOTIFICATION_PROGRESS:
136 pair.mNotificaiton.contentView.setProgressBar(R.id.status_text,
137 100,
138 data.getPercent(),
139 false);
140 return true;
141 case NOTIFICATION_SIMPLE:
142 pair.mNotificaiton = new Notification(R.drawable.icon,
143 data.getText(), System.currentTimeMillis());
144 mNM.notify(notification_id, pair.mNotificaiton);
145 return true;
146 default:
147 return false;
148 }
149 }
150
151 public void discardNotification(int notification_id) {
152 mNM.cancel(notification_id);
153 mNotificationMap.remove(notification_id);
154 }
155 }