1
0
mirror of https://github.com/nerzhul/ownCloud-SMS-App.git synced 2025-06-08 16:36:10 +00:00

Verify connection when restoring messages

This commit is contained in:
Loic Blot 2016-12-08 22:14:29 +01:00
parent 819fa4543d
commit 1bb93a5068
5 changed files with 94 additions and 36 deletions

View File

@ -35,12 +35,14 @@ import android.widget.TextView;
import fr.unix_experience.owncloud_sms.R;
import fr.unix_experience.owncloud_sms.engine.ASyncSMSRecovery;
import fr.unix_experience.owncloud_sms.engine.ConnectivityMonitor;
public class RestoreMessagesActivity extends AppCompatActivity {
Account _account = null;
String _defaultSmsApp;
private static final int REQUEST_DEFAULT_SMSAPP = 1;
boolean restoreInProgress = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -65,13 +67,58 @@ public class RestoreMessagesActivity extends AppCompatActivity {
throw new IllegalStateException(getString(R.string.err_didnt_find_account_restore));
}
initInterface();
Button fix_button = (Button) findViewById(R.id.button_fix_permissions);
final Button launch_restore = (Button) findViewById(R.id.button_launch_restore);
final ProgressBar pb = (ProgressBar) findViewById(R.id.progressbar_restore);
final RestoreMessagesActivity me = this;
fix_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
notifyIncompatibleVersion();
return;
}
if (!new ConnectivityMonitor(me).isValid()) {
notifyNoConnectivity();
return;
}
Log.i(RestoreMessagesActivity.TAG, "Ask to change the default SMS app");
Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, getBaseContext().getPackageName());
startActivityForResult(intent, REQUEST_DEFAULT_SMSAPP);
}
});
launch_restore.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!new ConnectivityMonitor(me).isValid()) {
notifyNoConnectivity();
return;
}
launch_restore.setVisibility(View.INVISIBLE);
pb.setVisibility(View.VISIBLE);
// Verify connectivity
Log.i(RestoreMessagesActivity.TAG, "Launching restore asynchronously");
restoreInProgress = true;
new ASyncSMSRecovery.SMSRecoveryTask(me, _account).execute();
}
});
}
private void initInterface() {
TextView tv_error = (TextView) findViewById(R.id.tv_error_default_smsapp);
tv_error.setText(R.string.error_make_default_sms_app);
findViewById(R.id.tv_restore_finished).setVisibility(View.INVISIBLE);
findViewById(R.id.tv_progress_value).setVisibility(View.INVISIBLE);
Button fix_button = (Button) findViewById(R.id.button_fix_permissions);
final Button launch_restore = (Button) findViewById(R.id.button_launch_restore);
final ProgressBar pb = (ProgressBar) findViewById(R.id.progressbar_restore);
Button launch_restore = (Button) findViewById(R.id.button_launch_restore);
ProgressBar pb = (ProgressBar) findViewById(R.id.progressbar_restore);
pb.setVisibility(View.INVISIBLE);
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
@ -91,46 +138,28 @@ public class RestoreMessagesActivity extends AppCompatActivity {
fix_button.setVisibility(View.INVISIBLE);
launch_restore.setVisibility(View.VISIBLE);
}
fix_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
notifyIncompatibleVersion();
return;
}
Log.i(RestoreMessagesActivity.TAG, "Ask to change the default SMS app");
Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, getBaseContext().getPackageName());
startActivityForResult(intent, REQUEST_DEFAULT_SMSAPP);
}
});
final RestoreMessagesActivity me = this;
launch_restore.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
launch_restore.setVisibility(View.INVISIBLE);
pb.setVisibility(View.VISIBLE);
// Verify connectivity
Log.i(RestoreMessagesActivity.TAG, "Launching restore asynchronously");
new ASyncSMSRecovery.SMSRecoveryTask(me, _account).execute();
}
});
}
private void notifyIncompatibleVersion() {
private void errorNotification(int err) {
TextView tv = (TextView) findViewById(R.id.tv_error_default_smsapp);
Button fix_button = (Button) findViewById(R.id.button_fix_permissions);
Button launch_restore = (Button) findViewById(R.id.button_launch_restore);
ProgressBar pb = (ProgressBar) findViewById(R.id.progressbar_restore);
tv.setText(R.string.err_kitkat_required);
tv.setText(err);
tv.setVisibility(View.VISIBLE);
fix_button.setVisibility(View.INVISIBLE);
launch_restore.setVisibility(View.INVISIBLE);
pb.setVisibility(View.INVISIBLE);
}
private void notifyIncompatibleVersion() {
errorNotification(R.string.err_kitkat_required);
}
private void notifyNoConnectivity() {
errorNotification(R.string.err_no_connection);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
@ -149,15 +178,33 @@ public class RestoreMessagesActivity extends AppCompatActivity {
}
}
@Override
protected void onResume() {
super.onResume();
if (!new ConnectivityMonitor(this).isValid()) {
notifyNoConnectivity();
return;
}
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
notifyIncompatibleVersion();
return;
}
if (!restoreInProgress) {
initInterface();
}
}
public void onRestoreDone() {
// @TODO
Log.i(RestoreMessagesActivity.TAG, "Sync is done, updating interface");
findViewById(R.id.progressbar_restore).setVisibility(View.INVISIBLE);
findViewById(R.id.tv_restore_finished).setVisibility(View.VISIBLE);
Intent finalIntent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
finalIntent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, _defaultSmsApp);
startActivity(finalIntent);
restoreInProgress = false;
}
public void onProgressUpdate(Integer value) {

View File

@ -49,10 +49,13 @@ public interface ASyncSMSRecovery {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
return null;
}
if (!new ConnectivityMonitor(_context).isValid()) {
Log.e(ASyncSMSRecovery.TAG, "Restore connectivity problems, aborting");
return null;
}
Log.i(ASyncSMSRecovery.TAG, "Starting background recovery");
// Verify connectivity
Long start = (long) 0;
OCSMSOwnCloudClient client = new OCSMSOwnCloudClient(_context, _account);
@ -119,6 +122,11 @@ public interface ASyncSMSRecovery {
}
start = obj.getLong("last_id");
if (!new ConnectivityMonitor(_context).isValid()) {
Log.e(ASyncSMSRecovery.TAG, "Restore connectivity problems, aborting");
return null;
}
obj = client.retrieveSomeMessages(start, 500);
}
} catch (JSONException e) {

View File

@ -178,5 +178,6 @@
<string name="err_kitkat_required">Android 4.4 or greater is required to use this feature.</string>
<string name="error_make_default_sms_app">Please make this application default SMS application to permit restore your messages. This limitation has been introduced by Android 4.4.</string>
<string name="x_messages_restored">messages restored...</string>
<string name="err_no_connection">No connection available, please ensure you have a valid data connection.</string>
</resources>

View File

@ -166,4 +166,5 @@
<string name="err_sync_get_smslist">Erreur #1: Données invalides reçues lors de l\'obtention des messages précédents.</string>
<string name="error_make_default_sms_app">Merci de bien vouloir mettre cette application en application SMS par défaut le temps de restaurer les messages. Cette limitation a été ajoutée depuis Android 4.4.</string>
<string name="x_messages_restored">messages restaurés...</string>
<string name="err_no_connection">Aucune connexion disponible, assurez voir d\'avoir une connection de données valide.</string>
</resources>

View File

@ -253,4 +253,5 @@
<string name="err_kitkat_required">Android 4.4 or greater is required to use this feature.</string>
<string name="restore_finished">SMS restauration is now finished.</string>
<string name="x_messages_restored">messages restored...</string>
<string name="err_no_connection">No connection available, please ensure you have a valid data connection.</string>
</resources>