|
2 | 2 |
|
3 | 3 | import android.app.Activity;
|
4 | 4 | import android.content.Intent;
|
5 |
| -import android.os.Build; |
6 | 5 | import android.os.Bundle;
|
7 | 6 | import android.os.Handler;
|
8 | 7 | import android.os.Looper;
|
|
13 | 12 | import androidx.activity.result.ActivityResultLauncher;
|
14 | 13 | import androidx.activity.result.IntentSenderRequest;
|
15 | 14 | import androidx.activity.result.contract.ActivityResultContracts;
|
16 |
| -import androidx.annotation.RequiresApi; |
| 15 | +import androidx.annotation.NonNull; |
17 | 16 | import androidx.appcompat.app.ActionBar;
|
18 | 17 | import androidx.appcompat.app.AppCompatActivity;
|
19 | 18 | import androidx.core.content.pm.ShortcutInfoCompat;
|
20 | 19 | import androidx.core.content.pm.ShortcutManagerCompat;
|
21 | 20 | import androidx.core.graphics.drawable.IconCompat;
|
22 | 21 | import androidx.core.splashscreen.SplashScreen;
|
| 22 | +import androidx.lifecycle.DefaultLifecycleObserver; |
| 23 | +import androidx.lifecycle.LifecycleOwner; |
23 | 24 | import androidx.lifecycle.ViewModelProvider;
|
24 | 25 | import androidx.navigation.NavController;
|
25 | 26 | import androidx.navigation.NavGraph;
|
|
31 | 32 | import com.d4rk.androidtutorials.java.R;
|
32 | 33 | import com.d4rk.androidtutorials.java.databinding.ActivityMainBinding;
|
33 | 34 | import com.d4rk.androidtutorials.java.notifications.managers.AppUpdateNotificationsManager;
|
34 |
| -import com.d4rk.androidtutorials.java.notifications.managers.AppUsageNotificationsManager; |
35 |
| -import com.d4rk.androidtutorials.java.notifications.managers.QuizReminderManager; |
36 | 35 | import com.d4rk.androidtutorials.java.ui.components.navigation.BottomSheetMenuFragment;
|
37 | 36 | import com.d4rk.androidtutorials.java.ui.screens.startup.StartupActivity;
|
38 | 37 | import com.d4rk.androidtutorials.java.ui.screens.startup.StartupViewModel;
|
@@ -77,6 +76,23 @@ public class MainActivity extends AppCompatActivity {
|
77 | 76 | private AppUpdateNotificationsManager appUpdateNotificationsManager;
|
78 | 77 | private AppUpdateManager appUpdateManager;
|
79 | 78 | private InstallStateUpdatedListener installStateUpdatedListener;
|
| 79 | + private final DefaultLifecycleObserver lifecycleObserver = new DefaultLifecycleObserver() { |
| 80 | + @Override |
| 81 | + public void onResume(@NonNull LifecycleOwner owner) { |
| 82 | + ConsentUtils.applyStoredConsent(MainActivity.this); |
| 83 | + if (mBinding.adView != null) { |
| 84 | + if (ConsentUtils.canShowAds(MainActivity.this)) { |
| 85 | + if (mBinding.adView.getVisibility() != View.VISIBLE) { |
| 86 | + MobileAds.initialize(MainActivity.this); |
| 87 | + mBinding.adView.setVisibility(View.VISIBLE); |
| 88 | + mBinding.adView.loadAd(new AdRequest.Builder().build()); |
| 89 | + } |
| 90 | + } else { |
| 91 | + mBinding.adView.setVisibility(View.GONE); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + }; |
80 | 96 |
|
81 | 97 | @Override
|
82 | 98 | protected void onCreate(Bundle savedInstanceState) {
|
@@ -122,6 +138,7 @@ protected void onCreate(Bundle savedInstanceState) {
|
122 | 138 | this.appUpdateManager = mainViewModel.getAppUpdateManager();
|
123 | 139 |
|
124 | 140 | registerInstallStateListener();
|
| 141 | + getLifecycle().addObserver(lifecycleObserver); |
125 | 142 | getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
|
126 | 143 | @Override
|
127 | 144 | public void handleOnBackPressed() {
|
@@ -240,30 +257,6 @@ public boolean onOptionsItemSelected(android.view.MenuItem item) {
|
240 | 257 | return super.onOptionsItemSelected(item);
|
241 | 258 | }
|
242 | 259 |
|
243 |
| - @RequiresApi(api = Build.VERSION_CODES.O) |
244 |
| - @Override |
245 |
| - protected void onResume() { |
246 |
| - super.onResume(); |
247 |
| - ConsentUtils.applyStoredConsent(this); |
248 |
| - if (mBinding.adView != null) { |
249 |
| - if (ConsentUtils.canShowAds(this)) { |
250 |
| - if (mBinding.adView.getVisibility() != View.VISIBLE) { |
251 |
| - MobileAds.initialize(this); |
252 |
| - mBinding.adView.setVisibility(View.VISIBLE); |
253 |
| - mBinding.adView.loadAd(new AdRequest.Builder().build()); |
254 |
| - } |
255 |
| - } else { |
256 |
| - mBinding.adView.setVisibility(View.GONE); |
257 |
| - } |
258 |
| - } |
259 |
| - AppUsageNotificationsManager appUsageNotificationsManager = new AppUsageNotificationsManager(this); |
260 |
| - appUsageNotificationsManager.scheduleAppUsageCheck(); |
261 |
| - QuizReminderManager quizReminderManager = new QuizReminderManager(this); |
262 |
| - quizReminderManager.scheduleDailyReminder(); |
263 |
| - appUpdateNotificationsManager.checkAndSendUpdateNotification(); |
264 |
| - checkForImmediateUpdate(); |
265 |
| - } |
266 |
| - |
267 | 260 | private void checkForImmediateUpdate() {
|
268 | 261 | appUpdateManager
|
269 | 262 | .getAppUpdateInfo()
|
|
0 commit comments