Last active
January 11, 2022 17:10
-
-
Save aminPial/fe91caa587a9c2d304fa6b6561491458 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static final int HIGH_PRIORITY_UPDATE = 5; | |
private static final int IMMEDIATE_UPDATE_CODE = 10; | |
//private static final int FLEXIBLE_UPDATE_CODE = 20; | |
AppUpdateManager appUpdateManager; | |
@Override | |
public final void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
airLocation.onActivityResult(requestCode, resultCode, data); | |
// check for update request result | |
if (requestCode == HIGH_PRIORITY_UPDATE) { | |
if (resultCode == RESULT_OK) | |
Snackbar.make(findViewById(android.R.id.content), | |
"Successfully updated the app", Snackbar.LENGTH_LONG).show(); | |
else if (resultCode == RESULT_CANCELED) { | |
//Toast.makeText(this, "Please update the app to use", Toast.LENGTH_SHORT).show(); | |
AlertDialog.Builder builder = new MaterialAlertDialogBuilder(MainActivity.this, | |
R.style.AlertDialogTheme); | |
builder.setCancelable(false); | |
builder. | |
setTitle("Update required") | |
.setMessage("Please Update the app to use it. We have improved the app.") | |
.setPositiveButton("update", ((dialogInterface, i) -> { | |
checkForUpdate(); | |
})) | |
.setNegativeButton("cancel & exit", ((dialogInterface, i) -> { | |
Process.killProcess(Process.myTid()); | |
System.exit(1); | |
})); | |
Dialog dialog = builder.create(); | |
dialog.setCanceledOnTouchOutside(false); | |
dialog.show(); | |
} else { | |
// update failed or something wrong happened, try again to update | |
checkForUpdate(); | |
} | |
} | |
// This code is for flexible update only | |
// if(requestCode == FLEXIBLE_UPDATE_CODE) { | |
// if(resultCode == RESULT_OK) | |
// Toast.makeText(this, R.string.update_downloading, Toast.LENGTH_SHORT).show(); | |
// else | |
// Toast.makeText(this, R.string.update_to_use_latest_feature, Toast.LENGTH_SHORT).show(); | |
// } | |
} | |
private void checkForUpdate() { | |
// Creates instance of the manager. | |
appUpdateManager = AppUpdateManagerFactory.create(getBaseContext()); | |
// Returns an intent object that you use to check for an update. | |
Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo(); | |
// Checks whether the platform allows the specified type of update, | |
// only check | |
appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> { | |
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) { | |
if (appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) { | |
// Request an immediate update. | |
try { | |
appUpdateManager.startUpdateFlowForResult( | |
// Pass the intent that is returned by 'getAppUpdateInfo()'. | |
appUpdateInfo, | |
// Or 'AppUpdateType.FLEXIBLE' for flexible updates. | |
AppUpdateType.IMMEDIATE, | |
// The current activity making the update request. | |
this, | |
// Include a request code to later monitor this update request. | |
HIGH_PRIORITY_UPDATE); | |
} catch (IntentSender.SendIntentException e) { | |
e.printStackTrace(); | |
} | |
} else { | |
// tell the user to update it on play store directly. | |
AlertDialog.Builder builder = new MaterialAlertDialogBuilder(MainActivity.this, | |
R.style.AlertDialogTheme); | |
builder.setCancelable(false); | |
builder | |
.setTitle("Update required") | |
.setMessage("Please Update the app to use it. We have improved the app.") | |
.setPositiveButton("Ok", ((dialogInterface, i) -> { | |
Process.killProcess(Process.myPid()); | |
System.exit(1); | |
})); | |
Dialog dialog = builder.create(); | |
dialog.setCanceledOnTouchOutside(false); | |
dialog.show(); | |
} | |
} | |
}); | |
} | |
// internet connection receiver and handler | |
@NonNull | |
private final BroadcastReceiver networkChangeReceiver = new BroadcastReceiver() { | |
@Override | |
public void onReceive(@NonNull Context context, Intent intent) { | |
if (!is_connected() && dialogInterface == null) { | |
Log.e("MainActivity", "dialogInterface Called"); | |
// was_disconnected_from_internet = true; | |
try { | |
checkForUpdate(); | |
dialogInterface.dismiss(); | |
dialogInterface.cancel(); | |
} catch (Exception e) { | |
Log.e("MainActivity", e.toString()); | |
} | |
new MaterialAlertDialogBuilder(MainActivity.this, R.style.AlertDialogTheme) | |
.setTitle("No Internet Connection") | |
.setMessage("Oops! Please have an active WIFI or cellular internet connection to continue.") | |
.setPositiveButton("Ok. I understand", | |
(dialogInterface_, i) -> { | |
dialogInterface = dialogInterface_; | |
dialogInterface_.dismiss(); | |
// Log.e("dismiss", "destroying dialog......."); | |
}).show(); | |
} else { | |
checkForUpdate(); | |
// Log.e("MainActivity", "Connected. No Issues"); | |
// Log.e("MainActivity", "was disconnected from internet: " + was_disconnected_from_internet); | |
// putMarkersOnGoogleMap(); | |
try { | |
sendBroadcast(new Intent("KEY").putExtra("type", "internet_conn")); | |
if (dialogInterface != null) { | |
dialogInterface.dismiss(); | |
dialogInterface.cancel(); | |
} | |
} catch (Exception e) { | |
Log.e("MainActivity", e.toString()); | |
} | |
} | |
} | |
}; | |
boolean is_connected() { | |
boolean haveConnectedWifi = false; | |
boolean haveConnectedMobile = false; | |
// add a broadcaster with | |
// https://stackoverflow.com/questions/15698790/broadcast-receiver-for-checking-internet-connection-in-android-app | |
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo[] netInfo = cm.getAllNetworkInfo(); | |
if (netInfo == null) | |
return false; | |
else { | |
for (NetworkInfo ni : netInfo) { | |
if (ni != null && ni.getTypeName().equalsIgnoreCase("WIFI")) | |
haveConnectedWifi = ni.isConnected(); | |
if (ni != null && ni.getTypeName().equalsIgnoreCase("MOBILE")) | |
haveConnectedMobile = ni.isConnected(); | |
} | |
return haveConnectedMobile || haveConnectedWifi; | |
} | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
IntentFilter intentFilter = new IntentFilter(); | |
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); | |
registerReceiver(networkChangeReceiver, intentFilter); | |
if (appUpdateManager != null) | |
appUpdateManager | |
.getAppUpdateInfo() | |
.addOnSuccessListener( | |
appUpdateInfo -> { | |
if (appUpdateInfo.updateAvailability() | |
== UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) { | |
// If an in-app update is already running, resume the update. | |
try { | |
appUpdateManager.startUpdateFlowForResult( | |
appUpdateInfo, | |
AppUpdateType.IMMEDIATE, | |
this, | |
HIGH_PRIORITY_UPDATE); | |
} catch (IntentSender.SendIntentException e) { | |
e.printStackTrace(); | |
} | |
} | |
}); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
unregisterReceiver(networkChangeReceiver); | |
} | |
private void checkForUpdate() { | |
// Creates instance of the manager. | |
appUpdateManager = AppUpdateManagerFactory.create(getBaseContext()); | |
// Returns an intent object that you use to check for an update. | |
Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo(); | |
// Checks whether the platform allows the specified type of update, | |
// only check | |
appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> { | |
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) { | |
if (appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) { | |
// Request an immediate update. | |
try { | |
appUpdateManager.startUpdateFlowForResult( | |
// Pass the intent that is returned by 'getAppUpdateInfo()'. | |
appUpdateInfo, | |
// Or 'AppUpdateType.FLEXIBLE' for flexible updates. | |
AppUpdateType.IMMEDIATE, | |
// The current activity making the update request. | |
this, | |
// Include a request code to later monitor this update request. | |
HIGH_PRIORITY_UPDATE); | |
} catch (IntentSender.SendIntentException e) { | |
e.printStackTrace(); | |
} | |
} else { | |
// tell the user to update it on play store directly. | |
AlertDialog.Builder builder = new MaterialAlertDialogBuilder(MainActivity.this, | |
R.style.AlertDialogTheme); | |
builder.setCancelable(false); | |
builder | |
.setTitle("Update required") | |
.setMessage("Please Update the app to use it. We have improved the app.") | |
.setPositiveButton("Ok", ((dialogInterface, i) -> { | |
Process.killProcess(Process.myPid()); | |
System.exit(1); | |
})); | |
Dialog dialog = builder.create(); | |
dialog.setCanceledOnTouchOutside(false); | |
dialog.show(); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment