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