Fix the wrong IMPORTANCE_ values.

Bug: 36697597
Test: cts-tradefed run cts-dev --skip-device-info --skip-preconditions --skip-system-status-check com.android.compatibility.common.tradefed.targetprep.NetworkConnectivityChecker -a armeabi-v7a -l INFO -m CtsAppTestCases -t android.app.cts.AlertWindowsTests

Change-Id: I3327d4668470400e8b30faeebb77534eeb2583ed
diff --git a/api/current.txt b/api/current.txt
index 6e30fd8..9552d93 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -3939,7 +3939,8 @@
     field public static final int IMPORTANCE_FOREGROUND = 100; // 0x64
     field public static final int IMPORTANCE_FOREGROUND_SERVICE = 125; // 0x7d
     field public static final int IMPORTANCE_GONE = 1000; // 0x3e8
-    field public static final int IMPORTANCE_PERCEPTIBLE = 130; // 0x82
+    field public static final int IMPORTANCE_PERCEPTIBLE = 230; // 0xe6
+    field public static final deprecated int IMPORTANCE_PERCEPTIBLE_DEPRECATED = 130; // 0x82
     field public static final int IMPORTANCE_SERVICE = 300; // 0x12c
     field public static final int IMPORTANCE_TOP_SLEEPING = 150; // 0x96
     field public static final int IMPORTANCE_VISIBLE = 200; // 0xc8
diff --git a/api/system-current.txt b/api/system-current.txt
index b0eb10d..e87775f 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -4083,7 +4083,8 @@
     field public static final int IMPORTANCE_FOREGROUND = 100; // 0x64
     field public static final int IMPORTANCE_FOREGROUND_SERVICE = 125; // 0x7d
     field public static final int IMPORTANCE_GONE = 1000; // 0x3e8
-    field public static final int IMPORTANCE_PERCEPTIBLE = 130; // 0x82
+    field public static final int IMPORTANCE_PERCEPTIBLE = 230; // 0xe6
+    field public static final deprecated int IMPORTANCE_PERCEPTIBLE_DEPRECATED = 130; // 0x82
     field public static final int IMPORTANCE_SERVICE = 300; // 0x12c
     field public static final int IMPORTANCE_TOP_SLEEPING = 150; // 0x96
     field public static final int IMPORTANCE_VISIBLE = 200; // 0xc8
diff --git a/api/test-current.txt b/api/test-current.txt
index 52a07fa..3690f4b 100644
--- a/api/test-current.txt
+++ b/api/test-current.txt
@@ -3949,7 +3949,8 @@
     field public static final int IMPORTANCE_FOREGROUND = 100; // 0x64
     field public static final int IMPORTANCE_FOREGROUND_SERVICE = 125; // 0x7d
     field public static final int IMPORTANCE_GONE = 1000; // 0x3e8
-    field public static final int IMPORTANCE_PERCEPTIBLE = 130; // 0x82
+    field public static final int IMPORTANCE_PERCEPTIBLE = 230; // 0xe6
+    field public static final deprecated int IMPORTANCE_PERCEPTIBLE_DEPRECATED = 130; // 0x82
     field public static final int IMPORTANCE_SERVICE = 300; // 0x12c
     field public static final int IMPORTANCE_TOP_SLEEPING = 150; // 0x96
     field public static final int IMPORTANCE_VISIBLE = 200; // 0xc8
diff --git a/core/java/android/app/ActivityManager.java b/core/java/android/app/ActivityManager.java
index 80482ca..0f6671b5 100644
--- a/core/java/android/app/ActivityManager.java
+++ b/core/java/android/app/ActivityManager.java
@@ -30,6 +30,8 @@
 import android.graphics.Matrix;
 import android.graphics.Point;
 import android.os.BatteryStats;
+import android.os.Build;
+import android.os.Build.VERSION_CODES;
 import android.os.IBinder;
 import android.os.ParcelFileDescriptor;
 
