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 version 2,
7 * as published by the Free Software Foundation.
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.
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/>.
19 package com
.owncloud
.android
.files
.managers
;
21 import java
.util
.HashMap
;
24 import com
.owncloud
.android
.R
;
25 import com
.owncloud
.android
.utils
.DisplayUtils
;
27 import android
.app
.Notification
;
28 import android
.app
.NotificationManager
;
29 import android
.content
.Context
;
30 import android
.widget
.RemoteViews
;
33 public class OCNotificationManager
{
35 enum NotificationType
{
40 static public class NotificationData
{
41 private String mText
, mSubtitle
;
43 private boolean mOngoing
;
45 public NotificationData(String text
, String subtitle
, boolean ongoing
) {
46 this(text
, subtitle
, -1, ongoing
);
49 public NotificationData(int percent
, boolean ongoing
) {
50 this(null
, null
, percent
, ongoing
);
53 public NotificationData(String text
, int percent
, boolean ongoing
) {
54 this(text
, null
, percent
, ongoing
);
57 public NotificationData(String text
, String subtitle
, int percent
, boolean ongoing
) {
64 public String
getText() { return mText
; }
65 public int getPercent() { return mPercent
; }
66 public String
getSubtitle() { return mSubtitle
; }
67 public boolean getOngoing() { return mOngoing
; }
70 static private OCNotificationManager mInstance
= null
;
72 private class NotificationTypePair
{
73 public Notification mNotificaiton
;
74 public NotificationType mType
;
75 public NotificationTypePair(Notification n
, NotificationType type
) {
81 private Context mContext
;
82 private Map
<Integer
, NotificationTypePair
> mNotificationMap
;
83 private int mNotificationCounter
;
84 NotificationManager mNM
;
86 static OCNotificationManager
getInstance(Context context
) {
87 if (mInstance
== null
)
88 mInstance
= new OCNotificationManager(context
);
92 OCNotificationManager(Context context
) {
94 mNotificationMap
= new HashMap
<Integer
, NotificationTypePair
>();
95 mNM
= (NotificationManager
)mContext
.getSystemService(Context
.NOTIFICATION_SERVICE
);
96 mNotificationCounter
= 0;
99 public int postNotification(NotificationType type
, NotificationData data
) {
100 mNotificationCounter
++;
101 Notification notification
= null
;
104 case NOTIFICATION_SIMPLE
:
105 notification
= new Notification(DisplayUtils
.getSeasonalIconId(), data
.getText(), System
.currentTimeMillis());
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
,
112 notification
.contentView
.setImageViewResource(R
.id
.status_icon
,
114 notification
.contentView
.setProgressBar(R
.id
.status_progress
,
122 if (data
.getOngoing()) {
123 notification
.flags
|= notification
.flags
| Notification
.FLAG_ONGOING_EVENT
;
126 mNotificationMap
.put(mNotificationCounter
, new NotificationTypePair(notification
, type
));
127 return mNotificationCounter
;
130 public boolean updateNotification(int notification_id
, NotificationData data
) {
131 if (!mNotificationMap
.containsKey(notification_id
)) {
134 NotificationTypePair pair
= mNotificationMap
.get(notification_id
);
135 switch (pair
.mType
) {
136 case NOTIFICATION_PROGRESS
:
137 pair
.mNotificaiton
.contentView
.setProgressBar(R
.id
.status_text
,
142 case NOTIFICATION_SIMPLE
:
143 pair
.mNotificaiton
= new Notification(DisplayUtils
.getSeasonalIconId(),
144 data
.getText(), System
.currentTimeMillis());
145 mNM
.notify(notification_id
, pair
.mNotificaiton
);
152 public void discardNotification(int notification_id
) {
153 mNM
.cancel(notification_id
);
154 mNotificationMap
.remove(notification_id
);