Merge remote-tracking branch 'upstream/develop' into fileCount
[pub/Android/ownCloud.git] / src / com / owncloud / android / notifications / NotificationBuilderWithProgressBar.java
1 /* ownCloud Android client application
2 * Copyright (C) 2014 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
18 package com.owncloud.android.notifications;
19
20 import com.owncloud.android.R;
21
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;
28
29 /**
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
34 *
35 * @author David A. Velasco
36 */
37 public class NotificationBuilderWithProgressBar extends NotificationCompat.Builder {
38
39 /**
40 * Custom view to replace the original layout of the notifications
41 */
42 private RemoteViews mContentView = null;
43
44 /**
45 * Fatory method.
46 *
47 * Instances of this class will be only returned in Android versions needing it.
48 *
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.
52 */
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);
56 } else {
57 return new NotificationCompat.Builder(context);
58 }
59 }
60
61 /**
62 * Constructor.
63 *
64 * @param context Context that will use the builder to create notifications.
65 */
66 private NotificationBuilderWithProgressBar(Context context) {
67 super(context);
68 mContentView = new RemoteViews(context.getPackageName(), R.layout.notification_with_progress_bar);
69 setContent(mContentView);
70 }
71
72 /**
73 * {@inheritDoc}
74 */
75 @Override
76 public NotificationCompat.Builder setProgress(int max, int progress, boolean indeterminate) {
77 mContentView.setProgressBar(R.id.progress, max, progress, indeterminate);
78 if (max > 0) {
79 mContentView.setViewVisibility(R.id.progressHolder, View.VISIBLE);
80 } else {
81 mContentView.setViewVisibility(R.id.progressHolder, View.GONE);
82 }
83 return this;
84 }
85
86 /**
87 * {@inheritDoc}
88 */
89 @Override
90 public NotificationCompat.Builder setSmallIcon(int icon) {
91 super.setSmallIcon(icon); // necessary
92 mContentView.setImageViewResource(R.id.icon, icon);
93 return this;
94 }
95
96 /**
97 * {@inheritDoc}
98 */
99 @Override
100 public NotificationCompat.Builder setContentTitle(CharSequence title) {
101 super.setContentTitle(title);
102 mContentView.setTextViewText(R.id.title, title);
103 return this;
104 }
105
106 /**
107 * {@inheritDoc}
108 */
109 @Override
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);
115 } else {
116 mContentView.setViewVisibility(R.id.text, View.GONE);
117 }
118 return this;
119 }
120
121 @Override
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;
127 }
128 return result;
129 }
130
131
132 }