Avoid system app crashes for absent TEXT_SERVICES_MANAGER_SERVICE in Wear

Wear intentionally removes the TEXT_SERVICES_MANAGER_SERVICE, so an absence of that service should not throw an exception for any app in Wear. See http://ag/q/topic:%22wear_TextServicesManagerService%22 for details.

Bug: 335456104
Bug: 366125318
Test: manual
Flag: android.server.remove_text_service
Change-Id: If17cb7fe36024f2e03cf4dd3843e99b06036cd00
diff --git a/core/java/android/app/SystemServiceRegistry.java b/core/java/android/app/SystemServiceRegistry.java
index 5147f12..337939f 100644
--- a/core/java/android/app/SystemServiceRegistry.java
+++ b/core/java/android/app/SystemServiceRegistry.java
@@ -593,6 +593,11 @@
             @Override
             public TextServicesManager createService(ContextImpl ctx)
                     throws ServiceNotFoundException {
+                 if (ctx.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH)
+                        && ServiceManager.getService(Context.TEXT_SERVICES_MANAGER_SERVICE) == null
+                        && android.server.Flags.removeTextService()) {
+                    return null;
+                }
                 return TextServicesManager.createInstance(ctx);
             }});
 
@@ -1887,6 +1892,12 @@
                         return null;
                     }
                     break;
+                case Context.TEXT_SERVICES_MANAGER_SERVICE:
+                    if (android.server.Flags.removeTextService()
+                            && hasSystemFeatureOpportunistic(ctx, PackageManager.FEATURE_WATCH)) {
+                        return null;
+                    }
+                    break;
             }
             Slog.wtf(TAG, "Manager wrapper not available: " + name);
             return null;