33import android .annotation .SuppressLint ;
44import android .app .AlertDialog ;
55import android .content .Intent ;
6+ import android .os .Build .VERSION ;
7+ import android .os .Build .VERSION_CODES ;
68import android .os .Bundle ;
79import android .view .LayoutInflater ;
810import android .view .Menu ;
911import android .view .MenuInflater ;
1012import android .view .MenuItem ;
1113import android .view .View ;
1214import android .widget .ImageView ;
15+ import android .widget .Switch ;
1316import android .widget .TextView ;
1417import androidx .annotation .Nullable ;
1518import androidx .core .view .GravityCompat ;
2124import butterknife .BindView ;
2225import butterknife .ButterKnife ;
2326import com .google .android .material .tabs .TabLayout ;
27+ import fr .free .nrw .commons .CommonsApplication ;
2428import fr .free .nrw .commons .R ;
2529import fr .free .nrw .commons .auth .SessionManager ;
2630import fr .free .nrw .commons .location .LocationServiceManager ;
3337import fr .free .nrw .commons .theme .NavigationBaseActivity ;
3438import fr .free .nrw .commons .upload .UploadService ;
3539import fr .free .nrw .commons .utils .ViewUtil ;
40+ import fr .free .nrw .commons .utils .ViewUtilWrapper ;
3641import io .reactivex .android .schedulers .AndroidSchedulers ;
3742import io .reactivex .schedulers .Schedulers ;
3843import java .util .List ;
@@ -56,6 +61,8 @@ public class MainActivity extends NavigationBaseActivity implements FragmentMana
5661 NotificationController notificationController ;
5762 @ Inject
5863 QuizChecker quizChecker ;
64+ @ Inject
65+ ViewUtilWrapper viewUtilWrapper ;
5966
6067
6168 public ContributionsActivityPagerAdapter contributionsActivityPagerAdapter ;
@@ -70,6 +77,7 @@ public class MainActivity extends NavigationBaseActivity implements FragmentMana
7077 private TextView notificationCount ;
7178 private NearbyParentFragment nearbyParentFragment ;
7279
80+ @ Override
7381 public void onCreate (Bundle savedInstanceState ) {
7482 super .onCreate (savedInstanceState );
7583 setContentView (R .layout .activity_contributions );
@@ -280,9 +288,25 @@ public boolean onCreateOptionsMenu(Menu menu) {
280288 this .menu = menu ;
281289 updateMenuItem ();
282290 setNotificationCount ();
291+
292+ updateLimitedConnectionToggle (menu );
293+
283294 return true ;
284295 }
285296
297+ private void updateLimitedConnectionToggle (Menu menu ) {
298+ MenuItem checkable = menu .findItem (R .id .toggle_limited_connection_mode );
299+ boolean isEnabled = defaultKvStore
300+ .getBoolean (CommonsApplication .IS_LIMITED_CONNECTION_MODE_ENABLED , false );
301+
302+ checkable .setChecked (isEnabled );
303+ final Switch switchToggleLimitedConnectionMode = checkable .getActionView ()
304+ .findViewById (R .id .switch_toggle_limited_connection_mode );
305+ switchToggleLimitedConnectionMode .setChecked (isEnabled );
306+ switchToggleLimitedConnectionMode .setOnCheckedChangeListener (
307+ (buttonView , isChecked ) -> toggleLimitedConnectionMode ());
308+ }
309+
286310 @ SuppressLint ("CheckResult" )
287311 private void setNotificationCount () {
288312 compositeDisposable .add (notificationController .getNotifications (false )
@@ -339,6 +363,27 @@ public boolean onOptionsItemSelected(MenuItem item) {
339363 }
340364 }
341365
366+ private void toggleLimitedConnectionMode () {
367+ defaultKvStore .putBoolean (CommonsApplication .IS_LIMITED_CONNECTION_MODE_ENABLED ,
368+ !defaultKvStore
369+ .getBoolean (CommonsApplication .IS_LIMITED_CONNECTION_MODE_ENABLED , false ));
370+ if (defaultKvStore
371+ .getBoolean (CommonsApplication .IS_LIMITED_CONNECTION_MODE_ENABLED , false )) {
372+ viewUtilWrapper
373+ .showShortToast (getBaseContext (), getString (R .string .limited_connection_enabled ));
374+ } else {
375+ Intent intent = new Intent (this , UploadService .class );
376+ intent .setAction (UploadService .PROCESS_PENDING_LIMITED_CONNECTION_MODE_UPLOADS );
377+ if (VERSION .SDK_INT >= VERSION_CODES .O ) {
378+ startForegroundService (intent );
379+ } else {
380+ startService (intent );
381+ }
382+ viewUtilWrapper
383+ .showShortToast (getBaseContext (), getString (R .string .limited_connection_disabled ));
384+ }
385+ }
386+
342387 public class ContributionsActivityPagerAdapter extends FragmentPagerAdapter {
343388 FragmentManager fragmentManager ;
344389
0 commit comments