Add ThermalObserver system service to capture thermal state uevents.

Bug: 21445745
Change-Id: I980d60b66ca51942a1fd62502d6cf1f09208fc3a
diff --git a/core/java/android/content/Intent.java b/core/java/android/content/Intent.java
index 12c2632..6bbee56 100644
--- a/core/java/android/content/Intent.java
+++ b/core/java/android/content/Intent.java
@@ -3022,6 +3022,39 @@
     public static final String EXTRA_PROCESS_TEXT_READONLY =
             "android.intent.extra.PROCESS_TEXT_READONLY";
 
+    /**
+     * Broadcast action: reports when a new thermal event has been reached. When the device
+     * is reaching its maximum temperatue, the thermal level reported
+     * {@hide}
+     */
+    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
+    public static final String ACTION_THERMAL_EVENT = "android.intent.action.THERMAL_EVENT";
+
+    /** {@hide} */
+    public static final String EXTRA_THERMAL_STATE = "android.intent.extra.THERMAL_STATE";
+
+    /**
+     * Thermal state when the device is normal. This state is sent in the
+     * {@link ACTION_THERMAL_EVENT} broadcast as {@link EXTRA_THERMAL_STATE}.
+     * {@hide}
+     */
+    public static final int EXTRA_THERMAL_STATE_NORMAL = 0;
+
+    /**
+     * Thermal state where the device is approaching its maximum threshold. This state is sent in
+     * the {@link ACTION_THERMAL_EVENT} broadcast as {@link EXTRA_THERMAL_STATE}.
+     * {@hide}
+     */
+    public static final int EXTRA_THERMAL_STATE_WARNING = 1;
+
+    /**
+     * Thermal state where the device has reached its maximum threshold. This state is sent in the
+     * {@link ACTION_THERMAL_EVENT} broadcast as {@link EXTRA_THERMAL_STATE}.
+     * {@hide}
+     */
+    public static final int EXTRA_THERMAL_STATE_EXCEEDED = 2;
+
+
     // ---------------------------------------------------------------------
     // ---------------------------------------------------------------------
     // Standard intent categories (see addCategory()).
diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml
index b051de4..0f46983 100644
--- a/core/res/AndroidManifest.xml
+++ b/core/res/AndroidManifest.xml
@@ -65,6 +65,7 @@
     <protected-broadcast android:name="android.intent.action.NEW_OUTGOING_CALL" />
     <protected-broadcast android:name="android.intent.action.REBOOT" />
     <protected-broadcast android:name="android.intent.action.DOCK_EVENT" />
+    <protected-broadcast android:name="android.intent.action.THERMAL_EVENT" />
     <protected-broadcast android:name="android.intent.action.MASTER_CLEAR_NOTIFICATION" />
     <protected-broadcast android:name="android.intent.action.USER_ADDED" />
     <protected-broadcast android:name="android.intent.action.USER_REMOVED" />
diff --git a/services/core/java/com/android/server/ThermalObserver.java b/services/core/java/com/android/server/ThermalObserver.java
new file mode 100644
index 0000000..aee28fb
--- /dev/null
+++ b/services/core/java/com/android/server/ThermalObserver.java
@@ -0,0 +1,146 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.server;
+
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.os.Binder;
+import android.os.Handler;
+import android.os.Message;
+import android.os.PowerManager;
+import android.os.UEventObserver;
+import android.os.UserHandle;
+
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
+
+/**
+ * ThermalObserver for monitoring temperature changes.
+ */
+public class ThermalObserver extends SystemService {
+    private static final String TAG = "ThermalObserver";
+
+    private static final String CALLSTATE_UEVENT_MATCH =
+            "DEVPATH=/devices/virtual/switch/thermalstate";
+
+    private static final int MSG_THERMAL_STATE_CHANGED = 0;
+
+    private static final int SWITCH_STATE_NORMAL = 0;
+    private static final int SWITCH_STATE_WARNING = 1;
+    private static final int SWITCH_STATE_EXCEEDED = 2;
+
+    private final PowerManager mPowerManager;
+    private final PowerManager.WakeLock mWakeLock;
+
+    private final Object mLock = new Object();
+    private Integer mLastState;
+
+    private final UEventObserver mThermalWarningObserver = new UEventObserver() {
+        @Override
+        public void onUEvent(UEventObserver.UEvent event) {
+            updateLocked(Integer.parseInt(event.get("SWITCH_STATE")));
+        }
+    };
+
+    private final Handler mHandler = new Handler(true /*async*/) {
+        @Override
+        public void handleMessage(Message msg) {
+            switch (msg.what) {
+                case MSG_THERMAL_STATE_CHANGED:
+                    handleThermalStateChange(msg.arg1);
+                    mWakeLock.release();
+                    break;
+            }
+        }
+    };
+
+    public ThermalObserver(Context context) {
+        super(context);
+        mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
+        mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
+
+        mThermalWarningObserver.startObserving(CALLSTATE_UEVENT_MATCH);
+    }
+
+    private void updateLocked(int state) {
+        Message message = new Message();
+        message.what = MSG_THERMAL_STATE_CHANGED;
+        message.arg1 = state;
+
+        mWakeLock.acquire();
+        mHandler.sendMessage(message);
+    }
+
+    private void handleThermalStateChange(int state) {
+        synchronized (mLock) {
+            mLastState = state;
+            Intent intent = new Intent(Intent.ACTION_THERMAL_EVENT);
+            intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
+
+            final int thermalState;
+
+            switch (state) {
+                case SWITCH_STATE_WARNING:
+                    thermalState = Intent.EXTRA_THERMAL_STATE_WARNING;
+                    break;
+                case SWITCH_STATE_EXCEEDED:
+                    thermalState = Intent.EXTRA_THERMAL_STATE_EXCEEDED;
+                    break;
+                case SWITCH_STATE_NORMAL:
+                default:
+                    thermalState = Intent.EXTRA_THERMAL_STATE_NORMAL;
+                    break;
+            }
+
+            intent.putExtra(Intent.EXTRA_THERMAL_STATE, thermalState);
+
+            getContext().sendBroadcastAsUser(intent, UserHandle.ALL);
+        }
+    }
+
+    @Override
+    public void onStart() {
+        publishBinderService(TAG, new BinderService());
+    }
+
+    private final class BinderService extends Binder {
+        @Override
+        protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
+            if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
+                    != PackageManager.PERMISSION_GRANTED) {
+                pw.println("Permission Denial: can't dump thermal observer service from from pid="
+                        + Binder.getCallingPid()
+                        + ", uid=" + Binder.getCallingUid());
+                return;
+            }
+
+            final long ident = Binder.clearCallingIdentity();
+            try {
+                synchronized (mLock) {
+                    if (args == null || args.length == 0 || "-a".equals(args[0])) {
+                        pw.println("Current Thermal Observer Service state:");
+                        pw.println("  last state change: "
+                                + (mLastState != null ? mLastState : "none"));
+                    }
+                }
+            } finally {
+                Binder.restoreCallingIdentity(ident);
+            }
+        }
+    }
+}
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index 3effe63..08816b9 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -825,6 +825,11 @@
 
             if (!disableNonCoreServices) {
                 mSystemServiceManager.startService(DockObserver.class);
+
+                if (context.getPackageManager().hasSystemFeature
+                        (PackageManager.FEATURE_WATCH)) {
+                    mSystemServiceManager.startService(ThermalObserver.class);
+                }
             }
 
             try {