diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsDetailActivity.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsDetailActivity.java index 1f7ed4ce..95b7514b 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsDetailActivity.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsDetailActivity.java @@ -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: diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderDetailFragment.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderDetailFragment.java index 99a61b62..2207bcd2 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderDetailFragment.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderDetailFragment.java @@ -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); } diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListActivity.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListActivity.java index bfd6def6..2a11f01c 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListActivity.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListActivity.java @@ -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); diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/PodcastFragmentActivity.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/PodcastFragmentActivity.java index c3ef1a90..cf061cd1 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/PodcastFragmentActivity.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/PodcastFragmentActivity.java @@ -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) { diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/adapter/NewsListRecyclerAdapter.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/adapter/NewsListRecyclerAdapter.java index a75641ca..7408164c 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/adapter/NewsListRecyclerAdapter.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/adapter/NewsListRecyclerAdapter.java @@ -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); } diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/di/ApiModule.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/di/ApiModule.java index 0339c45b..ea59b541 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/di/ApiModule.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/di/ApiModule.java @@ -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 diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/PostDelayHandler.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/PostDelayHandler.java index ec0933d4..4927596a 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/PostDelayHandler.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/helper/PostDelayHandler.java @@ -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); + } + } }