1 /* ownCloud Android client application
2 * Copyright (C) 2014 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.notifications
;
20 import com
.owncloud
.android
.R
;
22 import android
.app
.Notification
;
23 import android
.app
.NotificationManager
;
24 import android
.content
.Context
;
25 import android
.os
.Build
;
26 import android
.os
.Handler
;
27 import android
.os
.HandlerThread
;
28 import android
.os
.Process
;
29 import android
.support
.v4
.app
.NotificationCompat
;
30 import android
.view
.View
;
31 import android
.widget
.RemoteViews
;
34 * Extends the support class {@link NotificationCompat.Builder} to grant that
35 * a progress bar is available in every Android version, because
36 * {@link NotificationCompat.Builder#setProgress(int, int, boolean)} has no
37 * real effect for Android < 4.0
39 * @author David A. Velasco
41 public class NotificationBuilderWithProgressBar
extends NotificationCompat
.Builder
{
44 * Custom view to replace the original layout of the notifications
46 private RemoteViews mContentView
= null
;
51 * Instances of this class will be only returned in Android versions needing it.
53 * @param context Context that will use the builder to create notifications
54 * @return An instance of this class, or of the regular
55 * {@link NotificationCompat.Builder}, when it is good enough.
57 public static NotificationCompat
.Builder
newNotificationBuilderWithProgressBar(Context context
) {
58 if (Build
.VERSION
.SDK_INT
< Build
.VERSION_CODES
.ICE_CREAM_SANDWICH
) {
59 return new NotificationBuilderWithProgressBar(context
);
61 return new NotificationCompat
.Builder(context
);
68 * @param context Context that will use the builder to create notifications.
70 private NotificationBuilderWithProgressBar(Context context
) {
72 mContentView
= new RemoteViews(context
.getPackageName(), R
.layout
.notification_with_progress_bar
);
73 setContent(mContentView
);
80 public NotificationCompat
.Builder
setProgress(int max
, int progress
, boolean indeterminate
) {
81 mContentView
.setProgressBar(R
.id
.progress
, max
, progress
, indeterminate
);
83 mContentView
.setViewVisibility(R
.id
.progressHolder
, View
.VISIBLE
);
85 mContentView
.setViewVisibility(R
.id
.progressHolder
, View
.GONE
);
94 public NotificationCompat
.Builder
setSmallIcon(int icon
) {
95 super.setSmallIcon(icon
); // necessary
96 mContentView
.setImageViewResource(R
.id
.icon
, icon
);
104 public NotificationCompat
.Builder
setContentTitle(CharSequence title
) {
105 super.setContentTitle(title
);
106 mContentView
.setTextViewText(R
.id
.title
, title
);
114 public NotificationCompat
.Builder
setContentText(CharSequence text
) {
115 super.setContentText(text
);
116 mContentView
.setTextViewText(R
.id
.text
, text
);
117 if (text
!= null
&& text
.length() > 0) {
118 mContentView
.setViewVisibility(R
.id
.text
, View
.VISIBLE
);
120 mContentView
.setViewVisibility(R
.id
.text
, View
.GONE
);
126 public Notification
build() {
127 Notification result
= super.build();
128 if (Build
.VERSION
.SDK_INT
< Build
.VERSION_CODES
.HONEYCOMB
) {
129 // super.build() in Android 2.x totally ruins whatever was made #setContent
130 result
.contentView
= mContentView
;