From 446fbbba4ba245572d9429ca4f087bfa2325bdb2 Mon Sep 17 00:00:00 2001 From: Georgi 7DIGIT Date: Thu, 15 Feb 2024 13:40:40 +0200 Subject: [PATCH] Fix: update the Security provider via Google Play Services --- .../unicef/ecar/usupport/MainActivity.java | 92 ++++++++++++++++++- android/app/src/main/res/values-kk/colors.xml | 9 ++ .../res/values-kk/ic_launcher_background.xml | 4 + .../app/src/main/res/values-kk/strings.xml | 10 ++ android/app/src/main/res/values-kk/styles.xml | 18 ++++ android/app/src/main/res/values-ru/colors.xml | 9 ++ .../res/values-ru/ic_launcher_background.xml | 4 + .../app/src/main/res/values-ru/strings.xml | 10 ++ android/app/src/main/res/values-ru/styles.xml | 18 ++++ android/app/src/main/res/values/strings.xml | 5 +- 10 files changed, 177 insertions(+), 2 deletions(-) create mode 100644 android/app/src/main/res/values-kk/colors.xml create mode 100644 android/app/src/main/res/values-kk/ic_launcher_background.xml create mode 100644 android/app/src/main/res/values-kk/strings.xml create mode 100644 android/app/src/main/res/values-kk/styles.xml create mode 100644 android/app/src/main/res/values-ru/colors.xml create mode 100644 android/app/src/main/res/values-ru/ic_launcher_background.xml create mode 100644 android/app/src/main/res/values-ru/strings.xml create mode 100644 android/app/src/main/res/values-ru/styles.xml diff --git a/android/app/src/main/java/org/unicef/ecar/usupport/MainActivity.java b/android/app/src/main/java/org/unicef/ecar/usupport/MainActivity.java index 1aebdcac..4867b6e4 100644 --- a/android/app/src/main/java/org/unicef/ecar/usupport/MainActivity.java +++ b/android/app/src/main/java/org/unicef/ecar/usupport/MainActivity.java @@ -1,15 +1,26 @@ package org.unicef.ecar.usupport; +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.content.Intent; import android.os.Build; import android.os.Bundle; import com.facebook.react.ReactActivity; import com.facebook.react.ReactActivityDelegate; import com.facebook.react.ReactRootView; +import com.google.android.gms.common.GoogleApiAvailability; +import com.google.android.gms.security.ProviderInstaller; import expo.modules.ReactActivityDelegateWrapper; -public class MainActivity extends ReactActivity { +public class MainActivity extends ReactActivity + implements ProviderInstaller.ProviderInstallListener { + + private static final int ERROR_DIALOG_REQUEST_CODE = 1; + private boolean retryProviderInstall; + + @Override protected void onCreate(Bundle savedInstanceState) { // Set the theme to AppTheme BEFORE onCreate to support @@ -17,6 +28,85 @@ protected void onCreate(Bundle savedInstanceState) { // This is required for expo-splash-screen. setTheme(R.style.AppTheme); super.onCreate(null); + ProviderInstaller.installIfNeededAsync(this, this); + } + + /** + * This method is called if updating fails. The error code indicates + * whether the error is recoverable. + */ + public void onProviderInstallFailed(int errorCode, Intent recoveryIntent) { + GoogleApiAvailability availability = GoogleApiAvailability.getInstance(); + if (availability.isUserResolvableError(errorCode)) { + // Recoverable error. Show a dialog prompting the user to + // install/update/enable Google Play services. + availability.showErrorDialogFragment( + this, + errorCode, + ERROR_DIALOG_REQUEST_CODE, + new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + // The user chose not to take the recovery action. + onProviderInstallerNotAvailable(); + } + }); + } else { + // Google Play services isn't available. + onProviderInstallerNotAvailable(); + } + } + + @Override + public void onProviderInstalled() { + // Handle the success of the provider installation + // You might not need to do anything here, but the method must exist. + } + + @Override + public void onActivityResult(int requestCode, int resultCode, + Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (requestCode == ERROR_DIALOG_REQUEST_CODE) { + // Adding a fragment via GoogleApiAvailability.showErrorDialogFragment + // before the instance state is restored throws an error. So instead, + // set a flag here, which causes the fragment to delay until + // onPostResume. + retryProviderInstall = true; + } + } + + /** + * On resume, check whether a flag indicates that the provider needs to be + * reinstalled. + */ + @Override + protected void onPostResume() { + super.onPostResume(); + if (retryProviderInstall) { + // It's safe to retry installation. + ProviderInstaller.installIfNeededAsync(this, this); + } + retryProviderInstall = false; + } + + private void onProviderInstallerNotAvailable() { + // This is reached if the provider can't be updated for some reason. + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(getString(R.string.provider_install_failed_title)); + builder.setMessage(getString(R.string.provider_install_failed_message)); + + builder.setPositiveButton(getString(R.string.ok_button), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + // User clicked OK button. + finish(); // Close the app or navigate accordingly + } + }); + + // Create and show the AlertDialog + AlertDialog dialog = builder.create(); + dialog.show(); } /** diff --git a/android/app/src/main/res/values-kk/colors.xml b/android/app/src/main/res/values-kk/colors.xml new file mode 100644 index 00000000..c7f7adb8 --- /dev/null +++ b/android/app/src/main/res/values-kk/colors.xml @@ -0,0 +1,9 @@ + + + + #54CFD9 + diff --git a/android/app/src/main/res/values-kk/ic_launcher_background.xml b/android/app/src/main/res/values-kk/ic_launcher_background.xml new file mode 100644 index 00000000..4e51ddd6 --- /dev/null +++ b/android/app/src/main/res/values-kk/ic_launcher_background.xml @@ -0,0 +1,4 @@ + + + #7372F9 + \ No newline at end of file diff --git a/android/app/src/main/res/values-kk/strings.xml b/android/app/src/main/res/values-kk/strings.xml new file mode 100644 index 00000000..51a41650 --- /dev/null +++ b/android/app/src/main/res/values-kk/strings.xml @@ -0,0 +1,10 @@ + + + cover + uSupport + @string/CODEPUSH_ANDROID_DEPLOYMENT_KEY + Қамтамасыз етушіні орнату сәтсіз аяқталды + Қауіпсіздік қамтамасыз етушісі орнатылмады. Жаңартусыз жалғастыра алмайсыз. + Жарайды + + diff --git a/android/app/src/main/res/values-kk/styles.xml b/android/app/src/main/res/values-kk/styles.xml new file mode 100644 index 00000000..a9e3b253 --- /dev/null +++ b/android/app/src/main/res/values-kk/styles.xml @@ -0,0 +1,18 @@ + + + + + diff --git a/android/app/src/main/res/values-ru/colors.xml b/android/app/src/main/res/values-ru/colors.xml new file mode 100644 index 00000000..c7f7adb8 --- /dev/null +++ b/android/app/src/main/res/values-ru/colors.xml @@ -0,0 +1,9 @@ + + + + #54CFD9 + diff --git a/android/app/src/main/res/values-ru/ic_launcher_background.xml b/android/app/src/main/res/values-ru/ic_launcher_background.xml new file mode 100644 index 00000000..4e51ddd6 --- /dev/null +++ b/android/app/src/main/res/values-ru/ic_launcher_background.xml @@ -0,0 +1,4 @@ + + + #7372F9 + \ No newline at end of file diff --git a/android/app/src/main/res/values-ru/strings.xml b/android/app/src/main/res/values-ru/strings.xml new file mode 100644 index 00000000..1fffc676 --- /dev/null +++ b/android/app/src/main/res/values-ru/strings.xml @@ -0,0 +1,10 @@ + + + cover + uSupport + @string/CODEPUSH_ANDROID_DEPLOYMENT_KEY + Ошибка установки провайдера + Провайдер безопасности не может быть установлен. Вы не можете продолжить без обновления. + ОК + + diff --git a/android/app/src/main/res/values-ru/styles.xml b/android/app/src/main/res/values-ru/styles.xml new file mode 100644 index 00000000..a9e3b253 --- /dev/null +++ b/android/app/src/main/res/values-ru/styles.xml @@ -0,0 +1,18 @@ + + + + + diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml index 82ddebd8..23f3ed9e 100644 --- a/android/app/src/main/res/values/strings.xml +++ b/android/app/src/main/res/values/strings.xml @@ -1,6 +1,9 @@ cover - USupport + uSupport @string/CODEPUSH_ANDROID_DEPLOYMENT_KEY + Provider Installation Failed + The security provider could not be installed. You cannot proceed without updating. + OK