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 android
.app
.Notification
;
25 import android
.app
.NotificationManager
;
26 import android
.content
.Context
;
27 import android
.widget
.RemoteViews
;
29 import com
.owncloud
.android
.R
;
31 public class OCNotificationManager
{
33 enum NotificationType
{
38 static public class NotificationData
{
39 private String mText
, mSubtitle
;
41 private boolean mOngoing
;
43 public NotificationData(String text
, String subtitle
, boolean ongoing
) {
44 this(text
, subtitle
, -1, ongoing
);
47 public NotificationData(int percent
, boolean ongoing
) {
48 this(null
, null
, percent
, ongoing
);
51 public NotificationData(String text
, int percent
, boolean ongoing
) {
52 this(text
, null
, percent
, ongoing
);
55 public NotificationData(String text
, String subtitle
, int percent
, boolean ongoing
) {
62 public String
getText() { return mText
; }
63 public int getPercent() { return mPercent
; }
64 public String
getSubtitle() { return mSubtitle
; }
65 public boolean getOngoing() { return mOngoing
; }
68 static private OCNotificationManager mInstance
= null
;
70 private class NotificationTypePair
{
71 public Notification mNotificaiton
;
72 public NotificationType mType
;
73 public NotificationTypePair(Notification n
, NotificationType type
) {
79 private Context mContext
;
80 private Map
<Integer
, NotificationTypePair
> mNotificationMap
;
81 private int mNotificationCounter
;
82 NotificationManager mNM
;
84 static OCNotificationManager
getInstance(Context context
) {
85 if (mInstance
== null
)
86 mInstance
= new OCNotificationManager(context
);
90 OCNotificationManager(Context context
) {
92 mNotificationMap
= new HashMap
<Integer
, NotificationTypePair
>();
93 mNM
= (NotificationManager
)mContext
.getSystemService(Context
.NOTIFICATION_SERVICE
);
94 mNotificationCounter
= 0;
97 public int postNotification(NotificationType type
, NotificationData data
) {
98 mNotificationCounter
++;
99 Notification notification
= null
;
102 case NOTIFICATION_SIMPLE
:
103 notification
= new Notification(R
.drawable
.icon
, data
.getText(), System
.currentTimeMillis());
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
,
110 notification
.contentView
.setImageViewResource(R
.id
.status_icon
,
112 notification
.contentView
.setProgressBar(R
.id
.status_progress
,
120 if (data
.getOngoing()) {
121 notification
.flags
|= notification
.flags
| Notification
.FLAG_ONGOING_EVENT
;
124 mNotificationMap
.put(mNotificationCounter
, new NotificationTypePair(notification
, type
));
125 return mNotificationCounter
;
128 public boolean updateNotification(int notification_id
, NotificationData data
) {
129 if (!mNotificationMap
.containsKey(notification_id
)) {
132 NotificationTypePair pair
= mNotificationMap
.get(notification_id
);
133 switch (pair
.mType
) {
134 case NOTIFICATION_PROGRESS
:
135 pair
.mNotificaiton
.contentView
.setProgressBar(R
.id
.status_text
,
140 case NOTIFICATION_SIMPLE
:
141 pair
.mNotificaiton
= new Notification(R
.drawable
.icon
,
142 data
.getText(), System
.currentTimeMillis());
143 mNM
.notify(notification_id
, pair
.mNotificaiton
);
150 public void discardNotification(int notification_id
) {
151 mNM
.cancel(notification_id
);
152 mNotificationMap
.remove(notification_id
);