Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.android.test.activity; |
| 18 | |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.List; |
| 21 | |
| 22 | import android.app.Activity; |
| 23 | import android.app.ActivityManager; |
| 24 | import android.app.ActivityThread; |
| 25 | import android.app.Application; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 26 | import android.content.ActivityNotFoundException; |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 27 | import android.os.Bundle; |
| 28 | import android.graphics.BitmapFactory; |
| 29 | import android.graphics.Bitmap; |
| 30 | import android.graphics.Canvas; |
| 31 | import android.graphics.drawable.BitmapDrawable; |
| 32 | import android.graphics.drawable.Drawable; |
| 33 | import android.widget.ImageView; |
| 34 | import android.widget.LinearLayout; |
| 35 | import android.widget.TextView; |
| 36 | import android.widget.ScrollView; |
| 37 | import android.view.LayoutInflater; |
| 38 | import android.view.View; |
| 39 | import android.content.Context; |
| 40 | import android.content.pm.ApplicationInfo; |
| 41 | import android.content.pm.PackageManager; |
| 42 | import android.content.res.CompatibilityInfo; |
| 43 | import android.util.DisplayMetrics; |
| 44 | import android.util.Log; |
| 45 | |
| 46 | public class ActivityTestMain extends Activity { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 47 | ActivityManager mAm; |
| 48 | |
| 49 | private void addThumbnail(LinearLayout container, Bitmap bm, |
| 50 | final ActivityManager.RecentTaskInfo task, |
| 51 | final ActivityManager.TaskThumbnails thumbs, final int subIndex) { |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 52 | ImageView iv = new ImageView(this); |
| 53 | if (bm != null) { |
| 54 | iv.setImageBitmap(bm); |
| 55 | } |
| 56 | iv.setBackgroundResource(android.R.drawable.gallery_thumb); |
| 57 | int w = getResources().getDimensionPixelSize(android.R.dimen.thumbnail_width); |
| 58 | int h = getResources().getDimensionPixelSize(android.R.dimen.thumbnail_height); |
| 59 | container.addView(iv, new LinearLayout.LayoutParams(w, h)); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 60 | |
| 61 | iv.setOnClickListener(new View.OnClickListener() { |
| 62 | @Override |
| 63 | public void onClick(View v) { |
| 64 | if (task.id >= 0 && thumbs != null) { |
| 65 | if (subIndex < (thumbs.numSubThumbbails-1)) { |
| 66 | mAm.removeSubTask(task.id, subIndex+1); |
| 67 | } |
| 68 | mAm.moveTaskToFront(task.id, ActivityManager.MOVE_TASK_WITH_HOME); |
| 69 | } else { |
| 70 | try { |
| 71 | startActivity(task.baseIntent); |
| 72 | } catch (ActivityNotFoundException e) { |
| 73 | Log.w("foo", "Unable to start task: " + e); |
| 74 | } |
| 75 | } |
| 76 | buildUi(); |
| 77 | } |
| 78 | }); |
| 79 | iv.setOnLongClickListener(new View.OnLongClickListener() { |
| 80 | @Override |
| 81 | public boolean onLongClick(View v) { |
| 82 | if (task.id >= 0 && thumbs != null) { |
| 83 | if (subIndex < 0) { |
| 84 | mAm.removeTask(task.id, ActivityManager.REMOVE_TASK_KILL_PROCESS); |
| 85 | } else { |
| 86 | mAm.removeSubTask(task.id, subIndex); |
| 87 | } |
| 88 | buildUi(); |
| 89 | return true; |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | }); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
| 97 | protected void onCreate(Bundle savedInstanceState) { |
| 98 | super.onCreate(savedInstanceState); |
| 99 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 100 | mAm = (ActivityManager)getSystemService(ACTIVITY_SERVICE); |
| 101 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 102 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 103 | @Override |
| 104 | protected void onStart() { |
| 105 | super.onStart(); |
| 106 | buildUi(); |
| 107 | } |
| 108 | |
| 109 | private View scrollWrap(View view) { |
| 110 | ScrollView scroller = new ScrollView(this); |
| 111 | scroller.addView(view, new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, |
| 112 | ScrollView.LayoutParams.MATCH_PARENT)); |
| 113 | return scroller; |
| 114 | } |
| 115 | |
| 116 | private void buildUi() { |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 117 | LinearLayout top = new LinearLayout(this); |
| 118 | top.setOrientation(LinearLayout.VERTICAL); |
| 119 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 120 | List<ActivityManager.RecentTaskInfo> recents = mAm.getRecentTasks(10, |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 121 | ActivityManager.RECENT_WITH_EXCLUDED); |
| 122 | if (recents != null) { |
| 123 | for (int i=0; i<recents.size(); i++) { |
| 124 | ActivityManager.RecentTaskInfo r = recents.get(i); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 125 | ActivityManager.TaskThumbnails tt = mAm.getTaskThumbnails(r.persistentId); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 126 | TextView tv = new TextView(this); |
| 127 | tv.setText(r.baseIntent.getComponent().flattenToShortString()); |
| 128 | top.addView(tv, new LinearLayout.LayoutParams( |
| 129 | LinearLayout.LayoutParams.WRAP_CONTENT, |
| 130 | LinearLayout.LayoutParams.WRAP_CONTENT)); |
| 131 | LinearLayout item = new LinearLayout(this); |
| 132 | item.setOrientation(LinearLayout.HORIZONTAL); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 133 | addThumbnail(item, tt != null ? tt.mainThumbnail : null, r, tt, -1); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 134 | for (int j=0; j<tt.numSubThumbbails; j++) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 135 | addThumbnail(item, tt.getSubThumbnail(j), r, tt, j); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 136 | } |
| 137 | top.addView(item, new LinearLayout.LayoutParams( |
| 138 | LinearLayout.LayoutParams.WRAP_CONTENT, |
| 139 | LinearLayout.LayoutParams.WRAP_CONTENT)); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | setContentView(scrollWrap(top)); |
| 144 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 145 | } |