Snap for 7497286 from 4e903b627d412a1c6eb45bebe53510801f9d48a7 to sc-release

Change-Id: I4a38a89f0fe2a620762879fc52bd5e64b6934930
diff --git a/DirectRenderingCluster/AndroidManifest.xml b/DirectRenderingCluster/AndroidManifest.xml
index 57c31c0..5f0230b 100644
--- a/DirectRenderingCluster/AndroidManifest.xml
+++ b/DirectRenderingCluster/AndroidManifest.xml
@@ -80,6 +80,7 @@
 
         <activity android:name=".FakeFreeNavigationActivity"
             android:exported="false"
+            android:showForAllUsers="true"
             android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
             android:launchMode="singleInstance"
             android:resizeableActivity="true"
diff --git a/DirectRenderingCluster/src/android/car/cluster/ClusterRenderingService.java b/DirectRenderingCluster/src/android/car/cluster/ClusterRenderingService.java
index 11bcfac..331eeee 100644
--- a/DirectRenderingCluster/src/android/car/cluster/ClusterRenderingService.java
+++ b/DirectRenderingCluster/src/android/car/cluster/ClusterRenderingService.java
@@ -33,6 +33,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.pm.ActivityInfo;
 import android.graphics.Rect;
 import android.hardware.display.DisplayManager;
 import android.hardware.display.DisplayManager.DisplayListener;
@@ -209,8 +210,8 @@
     }
 
     private Intent getNavigationActivityIntent(int displayId) {
-        ComponentName component = MainClusterActivity.getNavigationActivity(this);
-        if (component == null) {
+        ActivityInfo activityInfo = MainClusterActivity.getNavigationActivity(this);
+        if (activityInfo == null) {
             Log.e(TAG, "Failed to resolve the navigation activity");
             return null;
         }
@@ -223,7 +224,7 @@
         setClusterActivityState(ClusterActivityState.create(/* visible= */ true,
                     /* unobscuredBounds= */ new Rect(0, 0, 240, 320)));
         return new Intent(Intent.ACTION_MAIN)
-            .setComponent(component)
+            .setClassName(activityInfo.packageName, activityInfo.name)
             .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
             .putExtra(Car.CAR_EXTRA_CLUSTER_ACTIVITY_STATE,
                 ClusterActivityState.create(/* visible= */ true,
diff --git a/DirectRenderingCluster/src/android/car/cluster/MainClusterActivity.java b/DirectRenderingCluster/src/android/car/cluster/MainClusterActivity.java
index f306e94..3b8d225 100644
--- a/DirectRenderingCluster/src/android/car/cluster/MainClusterActivity.java
+++ b/DirectRenderingCluster/src/android/car/cluster/MainClusterActivity.java
@@ -22,6 +22,7 @@
 import static android.content.Intent.ACTION_USER_UNLOCKED;
 import static android.content.PermissionChecker.PERMISSION_GRANTED;
 
+import android.annotation.NonNull;
 import android.app.ActivityManager;
 import android.app.ActivityOptions;
 import android.car.Car;
@@ -34,6 +35,7 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.ServiceConnection;
+import android.content.pm.ActivityInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.graphics.Rect;
@@ -437,13 +439,14 @@
         }
         mHandler.removeCallbacks(mRetryLaunchNavigationActivity);
 
-        ComponentName navigationActivity = getNavigationActivity(this);
+        ActivityInfo activityInfo = getNavigationActivity(this);
+        ComponentName navigationActivity = new ComponentName(activityInfo.packageName,
+                activityInfo.name);
+        int userId = (activityInfo.flags & ActivityInfo.FLAG_SHOW_FOR_ALL_USERS) != 0
+                ? UserHandle.USER_SYSTEM : ActivityManager.getCurrentUser();
         mClusterViewModel.setFreeNavigationActivity(navigationActivity);
 
         try {
-            if (navigationActivity == null) {
-                throw new ActivityNotFoundException();
-            }
             ClusterActivityState activityState = ClusterActivityState
                     .create(true, mNavigationDisplay.mUnobscuredBounds);
             Intent intent = new Intent(Intent.ACTION_MAIN)
@@ -452,12 +455,12 @@
                     .putExtra(Car.CAR_EXTRA_CLUSTER_ACTIVITY_STATE,
                             activityState.toBundle());
 
-            Log.d(TAG, "Launching: " + intent + " on display: " + mNavigationDisplay.mDisplayId);
+            Log.d(TAG, "Launching: " + intent + " on display" + mNavigationDisplay.mDisplayId
+                    + " as user" + userId);
             ActivityOptions activityOptions = ActivityOptions.makeBasic()
                     .setLaunchDisplayId(mNavigationDisplay.mDisplayId);
 
-            mService.startFixedActivityModeForDisplayAndUser(
-                    intent, activityOptions, ActivityManager.getCurrentUser());
+            mService.startFixedActivityModeForDisplayAndUser(intent, activityOptions, userId);
         } catch (ActivityNotFoundException ex) {
             // Some activities might not be available right on startup. We will retry.
             mHandler.postDelayed(mRetryLaunchNavigationActivity,
@@ -471,37 +474,27 @@
      * Returns a default navigation activity to show in the cluster.
      * In the current implementation we obtain this activity from an intent defined in a resources
      * file (which OEMs can overlay).
-     * Alternatively, other implementations could:
-     * <ul>
-     * <li>Dynamically detect what's the default navigation activity the user has selected on the
-     * head unit, and obtain the activity marked with
-     * {@link Car#CAR_CATEGORY_NAVIGATION} from the same package.
-     * <li>Let the user select one from settings.
-     * </ul>
+     * When it fails to find, parse or resolve the activity, it'll throw ActivityNotFoundException.
      */
-    static ComponentName getNavigationActivity(Context context) {
+    static @NonNull ActivityInfo getNavigationActivity(Context context) {
         PackageManager pm = context.getPackageManager();
-        int userId = ActivityManager.getCurrentUser();
         String intentString = context.getString(R.string.freeNavigationIntent);
 
         if (intentString == null) {
-            Log.w(TAG, "No free navigation activity defined");
-            return null;
+            throw new ActivityNotFoundException("No free navigation activity defined");
         }
         Log.i(TAG, "Free navigation intent: " + intentString);
 
         try {
             Intent intent = Intent.parseUri(intentString, Intent.URI_INTENT_SCHEME);
-            ResolveInfo navigationApp = pm.resolveActivityAsUser(intent,
-                    PackageManager.MATCH_DEFAULT_ONLY, userId);
+            ResolveInfo navigationApp = pm.resolveActivity(intent,
+                    PackageManager.MATCH_DEFAULT_ONLY);
             if (navigationApp == null) {
-                return null;
+                throw new ActivityNotFoundException("Can't resolve freeNavigationIntent");
             }
-            return new ComponentName(navigationApp.activityInfo.packageName,
-                    navigationApp.activityInfo.name);
+            return navigationApp.activityInfo;
         } catch (URISyntaxException ex) {
-            Log.e(TAG, "Unable to parse free navigation activity intent: '" + intentString + "'");
-            return null;
+            throw new ActivityNotFoundException("Unable to parse freeNavigationIntent");
         }
     }