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
.content
.Context
;
24 import android
.os
.Build
;
25 import android
.support
.v4
.app
.NotificationCompat
;
26 import android
.view
.View
;
27 import android
.widget
.RemoteViews
;
30 * Extends the support class {@link NotificationCompat.Builder} to grant that
31 * a progress bar is available in every Android version, because
32 * {@link NotificationCompat.Builder#setProgress(int, int, boolean)} has no
33 * real effect for Android < 4.0
35 * @author David A. Velasco
37 public class NotificationBuilderWithProgressBar
extends NotificationCompat
.Builder
{
40 * Custom view to replace the original layout of the notifications
42 private RemoteViews mContentView
= null
;
47 * Instances of this class will be only returned in Android versions needing it.
49 * @param context Context that will use the builder to create notifications
50 * @return An instance of this class, or of the regular
51 * {@link NotificationCompat.Builder}, when it is good enough.
53 public static NotificationCompat
.Builder
newNotificationBuilderWithProgressBar(Context context
) {
54 if (Build
.VERSION
.SDK_INT
< Build
.VERSION_CODES
.ICE_CREAM_SANDWICH
) {
55 return new NotificationBuilderWithProgressBar(context
);
57 return new NotificationCompat
.Builder(context
);
64 * @param context Context that will use the builder to create notifications.
66 private NotificationBuilderWithProgressBar(Context context
) {
68 mContentView
= new RemoteViews(context
.getPackageName(), R
.layout
.notification_with_progress_bar
);
69 setContent(mContentView
);
76 public NotificationCompat
.Builder
setProgress(int max
, int progress
, boolean indeterminate
) {
77 mContentView
.setProgressBar(R
.id
.progress
, max
, progress
, indeterminate
);
79 mContentView
.setViewVisibility(R
.id
.progressHolder
, View
.VISIBLE
);
81 mContentView
.setViewVisibility(R
.id
.progressHolder
, View
.GONE
);
90 public NotificationCompat
.Builder
setSmallIcon(int icon
) {
91 super.setSmallIcon(icon
); // necessary
92 mContentView
.setImageViewResource(R
.id
.icon
, icon
);
100 public NotificationCompat
.Builder
setContentTitle(CharSequence title
) {
101 super.setContentTitle(title
);
102 mContentView
.setTextViewText(R
.id
.title
, title
);
110 public NotificationCompat
.Builder
setContentText(CharSequence text
) {
111 super.setContentText(text
);
112 mContentView
.setTextViewText(R
.id
.text
, text
);
113 if (text
!= null
&& text
.length() > 0) {
114 mContentView
.setViewVisibility(R
.id
.text
, View
.VISIBLE
);
116 mContentView
.setViewVisibility(R
.id
.text
, View
.GONE
);
122 public Notification
build() {
123 Notification result
= super.build();
124 if (Build
.VERSION
.SDK_INT
< Build
.VERSION_CODES
.HONEYCOMB
) {
125 // super.build() in Android 2.x totally ruins whatever was made #setContent
126 result
.contentView
= mContentView
;