11package fr .free .nrw .commons .delete ;
22
3- import android .app .AlertDialog ;
3+ import android .app .NotificationManager ;
44import android .content .Context ;
5- import android .content .DialogInterface ;
6- import android .content .Intent ;
75import 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+
811import java .text .SimpleDateFormat ;
912import java .util .Calendar ;
1013import java .util .Locale ;
1821import fr .free .nrw .commons .mwapi .MediaWikiApi ;
1922import timber .log .Timber ;
2023
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 ;
2426
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 > {
2828
2929 @ Inject MediaWikiApi mwApi ;
3030 @ Inject SessionManager sessionManager ;
3131
32+ public static final int NOTIFICATION_DELETE = 1 ;
33+
34+ private NotificationManager notificationManager ;
35+ private Builder notificationBuilder ;
3236 private Context context ;
3337 private Media media ;
3438 private String reason ;
@@ -45,130 +49,133 @@ protected void onPreExecute(){
4549 .getInstance (context .getApplicationContext ())
4650 .getCommonsApplicationComponent ()
4751 .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 ();
4859 }
4960
5061 @ Override
51- protected Integer doInBackground (Void ...voids ) {
62+ protected Boolean doInBackground (Void ...voids ) {
63+ publishProgress (0 );
64+
5265 String editToken ;
5366 String authCookie ;
5467 String summary = "Nominating " + media .getFilename () +" for deletion." ;
5568
5669 authCookie = sessionManager .getAuthCookie ();
5770 mwApi .setAuthCookie (authCookie );
5871
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-
8072 Calendar calendar = Calendar .getInstance ();
8173 String fileDeleteString = "{{delete|reason=" + reason +
8274 "|subpage=" +media .getFilename () +
8375 "|day=" + calendar .get (Calendar .DAY_OF_MONTH ) +
8476 "|month=" + calendar .getDisplayName (Calendar .MONTH ,Calendar .LONG , Locale .getDefault ()) +
8577 "|year=" + calendar .get (Calendar .YEAR ) +
8678 "}}" ;
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- }
9579
9680 String subpageString = "=== [[:" + media .getFilename () + "]] ===\n " +
9781 reason +
9882 " ~~~~" ;
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- }
10783
10884 String logPageString = "\n {{Commons:Deletion requests/" + media .getFilename () +
10985 "}}\n " ;
11086 SimpleDateFormat sdf = new SimpleDateFormat ("yyyy/MM/dd" );
11187 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- }
12088
12189 String userPageString = "\n {{subst:idw|" + media .getFilename () +
12290 "}} ~~~~" ;
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+
124111 mwApi .appendEdit (editToken ,userPageString +"\n " ,
125112 "User_Talk:" +sessionManager .getCurrentAccount ().name ,summary );
113+ publishProgress (5 );
126114 }
127115 catch (Exception e ) {
128116 Timber .d (e .getMessage ());
129- return FAILED ;
117+ return false ;
130118 }
131- return SUCCESS ;
119+ return true ;
132120 }
133121
134122 @ Override
135- protected void onPostExecute (Integer result ) {
123+ protected void onProgressUpdate (Integer ... values ){
124+ super .onProgressUpdate (values );
125+
136126 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 " ;
143133 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" ;
147136 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" ;
152145 break ;
153146 }
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 ());
173180 }
174181}
0 commit comments