Skip to content

Commit 2a62fe4

Browse files
author
Suchit Kar
committed
Display deletion progress as notifications
1 parent 60f7e6a commit 2a62fe4

File tree

1 file changed

+64
-35
lines changed

1 file changed

+64
-35
lines changed

app/src/main/java/fr/free/nrw/commons/delete/DeleteTask.java

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package fr.free.nrw.commons.delete;
22

3-
import android.app.AlertDialog;
3+
import android.app.NotificationManager;
44
import android.content.Context;
5-
import android.content.DialogInterface;
6-
import android.content.Intent;
75
import android.os.AsyncTask;
6+
import android.support.v4.app.NotificationCompat;
7+
import android.support.v4.app.NotificationCompat.Builder;
8+
import android.view.Gravity;
89
import android.widget.Toast;
910

1011
import java.text.SimpleDateFormat;
@@ -20,14 +21,18 @@
2021
import fr.free.nrw.commons.mwapi.MediaWikiApi;
2122
import timber.log.Timber;
2223

23-
import static android.support.v4.content.ContextCompat.startActivity;
24-
import static android.widget.Toast.LENGTH_SHORT;
24+
import static android.support.v4.app.NotificationCompat.DEFAULT_ALL;
25+
import static android.support.v4.app.NotificationCompat.PRIORITY_HIGH;
2526

2627
public class DeleteTask extends AsyncTask<Void, Integer, Boolean> {
2728

2829
@Inject MediaWikiApi mwApi;
2930
@Inject SessionManager sessionManager;
3031

32+
public static final int NOTIFICATION_DELETE = 1;
33+
34+
private NotificationManager notificationManager;
35+
private Builder notificationBuilder;
3136
private Context context;
3237
private Media media;
3338
private String reason;
@@ -44,10 +49,19 @@ protected void onPreExecute(){
4449
.getInstance(context.getApplicationContext())
4550
.getCommonsApplicationComponent()
4651
.inject(this);
52+
53+
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
54+
notificationBuilder = new NotificationCompat.Builder(context);
55+
Toast toast = new Toast(context);
56+
toast.setGravity(Gravity.CENTER,0,0);
57+
toast = Toast.makeText(context,"Trying to nominate "+media.getDisplayTitle()+ " for deletion",Toast.LENGTH_SHORT);
58+
toast.show();
4759
}
4860

4961
@Override
5062
protected Boolean doInBackground(Void ...voids) {
63+
publishProgress(0);
64+
5165
String editToken;
5266
String authCookie;
5367
String summary = "Nominating " + media.getFilename() +" for deletion.";
@@ -96,6 +110,7 @@ protected Boolean doInBackground(Void ...voids) {
96110

97111
mwApi.appendEdit(editToken,userPageString+"\n",
98112
"User_Talk:"+sessionManager.getCurrentAccount().name,summary);
113+
publishProgress(5);
99114
}
100115
catch (Exception e) {
101116
Timber.d(e.getMessage());
@@ -106,47 +121,61 @@ protected Boolean doInBackground(Void ...voids) {
106121

107122
@Override
108123
protected void onProgressUpdate (Integer... values){
124+
super.onProgressUpdate(values);
125+
126+
String message = "";
127+
switch (values[0]){
128+
case 0:
129+
message = "Getting token";
130+
break;
131+
case 1:
132+
message = "Adding delete message to file";
133+
break;
134+
case 2:
135+
message = "Creating Delete requests sub-page";
136+
break;
137+
case 3:
138+
message = "Adding file to Delete requests log";
139+
break;
140+
case 4:
141+
message = "Notifying User on Talk page";
142+
break;
143+
case 5:
144+
message = "Done";
145+
break;
146+
}
109147

148+
notificationBuilder.setContentTitle("Nominating "+media.getDisplayTitle()+" for deletion")
149+
.setStyle(new NotificationCompat.BigTextStyle()
150+
.bigText(message))
151+
.setSmallIcon(R.drawable.ic_launcher)
152+
.setProgress(5, values[0], false)
153+
.setOngoing(true);
154+
notificationManager.notify(NOTIFICATION_DELETE, notificationBuilder.build());
110155
}
111156

112157
@Override
113158
protected void onPostExecute(Boolean result) {
114159
String message = "";
115-
String title = "";
160+
String title = "Nominating for Deletion";
116161

117162
if (result){
118-
title = "Success";
119-
message = "Successfully nominated " + media.getDisplayTitle() + " deletion.\n" +
120-
"Check the webpage for more details";
163+
title += ": Success";
164+
message = "Successfully nominated " + media.getDisplayTitle() + " deletion.";
121165
}
122166
else {
123-
title = "Failed";
124-
message = "Could not request deletion. Something went wrong.";
167+
title += ": Failed";
168+
message = "Could not request deletion.";
125169
}
126170

127-
AlertDialog alert;
128-
AlertDialog.Builder builder = new AlertDialog.Builder(context);
129-
builder.setTitle(title);
130-
builder.setMessage(message);
131-
builder.setCancelable(true);
132-
builder.setPositiveButton(
133-
R.string.ok,
134-
new DialogInterface.OnClickListener() {
135-
public void onClick(DialogInterface dialog, int id) {}
136-
});
137-
builder.setNeutralButton(R.string.view_browser,
138-
new DialogInterface.OnClickListener() {
139-
public void onClick(DialogInterface dialog, int id) {
140-
Intent browserIntent = new Intent(Intent.ACTION_VIEW, media.getFilePageTitle().getMobileUri());
141-
if (browserIntent.resolveActivity(context.getPackageManager()) != null) {
142-
startActivity(context,browserIntent,null);
143-
} else {
144-
Toast toast = Toast.makeText(context, R.string.no_web_browser, LENGTH_SHORT);
145-
toast.show();
146-
}
147-
}
148-
});
149-
alert = builder.create();
150-
alert.show();
171+
notificationBuilder.setDefaults(DEFAULT_ALL)
172+
.setContentTitle(title)
173+
.setStyle(new NotificationCompat.BigTextStyle()
174+
.bigText(message))
175+
.setSmallIcon(R.drawable.ic_launcher)
176+
.setProgress(0,0,false)
177+
.setOngoing(false)
178+
.setPriority(PRIORITY_HIGH);
179+
notificationManager.notify(NOTIFICATION_DELETE, notificationBuilder.build());
151180
}
152181
}

0 commit comments

Comments
 (0)