Show some limited system usage in ongoing usage dialog

Bug: 162547999
Test: Created system usage and then started dialog
Change-Id: I96e9a3ed34a4f48f4c1f74b1f5feb32dba055f75
diff --git a/res/layout/ongoing_usage_dialog_content.xml b/res/layout/ongoing_usage_dialog_content.xml
index 100c9d7..f9a0f07 100644
--- a/res/layout/ongoing_usage_dialog_content.xml
+++ b/res/layout/ongoing_usage_dialog_content.xml
@@ -44,6 +44,14 @@
             android:id="@+id/other_use_content"
             style="@style/PermissionUsageDialogOtherUseContent"/>
 
+        <View
+            android:id="@+id/other_use_inside_spacer"
+            style="@style/PermissionUsageDialogOtherUseInsideSpacer"/>
+
+        <TextView
+            android:id="@+id/system_use_content"
+            style="@style/PermissionUsageDialogSystemUseContent"/>
+
     </LinearLayout>
 
 </ScrollView>
diff --git a/res/values/overlayable.xml b/res/values/overlayable.xml
index 0a0f976..aa683a4 100644
--- a/res/values/overlayable.xml
+++ b/res/values/overlayable.xml
@@ -155,6 +155,8 @@
             <item type="style" name="PermissionUsageDialogItemIconsContainer" />
             <item type="style" name="PermissionUsageDialogOtherUseHeader" />
             <item type="style" name="PermissionUsageDialogOtherUseContent" />
+            <item type="style" name="PermissionUsageDialogOtherUseInsideSpacer" />
+            <item type="style" name="PermissionUsageDialogSystemUseContent" />
             <!-- END ONGOING USAGE DIALOG -->
 
             <!-- START REQUEST ROLE DIALOG TITLE -->
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 653e214..e7804ff 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -887,9 +887,16 @@
     <!-- Message telling the user that a phone call is currently using the microphone [CHAR LIMIT=none] -->
     <string name="phone_call_uses_microphone">Microphone is used in &lt;b>phone call&lt;/b></string>
     <!-- Message telling the user that a phone call is currently using the microphone and the camera [CHAR LIMIT=none] -->
-    <string name="phone_call_uses_microphone_and_camera">Camera and Microphone are used in &lt;b>phone call&lt;/b></string>
+    <string name="phone_call_uses_microphone_and_camera">Camera and Microphone are used in &lt;b>video call&lt;/b></string>
     <!-- Message telling the user that a phone call is currently using the camera [CHAR LIMIT=none] -->
-    <string name="phone_call_uses_camera">Camera is used in &lt;b>phone call&lt;/b></string>
+    <string name="phone_call_uses_camera">Camera is used in &lt;b>video call&lt;/b></string>
+
+    <!-- Message telling the user that a system service is currently using the microphone [CHAR LIMIT=none] -->
+    <string name="system_uses_microphone">Microphone is accessed using system service</string>
+    <!-- Message telling the user that a system service is currently using the microphone and the camera [CHAR LIMIT=none] -->
+    <string name="system_uses_microphone_and_camera">Camera and Microphone are accessed using system service</string>
+    <!-- Message telling the user that a system service is currently using the camera [CHAR LIMIT=none] -->
+    <string name="system_uses_camera">Camera is accessed using system service</string>
 
     <!-- Line above a list of other apps and system service that are currently microphone or camera [CHAR LIMIT=60] -->
     <string name="other_use">Other use:</string>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index b269a4c..c1c8e2d 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -677,6 +677,18 @@
         <item name="android:layout_marginStart">16dp</item>
     </style>
 
+    <style name="PermissionUsageDialogOtherUseInsideSpacer">
+        <item name="android:layout_width">0dp</item>
+        <item name="android:layout_height">16dp</item>
+    </style>
+
+    <style name="PermissionUsageDialogSystemUseContent">
+        <item name="android:layout_width">wrap_content</item>
+        <item name="android:layout_height">wrap_content</item>
+        <item name="android:textAppearance">?android:textAppearanceListItemSecondary</item>
+        <item name="android:layout_marginStart">16dp</item>
+    </style>
+
     <!-- END ONGOING USAGE DIALOG -->
 
     <!-- START REQUEST ROLE DIALOG TITLE -->
diff --git a/src/com/android/permissioncontroller/permission/ui/handheld/ReviewOngoingUsageFragment.java b/src/com/android/permissioncontroller/permission/ui/handheld/ReviewOngoingUsageFragment.java
index 1ffb923..41b8a21 100644
--- a/src/com/android/permissioncontroller/permission/ui/handheld/ReviewOngoingUsageFragment.java
+++ b/src/com/android/permissioncontroller/permission/ui/handheld/ReviewOngoingUsageFragment.java
@@ -28,10 +28,12 @@
 import android.app.AlertDialog;
 import android.content.Context;
 import android.content.Intent;
+import android.location.LocationManager;
 import android.os.Bundle;
 import android.os.UserHandle;
 import android.text.Html;
 import android.util.ArrayMap;
+import android.util.ArraySet;
 import android.util.Pair;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -77,6 +79,7 @@
     private @Nullable AlertDialog mDialog;
     private OpUsageLiveData mOpUsageLiveData;
     private @Nullable Map<String, List<OpAccess>> mOpUsage;
