|
35 | 35 | import android.content.pm.InstallSourceInfo; |
36 | 36 | import android.content.pm.PackageInfo; |
37 | 37 | import android.content.pm.PackageInstaller; |
| 38 | +import android.content.pm.PackageManager; |
38 | 39 | import android.content.pm.SharedLibraryInfo; |
39 | 40 | import android.content.pm.VersionedPackage; |
40 | 41 | import android.ext.PackageId; |
41 | | -import android.os.Build; |
42 | 42 | import android.os.Process; |
43 | 43 | import android.os.RemoteException; |
44 | 44 | import android.os.UserHandle; |
45 | | -import android.provider.Settings; |
46 | 45 | import android.util.ArrayMap; |
47 | 46 | import android.util.ArraySet; |
48 | 47 | import android.util.Log; |
@@ -529,7 +528,6 @@ private static boolean shouldHideDisabledState(String pkgName) { |
529 | 528 | } |
530 | 529 |
|
531 | 530 | switch (pkgName) { |
532 | | - case GmsInfo.PACKAGE_GSF: |
533 | 531 | case GmsInfo.PACKAGE_GMS_CORE: |
534 | 532 | return false; |
535 | 533 | default: |
@@ -626,4 +624,43 @@ public void setComponentEnabledSettings(List<ComponentEnabledSetting> settings) |
626 | 624 | } |
627 | 625 | } |
628 | 626 |
|
| 627 | + /** @see android.app.ContextImpl#createPackageContext */ |
| 628 | + @Nullable |
| 629 | + public static Context maybeOverrideGsfPackageContext(String packageName) { |
| 630 | + if (!GmsCompat.isGmsCore()) { |
| 631 | + return null; |
| 632 | + } |
| 633 | + |
| 634 | + if (!PackageId.GSF_NAME.equals(packageName)) { |
| 635 | + return null; |
| 636 | + } |
| 637 | + |
| 638 | + // On first launch, GmsCore attempts to migrate GSF databases into itself. GSF is a |
| 639 | + // hasCode=false package since Android 15 and is not needed for fresh installs of GmsCore. |
| 640 | + // If GSF is absent, GmsCore crashes when it tries to create package context for GSF as |
| 641 | + // part of database migration. To prevent this crash, return GmsCore app context instead |
| 642 | + // of GSF package context, which turns database migration into a no-op. |
| 643 | + |
| 644 | + Context ctx = GmsCompat.appContext(); |
| 645 | + PackageManager pkgManager = ctx.getPackageManager(); |
| 646 | + |
| 647 | + try { |
| 648 | + pkgManager.getApplicationInfo(packageName, 0); |
| 649 | + } catch (PackageManager.NameNotFoundException e) { |
| 650 | + Log.d(TAG, "replacing GSF package context with GmsCore app context", new Throwable()); |
| 651 | + return ctx; |
| 652 | + } |
| 653 | + |
| 654 | + try { |
| 655 | + PackageInfo pi = pkgManager.getPackageInfo(PackageId.GMS_CORE_NAME, 0); |
| 656 | + if (pi.sharedUserId == null) { |
| 657 | + // GmsCore has left the GSF sharedUid but GSF is still present |
| 658 | + Log.d(TAG, "maybeReplaceGsfPackageName: sharedUserId is null, ignoring GSF", new Throwable()); |
| 659 | + return ctx; |
| 660 | + } |
| 661 | + return null; |
| 662 | + } catch (NameNotFoundException e) { |
| 663 | + throw new IllegalStateException(e); |
| 664 | + } |
| 665 | + } |
629 | 666 | } |
0 commit comments