|
30 | 30 | import androidx.navigation.ui.NavigationUI; |
31 | 31 | import androidx.preference.PreferenceManager; |
32 | 32 |
|
| 33 | +import com.d4rk.androidtutorials.java.BuildConfig; |
33 | 34 | import com.d4rk.androidtutorials.java.R; |
34 | 35 | import com.d4rk.androidtutorials.java.databinding.ActivityMainBinding; |
| 36 | +import com.d4rk.androidtutorials.java.notifications.managers.AppUpdateNotificationsManager; |
| 37 | +import com.d4rk.androidtutorials.java.notifications.managers.AppUsageNotificationsManager; |
35 | 38 | import com.d4rk.androidtutorials.java.ui.components.navigation.BottomSheetMenuFragment; |
36 | 39 | import com.d4rk.androidtutorials.java.ui.screens.startup.StartupActivity; |
37 | 40 | import com.d4rk.androidtutorials.java.ui.screens.startup.StartupViewModel; |
|
48 | 51 | import com.google.android.play.core.install.InstallStateUpdatedListener; |
49 | 52 | import com.google.android.play.core.install.model.AppUpdateType; |
50 | 53 | import com.google.android.play.core.install.model.InstallStatus; |
| 54 | +import com.google.android.play.core.install.model.UpdateAvailability; |
51 | 55 | import com.google.android.ump.ConsentInformation; |
52 | 56 | import com.google.android.ump.ConsentRequestParameters; |
53 | 57 | import com.google.android.ump.UserMessagingPlatform; |
@@ -92,6 +96,7 @@ public void onResume(@NonNull LifecycleOwner owner) { |
92 | 96 | private NavController navController; |
93 | 97 | private int currentNavIndex; |
94 | 98 | private AppUpdateManager appUpdateManager; |
| 99 | + private AppUpdateNotificationsManager appUpdateNotificationsManager; |
95 | 100 | private InstallStateUpdatedListener installStateUpdatedListener; |
96 | 101 | private long backPressedTime; |
97 | 102 |
|
@@ -134,6 +139,7 @@ protected void onCreate(Bundle savedInstanceState) { |
134 | 139 | } |
135 | 140 |
|
136 | 141 | this.appUpdateManager = mainViewModel.getAppUpdateManager(); |
| 142 | + setupUpdateNotifications(); |
137 | 143 |
|
138 | 144 | registerInstallStateListener(); |
139 | 145 | getLifecycle().addObserver(lifecycleObserver); |
@@ -238,17 +244,19 @@ private void observeViewModel() { |
238 | 244 | .build(); |
239 | 245 |
|
240 | 246 | if (useRail) { |
241 | | - NavigationUI.setupWithNavController(mBinding.navRail, navController); |
242 | | - mBinding.navRail.setOnItemSelectedListener(item -> { |
243 | | - if (item.getItemId() == navController.getCurrentDestination().getId()) { |
| 247 | + if (mBinding.navRail != null) { |
| 248 | + NavigationUI.setupWithNavController(mBinding.navRail, navController); |
| 249 | + mBinding.navRail.setOnItemSelectedListener(item -> { |
| 250 | + if (item.getItemId() == navController.getCurrentDestination().getId()) { |
| 251 | + return true; |
| 252 | + } |
| 253 | + int newIndex = navOrder.get(item.getItemId()); |
| 254 | + NavOptions options = newIndex > currentNavIndex ? forwardOptions : backwardOptions; |
| 255 | + navController.navigate(item.getItemId(), null, options); |
| 256 | + currentNavIndex = newIndex; |
244 | 257 | return true; |
245 | | - } |
246 | | - int newIndex = navOrder.get(item.getItemId()); |
247 | | - NavOptions options = newIndex > currentNavIndex ? forwardOptions : backwardOptions; |
248 | | - navController.navigate(item.getItemId(), null, options); |
249 | | - currentNavIndex = newIndex; |
250 | | - return true; |
251 | | - }); |
| 258 | + }); |
| 259 | + } |
252 | 260 | } else { |
253 | 261 | NavigationUI.setupWithNavController(navBarView, navController); |
254 | 262 | navBarView.setOnItemSelectedListener(item -> { |
@@ -299,12 +307,47 @@ public boolean onOptionsItemSelected(android.view.MenuItem item) { |
299 | 307 | return super.onOptionsItemSelected(item); |
300 | 308 | } |
301 | 309 |
|
| 310 | + @Override |
| 311 | + protected void onResume() { |
| 312 | + super.onResume(); |
| 313 | + AppUsageNotificationsManager appUsageNotificationsManager = new AppUsageNotificationsManager(this); |
| 314 | + appUsageNotificationsManager.scheduleAppUsageCheck(); |
| 315 | + appUpdateNotificationsManager.checkAndSendUpdateNotification(); |
| 316 | + checkForFlexibleOrImmediateUpdate(); |
| 317 | + } |
| 318 | + |
| 319 | + private void checkForFlexibleOrImmediateUpdate() { |
| 320 | + appUpdateManager.getAppUpdateInfo().addOnSuccessListener(appUpdateInfo -> { |
| 321 | + boolean updateAvailable = appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE; |
| 322 | + if (updateAvailable) { |
| 323 | + startImmediateUpdate(appUpdateInfo); |
| 324 | + } |
| 325 | + }) |
| 326 | + .addOnFailureListener(e -> { |
| 327 | + if (!BuildConfig.DEBUG) { |
| 328 | + Snackbar.make( |
| 329 | + findViewById(android.R.id.content), |
| 330 | + getString(R.string.snack_general_error), |
| 331 | + Snackbar.LENGTH_LONG |
| 332 | + ).show(); |
| 333 | + } |
| 334 | + }); |
| 335 | + } |
| 336 | + |
302 | 337 | private void startImmediateUpdate(AppUpdateInfo appUpdateInfo) { |
303 | | - appUpdateManager.startUpdateFlowForResult( |
304 | | - appUpdateInfo, |
305 | | - updateActivityResultLauncher, |
306 | | - AppUpdateOptions.newBuilder(AppUpdateType.IMMEDIATE).build() |
307 | | - ); |
| 338 | + try { |
| 339 | + appUpdateManager.startUpdateFlowForResult( |
| 340 | + appUpdateInfo, |
| 341 | + updateActivityResultLauncher, |
| 342 | + AppUpdateOptions.newBuilder(AppUpdateType.IMMEDIATE).build() |
| 343 | + ); |
| 344 | + } catch (Exception e) { |
| 345 | + Log.e("MainActivity", "Error starting in-app update", e); |
| 346 | + } |
| 347 | + } |
| 348 | + |
| 349 | + private void setupUpdateNotifications() { |
| 350 | + appUpdateNotificationsManager = new AppUpdateNotificationsManager(this); |
308 | 351 | } |
309 | 352 |
|
310 | 353 | private void registerInstallStateListener() { |
|
0 commit comments