+    private ArraySet<String> mSystemUsage = new ArraySet<>(0);
     private long mStartTime;
 
     /**
@@ -157,11 +160,13 @@
                     }
                 }
 
-                if (!Utils.isGroupOrBgGroupUserSensitive(groupUsage.getGroup())) {
-                    continue;
+                if (Utils.isGroupOrBgGroupUserSensitive(groupUsage.getGroup())) {
+                    usedGroups.add(appGroups.get(groupNum));
+                } else if (getContext().getSystemService(LocationManager.class).isProviderPackage(
+                        appUsage.getPackageName())
+                        && (groupName.equals(CAMERA) || groupName.equals(MICROPHONE))) {
+                    mSystemUsage.add(groupName);
                 }
-
-                usedGroups.add(appGroups.get(groupNum));
             }
 
             if (!usedGroups.isEmpty()) {
@@ -170,7 +175,7 @@
             }
         }
 
-        if (usages.isEmpty() && mOpUsage.isEmpty()) {
+        if (usages.isEmpty() && mOpUsage.isEmpty() && mSystemUsage.isEmpty()) {
             getActivity().finish();
             return;
         }
@@ -230,7 +235,6 @@
 
     private @NonNull View createDialogView(
             @NonNull List<Pair<AppPermissionUsage, List<GroupUsage>>> usages) {
-        Map<String, List<OpAccess>> otherAccesses = mOpUsage;
         Context context = getActivity();
         LayoutInflater inflater = LayoutInflater.from(context);
         View contentView = inflater.inflate(R.layout.ongoing_usage_dialog_content, null);
@@ -250,38 +254,67 @@
 
         TextView otherUseHeader = contentView.requireViewById(R.id.other_use_header);
         TextView otherUseContent = contentView.requireViewById(R.id.other_use_content);
-        if (otherAccesses == null) {
+        TextView systemUseContent = contentView.requireViewById(R.id.system_use_content);
+        View otherUseSpacer = contentView.requireViewById(R.id.other_use_inside_spacer);
+
+        if (mOpUsage.isEmpty() && mSystemUsage.isEmpty()) {
             otherUseHeader.setVisibility(View.GONE);
             otherUseContent.setVisibility(View.GONE);
-        } else {
-            if (numUsages == 0) {
-                otherUseHeader.setVisibility(View.GONE);
-            }
+        }
 
-            if (otherAccesses.containsKey(VIDEO_CALL) && otherAccesses.containsKey(PHONE_CALL)) {
+        if (numUsages == 0) {
+            otherUseHeader.setVisibility(View.GONE);
+            appsList.setVisibility(View.GONE);
+        }
+
+        if (mOpUsage.isEmpty() || mSystemUsage.isEmpty()) {
+            otherUseSpacer.setVisibility(View.GONE);
+        }
+
+        if (mOpUsage.isEmpty()) {
+            otherUseContent.setVisibility(View.GONE);
+        }
+
+        if (mSystemUsage.isEmpty()) {
+            systemUseContent.setVisibility(View.GONE);
+        }
+
+        if (!mOpUsage.isEmpty()) {
+            if (mOpUsage.containsKey(VIDEO_CALL) && mOpUsage.containsKey(
+                    PHONE_CALL)) {
                 otherUseContent.setText(
                         Html.fromHtml(getString(R.string.phone_call_uses_microphone_and_camera),
                                 0));
-            } else if (otherAccesses.containsKey(VIDEO_CALL)) {
+            } else if (mOpUsage.containsKey(VIDEO_CALL)) {
                 otherUseContent.setText(
                         Html.fromHtml(getString(R.string.phone_call_uses_camera), 0));
-            } else {
+            } else if (mOpUsage.containsKey(PHONE_CALL)) {
                 otherUseContent.setText(
                         Html.fromHtml(getString(R.string.phone_call_uses_microphone), 0));
             }
 
-            if (otherAccesses.containsKey(VIDEO_CALL)) {
+            if (mOpUsage.containsKey(VIDEO_CALL)) {
                 usedGroups.put(CAMERA, KotlinUtils.INSTANCE.getPermGroupLabel(context, CAMERA));
             }
 
-            if (otherAccesses.containsKey(PHONE_CALL)) {
+            if (mOpUsage.containsKey(PHONE_CALL)) {
                 usedGroups.put(MICROPHONE,
                         KotlinUtils.INSTANCE.getPermGroupLabel(context, MICROPHONE));
             }
         }
 
-        if (numUsages == 0) {
-            appsList.setVisibility(View.GONE);
+        if (!mSystemUsage.isEmpty()) {
+            if (mSystemUsage.contains(MICROPHONE) && mSystemUsage.contains(CAMERA)) {
+                systemUseContent.setText(getString(R.string.system_uses_microphone_and_camera));
+            } else if (mSystemUsage.contains(CAMERA)) {
+                systemUseContent.setText(getString(R.string.system_uses_camera));
+            } else if (mSystemUsage.contains(MICROPHONE) ) {
+                systemUseContent.setText(getString(R.string.system_uses_microphone));
+            }
+
+            for (String usage : mSystemUsage) {
+                usedGroups.put(usage, KotlinUtils.INSTANCE.getPermGroupLabel(context, usage));
+            }
         }
 
         // Add the layout for each app.