This class handles errors raised by connection failure to the health data store.
Considerations for Connection Failure
A partner app works with
Samsung Health.
The partner app can meet the following error cases when it connects to
Samsung Health.
Handling Connection Exceptions
Samsung Health SDK for Android helps you to handle all available error cases with HealthConnectionError
when your application connects to the health data store.
HealthConnectionError is retrieved in HealthDataStore.ConnectionListener's event handler
and all Samsung Health's partner applications need to check the health data store's connection error mandatorily.
The following example shows how to handle the connection error.
public class MainActivity extends Activity {
private HealthDataStore mStore;
private HealthConnectionErrorResult mConnError;
private static MainActivity mInstance = null;
@Override
public void onCreate(Bundle savedInstanceState) {
// ...
mStore = new HealthDataStore(this, mConnectionListener);
mStore.connectService();
}
@Override
public void onDestroy() {
mStore.disconnectService();
// ...
}
private final HealthDataStore.ConnectionListener mConnectionListener = new HealthDataStore.ConnectionListener() {
@Override
public void onConnected() {
// Connected
}
@Override
public void onConnectionFailed(HealthConnectionErrorResult error) {
// Connection fails
showConnectionFailureDialog(error);
}
@Override
public void onDisconnected() {
// Connection is disconnected
}
};
private void showConnectionFailureDialog(HealthConnectionErrorResult error) {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
mConnError = error;
if (mConnError.hasResolution()) {
switch(error.getErrorCode()) {
case HealthConnectionErrorResult.PLATFORM_NOT_INSTALLED:
message = "Please install Samsung Health";
break;
case HealthConnectionErrorResult.OLD_VERSION_PLATFORM:
message = "Please upgrade Samsung Health";
break;
case HealthConnectionErrorResult.PLATFORM_DISABLED:
message = "Please enable Samsung Health";
break;
case HealthConnectionErrorResult.USER_AGREEMENT_NEEDED:
message = "Please agree to Samsung Health policy";
break;
default:
message = "Please make Samsung Health available";
break;
}
} else {
// In case that the device doesn't support Samsung Health,
// hasResolution() returns false also.
alert.setMessage(R.string.msg_conn_not_available);
}
alert.setPositiveButton(R.string.ok, (dialog, id) -> {
if (error.hasResolution()) {
error.resolve(MainActivity.this);
}
});
if (error.hasResolution()) {
alert.setNegativeButton("Cancel", null);
}
alert.show();
}
}