Share the file with a user or group when selected on the list of suggestions
[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 setColor(context.getResources().getColor(R.color.primary));
60 }
61 }
62
63 /**
64 * Constructor.
65 *
66 * @param context Context that will use the builder to create notifications.
67 */
68 private NotificationBuilderWithProgressBar(Context context) {
69 super(context);
70 mContentView = new RemoteViews(context.getPackageName(), R.layout.notification_with_progress_bar);
71 setContent(mContentView);
72 }
73
74 /**
75 * {@inheritDoc}
76 */
77 @Override
78 public NotificationCompat.Builder setProgress(int max, int progress, boolean indeterminate) {
79 mContentView.setProgressBar(R.id.progress, max, progress, indeterminate);
80 if (max > 0) {
81 mContentView.setViewVisibility(R.id.progressHolder, View.VISIBLE);
82 } else {
83 mContentView.setViewVisibility(R.id.progressHolder, View.GONE);
84 }
85 return this;
86 }
87
88 /**
89 * {@inheritDoc}
90 */
91 @Override
92 public NotificationCompat.Builder setSmallIcon(int icon) {
93 super.setSmallIcon(icon); // necessary
94 mContentView.setImageViewResource(R.id.icon, icon);
95 return this;
96 }
97
98 /**
99 * {@inheritDoc}
100 */
101 @Override
102 public NotificationCompat.Builder setContentTitle(CharSequence title) {
103 super.setContentTitle(title);
104 mContentView.setTextViewText(R.id.title, title);
105 return this;
106 }
107
108 /**
109 * {@inheritDoc}
110 */
111 @Override
112 public NotificationCompat.Builder setContentText(CharSequence text) {
113 super.setContentText(text);
114 mContentView.setTextViewText(R.id.text, text);
115 if (text != null && text.length() > 0) {
116 mContentView.setViewVisibility(R.id.text, View.VISIBLE);
117 } else {
118 mContentView.setViewVisibility(R.id.text, View.GONE);
119 }
120 return this;
121 }
122
123 @Override
124 public Notification build() {
125 Notification result = super.build();
126 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
127 // super.build() in Android 2.x totally ruins whatever was made #setContent
128 result.contentView = mContentView;
129 }
130 return result;
131 }
132
133
134 }