From 89ad456ea49cb62615ebdcac83a2515743bbe5fa Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Sun, 24 Aug 2014 16:45:38 -0700 Subject: Fix issue #16311398: Limit number of documents a process can open In application processes, monitor for when we start getting close to the Dalvik heap limit, and ask the activity manager to try to prune old activity instances in that case. Add an explicit API for apps to ask that they have their own activity instances cleaned up, if they want. Fix some bugs in launching activities that were not correctly applying the "multi task" behavior in the appropriate situations of document-centric recents. Clean up the activity manager's process removal code to all share a common path. Add a new "Spam" option to ActivityTests, which continually creates new tasks, checking that the activity manager will now prune old tasks rather than letting the app run out of RAM. And while I was was doing this, I found problems with the path for bringing an empty task to the foreground -- it could make a new task instead of re-starting the root activity in the existing task. This is fixed, and some code in the recents UI for working around the bug is removed. And as long as I am doing that, we now have nice hooks in to the activity manager for AppTask to give some APIs for better managing the task, so add those along with more tests for these APIs in ActivityTests. We should look at also having the activity manager try to prune old tasks when it sees app processes being killed, to better balance memory use across multiple processes when some processes may host many documents. That however is for another CL... Change-Id: I2bb81c3f92819350c868c7a7470b35817eb9bea9 --- tests/ActivityTests/AndroidManifest.xml | 5 + .../android/test/activity/ActivityTestMain.java | 105 ++++++++++++++++++++- .../google/android/test/activity/DocActivity.java | 35 +++++++ .../google/android/test/activity/SpamActivity.java | 31 ++++++ 4 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 tests/ActivityTests/src/com/google/android/test/activity/DocActivity.java create mode 100644 tests/ActivityTests/src/com/google/android/test/activity/SpamActivity.java (limited to 'tests/ActivityTests') diff --git a/tests/ActivityTests/AndroidManifest.xml b/tests/ActivityTests/AndroidManifest.xml index c0898d3..3fb547d 100644 --- a/tests/ActivityTests/AndroidManifest.xml +++ b/tests/ActivityTests/AndroidManifest.xml @@ -31,6 +31,11 @@ + + + + diff --git a/tests/ActivityTests/src/com/google/android/test/activity/ActivityTestMain.java b/tests/ActivityTests/src/com/google/android/test/activity/ActivityTestMain.java index 7f3aa77..ea0db56 100644 --- a/tests/ActivityTests/src/com/google/android/test/activity/ActivityTestMain.java +++ b/tests/ActivityTests/src/com/google/android/test/activity/ActivityTestMain.java @@ -21,6 +21,7 @@ import java.util.List; import android.app.Activity; import android.app.ActivityManager; +import android.app.ActivityOptions; import android.app.AlertDialog; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiver; @@ -29,13 +30,15 @@ import android.content.ContentProviderClient; import android.content.Intent; import android.content.ServiceConnection; import android.graphics.BitmapFactory; +import android.net.Uri; import android.os.Bundle; +import android.os.Handler; import android.os.IBinder; +import android.os.Message; import android.os.RemoteException; import android.os.UserHandle; import android.os.UserManager; import android.graphics.Bitmap; -import android.view.WindowManager; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; @@ -60,6 +63,28 @@ public class ActivityTestMain extends Activity { ArrayList mConnections = new ArrayList(); + static final int MSG_SPAM = 1; + + final Handler mHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case MSG_SPAM: { + boolean fg = msg.arg1 != 0; + Intent intent = new Intent(ActivityTestMain.this, SpamActivity.class); + Bundle options = null; + if (fg) { + ActivityOptions opts = ActivityOptions.makeLaunchTaskBehindAnimation(); + options = opts.toBundle(); + } + startActivity(intent, options); + scheduleSpam(!fg); + } break; + } + super.handleMessage(msg); + } + }; + class BroadcastResultReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { @@ -143,7 +168,8 @@ public class ActivityTestMain extends Activity { @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add("Animate!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { - @Override public boolean onMenuItemClick(MenuItem item) { + @Override + public boolean onMenuItemClick(MenuItem item) { AlertDialog.Builder builder = new AlertDialog.Builder(ActivityTestMain.this, R.style.SlowDialog); builder.setTitle("This is a title"); @@ -316,6 +342,49 @@ public class ActivityTestMain extends Activity { return true; } }); + menu.add("Open Doc").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { + @Override public boolean onMenuItemClick(MenuItem item) { + ActivityManager.AppTask task = findDocTask(); + if (task == null) { + Intent intent = new Intent(ActivityTestMain.this, DocActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT + | Intent.FLAG_ACTIVITY_MULTIPLE_TASK + | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS); + startActivity(intent); + } else { + task.moveToFront(); + } + return true; + } + }); + menu.add("Stack Doc").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { + @Override public boolean onMenuItemClick(MenuItem item) { + ActivityManager.AppTask task = findDocTask(); + if (task != null) { + ActivityManager.RecentTaskInfo recent = task.getTaskInfo(); + Intent intent = new Intent(ActivityTestMain.this, DocActivity.class); + if (recent.id >= 0) { + // Stack on top. + intent.putExtra(DocActivity.LABEL, "Stacked"); + task.startActivity(ActivityTestMain.this, intent, null); + } else { + // Start root activity. + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT + | Intent.FLAG_ACTIVITY_MULTIPLE_TASK + | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS); + intent.putExtra(DocActivity.LABEL, "New Root"); + task.startActivity(ActivityTestMain.this, intent, null); + } + } + return true; + } + }); + menu.add("Spam!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { + @Override public boolean onMenuItemClick(MenuItem item) { + scheduleSpam(false); + return true; + } + }); return true; } @@ -342,6 +411,12 @@ public class ActivityTestMain extends Activity { mConnections.clear(); } + @Override + protected void onDestroy() { + super.onDestroy(); + mHandler.removeMessages(MSG_SPAM); + } + void addAppRecents(int count) { ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); Intent intent = new Intent(Intent.ACTION_MAIN); @@ -371,7 +446,31 @@ public class ActivityTestMain extends Activity { } } } - private View scrollWrap(View view) { + + ActivityManager.AppTask findDocTask() { + ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); + List tasks = am.getAppTasks(); + if (tasks != null) { + for (int i=0; i