1
1
package fr .free .nrw .commons .delete ;
2
2
3
- import android .app .AlertDialog ;
3
+ import android .app .NotificationManager ;
4
4
import android .content .Context ;
5
- import android .content .DialogInterface ;
6
- import android .content .Intent ;
7
5
import android .os .AsyncTask ;
6
+ import android .support .v4 .app .NotificationCompat ;
7
+ import android .support .v4 .app .NotificationCompat .Builder ;
8
+ import android .view .Gravity ;
9
+ import android .widget .Toast ;
10
+
8
11
import java .text .SimpleDateFormat ;
9
12
import java .util .Calendar ;
10
13
import java .util .Locale ;
18
21
import fr .free .nrw .commons .mwapi .MediaWikiApi ;
19
22
import timber .log .Timber ;
20
23
21
- import static android .support .v4 .content .ContextCompat .startActivity ;
22
-
23
- public class DeleteTask extends AsyncTask <Void , Void , Integer > {
24
+ import static android .support .v4 .app .NotificationCompat .DEFAULT_ALL ;
25
+ import static android .support .v4 .app .NotificationCompat .PRIORITY_HIGH ;
24
26
25
- private static final int SUCCESS = 0 ;
26
- private static final int FAILED = -1 ;
27
- private static final int ALREADY_DELETED = -2 ;
27
+ public class DeleteTask extends AsyncTask <Void , Integer , Boolean > {
28
28
29
29
@ Inject MediaWikiApi mwApi ;
30
30
@ Inject SessionManager sessionManager ;
31
31
32
+ public static final int NOTIFICATION_DELETE = 1 ;
33
+
34
+ private NotificationManager notificationManager ;
35
+ private Builder notificationBuilder ;
32
36
private Context context ;
33
37
private Media media ;
34
38
private String reason ;
@@ -45,130 +49,133 @@ protected void onPreExecute(){
45
49
.getInstance (context .getApplicationContext ())
46
50
.getCommonsApplicationComponent ()
47
51
.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 ();
48
59
}
49
60
50
61
@ Override
51
- protected Integer doInBackground (Void ...voids ) {
62
+ protected Boolean doInBackground (Void ...voids ) {
63
+ publishProgress (0 );
64
+
52
65
String editToken ;
53
66
String authCookie ;
54
67
String summary = "Nominating " + media .getFilename () +" for deletion." ;
55
68
56
69
authCookie = sessionManager .getAuthCookie ();
57
70
mwApi .setAuthCookie (authCookie );
58
71
59
- try {
60
- if (mwApi .pageExists ("Commons:Deletion_requests/" +media .getFilename ())){
61
- return ALREADY_DELETED ;
62
- }
63
- }
64
- catch (Exception e ) {
65
- Timber .d (e .getMessage ());
66
- return FAILED ;
67
- }
68
-
69
- try {
70
- editToken = mwApi .getEditToken ();
71
- }
72
- catch (Exception e ){
73
- Timber .d (e .getMessage ());
74
- return FAILED ;
75
- }
76
- if (editToken .equals ("+\\ " )) {
77
- return FAILED ;
78
- }
79
-
80
72
Calendar calendar = Calendar .getInstance ();
81
73
String fileDeleteString = "{{delete|reason=" + reason +
82
74
"|subpage=" +media .getFilename () +
83
75
"|day=" + calendar .get (Calendar .DAY_OF_MONTH ) +
84
76
"|month=" + calendar .getDisplayName (Calendar .MONTH ,Calendar .LONG , Locale .getDefault ()) +
85
77
"|year=" + calendar .get (Calendar .YEAR ) +
86
78
"}}" ;
87
- try {
88
- mwApi .prependEdit (editToken ,fileDeleteString +"\n " ,
89
- media .getFilename (),summary );
90
- }
91
- catch (Exception e ) {
92
- Timber .d (e .getMessage ());
93
- return FAILED ;
94
- }
95
79
96
80
String subpageString = "=== [[:" + media .getFilename () + "]] ===\n " +
97
81
reason +
98
82
" ~~~~" ;
99
- try {
100
- mwApi .edit (editToken ,subpageString +"\n " ,
101
- "Commons:Deletion_requests/" +media .getFilename (),summary );
102
- }
103
- catch (Exception e ) {
104
- Timber .d (e .getMessage ());
105
- return FAILED ;
106
- }
107
83
108
84
String logPageString = "\n {{Commons:Deletion requests/" + media .getFilename () +
109
85
"}}\n " ;
110
86
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy/MM/dd" );
111
87
String date = sdf .format (calendar .getTime ());
112
- try {
113
- mwApi .appendEdit (editToken ,logPageString +"\n " ,
114
- "Commons:Deletion_requests/" +date ,summary );
115
- }
116
- catch (Exception e ) {
117
- Timber .d (e .getMessage ());
118
- return FAILED ;
119
- }
120
88
121
89
String userPageString = "\n {{subst:idw|" + media .getFilename () +
122
90
"}} ~~~~" ;
123
- try {
91
+
92
+ try {
93
+ editToken = mwApi .getEditToken ();
94
+ if (editToken .equals ("+\\ " )) {
95
+ return false ;
96
+ }
97
+ publishProgress (1 );
98
+
99
+ mwApi .prependEdit (editToken ,fileDeleteString +"\n " ,
100
+ media .getFilename (),summary );
101
+ publishProgress (2 );
102
+
103
+ mwApi .edit (editToken ,subpageString +"\n " ,
104
+ "Commons:Deletion_requests/" +media .getFilename (),summary );
105
+ publishProgress (3 );
106
+
107
+ mwApi .appendEdit (editToken ,logPageString +"\n " ,
108
+ "Commons:Deletion_requests/" +date ,summary );
109
+ publishProgress (4 );
110
+
124
111
mwApi .appendEdit (editToken ,userPageString +"\n " ,
125
112
"User_Talk:" +sessionManager .getCurrentAccount ().name ,summary );
113
+ publishProgress (5 );
126
114
}
127
115
catch (Exception e ) {
128
116
Timber .d (e .getMessage ());
129
- return FAILED ;
117
+ return false ;
130
118
}
131
- return SUCCESS ;
119
+ return true ;
132
120
}
133
121
134
122
@ Override
135
- protected void onPostExecute (Integer result ) {
123
+ protected void onProgressUpdate (Integer ... values ){
124
+ super .onProgressUpdate (values );
125
+
136
126
String message = "" ;
137
- String title = "" ;
138
- switch ( result ){
139
- case SUCCESS :
140
- title = "Success" ;
141
- message = "Successfully nominated " + media . getDisplayTitle () + " deletion. \n " +
142
- "Check the webpage for more details " ;
127
+ switch ( values [ 0 ]){
128
+ case 0 :
129
+ message = "Getting token" ;
130
+ break ;
131
+ case 1 :
132
+ message = "Adding delete message to file " ;
143
133
break ;
144
- case FAILED :
145
- title = "Failed" ;
146
- message = "Could not request deletion. Something went wrong." ;
134
+ case 2 :
135
+ message = "Creating Delete requests sub-page" ;
147
136
break ;
148
- case ALREADY_DELETED :
149
- title = "Already Nominated" ;
150
- message = media .getDisplayTitle () + " has already been nominated for deletion.\n " +
151
- "Check the webpage for more details" ;
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" ;
152
145
break ;
153
146
}
154
- AlertDialog alert ;
155
- AlertDialog .Builder builder = new AlertDialog .Builder (context );
156
- builder .setTitle (title );
157
- builder .setMessage (message );
158
- builder .setCancelable (true );
159
- builder .setPositiveButton (
160
- R .string .ok ,
161
- new DialogInterface .OnClickListener () {
162
- public void onClick (DialogInterface dialog , int id ) {}
163
- });
164
- builder .setNeutralButton (R .string .view_browser ,
165
- new DialogInterface .OnClickListener () {
166
- public void onClick (DialogInterface dialog , int id ) {
167
- Intent browserIntent = new Intent (Intent .ACTION_VIEW , media .getFilePageTitle ().getMobileUri ());
168
- startActivity (context ,browserIntent ,null );
169
- }
170
- });
171
- alert = builder .create ();
172
- alert .show ();
147
+
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 ());
155
+ }
156
+
157
+ @ Override
158
+ protected void onPostExecute (Boolean result ) {
159
+ String message = "" ;
160
+ String title = "Nominating for Deletion" ;
161
+
162
+ if (result ){
163
+ title += ": Success" ;
164
+ message = "Successfully nominated " + media .getDisplayTitle () + " deletion." ;
165
+ }
166
+ else {
167
+ title += ": Failed" ;
168
+ message = "Could not request deletion." ;
169
+ }
170
+
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 ());
173
180
}
174
181
}
0 commit comments