Convert OwnCloud_Reader to a singleton

This commit is contained in:
Daniel Schaal 2015-09-29 06:43:54 +02:00
parent eb45690702
commit 2c887ff4fb
4 changed files with 27 additions and 31 deletions

View file

@ -129,7 +129,6 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements
private static MenuItem menuItemUpdater;
private static MenuItem menuItemDownloadMoreItems;
private static OwnCloud_Reader _Reader;
//private Date mLastSyncDate = new Date(0);
private boolean mSyncOnStartupPerformed = false;
@ -815,8 +814,7 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements
String password = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("edt_password", "");
if(username != null) {
_Reader = new OwnCloud_Reader();
_Reader.Start_AsyncTask_GetVersion(this, onAsyncTaskGetVersionFinished);
OwnCloud_Reader.getInstance().getInstance().Start_AsyncTask_GetVersion(this, onAsyncTaskGetVersionFinished);
Toast.makeText(this, getString(R.string.toast_GettingMoreItems), Toast.LENGTH_SHORT).show();
}
@ -826,16 +824,14 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements
@Override
public void onAsyncTaskCompleted(int task_id, Object task_result) {
if(_Reader != null) {
String appVersion = task_result.toString();
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(NewsReaderListActivity.this);
String baseUrl = mPrefs.getString(SettingsActivity.EDT_OWNCLOUDROOTPATH_STRING, "");
API api = API.GetRightApiForVersion(appVersion, baseUrl);
_Reader.setApi(api);
OwnCloud_Reader.getInstance().setApi(api);
NewsReaderDetailFragment ndf = getNewsReaderDetailFragment();
_Reader.Start_AsyncTask_GetOldItems(NewsReaderListActivity.this, onAsyncTaskComplete, ndf.getIdFeed(), ndf.getIdFolder());
}
OwnCloud_Reader.getInstance().Start_AsyncTask_GetOldItems(NewsReaderListActivity.this, onAsyncTaskComplete, ndf.getIdFeed(), ndf.getIdFolder());
}
};

View file