@@ -138,14 +140,17 @@
 
     static final class UidObserver extends IUidObserver.Stub {
         final OnUidImportanceListener mListener;
+        final Context mContext;
 
-        UidObserver(OnUidImportanceListener listener) {
+        UidObserver(OnUidImportanceListener listener, Context clientContext) {
             mListener = listener;
+            mContext = clientContext;
         }
 
         @Override
         public void onUidStateChanged(int uid, int procState, long procStateSeq) {
-            mListener.onUidImportance(uid, RunningAppProcessInfo.procStateToImportance(procState));
+            mListener.onUidImportance(uid, RunningAppProcessInfo.procStateToImportanceForClient(
+                    procState, mContext));
         }
 
         @Override
@@ -3081,10 +3086,32 @@
         public static final int IMPORTANCE_VISIBLE = 200;
 
         /**
-         * Constant for {@link #importance}: This process is not something the user
-         * is directly aware of, but is otherwise perceptable to them to some degree.
+         * Constant for {@link #importance}: {@link #IMPORTANCE_PERCEPTIBLE} had this wrong value
+         * before {@link Build.VERSION_CODES#O}.  Since the {@link Build.VERSION_CODES#O} SDK,
+         * the value of {@link #IMPORTANCE_PERCEPTIBLE} has been fixed.
+         *
+         * @deprecated Use {@link #IMPORTANCE_PERCEPTIBLE} instead.
          */
-        public static final int IMPORTANCE_PERCEPTIBLE = 130;
+        @Deprecated
+        public static final int IMPORTANCE_PERCEPTIBLE_DEPRECATED = 130;
+
+        /**
+         * Constant for {@link #importance}: This process is not something the user
+         * is directly aware of, but is otherwise perceptible to them to some degree.
+         */
+        public static final int IMPORTANCE_PERCEPTIBLE = 230;
+
+        /**
+         * Constant for {@link #importance}: {@link #IMPORTANCE_CANT_SAVE_STATE} had
+         * this wrong value
+         * before {@link Build.VERSION_CODES#O}.  Since the {@link Build.VERSION_CODES#O} SDK,
+         * the value of {@link #IMPORTANCE_CANT_SAVE_STATE} has been fixed.
+         *
+         * @deprecated Use {@link #IMPORTANCE_CANT_SAVE_STATE} instead.
+         * @hide
+         */
+        @Deprecated
+        public static final int IMPORTANCE_CANT_SAVE_STATE_DEPRECATED = 170;
 
         /**
          * Constant for {@link #importance}: This process is running an
@@ -3092,7 +3119,7 @@
          * while in the background.
          * @hide
          */
-        public static final int IMPORTANCE_CANT_SAVE_STATE = 170;
+        public static final int IMPORTANCE_CANT_SAVE_STATE= 270;
 
         /**
          * Constant for {@link #importance}: This process is contains services
@@ -3128,7 +3155,11 @@
          */
         public static final int IMPORTANCE_GONE = 1000;
 
-        /** @hide */
+        /**
+         * Convert a proc state to the correspondent IMPORTANCE_* constant.  If the return value
+         * will be passed to a client, use {@link #procStateToImportanceForClient}.
+         * @hide
+         */
         public static int procStateToImportance(int procState) {
             if (procState == PROCESS_STATE_NONEXISTENT) {
                 return IMPORTANCE_GONE;
@@ -3151,6 +3182,28 @@
             }
         }
 
+        /**
+         * Convert a proc state to the correspondent IMPORTANCE_* constant for a client represented
+         * by a given {@link Context}, with converting {@link #IMPORTANCE_PERCEPTIBLE}
+         * and {@link #IMPORTANCE_CANT_SAVE_STATE} to the corresponding "wrong" value if the
+         * client's target SDK < {@link VERSION_CODES#O}.
+         * @hide
+         */
+        public static int procStateToImportanceForClient(int procState, Context clientContext) {
+            final int importance = procStateToImportance(procState);
+
+            // For pre O apps, convert to the old, wrong values.
+            if (clientContext.getApplicationInfo().targetSdkVersion < VERSION_CODES.O) {
+                switch (importance) {
+                    case IMPORTANCE_PERCEPTIBLE:
+                        return IMPORTANCE_PERCEPTIBLE_DEPRECATED;
+                    case IMPORTANCE_CANT_SAVE_STATE:
+                        return IMPORTANCE_CANT_SAVE_STATE_DEPRECATED;
+                }
+            }
+            return importance;
+        }
+
         /** @hide */
         public static int importanceToProcState(int importance) {
             if (importance == IMPORTANCE_GONE) {
@@ -3380,7 +3433,7 @@
         try {
             int procState = getService().getPackageProcessState(packageName,
                     mContext.getOpPackageName());
-            return RunningAppProcessInfo.procStateToImportance(procState);
+            return RunningAppProcessInfo.procStateToImportanceForClient(procState, mContext);
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
@@ -3400,7 +3453,7 @@
         try {
             int procState = getService().getUidProcessState(uid,
                     mContext.getOpPackageName());
-            return RunningAppProcessInfo.procStateToImportance(procState);
+            return RunningAppProcessInfo.procStateToImportanceForClient(procState, mContext);
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
@@ -3450,7 +3503,7 @@
                 throw new IllegalArgumentException("Listener already registered: " + listener);
             }
             // TODO: implement the cut point in the system process to avoid IPCs.
-            UidObserver observer = new UidObserver(listener);
+            UidObserver observer = new UidObserver(listener, mContext);
             try {
                 getService().registerUidObserver(observer,
                         UID_OBSERVER_PROCSTATE | UID_OBSERVER_GONE,