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