Merge branch 'develop' into add_cookie_based_session_support
[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.app.NotificationManager;
24 import android.content.Context;
25 import android.os.Build;
26 import android.os.Handler;
27 import android.os.HandlerThread;
28 import android.os.Process;
29 import android.support.v4.app.NotificationCompat;
30 import android.view.View;
31 import android.widget.RemoteViews;
32
33 /**
34 * Extends the support class {@link NotificationCompat.Builder} to grant that
35 * a progress bar is available in every Android version, because
36 * {@link NotificationCompat.Builder#setProgress(int, int, boolean)} has no
37 * real effect for Android < 4.0
38 *
39 * @author David A. Velasco
40 */
41 public class NotificationBuilderWithProgressBar extends NotificationCompat.Builder {
42
43 /**
44 * Custom view to replace the original layout of the notifications
45 */
46 private RemoteViews mContentView = null;
47
48 /**
49 * Fatory method.
50 *
51 * Instances of this class will be only returned in Android versions needing it.
52 *
53 * @param context Context that will use the builder to create notifications
54 * @return An instance of this class, or of the regular
55 * {@link NotificationCompat.Builder}, when it is good enough.
56 */
57 public static NotificationCompat.Builder newNotificationBuilderWithProgressBar(Context context) {
58 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
59 return new NotificationBuilderWithProgressBar(context);
60 } else {
61 return new NotificationCompat.Builder(context);
62 }
63 }
64
65 /**
66 * Constructor.
67 *
68 * @param context Context that will use the builder to create notifications.
69 */
70 private NotificationBuilderWithProgressBar(Context context) {
71 super(context);
72 mContentView = new RemoteViews(context.getPackageName(), R.layout.notification_with_progress_bar);
73 setContent(mContentView);
74 }
75
76 /**
77 * {@inheritDoc}
78 */
79 @Override
80 public NotificationCompat.Builder setProgress(int max, int progress, boolean indeterminate) {
81 mContentView.setProgressBar(R.id.progress, max, progress, indeterminate);
82 if (max > 0) {
83 mContentView.setViewVisibility(R.id.progressHolder, View.VISIBLE);
84 } else {
85 mContentView.setViewVisibility(R.id.progressHolder, View.GONE);
86 }
87 return this;
88 }
89
90 /**
91 * {@inheritDoc}
92 */
93 @Override
94 public NotificationCompat.Builder setSmallIcon(int icon) {
95 super.setSmallIcon(icon); // necessary
96 mContentView.setImageViewResource(R.id.icon, icon);
97 return this;
98 }
99
100 /**
101 * {@inheritDoc}
102 */
103 @Override
104 public NotificationCompat.Builder setContentTitle(CharSequence title) {
105 super.setContentTitle(title);
106 mContentView.setTextViewText(R.id.title, title);
107 return this;
108 }
109
110 /**
111 * {@inheritDoc}
112 */
113 @Override
114 public NotificationCompat.Builder setContentText(CharSequence text) {
115 super.setContentText(text);
116 mContentView.setTextViewText(R.id.text, text);
117 if (text != null && text.length() > 0) {
118 mContentView.setViewVisibility(R.id.text, View.VISIBLE);
119 } else {
120 mContentView.setViewVisibility(R.id.text, View.GONE);
121 }
122 return this;
123 }
124
125 @Override
126 public Notification build() {
127 Notification result = super.build();
128 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
129 // super.build() in Android 2.x totally ruins whatever was made #setContent
130 result.contentView = mContentView;
131 }
132 return result;
133 }
134
135
136 }