NetworkType.UNMETERED vs NetworkType.METERED – PeriodicWork

in my application I am using work manager for periodic work. I am uploading files to server. I have one button on click of that button one dialog shown up and ask user – Which network you want to use while uploading file – 1. Wifi 2. Any

If user click on wifi I am uploading file after every 30 Min, If user click on Any I am uploading file after every 1 hr.

Following is my code for this: 1. If user select WIFI

PeriodicWorkRequest.Builder wifiWorkBuilder =
                            new PeriodicWorkRequest.Builder(FileUpload.class, 30,
                                    TimeUnit.MINUTES)
                                    .addTag("WIFIJOB1")
                                    .setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.UNMETERED).build());
                    wifiWork = wifiWorkBuilder.build();
                    WorkManager.getInstance().enqueueUniquePeriodicWork("wifiJob", ExistingPeriodicWorkPolicy.REPLACE, wifiWork);

If User select Any:

PeriodicWorkRequest.Builder mobileDataWorkBuilder =
                                new PeriodicWorkRequest.Builder(FileUpload.class, 1,
                                        TimeUnit.HOURS)
                                        .addTag("MOBILEDATAJOB1")
                                        .setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build());
                        mobileDataWork = mobileDataWorkBuilder.build();
                        WorkManager.getInstance().enqueueUniquePeriodicWork("mobileDataJob", ExistingPeriodicWorkPolicy.REPLACE, mobileDataWork);

For any network it works perfectly and upload apk after every 1 hr. But if user select Wifi then here is problem –

If user connected to wifi of other mobile(say he is using hotspot) so here network is I guess consider as Metered network so it will not upload file. I just want to know our House or office network are by default are Unmetered network or not. If suppose its not fix (Means some are metered and some are unmetered) then using this code if user select wifi and user wifi is considered as metered then from his device file will never get uploaded.

Or should I create another task like :

PeriodicWorkRequest.Builder meteredwifiWorkBuilder =
                            new PeriodicWorkRequest.Builder(FileUpload.class, 45,
                                    TimeUnit.MINUTES)
                                    .addTag("METEREDWIFIJOB")
                                    .setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.METERED).build());
                    wifiWork = wifiWorkBuilder.build();
                    WorkManager.getInstance().enqueueUniquePeriodicWork("meteredwifiJob", ExistingPeriodicWorkPolicy.REPLACE, wifiWork);

So if user not connected to wifi file will be uploaded after every 1 hr, If connected to wifi (unmetered) file will be uploaded after every 30 min and if connected to metered wifi then file will be uploaded after every 45 min.

Is above logic make sense to create 3 sepearte task to upload file. Any suggestion will be appreciated. Thanks in advance

Leave a Comment