Checking network status before requests

This commit is contained in:
Przemek Grondek 2014-08-01 14:30:45 +02:00
parent 5f87f442c6
commit 9ea0c789bb
7 changed files with 83 additions and 42 deletions

View file

@ -37,5 +37,6 @@
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

View file

@ -45,12 +45,16 @@ public class DatabaseFragment extends Fragment implements AdapterView.OnItemClic
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String chosenDatabase = (String) listAdapter.getItem(position);
listAdapter.getItem(position);
Intent intent = new Intent(getActivity(), TableActivity.class);
intent.putExtra(Static.DATABASE_NAME_ARG,chosenDatabase);
Static.asyncDatabaseConnector.setDatabaseInUse(chosenDatabase);
startActivity(intent);
if(Static.isNetworkConnected(getActivity())) {
String chosenDatabase = (String) listAdapter.getItem(position);
listAdapter.getItem(position);
Intent intent = new Intent(getActivity(), TableActivity.class);
intent.putExtra(Static.DATABASE_NAME_ARG, chosenDatabase);
Static.asyncDatabaseConnector.setDatabaseInUse(chosenDatabase);
startActivity(intent);
} else {
Static.showErrorAlert(getResources().getString(R.string.no_connection), getActivity());
}
}
@Override

View file

@ -139,21 +139,23 @@ public class EntriesFragment extends Fragment implements AsyncDatabaseConnector.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_previous:
page--;
break;
case R.id.action_next:
page++;
break;
if(Static.isNetworkConnected(getActivity())) {
switch (item.getItemId()) {
case R.id.action_previous:
page--;
break;
case R.id.action_next:
page++;
break;
}
changeMenus(page);
entriesTable.removeAllViews(); // clean table
setLoading(true);
Static.asyncDatabaseConnector.getRows(tableName, entriesLimit, page); // get new entries
} else {
Static.showErrorAlert(getResources().getString(R.string.no_connection), getActivity());
}
changeMenus(page);
entriesTable.removeAllViews(); // clean table
setLoading(true);
Static.asyncDatabaseConnector.getRows(tableName, entriesLimit, page); // get new entries
return super.onOptionsItemSelected(item);
}

View file

@ -85,9 +85,16 @@ public class LoginFragment extends Fragment implements View.OnClickListener, Asy
login = loginTextbox.getText().toString();
password = passwordTextbox.getText().toString();
url = urlTextbox.getText().toString();
asyncDatabaseConnector = new AsyncDatabaseConnector(login, password, url);
asyncDatabaseConnector.setBooleanReturnListener(this);
asyncDatabaseConnector.checkLogin();
if(Static.isNetworkConnected(getActivity())) {
asyncDatabaseConnector = new AsyncDatabaseConnector(login, password, url);
asyncDatabaseConnector.setBooleanReturnListener(this);
asyncDatabaseConnector.checkLogin();
} else {
Static.showErrorAlert(getResources().getString(R.string.no_connection), getActivity());
loginButton.setEnabled(true); // Now we can click button again
progressBar.setVisibility(View.INVISIBLE);
}
}
@Override
@ -98,21 +105,11 @@ public class LoginFragment extends Fragment implements View.OnClickListener, Asy
startActivity(intent);
}
else {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(Static.asyncDatabaseConnector.errorMsg);
builder.setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Nothing to do here
// Cleaning inputs is stupid
}
});
builder.setTitle(R.string.error);
builder.setIcon(R.drawable.ic_action_warning);
builder.create();
builder.show();
Static.showErrorAlert(Static.asyncDatabaseConnector.errorMsg, getActivity());
}
loginButton.setEnabled(true); // Now we can click button again
progressBar.setVisibility(View.INVISIBLE);
}
}

View file

@ -1,7 +1,13 @@
package info.nerull7.mysqlbrowser;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import java.net.NetworkInterface;
import info.nerull7.mysqlbrowser.db.AsyncDatabaseConnector;
@ -18,4 +24,29 @@ public class Static {
Intent intent = new Intent(context, SettingsActivity.class);
context.startActivity(intent);
}
public static boolean isNetworkConnected(Context context){
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo!=null && networkInfo.isConnected()){
return true;
} else
return false;
}
public static void showErrorAlert(String errorMessage, Context context){
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(errorMessage);
builder.setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Nothing to do here
// Cleaning inputs is stupid
}
});
builder.setTitle(R.string.error);
builder.setIcon(R.drawable.ic_action_warning);
builder.create();
builder.show();
}
}

View file

@ -45,12 +45,17 @@ public class TableFragment extends Fragment implements AdapterView.OnItemClickLi
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String chosenTable = (String) listAdapter.getItem(position);
listAdapter.getItem(position);
Intent intent = new Intent(getActivity(), EntriesActivity.class);
intent.putExtra(Static.DATABASE_NAME_ARG,databaseName);
intent.putExtra(Static.TABLE_NAME_ARG,chosenTable);
startActivity(intent);
if(Static.isNetworkConnected(getActivity())) {
String chosenTable = (String) listAdapter.getItem(position);
listAdapter.getItem(position);
Intent intent = new Intent(getActivity(), EntriesActivity.class);
intent.putExtra(Static.DATABASE_NAME_ARG,databaseName);
intent.putExtra(Static.TABLE_NAME_ARG,chosenTable);
startActivity(intent);
} else {
Static.showErrorAlert(getResources().getString(R.string.no_connection), getActivity());
}
}
@Override

View file

@ -30,5 +30,6 @@
<string name="login_settings">Login credentials</string>
<string name="save_credentials">Save credentials</string>
<string name="connector_url">Connector URL</string>
<string name="no_connection">No Internet Connection</string>
</resources>