Fix issues with service getting destroyed

This commit is contained in:
David Development 2015-04-03 14:41:40 +02:00
parent 8895a06fe5
commit 942154ef64
2 changed files with 32 additions and 26 deletions

View file

@ -24,6 +24,7 @@ package de.luhmer.owncloudnewsreader;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
@ -40,7 +41,6 @@ import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@ -100,7 +100,7 @@ public class NewsReaderListFragment extends Fragment implements OnCreateContextM
Handler refresh = new Handler(Looper.getMainLooper());
refresh.post(new Runnable() {
public void run() {
UpdateSyncButtonLayout();
((NewsReaderListActivity)getActivity()).UpdateButtonLayout();;
}
});
}
@ -110,7 +110,7 @@ public class NewsReaderListFragment extends Fragment implements OnCreateContextM
Handler refresh = new Handler(Looper.getMainLooper());
refresh.post(new Runnable() {
public void run() {
UpdateSyncButtonLayout();
((NewsReaderListActivity) getActivity()).UpdateButtonLayout();
}
});
@ -237,15 +237,8 @@ public class NewsReaderListFragment extends Fragment implements OnCreateContextM
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setRetainInstance(true);
try
{
//SettingsActivity.CheckForUnsycedChangesInDatabase(context);
//dbConn.resetDatabase();
//dbConn.clearDatabaseOverSize();
//dbConn.resetRssItemsDatabase();
mListener = new BaseMessage.OnMessageClickListener()
{
@Override
@ -258,8 +251,6 @@ public class NewsReaderListFragment extends Fragment implements OnCreateContextM
{
//Toast.makeText(getActivity(), "button 1 pressed", 3000).show();
//TODO needs testing!
NewsReaderDetailFragment ndf = ((NewsReaderDetailFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.content_frame));
if(ndf != null) {
//ndf.reloadAdapterFromScratch();
@ -267,7 +258,6 @@ public class NewsReaderListFragment extends Fragment implements OnCreateContextM
}
}
};
}
catch(Exception ex)
{
@ -281,10 +271,23 @@ public class NewsReaderListFragment extends Fragment implements OnCreateContextM
public void onStart() {
Intent serviceIntent = new Intent(getActivity(), OwnCloudSyncService.class);
mConnection = generateServiceConnection();
if(!isMyServiceRunning(OwnCloudSyncService.class)) {
getActivity().startService(serviceIntent);
}
getActivity().bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
super.onStart();
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
@Override
public void onStop() {
if(_ownCloudSyncService != null) {
@ -341,7 +344,7 @@ public class NewsReaderListFragment extends Fragment implements OnCreateContextM
StartSync();
if(getActivity() instanceof NewsReaderListActivity)
((NewsReaderListActivity) getActivity()).UpdateButtonSyncLayout();
((NewsReaderListActivity) getActivity()).UpdateButtonLayout();
}
catch (Exception e) {
e.printStackTrace();
@ -388,7 +391,7 @@ public class NewsReaderListFragment extends Fragment implements OnCreateContextM
ContentResolver.requestSync(acc, AccountGeneral.ACCOUNT_TYPE, accBundle);
//http://stackoverflow.com/questions/5253858/why-does-contentresolver-requestsync-not-trigger-a-sync
} else {
UpdateSyncButtonLayout();
((NewsReaderListActivity) getActivity()).UpdateButtonLayout();
}
} catch (RemoteException e) {
e.printStackTrace();
@ -406,15 +409,12 @@ public class NewsReaderListFragment extends Fragment implements OnCreateContextM
else if(ex instanceof HttpResponseException)
{
HttpResponseException responseException = (HttpResponseException) ex;
//if(responseException.getStatusCode() == 401)
// ShowToastLong("Authentication failed");
//else
ShowToastLong(responseException.getLocalizedMessage());
}
else
ShowToastLong(ex.getLocalizedMessage());
UpdateSyncButtonLayout();
((NewsReaderListActivity) getActivity()).UpdateButtonLayout();
}
@ -541,10 +541,4 @@ public class NewsReaderListFragment extends Fragment implements OnCreateContextM
eListView.setChoiceMode(activateOnItemClick ? ListView.CHOICE_MODE_SINGLE : ListView.CHOICE_MODE_NONE);
}
public void UpdateSyncButtonLayout()
{
if(getActivity() != null)
((NewsReaderListActivity) getActivity()).UpdateButtonSyncLayout();
}
}

View file

@ -95,8 +95,18 @@ public class OwnCloudSyncService extends Service {
Log.d(TAG, "onCreate() called");
}
@Override
public boolean onUnbind(Intent intent) {
//Destroy service if no sync is running
if(!_Reader.isSyncRunning()) {
Log.v(TAG, "Stopping service because of inactivity");
stopSelf();
}
OnAsyncTaskCompletedListener onAsyncTask_GetVersionFinished = new OnAsyncTaskCompletedListener() {
return super.onUnbind(intent);
}
OnAsyncTaskCompletedListener onAsyncTask_GetVersionFinished = new OnAsyncTaskCompletedListener() {
@Override
public void onAsyncTaskCompleted(int task_id, Object task_result) {
@ -256,6 +266,8 @@ public class OwnCloudSyncService extends Service {
}
private void finishedSync(SYNC_TYPES sync_type) {
Log.v(TAG, "Finished Sync: " + sync_type.toString());
List<IOwnCloudSyncServiceCallback> callbackList = getCallBackItemsAndBeginBroadcast();
for(IOwnCloudSyncServiceCallback icb : callbackList) {
try {