@ -23,14 +23,22 @@ package de.luhmer.owncloudnewsreader.reader.owncloud;
import android.content.Context;
import de.luhmer.owncloudnewsreader.reader.AsyncTask_Reader;
import de.luhmer.owncloudnewsreader.reader.FeedItemTags;
import de.luhmer.owncloudnewsreader.reader.OnAsyncTaskCompletedListener;
public class OwnCloud_Reader {
private static OwnCloud_Reader instance;
public static OwnCloud_Reader getInstance() {
if(instance == null)
instance = new OwnCloud_Reader();
return instance;
}
boolean isSyncRunning = false;
private API api = null;
public OwnCloud_Reader() {
private OwnCloud_Reader() {
}
public void Start_AsyncTask_GetItems(Context context, OnAsyncTaskCompletedListener listener, FeedItemTags tag) {

View file

@ -72,11 +72,11 @@ public class OwnCloudSyncService extends Service {
public void startSync() throws RemoteException {
if(!isSyncRunning()) {
// Only check for API version once
if(_Reader.getApi() == null) {
_Reader.Start_AsyncTask_GetVersion(OwnCloudSyncService.this, onAsyncTask_GetVersionFinished);
if(OwnCloud_Reader.getInstance().getApi() == null) {
OwnCloud_Reader.getInstance().Start_AsyncTask_GetVersion(OwnCloudSyncService.this, onAsyncTask_GetVersionFinished);
startedSync(SYNC_TYPES.SYNC_TYPE__GET_API);
} else {
_Reader.Start_AsyncTask_PerformItemStateChange(OwnCloudSyncService.this, onAsyncTask_PerformTagExecute);
OwnCloud_Reader.getInstance().Start_AsyncTask_PerformItemStateChange(OwnCloudSyncService.this, onAsyncTask_PerformTagExecute);
startedSync(SYNC_TYPES.SYNC_TYPE__ITEM_STATES);
}
}
@ -84,25 +84,20 @@ public class OwnCloudSyncService extends Service {
@Override
public boolean isSyncRunning() throws RemoteException {
return _Reader.isSyncRunning();
return OwnCloud_Reader.getInstance().isSyncRunning();
}
};
static OwnCloud_Reader _Reader;
@Override
public void onCreate() {
super.onCreate();
if(_Reader == null)
_Reader = new OwnCloud_Reader();
Log.d(TAG, "onCreate() called");
}
@Override
public boolean onUnbind(Intent intent) {
//Destroy service if no sync is running
if(!_Reader.isSyncRunning()) {
if(!OwnCloud_Reader.getInstance().isSyncRunning()) {
Log.v(TAG, "Stopping service because of inactivity");
stopSelf();
}
@ -123,9 +118,9 @@ public class OwnCloudSyncService extends Service {
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(OwnCloudSyncService.this);
String baseUrl = mPrefs.getString(SettingsActivity.EDT_OWNCLOUDROOTPATH_STRING, "");
API api = API.GetRightApiForVersion(appVersion, baseUrl);
_Reader.setApi(api);
_Reader.Start_AsyncTask_PerformItemStateChange(OwnCloudSyncService.this, onAsyncTask_PerformTagExecute);
OwnCloud_Reader.getInstance().setApi(api);
OwnCloud_Reader.getInstance().Start_AsyncTask_PerformItemStateChange(OwnCloudSyncService.this, onAsyncTask_PerformTagExecute);
startedSync(SYNC_TYPES.SYNC_TYPE__ITEM_STATES);
}
@ -146,13 +141,13 @@ public class OwnCloudSyncService extends Service {
if((Boolean) task_result)
{
if(task_id == Constants.TaskID_PerformStateChange) {
_Reader.Start_AsyncTask_GetFolder(OwnCloudSyncService.this, onAsyncTask_GetFolder);
OwnCloud_Reader.getInstance().Start_AsyncTask_GetFolder(OwnCloudSyncService.this, onAsyncTask_GetFolder);
startedSync(SYNC_TYPES.SYNC_TYPE__FOLDER);
}
else
_Reader.setSyncRunning(true);
OwnCloud_Reader.getInstance().setSyncRunning(true);
}
}
}
@ -168,7 +163,7 @@ public class OwnCloudSyncService extends Service {
if(task_result != null)
ThrowException((Exception) task_result);
else {
_Reader.Start_AsyncTask_GetFeeds(OwnCloudSyncService.this, onAsyncTask_GetFeed);
OwnCloud_Reader.getInstance().Start_AsyncTask_GetFeeds(OwnCloudSyncService.this, onAsyncTask_GetFeed);
startedSync(SYNC_TYPES.SYNC_TYPE__FEEDS);
}
@ -188,7 +183,7 @@ public class OwnCloudSyncService extends Service {
if(task_result != null)
ThrowException((Exception) task_result);
else {
_Reader.Start_AsyncTask_GetItems(OwnCloudSyncService.this, onAsyncTask_GetItems, FeedItemTags.ALL);//Recieve all unread Items
OwnCloud_Reader.getInstance().Start_AsyncTask_GetItems(OwnCloudSyncService.this, onAsyncTask_GetItems, FeedItemTags.ALL);//Recieve all unread Items
startedSync(SYNC_TYPES.SYNC_TYPE__ITEMS);
}

View file

@ -36,9 +36,6 @@ import de.luhmer.owncloudnewsreader.reader.owncloud.API;
import de.luhmer.owncloudnewsreader.reader.owncloud.OwnCloud_Reader;
public class SyncItemStateService extends IntentService {
OwnCloud_Reader _Reader = new OwnCloud_Reader();
public SyncItemStateService() {
super(null);
}
@ -49,7 +46,7 @@ public class SyncItemStateService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
_Reader.Start_AsyncTask_GetVersion(this, onAsyncTask_GetVersionFinished);
OwnCloud_Reader.getInstance().Start_AsyncTask_GetVersion(this, onAsyncTask_GetVersionFinished);
}
OnAsyncTaskCompletedListener onAsyncTask_GetVersionFinished = new OnAsyncTaskCompletedListener() {
@ -64,9 +61,9 @@ public class SyncItemStateService extends IntentService {
String baseUrl = mPrefs.getString(SettingsActivity.EDT_OWNCLOUDROOTPATH_STRING, "");
API api = API.GetRightApiForVersion(appVersion, baseUrl);
_Reader.setApi(api);
OwnCloud_Reader.getInstance().setApi(api);
_Reader.Start_AsyncTask_PerformItemStateChange(SyncItemStateService.this, null);
OwnCloud_Reader.getInstance().Start_AsyncTask_PerformItemStateChange(SyncItemStateService.this, null);
}
}
};