Skip to content

Commit

Permalink
Merge pull request getodk#3721 from grzesiek2010/network
Browse files Browse the repository at this point in the history
Factored out NetworkStateProvider class
  • Loading branch information
seadowg authored Mar 20, 2020
2 parents 6fa1c4f + 244a985 commit 9594e13
Show file tree
Hide file tree
Showing 19 changed files with 107 additions and 106 deletions.
4 changes: 2 additions & 2 deletions collect_app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ the specific language governing permissions and limitations under the License.
android:windowSoftInputMode="stateHidden" />
<activity android:name=".activities.InstanceChooserList" />
<activity android:name=".activities.FormChooserListActivity" />
<activity android:name=".activities.FormDownloadList" />
<activity android:name=".activities.FormDownloadListActivity" />
<activity
android:name=".activities.FileManagerTabs"
android:configChanges="orientation|screenSize" />
Expand Down Expand Up @@ -236,7 +236,7 @@ the specific language governing permissions and limitations under the License.

<activity-alias
android:name=".activities.FormDownloadList"
android:targetActivity=".activities.FormDownloadList">
android:targetActivity=".activities.FormDownloadListActivity">
<intent-filter>
<action android:name="org.odk.collect.android.FORM_DOWNLOAD" />
<category android:name="android.intent.category.DEFAULT"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
Expand All @@ -41,6 +38,7 @@
import org.odk.collect.android.adapters.FormDownloadListAdapter;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.dao.FormsDao;
import org.odk.collect.android.network.NetworkStateProvider;
import org.odk.collect.android.openrosa.HttpCredentialsInterface;
import org.odk.collect.android.injection.DaggerUtils;
import org.odk.collect.android.listeners.DownloadFormsTaskListener;
Expand Down Expand Up @@ -88,7 +86,7 @@
*
* @author Carl Hartung ([email protected])
*/
public class FormDownloadList extends FormListActivity implements FormListDownloaderListener,
public class FormDownloadListActivity extends FormListActivity implements FormListDownloaderListener,
DownloadFormsTaskListener, AuthDialogUtility.AuthDialogUtilityResultListener, AdapterView.OnItemClickListener {
private static final String FORM_DOWNLOAD_LIST_SORTING_ORDER = "formDownloadListSortingOrder";

Expand Down Expand Up @@ -126,6 +124,9 @@ public class FormDownloadList extends FormListActivity implements FormListDownlo
@Inject
FormListDownloader formListDownloader;

@Inject
NetworkStateProvider connectivityProvider;

@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -145,7 +146,7 @@ public void granted() {
try {
new StorageInitializer().createOdkDirsOnStorage();
} catch (RuntimeException e) {
DialogUtils.showDialog(DialogUtils.createErrorDialog(FormDownloadList.this, e.getMessage(), EXIT), FormDownloadList.this);
DialogUtils.showDialog(DialogUtils.createErrorDialog(FormDownloadListActivity.this, e.getMessage(), EXIT), FormDownloadListActivity.this);
return;
}

Expand Down Expand Up @@ -297,11 +298,7 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
* Starts the download task and shows the progress dialog.
*/
private void downloadFormList() {
ConnectivityManager connectivityManager =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = connectivityManager.getActiveNetworkInfo();

if (ni == null || !ni.isConnected()) {
if (!connectivityProvider.isDeviceOnline()) {
ToastUtils.showShortToast(R.string.no_connection);

if (viewModel.isDownloadOnlyMode()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
import org.odk.collect.android.logic.FormController.FailedConstraint;
import org.odk.collect.android.logic.FormInfo;
import org.odk.collect.android.logic.ImmutableDisplayableQuestion;
import org.odk.collect.android.network.NetworkStateProvider;
import org.odk.collect.android.preferences.AdminKeys;
import org.odk.collect.android.preferences.AdminSharedPreferences;
import org.odk.collect.android.preferences.GeneralKeys;
Expand Down Expand Up @@ -309,6 +310,9 @@ enum AnimationType {
@Inject
Analytics analytics;

@Inject
NetworkStateProvider connectivityProvider;

private final LocationProvidersReceiver locationProvidersReceiver = new LocationProvidersReceiver();

/**
Expand Down Expand Up @@ -858,7 +862,7 @@ protected void onActivityResult(int requestCode, int resultCode, final Intent in
progressDialog.setMessage(getString(R.string.please_wait));
progressDialog.show(getSupportFragmentManager(), ProgressDialogFragment.COLLECT_PROGRESS_DIALOG_TAG);

mediaLoadingFragment.beginMediaLoadingTask(intent.getData());
mediaLoadingFragment.beginMediaLoadingTask(intent.getData(), connectivityProvider);
break;
case RequestCodes.AUDIO_CAPTURE:
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import androidx.annotation.NonNull;
Expand All @@ -50,6 +47,7 @@
import org.odk.collect.android.listeners.PermissionListener;
import org.odk.collect.android.listeners.TaskListener;
import org.odk.collect.android.logic.DriveListItem;
import org.odk.collect.android.network.NetworkStateProvider;
import org.odk.collect.android.storage.StoragePathProvider;
import org.odk.collect.android.storage.StorageSubdirectory;
import org.odk.collect.android.utilities.DialogUtils;
Expand Down Expand Up @@ -111,6 +109,9 @@ public class GoogleDriveActivity extends FormListActivity implements View.OnClic
@Inject
GoogleAccountsManager accountsManager;

@Inject
NetworkStateProvider connectivityProvider;

private void initToolbar() {
Toolbar toolbar = findViewById(R.id.toolbar);
setTitle(getString(R.string.google_drive));
Expand Down Expand Up @@ -151,7 +152,7 @@ public void onCreate(Bundle savedInstanceState) {
// new
myDrive = false;

if (!isDeviceOnline()) {
if (!connectivityProvider.isDeviceOnline()) {
createAlertDialog(getString(R.string.no_connection));
}
}
Expand Down Expand Up @@ -225,7 +226,7 @@ private void getResultsFromApi() {
if (!accountsManager.isAccountSelected()) {
selectAccount();
} else {
if (isDeviceOnline()) {
if (connectivityProvider.isDeviceOnline()) {
toDownload.clear();
filteredList.clear();
driveList.clear();
Expand Down Expand Up @@ -264,18 +265,6 @@ public void denied() {
});
}

/**
* Checks whether the device currently has a network connection.
*
* @return true if the device has a network connection, false otherwise.
*/
private boolean isDeviceOnline() {
ConnectivityManager connMgr =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(MY_DRIVE_KEY, myDrive);
Expand Down Expand Up @@ -566,7 +555,7 @@ public void onClick(View v) {
downloadButton.setEnabled(false);
toDownload.clear();
driveList.clear();
if (isDeviceOnline()) {
if (connectivityProvider.isDeviceOnline()) {
if (folderIdStack.empty()) {
parentId = ROOT_KEY;
} else {
Expand All @@ -590,7 +579,7 @@ public void onClick(View v) {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
DriveListItem item = filteredList.get(position);
if (item != null && item.getType() == DriveListItem.DIR) {
if (isDeviceOnline()) {
if (connectivityProvider.isDeviceOnline()) {
toDownload.clear();
driveList.clear();
clearSearchView();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
Expand All @@ -41,6 +38,7 @@
import org.odk.collect.android.injection.DaggerUtils;
import org.odk.collect.android.listeners.InstanceUploaderListener;
import org.odk.collect.android.listeners.PermissionListener;
import org.odk.collect.android.network.NetworkStateProvider;
import org.odk.collect.android.preferences.GeneralKeys;
import org.odk.collect.android.tasks.InstanceGoogleSheetsUploaderTask;
import org.odk.collect.android.utilities.ArrayUtils;
Expand Down Expand Up @@ -74,6 +72,9 @@ public class GoogleSheetsUploaderActivity extends CollectAbstractActivity implem
@Inject
GoogleAccountsManager accountsManager;

@Inject
NetworkStateProvider connectivityProvider;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -143,7 +144,7 @@ private void runTask() {
private void getResultsFromApi() {
if (!accountsManager.isAccountSelected()) {
selectAccount();
} else if (!isDeviceOnline()) {
} else if (!connectivityProvider.isDeviceOnline()) {
ToastUtils.showShortToast("No network connection available.");
} else {
runTask();
Expand Down Expand Up @@ -200,18 +201,6 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
}

/**
* Checks whether the device currently has a network connection.
*
* @return true if the device has a network connection, false otherwise.
*/
private boolean isDeviceOnline() {
ConnectivityManager connMgr =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}

@Override
protected void onResume() {
if (instanceGoogleSheetsUploaderTask != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -47,6 +45,7 @@
import org.odk.collect.android.injection.DaggerUtils;
import org.odk.collect.android.listeners.DiskSyncListener;
import org.odk.collect.android.listeners.PermissionListener;
import org.odk.collect.android.network.NetworkStateProvider;
import org.odk.collect.android.preferences.GeneralSharedPreferences;
import org.odk.collect.android.preferences.PreferencesActivity;
import org.odk.collect.android.preferences.Transport;
Expand Down Expand Up @@ -132,6 +131,9 @@ public void onReceive(Context context, Intent intent) {
@Inject
PermissionUtils permissionUtils;

@Inject
NetworkStateProvider connectivityProvider;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -175,11 +177,7 @@ public void onUploadButtonsClicked(Button button) {
Transport transport = Transport.fromPreference(GeneralSharedPreferences.getInstance().get(KEY_SUBMISSION_TRANSPORT_TYPE));

if (!transport.equals(Transport.Sms) && button.getId() == R.id.upload_button) {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = connectivityManager.getActiveNetworkInfo();

if (ni == null || !ni.isConnected()) {
if (!connectivityProvider.isDeviceOnline()) {
ToastUtils.showShortToast(R.string.no_connection);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public void onClick(View v) {
}
} else {
i = new Intent(getApplicationContext(),
FormDownloadList.class);
FormDownloadListActivity.class);
}
startActivity(i);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
import java.util.ArrayList;
import java.util.HashMap;

import static org.odk.collect.android.activities.FormDownloadList.FORMID_DISPLAY;
import static org.odk.collect.android.activities.FormDownloadList.FORMNAME;
import static org.odk.collect.android.activities.FormDownloadList.FORM_ID_KEY;
import static org.odk.collect.android.activities.FormDownloadListActivity.FORMID_DISPLAY;
import static org.odk.collect.android.activities.FormDownloadListActivity.FORMNAME;
import static org.odk.collect.android.activities.FormDownloadListActivity.FORM_ID_KEY;

public class FormDownloadListAdapter extends ArrayAdapter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.SystemClock;
import android.preference.PreferenceManager;
Expand Down Expand Up @@ -161,20 +159,6 @@ public void setExternalDataManager(ExternalDataManager externalDataManager) {
this.externalDataManager = externalDataManager;
}

/**
* Get a User-Agent string that provides the platform details followed by the application ID
* and application version name: {@code Dalvik/<version> (platform info) org.odk.collect.android/v<version>}.
*
* This deviates from the recommended format as described in https://github.com/opendatakit/collect/issues/3253.
*/

public boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager) getInstance()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo currentNetworkInfo = manager.getActiveNetworkInfo();
return currentNetworkInfo != null && currentNetworkInfo.isConnected();
}

/*
Adds support for multidex support library. For more info check out the link below,
https://developer.android.com/studio/build/multidex.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
import androidx.annotation.Nullable;

import org.odk.collect.android.activities.FormEntryActivity;
import org.odk.collect.android.network.NetworkStateProvider;
import org.odk.collect.android.tasks.MediaLoadingTask;

public class MediaLoadingFragment extends Fragment {

private MediaLoadingTask mediaLoadingTask;
private FormEntryActivity formEntryActivity;

public void beginMediaLoadingTask(Uri uri) {
mediaLoadingTask = new MediaLoadingTask(formEntryActivity);
public void beginMediaLoadingTask(Uri uri, NetworkStateProvider connectivityProvider) {
mediaLoadingTask = new MediaLoadingTask(formEntryActivity, connectivityProvider);
mediaLoadingTask.execute(uri);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import android.telephony.SmsManager;

import org.javarosa.core.reference.ReferenceManager;
import org.odk.collect.android.activities.FormDownloadList;
import org.odk.collect.android.activities.FormDownloadListActivity;
import org.odk.collect.android.activities.FormEntryActivity;
import org.odk.collect.android.activities.FormMapActivity;
import org.odk.collect.android.activities.GeoPointMapActivity;
Expand Down Expand Up @@ -113,7 +113,7 @@ interface Builder {

void inject(AuthDialogUtility authDialogUtility);

void inject(FormDownloadList formDownloadList);
void inject(FormDownloadListActivity formDownloadListActivity);

void inject(InstanceUploaderListActivity activity);

Expand Down
Loading

0 comments on commit 9594e13

Please sign in to comment.