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

Better OCSMSOwncloudClient object creation

This commit is contained in:
Loic Blot 2016-12-06 23:04:51 +01:00
parent a3088e7718
commit f76de90a6e
4 changed files with 31 additions and 41 deletions

View File

@ -5,7 +5,6 @@ import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.ContactsContract;
import android.support.v4.widget.SwipeRefreshLayout;
@ -51,19 +50,14 @@ public interface ASyncContactLoad {
}
@Override
protected Boolean doInBackground(Void... params) {
// Create client
String ocURI = ContactLoadTask._accountMgr.getUserData(ContactLoadTask._account, "ocURI");
if (ocURI == null) {
// @TODO: Handle the problem
OCSMSOwnCloudClient _client = null;
try {
_client = new OCSMSOwnCloudClient(_context, ContactLoadTask._account);
}
catch (IllegalStateException e) {
return false;
}
Uri serverURI = Uri.parse(ocURI);
OCSMSOwnCloudClient _client = new OCSMSOwnCloudClient(_context,
serverURI, ContactLoadTask._accountMgr.getUserData(ContactLoadTask._account, "ocLogin"),
ContactLoadTask._accountMgr.getPassword(ContactLoadTask._account));
// Remove all objects, due to refreshing handling
_objects.clear();
try {

View File

@ -47,15 +47,13 @@ public interface ASyncSMSSync {
// Notify that we are syncing SMS
for (Account element : myAccountList) {
Uri serverURI = Uri.parse(_accountMgr.getUserData(element, "ocURI"));
OCSMSOwnCloudClient _client = new OCSMSOwnCloudClient(_context,
serverURI, _accountMgr.getUserData(element, "ocLogin"),
_accountMgr.getPassword(element));
try {
OCSMSOwnCloudClient _client = new OCSMSOwnCloudClient(_context, element);
_client.doPushRequest(_smsList);
OCSMSNotificationUI.cancel(_context);
} catch (IllegalStateException e) { // Fail to read account data
OCSMSNotificationUI.notify(_context, _context.getString(R.string.fatal_error),
e.toString(), OCSMSNotificationType.SYNC_FAILED.ordinal());
} catch (OCSyncException e) {
Log.e(ASyncSMSSync.TAG, _context.getString(e.getErrorId()));
OCSMSNotificationUI.notify(_context, _context.getString(R.string.fatal_error),

View File

@ -17,6 +17,8 @@ package fr.unix_experience.owncloud_sms.engine;
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
@ -42,10 +44,20 @@ import fr.unix_experience.owncloud_sms.prefs.OCSMSSharedPrefs;
@SuppressWarnings("deprecation")
public class OCSMSOwnCloudClient {
public OCSMSOwnCloudClient(Context context, Uri serverURI, String accountName, String accountPassword) {
public OCSMSOwnCloudClient(Context context, Account account) {
_context = context;
_serverAPIVersion = 1;
_http = new HTTPRequestBuilder(context, serverURI, accountName, accountPassword);
AccountManager accountManager = AccountManager.get(context);
String ocURI = accountManager.getUserData(account, "ocURI");
if (ocURI == null) {
throw new IllegalStateException(context.getString(R.string.err_sync_account_unparsable));
}
Uri serverURI = Uri.parse(ocURI);
_http = new HTTPRequestBuilder(context, serverURI,
accountManager.getUserData(account, "ocLogin"),
accountManager.getPassword(account));
_connectivityMonitor = new ConnectivityMonitor(_context);
}

View File

@ -18,12 +18,10 @@ package fr.unix_experience.owncloud_sms.sync_adapters;
*/
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.AbstractThreadedSyncAdapter;
import android.content.ContentProviderClient;
import android.content.Context;
import android.content.SyncResult;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
@ -34,40 +32,30 @@ import fr.unix_experience.owncloud_sms.enums.OCSyncErrorType;
import fr.unix_experience.owncloud_sms.exceptions.OCSyncException;
import fr.unix_experience.owncloud_sms.notifications.OCSMSNotificationUI;
public class SmsSyncAdapter extends AbstractThreadedSyncAdapter {
class SmsSyncAdapter extends AbstractThreadedSyncAdapter {
public SmsSyncAdapter(Context context, boolean autoInitialize) {
SmsSyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
_accountMgr = AccountManager.get(context);
}
@Override
public void onPerformSync(Account account, Bundle extras, String authority,
ContentProviderClient provider, SyncResult syncResult) {
// Create client
String ocURI = _accountMgr.getUserData(account, "ocURI");
if (ocURI == null) {
OCSMSNotificationUI.notify(getContext(), getContext().getString(R.string.fatal_error),
getContext().getString(R.string.err_sync_account_unparsable),
OCSMSNotificationType.SYNC_FAILED.ordinal());
return;
}
Uri serverURI = Uri.parse(ocURI);
OCSMSNotificationUI.notify(getContext(), getContext().getString(R.string.sync_title),
getContext().getString(R.string.sync_inprogress), OCSMSNotificationType.SYNC.ordinal());
OCSMSOwnCloudClient _client = new OCSMSOwnCloudClient(getContext(),
serverURI, _accountMgr.getUserData(account, "ocLogin"),
_accountMgr.getPassword(account));
try {
OCSMSOwnCloudClient _client = new OCSMSOwnCloudClient(getContext(), account);
// getServerAPI version
Log.i(SmsSyncAdapter.TAG, "Server API version: " + _client.getServerAPIVersion());
// and push datas
_client.doPushRequest(null);
OCSMSNotificationUI.cancel(getContext());
OCSMSNotificationUI.cancel(getContext());
} catch (IllegalStateException e) {
OCSMSNotificationUI.notify(getContext(), getContext().getString(R.string.fatal_error),
e.toString(), OCSMSNotificationType.SYNC_FAILED.ordinal());
} catch (OCSyncException e) {
OCSMSNotificationUI.cancel(getContext());
OCSMSNotificationUI.notify(getContext(), getContext().getString(R.string.fatal_error),
@ -87,7 +75,5 @@ public class SmsSyncAdapter extends AbstractThreadedSyncAdapter {
}
}
private final AccountManager _accountMgr;
private static final String TAG = SmsSyncAdapter.class.getSimpleName();
}