Merge "[Telephony Mainline] Telephony uses own user activity notification"
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 813ad12..8ece95a 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -34,6 +34,7 @@
     <protected-broadcast android:name="android.intent.action.ANY_DATA_STATE" />
     <protected-broadcast android:name="android.intent.action.DATA_STALL_DETECTED" />
     <protected-broadcast android:name="android.intent.action.SIM_STATE_CHANGED" />
+    <protected-broadcast android:name="android.intent.action.USER_ACTIVITY_NOTIFICATION" />
     <protected-broadcast android:name="android.telephony.action.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS" />
     <protected-broadcast android:name="android.intent.action.ACTION_MDN_STATE_CHANGED" />
     <protected-broadcast android:name="android.provider.Telephony.SPN_STRINGS_UPDATED" />
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 86093a1..ab03c80 100755
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -188,6 +188,7 @@
 import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.function.Consumer;
 
 /**
@@ -276,6 +277,7 @@
     private static final int EVENT_SET_ICC_LOCK_ENABLED_DONE = 79;
     private static final int CMD_SET_SYSTEM_SELECTION_CHANNELS = 80;
     private static final int EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE = 81;
+    private static final int MSG_NOTIFY_USER_ACTIVITY = 82;
 
     // Parameters of select command.
     private static final int SELECT_COMMAND = 0xA4;
@@ -296,6 +298,12 @@
     private SharedPreferences mTelephonySharedPreferences;
     private PhoneConfigurationManager mPhoneConfigurationManager;
 
+    /** User Activity */
+    private AtomicBoolean mNotifyUserActivity;
+    private static final String ACTION_USER_ACTIVITY_NOTIFICATION =
+            "android.intent.action.USER_ACTIVITY_NOTIFICATION";
+    private static final int USER_ACTIVITY_NOTIFICATION_DELAY = 200;
+
     private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
     private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
     private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
@@ -1294,6 +1302,13 @@
                     notifyRequester(request);
                     break;
 
+                case MSG_NOTIFY_USER_ACTIVITY:
+                    removeMessages(MSG_NOTIFY_USER_ACTIVITY);
+                    Intent intent = new Intent(ACTION_USER_ACTIVITY_NOTIFICATION);
+                    intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
+                    getDefaultPhone().getContext().sendBroadcastAsUser(
+                            intent, UserHandle.ALL, permission.USER_ACTIVITY);
+                    break;
                 default:
                     Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
                     break;
@@ -8239,4 +8254,42 @@
         }
         return 0;
     }
+
+    /**
+     * Request for receiving user activity notification
+     */
+    @Override
+    public void requestUserActivityNotification() {
+        if (!mNotifyUserActivity.get()
+                && !mMainThreadHandler.hasMessages(MSG_NOTIFY_USER_ACTIVITY)) {
+            mNotifyUserActivity.set(true);
+        }
+    }
+
+    /**
+     * Called when userActivity is signalled in the power manager.
+     * This is safe to call from any thread, with any window manager locks held or not.
+     */
+    @Override
+    public void userActivity() {
+        // ***************************************
+        // *  Inherited from PhoneWindowManager  *
+        // ***************************************
+        // THIS IS CALLED FROM DEEP IN THE POWER MANAGER
+        // WITH ITS LOCKS HELD.
+        //
+        // This code must be VERY careful about the locks
+        // it acquires.
+        // In fact, the current code acquires way too many,
+        // and probably has lurking deadlocks.
+
+        if (Binder.getCallingUid() != Process.SYSTEM_UID) {
+            throw new SecurityException("Only the OS may call notifyUserActivity()");
+        }
+
+        if (mNotifyUserActivity.getAndSet(false)) {
+            mMainThreadHandler.sendEmptyMessageDelayed(MSG_NOTIFY_USER_ACTIVITY,
+                    USER_ACTIVITY_NOTIFICATION_DELAY);
+        }
+    }
 }