1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package com
.owncloud
.android
.files
.managers
;
22 import java
.util
.HashMap
;
25 import android
.app
.Notification
;
26 import android
.app
.NotificationManager
;
27 import android
.content
.Context
;
28 import android
.widget
.RemoteViews
;
30 import com
.owncloud
.android
.R
;
32 public class OCNotificationManager
{
34 enum NotificationType
{
39 static public class NotificationData
{
40 private String mText
, mSubtitle
;
42 private boolean mOngoing
;
44 public NotificationData(String text
, String subtitle
, boolean ongoing
) {
45 this(text
, subtitle
, -1, ongoing
);
48 public NotificationData(int percent
, boolean ongoing
) {
49 this(null
, null
, percent
, ongoing
);
52 public NotificationData(String text
, int percent
, boolean ongoing
) {
53 this(text
, null
, percent
, ongoing
);
56 public NotificationData(String text
, String subtitle
, int percent
, boolean ongoing
) {
63 public String
getText() { return mText
; }
64 public int getPercent() { return mPercent
; }
65 public String
getSubtitle() { return mSubtitle
; }
66 public boolean getOngoing() { return mOngoing
; }
69 static private OCNotificationManager mInstance
= null
;
71 private class NotificationTypePair
{
72 public Notification mNotificaiton
;
73 public NotificationType mType
;
74 public NotificationTypePair(Notification n
, NotificationType type
) {
80 private Context mContext
;
81 private Map
<Integer
, NotificationTypePair
> mNotificationMap
;
82 private int mNotificationCounter
;
83 NotificationManager mNM
;
85 static OCNotificationManager
getInstance(Context context
) {
86 if (mInstance
== null
)
87 mInstance
= new OCNotificationManager(context
);
91 OCNotificationManager(Context context
) {
93 mNotificationMap
= new HashMap
<Integer
, NotificationTypePair
>();
94 mNM
= (NotificationManager
)mContext
.getSystemService(Context
.NOTIFICATION_SERVICE
);
95 mNotificationCounter
= 0;
98 public int postNotification(NotificationType type
, NotificationData data
) {
99 mNotificationCounter
++;
100 Notification notification
= null
;
103 case NOTIFICATION_SIMPLE
:
104 notification
= new Notification(R
.drawable
.icon
, data
.getText(), System
.currentTimeMillis());
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
,
111 notification
.contentView
.setImageViewResource(R
.id
.status_icon
,
113 notification
.contentView
.setProgressBar(R
.id
.status_progress
,
121 if (data
.getOngoing()) {
122 notification
.flags
|= notification
.flags
| Notification
.FLAG_ONGOING_EVENT
;
125 mNotificationMap
.put(mNotificationCounter
, new NotificationTypePair(notification
, type
));
126 return mNotificationCounter
;
129 public boolean updateNotification(int notification_id
, NotificationData data
) {
130 if (!mNotificationMap
.containsKey(notification_id
)) {
133 NotificationTypePair pair
= mNotificationMap
.get(notification_id
);
134 switch (pair
.mType
) {
135 case NOTIFICATION_PROGRESS
:
136 pair
.mNotificaiton
.contentView
.setProgressBar(R
.id
.status_text
,
141 case NOTIFICATION_SIMPLE
:
142 pair
.mNotificaiton
= new Notification(R
.drawable
.icon
,
143 data
.getText(), System
.currentTimeMillis());
144 mNM
.notify(notification_id
, pair
.mNotificaiton
);
151 public void discardNotification(int notification_id
) {
152 mNM
.cancel(notification_id
);
153 mNotificationMap
.remove(notification_id
);