66
77import android .Manifest ;
88import android .annotation .SuppressLint ;
9- import android .app .Activity ;
10- import android .content .ComponentName ;
119import android .content .Context ;
12- import android .content .Intent ;
13- import android .content .ServiceConnection ;
1410import android .os .Bundle ;
15- import android .os .IBinder ;
1611import android .view .LayoutInflater ;
1712import android .view .Menu ;
1813import android .view .MenuInflater ;
1914import android .view .MenuItem ;
2015import android .view .MenuItem .OnMenuItemClickListener ;
2116import android .view .View ;
22- import android .view .View .OnClickListener ;
2317import android .view .ViewGroup ;
2418import android .widget .CheckBox ;
2519import android .widget .LinearLayout ;
2620import android .widget .TextView ;
2721import android .widget .Toast ;
2822import androidx .annotation .NonNull ;
2923import androidx .annotation .Nullable ;
30- import androidx .appcompat .widget .SwitchCompat ;
3124import androidx .fragment .app .Fragment ;
3225import androidx .fragment .app .FragmentManager .OnBackStackChangedListener ;
3326import androidx .fragment .app .FragmentTransaction ;
34-
35- import fr .free .nrw .commons .CommonsApplication ;
36- import fr .free .nrw .commons .MediaDataExtractor ;
37- import fr .free .nrw .commons .auth .SessionManager ;
38- import fr .free .nrw .commons .notification .Notification ;
39- import fr .free .nrw .commons .notification .NotificationController ;
40- import fr .free .nrw .commons .theme .BaseActivity ;
41- import fr .free .nrw .commons .upload .UploadService .ServiceCallback ;
42- import io .reactivex .disposables .Disposable ;
43- import java .util .List ;
44-
45- import javax .inject .Inject ;
46- import javax .inject .Named ;
47-
4827import butterknife .BindView ;
4928import butterknife .ButterKnife ;
29+ import fr .free .nrw .commons .CommonsApplication ;
5030import fr .free .nrw .commons .Media ;
5131import fr .free .nrw .commons .R ;
32+ import fr .free .nrw .commons .auth .SessionManager ;
5233import fr .free .nrw .commons .campaigns .Campaign ;
5334import fr .free .nrw .commons .campaigns .CampaignView ;
5435import fr .free .nrw .commons .campaigns .CampaignsPresenter ;
6647import fr .free .nrw .commons .nearby .NearbyController ;
6748import fr .free .nrw .commons .nearby .NearbyNotificationCardView ;
6849import fr .free .nrw .commons .nearby .Place ;
50+ import fr .free .nrw .commons .notification .Notification ;
6951import fr .free .nrw .commons .notification .NotificationActivity ;
70- import fr .free .nrw .commons .upload .UploadService ;
52+ import fr .free .nrw .commons .notification .NotificationController ;
53+ import fr .free .nrw .commons .theme .BaseActivity ;
7154import fr .free .nrw .commons .utils .ConfigUtils ;
7255import fr .free .nrw .commons .utils .DialogUtil ;
7356import fr .free .nrw .commons .utils .NetworkUtils ;
7760import io .reactivex .android .schedulers .AndroidSchedulers ;
7861import io .reactivex .disposables .CompositeDisposable ;
7962import io .reactivex .schedulers .Schedulers ;
63+ import java .util .List ;
64+ import javax .inject .Inject ;
65+ import javax .inject .Named ;
8066import timber .log .Timber ;
8167
8268public class ContributionsFragment
@@ -85,16 +71,14 @@ public class ContributionsFragment
8571 OnBackStackChangedListener ,
8672 LocationUpdateListener ,
8773 MediaDetailProvider ,
88- ICampaignsView , ContributionsContract .View , Callback , ServiceCallback {
74+ ICampaignsView , ContributionsContract .View , Callback {
8975 @ Inject @ Named ("default_preferences" ) JsonKvStore store ;
9076 @ Inject NearbyController nearbyController ;
9177 @ Inject OkHttpJsonApiClient okHttpJsonApiClient ;
9278 @ Inject CampaignsPresenter presenter ;
9379 @ Inject LocationServiceManager locationManager ;
9480 @ Inject NotificationController notificationController ;
9581
96- private UploadService uploadService ;
97- private boolean isUploadServiceConnected ;
9882 private CompositeDisposable compositeDisposable = new CompositeDisposable ();
9983
10084 private ContributionsListFragment contributionsListFragment ;
@@ -127,33 +111,7 @@ public static ContributionsFragment newInstance() {
127111 return fragment ;
128112 }
129113
130- /**
131- * Since we will need to use parent activity on onAuthCookieAcquired, we have to wait
132- * fragment to be attached. Latch will be responsible for this sync.
133- */
134- private ServiceConnection uploadServiceConnection = new ServiceConnection () {
135- @ Override
136- public void onServiceConnected (ComponentName componentName , IBinder binder ) {
137- uploadService = (UploadService ) ((UploadService .UploadServiceLocalBinder ) binder )
138- .getService ();
139- uploadService .setServiceCallback (ContributionsFragment .this );
140- isUploadServiceConnected = true ;
141- }
142-
143- @ Override
144- public void onServiceDisconnected (ComponentName componentName ) {
145- // this should never happen
146- Timber .e (new RuntimeException ("UploadService died but the rest of the process did not!" ));
147- isUploadServiceConnected = false ;
148- }
149-
150- @ Override
151- public void onBindingDied (final ComponentName name ) {
152- isUploadServiceConnected = false ;
153- }
154- };
155114 private boolean shouldShowMediaDetailsFragment ;
156- private boolean isAuthCookieAcquired ;
157115
158116 @ Override
159117 public void onCreate (@ Nullable Bundle savedInstanceState ) {
@@ -272,7 +230,6 @@ public void onAttach(Context context) {
272230 until fragment life time ends.
273231 */
274232 if (!isFragmentAttachedBefore && getActivity () != null ) {
275- onAuthCookieAcquired ();
276233 isFragmentAttachedBefore = true ;
277234 }
278235 }
@@ -312,19 +269,6 @@ public void onBackStackChanged() {
312269 fetchCampaigns ();
313270 }
314271
315- /**
316- * Called when onAuthCookieAcquired is called on authenticated parent activity
317- */
318- void onAuthCookieAcquired () {
319- // Since we call onAuthCookieAcquired method from onAttach, isAdded is still false. So don't use it
320- isAuthCookieAcquired =true ;
321- if (getActivity () != null ) { // If fragment is attached to parent activity
322- getActivity ().bindService (getUploadServiceIntent (), uploadServiceConnection , Context .BIND_AUTO_CREATE );
323- isUploadServiceConnected = true ;
324- }
325-
326- }
327-
328272 private void initFragments () {
329273 if (null == contributionsListFragment ) {
330274 contributionsListFragment = new ContributionsListFragment ();
@@ -381,13 +325,6 @@ public void removeFragment(Fragment fragment) {
381325 getChildFragmentManager ().executePendingTransactions ();
382326 }
383327
384-
385- public Intent getUploadServiceIntent (){
386- Intent intent = new Intent (getActivity (), UploadService .class );
387- intent .setAction (UploadService .ACTION_START_SERVICE );
388- return intent ;
389- }
390-
391328 @ SuppressWarnings ("ConstantConditions" )
392329 private void setUploadCount () {
393330 compositeDisposable .add (okHttpJsonApiClient
@@ -524,14 +461,6 @@ public void onDestroy() {
524461 locationManager .unregisterLocationManager ();
525462 locationManager .removeLocationListener (this );
526463 super .onDestroy ();
527-
528- if (isUploadServiceConnected ) {
529- if (getActivity () != null ) {
530- uploadService .setServiceCallback (null );
531- getActivity ().unbindService (uploadServiceConnection );
532- isUploadServiceConnected = false ;
533- }
534- }
535464 } catch (IllegalArgumentException | IllegalStateException exception ) {
536465 Timber .e (exception );
537466 }
@@ -594,7 +523,6 @@ private void fetchCampaigns() {
594523
595524 @ Override public void onDestroyView () {
596525 super .onDestroyView ();
597- isUploadServiceConnected = false ;
598526 presenter .onDetachView ();
599527 }
600528
@@ -606,8 +534,9 @@ private void fetchCampaigns() {
606534 @ Override
607535 public void retryUpload (Contribution contribution ) {
608536 if (NetworkUtils .isInternetConnectionEstablished (getContext ())) {
609- if (contribution .getState () == STATE_FAILED || contribution .getState () == STATE_PAUSED || contribution .getState ()==Contribution .STATE_QUEUED_LIMITED_CONNECTION_MODE && null != uploadService ) {
610- uploadService .queue (contribution );
537+ if (contribution .getState () == STATE_FAILED || contribution .getState () == STATE_PAUSED || contribution .getState ()==Contribution .STATE_QUEUED_LIMITED_CONNECTION_MODE ) {
538+ contribution .setState (Contribution .STATE_QUEUED );
539+ contributionsPresenter .saveContribution (contribution );
611540 Timber .d ("Restarting for %s" , contribution .toString ());
612541 } else {
613542 Timber .d ("Skipping re-upload for non-failed %s" , contribution .toString ());
@@ -624,7 +553,11 @@ public void retryUpload(Contribution contribution) {
624553 */
625554 @ Override
626555 public void pauseUpload (Contribution contribution ) {
627- uploadService .pauseUpload (contribution );
556+ //Pause the upload in the global singleton
557+ CommonsApplication .pauseUploads .put (contribution .getPageId (), true );
558+ //Retain the paused state in DB
559+ contribution .setState (STATE_PAUSED );
560+ contributionsPresenter .saveContribution (contribution );
628561 }
629562
630563 /**
@@ -677,10 +610,5 @@ public void backButtonClicked() {
677610 public MediaDetailPagerFragment getMediaDetailPagerFragment () {
678611 return mediaDetailPagerFragment ;
679612 }
680-
681- @ Override
682- public void updateUploadCount () {
683- setUploadCount ();
684- }
685613}
686614
0 commit comments