Launch item state sync when switching from this app to another - fix #717

This commit is contained in:
David Luhmer 2019-01-27 16:05:22 +01:00
parent e8f9891028
commit c1d83a05e3
7 changed files with 75 additions and 42 deletions

View file

@ -83,8 +83,6 @@ public class NewsDetailActivity extends PodcastFragmentActivity {
private ViewPager mViewPager;
private int currentPosition;
private PostDelayHandler pDelayHandler;
private MenuItem menuItem_PlayPodcast;
private MenuItem menuItem_Starred;
private MenuItem menuItem_Read;
@ -105,8 +103,6 @@ public class NewsDetailActivity extends PodcastFragmentActivity {
setSupportActionBar(toolbar);
}
pDelayHandler = new PostDelayHandler(this);
dbConn = new DatabaseConnectionOrm(this);
Intent intent = getIntent();
@ -258,7 +254,7 @@ public class NewsDetailActivity extends PodcastFragmentActivity {
{
markItemAsReadUnread(rssItems.get(position), true);
pDelayHandler.DelayTimer();
mPostDelayHandler.delayTimer();
Log.v("PAGE CHANGED", "PAGE: " + position + " - IDFEED: " + rssItems.get(position).getId());
}
@ -375,7 +371,7 @@ public class NewsDetailActivity extends PodcastFragmentActivity {
case R.id.action_read:
markItemAsReadUnread(rssItem, !menuItem_Read.isChecked());
UpdateActionBarIcons();
pDelayHandler.DelayTimer();
mPostDelayHandler.delayTimer();
break;
case R.id.action_starred:
@ -385,7 +381,7 @@ public class NewsDetailActivity extends PodcastFragmentActivity {
UpdateActionBarIcons();
pDelayHandler.DelayTimer();
mPostDelayHandler.delayTimer();
break;
case R.id.action_openInBrowser:

View file

@ -321,7 +321,7 @@ public class NewsReaderDetailFragment extends Fragment {
try {
NewsListRecyclerAdapter nra = ((NewsListRecyclerAdapter) recyclerView.getAdapter());
if (nra == null) {
nra = new NewsListRecyclerAdapter(getActivity(), recyclerView, (PodcastFragmentActivity) getActivity());
nra = new NewsListRecyclerAdapter(getActivity(), recyclerView, (PodcastFragmentActivity) getActivity(), ((PodcastFragmentActivity) getActivity()).mPostDelayHandler);
recyclerView.setAdapter(nra);
}

View file

@ -355,7 +355,7 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements
savedInstanceState.containsKey(OPTIONAL_FOLDER_ID)) {
NewsListRecyclerAdapter adapter = new NewsListRecyclerAdapter(this, getNewsReaderDetailFragment().recyclerView, this);
NewsListRecyclerAdapter adapter = new NewsListRecyclerAdapter(this, getNewsReaderDetailFragment().recyclerView, this, mPostDelayHandler);
adapter.setTotalItemCount(savedInstanceState.getInt(LIST_ADAPTER_TOTAL_COUNT));
adapter.setCachedPages(savedInstanceState.getInt(LIST_ADAPTER_PAGE_COUNT));
@ -633,7 +633,7 @@ public class NewsReaderListActivity extends PodcastFragmentActivity implements
StartLoginFragment(this);
} else {
if (!OwnCloudSyncService.isSyncRunning()) {
new PostDelayHandler(this).stopRunningPostDelayHandler();//Stop pending sync handler
mPostDelayHandler.stopRunningPostDelayHandler(); //Stop pending sync handler
Bundle accBundle = new Bundle();
accBundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);

View file

@ -51,6 +51,7 @@ import de.luhmer.owncloudnewsreader.events.podcast.RegisterVideoOutput;
import de.luhmer.owncloudnewsreader.events.podcast.RegisterYoutubeOutput;
import de.luhmer.owncloudnewsreader.events.podcast.UpdatePodcastStatusEvent;
import de.luhmer.owncloudnewsreader.events.podcast.VideoDoubleClicked;
import de.luhmer.owncloudnewsreader.helper.PostDelayHandler;
import de.luhmer.owncloudnewsreader.helper.SizeAnimator;
import de.luhmer.owncloudnewsreader.helper.ThemeChooser;
import de.luhmer.owncloudnewsreader.interfaces.IPlayPausePodcastClicked;
@ -77,6 +78,7 @@ public class PodcastFragmentActivity extends AppCompatActivity implements IPlayP
@Inject protected SharedPreferences mPrefs;
@Inject protected ApiProvider mApi;
@Inject protected MemorizingTrustManager mMTM;
@Inject protected PostDelayHandler mPostDelayHandler;
private MediaBrowserCompat mMediaBrowser;
private EventBus eventBus;
@ -124,6 +126,8 @@ public class PodcastFragmentActivity extends AppCompatActivity implements IPlayP
e.printStackTrace();
}
}*/
//mPostDelayHandler.delayTimer();
mPostDelayHandler.stopRunningPostDelayHandler();
}
@Override
@ -179,6 +183,13 @@ public class PodcastFragmentActivity extends AppCompatActivity implements IPlayP
mMediaBrowser.disconnect();
}
@Override
public void onUserLeaveHint() {
super.onUserLeaveHint();
mPostDelayHandler.delayOnExitTimer();
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
if (hasWindowFocus) {

View file

@ -70,11 +70,11 @@ public class NewsListRecyclerAdapter extends RecyclerView.Adapter {
// before loading more.
private int visibleThreshold = 5;
public NewsListRecyclerAdapter(FragmentActivity activity, RecyclerView recyclerView, IPlayPausePodcastClicked playPausePodcastClicked) {
public NewsListRecyclerAdapter(FragmentActivity activity, RecyclerView recyclerView, IPlayPausePodcastClicked playPausePodcastClicked, PostDelayHandler postDelayHandler) {
this.activity = activity;
this.playPausePodcastClicked = playPausePodcastClicked;
pDelayHandler = new PostDelayHandler(activity);
pDelayHandler = postDelayHandler;
dbConn = new DatabaseConnectionOrm(activity);
setHasStableIds(true);
@ -246,7 +246,7 @@ public class NewsListRecyclerAdapter extends RecyclerView.Adapter {
rssItem.setRead_temp(isChecked);
dbConn.updateRssItem(rssItem);
pDelayHandler.DelayTimer();
pDelayHandler.delayTimer();
viewHolder.setReadState(isChecked);
//notifyItemChanged(viewHolder.getAdapterPosition());
@ -269,7 +269,7 @@ public class NewsListRecyclerAdapter extends RecyclerView.Adapter {
changeReadStateOfItem(viewHolder, true);
} else {
dbConn.updateRssItem(rssItem);
pDelayHandler.DelayTimer();
pDelayHandler.delayTimer();
}
viewHolder.setStarred(isStarred);
}

View file

@ -13,6 +13,7 @@ import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import de.luhmer.owncloudnewsreader.helper.PostDelayHandler;
import de.luhmer.owncloudnewsreader.ssl.MemorizingTrustManager;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
@ -71,6 +72,12 @@ public class ApiModule {
return client;
}
@Provides
@Singleton
PostDelayHandler providePostDelayHandler() {
return new PostDelayHandler(mApplication);
}
/*
@Provides

View file

@ -29,36 +29,55 @@ import android.util.Log;
import de.luhmer.owncloudnewsreader.services.SyncItemStateService;
public class PostDelayHandler {
private static final String TAG = "PostDelayHandler";
private static Handler handlerTimer;
private final int delayTime = 5 * 60000;//60 000 = 1min
private Context context;
private static boolean isDelayed = false;
public PostDelayHandler(Context context) {
if(handlerTimer == null)
handlerTimer = new Handler();
this.context = context;
}
public void stopRunningPostDelayHandler() {
handlerTimer.removeCallbacksAndMessages(null);
}
public void DelayTimer() {
if(!isDelayed) {
isDelayed = true;
handlerTimer.postDelayed(new Runnable(){
public void run() {
isDelayed = false;
Log.v(TAG, "Time exceeded.. Sync state of changed items");
if((!SyncItemStateService.isMyServiceRunning(context)) && NetworkConnection.isNetworkAvailable(context))
{
private Context context;
private static boolean isDelayed = false;
public PostDelayHandler(Context context) {
this.context = context;
if(handlerTimer == null) {
handlerTimer = new Handler();
}
}
public void stopRunningPostDelayHandler() {
Log.v(TAG, "stopRunningPostDelayHandler() called");
handlerTimer.removeCallbacksAndMessages(null);
isDelayed = false;
}
public void delayTimer() {
// Time to wait until a sync is triggered (after last change in the app)
//60 000 = 1min
delay(5 * 60000);
//delay(10000); // 10 seconds
}
public void delayOnExitTimer() {
stopRunningPostDelayHandler();
// Time to wait until a sync is triggered when the user switches activities / exists the app
//delay(10000); // 10 seconds
delay(5000); // 5 seconds
}
private void delay(final int time) {
Log.v(TAG, "delay() called with: time = [" + time + "]");
if(!isDelayed) {
isDelayed = true;
handlerTimer.postDelayed(new Runnable(){
public void run() {
isDelayed = false;
Log.v(TAG, "Time exceeded.. Sync state of changed items. Delay was: " + time);
if((!SyncItemStateService.isMyServiceRunning(context)) && NetworkConnection.isNetworkAvailable(context))
{
Log.v(TAG, "Starting SyncItemStateService");
SyncItemStateService.enqueueWork(context, new Intent());
}
}}, delayTime);
}
}
SyncItemStateService.enqueueWork(context, new Intent());
}
}}, time);
}
}
}