OmniLib: Add TaskUtils

Change-Id: Ieda90aa40d8fdef68f566bdc8cf4f302330a3438
diff --git a/res/anim/last_app_in.xml b/res/anim/last_app_in.xml
new file mode 100644
index 0000000..f3d8f0e
--- /dev/null
+++ b/res/anim/last_app_in.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2014 The CyanogenMod 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.
+-->
+
+<set xmlns:android="http://schemas.android.com/apk/res/android"
+    android:interpolator="@android:interpolator/linear">
+
+    <translate android:fromXDelta="0%" android:toXDelta="-35%"
+        android:zAdjustment="bottom"
+        android:duration="@android:integer/config_shortAnimTime"
+        />
+    <scale android:fromXScale="0.80" android:toXScale="1.0"
+        android:fromYScale="0.80" android:toYScale="1.0"
+        android:pivotX="50%" android:pivotY="50%"
+        android:duration="@android:integer/config_shortAnimTime"
+        />
+    <translate android:fromXDelta="-35%" android:toXDelta="35%"
+        android:zAdjustment="top"
+        android:startOffset="@android:integer/config_shortAnimTime"
+        android:duration="@android:integer/config_shortAnimTime"
+        />
+    <alpha android:fromAlpha="0.6" android:toAlpha="1.0"
+        android:duration="@android:integer/config_shortAnimTime"
+        />
+</set>
diff --git a/res/anim/last_app_out.xml b/res/anim/last_app_out.xml
new file mode 100644
index 0000000..2633f73
--- /dev/null
+++ b/res/anim/last_app_out.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2014 The CyanogenMod 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.
+-->
+
+<set xmlns:android="http://schemas.android.com/apk/res/android"
+    android:interpolator="@android:interpolator/linear">
+
+    <translate android:fromXDelta="-35%" android:toXDelta="35%"
+        android:zAdjustment="top"
+        android:duration="@android:integer/config_shortAnimTime"
+        />
+    <scale android:fromXScale="1.0" android:toXScale="0.80"
+        android:fromYScale="1.0" android:toYScale="0.80"
+        android:pivotX="50%" android:pivotY="50%"
+        android:duration="@android:integer/config_shortAnimTime"
+        />
+    <translate android:fromXDelta="35%" android:toXDelta="-35%"
+        android:zAdjustment="bottom"
+        android:startOffset="@android:integer/config_shortAnimTime"
+        android:duration="@android:integer/config_shortAnimTime"
+        />
+    <alpha android:fromAlpha="1.0" android:toAlpha="0.6"
+        android:duration="@android:integer/config_shortAnimTime"
+        />
+</set>
diff --git a/src/org/omnirom/omnilib/utils/TaskUtils.java b/src/org/omnirom/omnilib/utils/TaskUtils.java
new file mode 100644
index 0000000..66db0b8
--- /dev/null
+++ b/src/org/omnirom/omnilib/utils/TaskUtils.java
@@ -0,0 +1,102 @@
+/*
+* Copyright (C) 2014-2022 The OmniROM 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 org.omnirom.omnilib.utils;
+
+import android.app.Activity;
+import android.app.ActivityOptions;
+import android.app.ActivityManager;
+import android.app.ActivityManagerNative;
+import android.content.Context;
+import android.content.ComponentName;
+import android.content.Intent;
+import android.content.pm.ActivityInfo;
+import android.content.pm.PackageManager;
+import android.os.RemoteException;
+
+import org.omnirom.omnilib.R;
+
+import java.util.List;
+
+public class TaskUtils {
+
+    public static void toggleLastApp(Context context, int userId) {
+        final ActivityInfo homeInfo = new Intent(Intent.ACTION_MAIN)
+                .addCategory(Intent.CATEGORY_HOME).resolveActivityInfo(context.getPackageManager(), 0);
+        final ActivityManager am = (ActivityManager) context
+                .getSystemService(Activity.ACTIVITY_SERVICE);
+        final List<ActivityManager.RecentTaskInfo> tasks = am.getRecentTasks(8,
+                ActivityManager.RECENT_IGNORE_UNAVAILABLE |
+                ActivityManager.RECENT_WITH_EXCLUDED);
+
+        int lastAppId = 0;
+        for (int i = 1; i < tasks.size(); i++) {
+            final ActivityManager.RecentTaskInfo info = tasks.get(i);
+            boolean isExcluded = (info.baseIntent.getFlags() & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
+                    == Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS;
+            if (isExcluded) {
+                continue;
+            }
+            if (isCurrentHomeActivity(info.baseIntent.getComponent(), homeInfo)) {
+                continue;
+            }
+            lastAppId = info.persistentId;
+            break;
+        }
+        if (lastAppId > 0) {
+            ActivityOptions options = ActivityOptions.makeCustomAnimation(context,
+                    R.anim.last_app_in, R.anim.last_app_out);
+            try {
+                ActivityManagerNative.getDefault().startActivityFromRecents(
+                        lastAppId,  options.toBundle());
+            } catch (RemoteException e) {
+            }
+        }
+    }
+
+    private static boolean isCurrentHomeActivity(ComponentName component,
+            ActivityInfo homeInfo) {
+        return homeInfo != null
+                && homeInfo.packageName.equals(component.getPackageName())
+                && homeInfo.name.equals(component.getClassName());
+    }
+
+    private static int getRunningTask(Context context) {
+        final ActivityManager am = (ActivityManager) context
+                .getSystemService(Context.ACTIVITY_SERVICE);
+
+        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
+        if (tasks != null && !tasks.isEmpty()) {
+            return tasks.get(0).id;
+        }
+        return -1;
+    }
+
+    public static ActivityInfo getRunningActivityInfo(Context context) {
+        final ActivityManager am = (ActivityManager) context
+                .getSystemService(Context.ACTIVITY_SERVICE);
+        final PackageManager pm = context.getPackageManager();
+
+        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
+        if (tasks != null && !tasks.isEmpty()) {
+            ActivityManager.RunningTaskInfo top = tasks.get(0);
+            try {
+                return pm.getActivityInfo(top.topActivity, 0);
+            } catch (PackageManager.NameNotFoundException e) {
+            }
+        }
+        return null;
+    }
+}