2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com
.owncloud
.android
.notifications
;
23 import com
.owncloud
.android
.R
;
25 import android
.app
.Notification
;
26 import android
.content
.Context
;
27 import android
.os
.Build
;
28 import android
.support
.v4
.app
.NotificationCompat
;
29 import android
.view
.View
;
30 import android
.widget
.RemoteViews
;
33 * Extends the support class {@link NotificationCompat.Builder} to grant that
34 * a progress bar is available in every Android version, because
35 * {@link NotificationCompat.Builder#setProgress(int, int, boolean)} has no
36 * real effect for Android < 4.0
38 public class NotificationBuilderWithProgressBar
extends NotificationCompat
.Builder
{
41 * Custom view to replace the original layout of the notifications
43 private RemoteViews mContentView
= null
;
48 * Instances of this class will be only returned in Android versions needing it.
50 * @param context Context that will use the builder to create notifications
51 * @return An instance of this class, or of the regular
52 * {@link NotificationCompat.Builder}, when it is good enough.
54 public static NotificationCompat
.Builder
newNotificationBuilderWithProgressBar(Context context
) {
55 if (Build
.VERSION
.SDK_INT
< Build
.VERSION_CODES
.ICE_CREAM_SANDWICH
) {
56 return new NotificationBuilderWithProgressBar(context
);
58 return new NotificationCompat
.Builder(context
);
65 * @param context Context that will use the builder to create notifications.
67 private NotificationBuilderWithProgressBar(Context context
) {
69 mContentView
= new RemoteViews(context
.getPackageName(), R
.layout
.notification_with_progress_bar
);
70 setContent(mContentView
);
77 public NotificationCompat
.Builder
setProgress(int max
, int progress
, boolean indeterminate
) {
78 mContentView
.setProgressBar(R
.id
.progress
, max
, progress
, indeterminate
);
80 mContentView
.setViewVisibility(R
.id
.progressHolder
, View
.VISIBLE
);
82 mContentView
.setViewVisibility(R
.id
.progressHolder
, View
.GONE
);
91 public NotificationCompat
.Builder
setSmallIcon(int icon
) {
92 super.setSmallIcon(icon
); // necessary
93 mContentView
.setImageViewResource(R
.id
.icon
, icon
);
101 public NotificationCompat
.Builder
setContentTitle(CharSequence title
) {
102 super.setContentTitle(title
);
103 mContentView
.setTextViewText(R
.id
.title
, title
);
111 public NotificationCompat
.Builder
setContentText(CharSequence text
) {
112 super.setContentText(text
);
113 mContentView
.setTextViewText(R
.id
.text
, text
);
114 if (text
!= null
&& text
.length() > 0) {
115 mContentView
.setViewVisibility(R
.id
.text
, View
.VISIBLE
);
117 mContentView
.setViewVisibility(R
.id
.text
, View
.GONE
);
123 public Notification
build() {
124 Notification result
= super.build();
125 if (Build
.VERSION
.SDK_INT
< Build
.VERSION_CODES
.HONEYCOMB
) {
126 // super.build() in Android 2.x totally ruins whatever was made #setContent
127 result
.contentView
= mContentView
;