mirror of
				https://github.com/nerzhul/ownCloud-SMS-App.git
				synced 2025-10-31 02:17:53 +00:00 
			
		
		
		
	Better OCSMSOwncloudClient object creation
This commit is contained in:
		
							parent
							
								
									a3088e7718
								
							
						
					
					
						commit
						f76de90a6e
					
				| @ -5,7 +5,6 @@ import android.accounts.AccountManager; | |||||||
| import android.content.ContentResolver; | import android.content.ContentResolver; | ||||||
| import android.content.Context; | import android.content.Context; | ||||||
| import android.database.Cursor; | import android.database.Cursor; | ||||||
| import android.net.Uri; |  | ||||||
| import android.os.AsyncTask; | import android.os.AsyncTask; | ||||||
| import android.provider.ContactsContract; | import android.provider.ContactsContract; | ||||||
| import android.support.v4.widget.SwipeRefreshLayout; | import android.support.v4.widget.SwipeRefreshLayout; | ||||||
| @ -51,19 +50,14 @@ public interface ASyncContactLoad { | |||||||
| 		} | 		} | ||||||
| 		@Override | 		@Override | ||||||
| 		protected Boolean doInBackground(Void... params) { | 		protected Boolean doInBackground(Void... params) { | ||||||
| 			// Create client | 			OCSMSOwnCloudClient _client = null; | ||||||
| 			String ocURI = ContactLoadTask._accountMgr.getUserData(ContactLoadTask._account, "ocURI"); | 			try { | ||||||
| 			if (ocURI == null) { | 				_client = new OCSMSOwnCloudClient(_context, ContactLoadTask._account); | ||||||
| 				// @TODO: Handle the problem | 			} | ||||||
|  | 			catch (IllegalStateException e) { | ||||||
| 				return false; | 				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 | 			// Remove all objects, due to refreshing handling | ||||||
| 			_objects.clear(); | 			_objects.clear(); | ||||||
| 			try { | 			try { | ||||||
|  | |||||||
| @ -47,15 +47,13 @@ public interface ASyncSMSSync { | |||||||
| 
 | 
 | ||||||
| 			// Notify that we are syncing SMS | 			// Notify that we are syncing SMS | ||||||
| 			for (Account element : myAccountList) { | 			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 { | 				try { | ||||||
|  | 					OCSMSOwnCloudClient _client = new OCSMSOwnCloudClient(_context, element); | ||||||
| 					_client.doPushRequest(_smsList); | 					_client.doPushRequest(_smsList); | ||||||
| 					OCSMSNotificationUI.cancel(_context); | 					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) { | 				} catch (OCSyncException e) { | ||||||
| 					Log.e(ASyncSMSSync.TAG, _context.getString(e.getErrorId())); | 					Log.e(ASyncSMSSync.TAG, _context.getString(e.getErrorId())); | ||||||
|                     OCSMSNotificationUI.notify(_context, _context.getString(R.string.fatal_error), |                     OCSMSNotificationUI.notify(_context, _context.getString(R.string.fatal_error), | ||||||
|  | |||||||
| @ -17,6 +17,8 @@ package fr.unix_experience.owncloud_sms.engine; | |||||||
|  *  along with this program.  If not, see <http://www.gnu.org/licenses/>. |  *  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.content.Context; | ||||||
| import android.net.Uri; | import android.net.Uri; | ||||||
| import android.util.Log; | import android.util.Log; | ||||||
| @ -42,10 +44,20 @@ import fr.unix_experience.owncloud_sms.prefs.OCSMSSharedPrefs; | |||||||
| @SuppressWarnings("deprecation") | @SuppressWarnings("deprecation") | ||||||
| public class OCSMSOwnCloudClient { | public class OCSMSOwnCloudClient { | ||||||
| 
 | 
 | ||||||
| 	public OCSMSOwnCloudClient(Context context, Uri serverURI, String accountName, String accountPassword) { | 	public OCSMSOwnCloudClient(Context context, Account account) { | ||||||
| 		_context = context; | 		_context = context; | ||||||
| 		_serverAPIVersion = 1; | 		_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); |         _connectivityMonitor = new ConnectivityMonitor(_context); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -18,12 +18,10 @@ package fr.unix_experience.owncloud_sms.sync_adapters; | |||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| import android.accounts.Account; | import android.accounts.Account; | ||||||
| import android.accounts.AccountManager; |  | ||||||
| import android.content.AbstractThreadedSyncAdapter; | import android.content.AbstractThreadedSyncAdapter; | ||||||
| import android.content.ContentProviderClient; | import android.content.ContentProviderClient; | ||||||
| import android.content.Context; | import android.content.Context; | ||||||
| import android.content.SyncResult; | import android.content.SyncResult; | ||||||
| import android.net.Uri; |  | ||||||
| import android.os.Bundle; | import android.os.Bundle; | ||||||
| import android.util.Log; | 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.exceptions.OCSyncException; | ||||||
| import fr.unix_experience.owncloud_sms.notifications.OCSMSNotificationUI; | 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); | 		super(context, autoInitialize); | ||||||
| 		_accountMgr = AccountManager.get(context); |  | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	@Override | 	@Override | ||||||
| 	public void onPerformSync(Account account, Bundle extras, String authority, | 	public void onPerformSync(Account account, Bundle extras, String authority, | ||||||
|                               ContentProviderClient provider, SyncResult syncResult) { |                               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), |         OCSMSNotificationUI.notify(getContext(), getContext().getString(R.string.sync_title), | ||||||
|                 getContext().getString(R.string.sync_inprogress), OCSMSNotificationType.SYNC.ordinal()); |                 getContext().getString(R.string.sync_inprogress), OCSMSNotificationType.SYNC.ordinal()); | ||||||
| 
 | 
 | ||||||
| 		OCSMSOwnCloudClient _client = new OCSMSOwnCloudClient(getContext(), |  | ||||||
| 				serverURI, _accountMgr.getUserData(account, "ocLogin"), |  | ||||||
| 				_accountMgr.getPassword(account)); |  | ||||||
| 
 |  | ||||||
| 		try { | 		try { | ||||||
|  | 			OCSMSOwnCloudClient _client = new OCSMSOwnCloudClient(getContext(), account); | ||||||
|  | 
 | ||||||
| 			// getServerAPI version | 			// getServerAPI version | ||||||
| 			Log.i(SmsSyncAdapter.TAG, "Server API version: " + _client.getServerAPIVersion()); | 			Log.i(SmsSyncAdapter.TAG, "Server API version: " + _client.getServerAPIVersion()); | ||||||
| 
 | 
 | ||||||
| 			// and push datas | 			// and push datas | ||||||
| 			_client.doPushRequest(null); | 			_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) { | 		} catch (OCSyncException e) { | ||||||
|             OCSMSNotificationUI.cancel(getContext()); |             OCSMSNotificationUI.cancel(getContext()); | ||||||
|             OCSMSNotificationUI.notify(getContext(), getContext().getString(R.string.fatal_error), |             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(); | 	private static final String TAG = SmsSyncAdapter.class.getSimpleName(); | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user