ActivityTestMain.java revision b4163a6e12ee7100c758c6d3d062ade1f2843fce
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 17package com.google.android.test.activity; 18 19import java.util.List; 20 21import android.app.Activity; 22import android.app.ActivityManager; 23import android.app.AlertDialog; 24import android.content.ActivityNotFoundException; 25import android.content.ComponentName; 26import android.content.Intent; 27import android.content.ServiceConnection; 28import android.os.Bundle; 29import android.os.IBinder; 30import android.graphics.Bitmap; 31import android.widget.ImageView; 32import android.widget.LinearLayout; 33import android.widget.TextView; 34import android.widget.ScrollView; 35import android.view.Menu; 36import android.view.MenuItem; 37import android.view.View; 38import android.content.Context; 39import android.util.Log; 40 41public class ActivityTestMain extends Activity { 42 static final String TAG = "ActivityTest"; 43 44 ActivityManager mAm; 45 46 private void addThumbnail(LinearLayout container, Bitmap bm, 47 final ActivityManager.RecentTaskInfo task, 48 final ActivityManager.TaskThumbnails thumbs, final int subIndex) { 49 ImageView iv = new ImageView(this); 50 if (bm != null) { 51 iv.setImageBitmap(bm); 52 } 53 iv.setBackgroundResource(android.R.drawable.gallery_thumb); 54 int w = getResources().getDimensionPixelSize(android.R.dimen.thumbnail_width); 55 int h = getResources().getDimensionPixelSize(android.R.dimen.thumbnail_height); 56 container.addView(iv, new LinearLayout.LayoutParams(w, h)); 57 58 iv.setOnClickListener(new View.OnClickListener() { 59 @Override 60 public void onClick(View v) { 61 if (task.id >= 0 && thumbs != null) { 62 if (subIndex < (thumbs.numSubThumbbails-1)) { 63 mAm.removeSubTask(task.id, subIndex+1); 64 } 65 mAm.moveTaskToFront(task.id, ActivityManager.MOVE_TASK_WITH_HOME); 66 } else { 67 try { 68 startActivity(task.baseIntent); 69 } catch (ActivityNotFoundException e) { 70 Log.w("foo", "Unable to start task: " + e); 71 } 72 } 73 buildUi(); 74 } 75 }); 76 iv.setOnLongClickListener(new View.OnLongClickListener() { 77 @Override 78 public boolean onLongClick(View v) { 79 if (task.id >= 0 && thumbs != null) { 80 if (subIndex < 0) { 81 mAm.removeTask(task.id, ActivityManager.REMOVE_TASK_KILL_PROCESS); 82 } else { 83 mAm.removeSubTask(task.id, subIndex); 84 } 85 buildUi(); 86 return true; 87 } 88 return false; 89 } 90 }); 91 } 92 93 @Override 94 protected void onCreate(Bundle savedInstanceState) { 95 super.onCreate(savedInstanceState); 96 97 mAm = (ActivityManager)getSystemService(ACTIVITY_SERVICE); 98 } 99 100 @Override 101 public boolean onCreateOptionsMenu(Menu menu) { 102 menu.add("Animate!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { 103 @Override public boolean onMenuItemClick(MenuItem item) { 104 AlertDialog.Builder builder = new AlertDialog.Builder(ActivityTestMain.this, 105 R.style.SlowDialog); 106 builder.setTitle("This is a title"); 107 builder.show(); 108 return true; 109 } 110 }); 111 menu.add("Bind!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { 112 @Override public boolean onMenuItemClick(MenuItem item) { 113 Intent intent = new Intent(ActivityTestMain.this, SingleUserService.class); 114 ServiceConnection conn = new ServiceConnection() { 115 @Override 116 public void onServiceConnected(ComponentName name, IBinder service) { 117 Log.i(TAG, "Service connected " + name + " " + service); 118 } 119 @Override 120 public void onServiceDisconnected(ComponentName name) { 121 Log.i(TAG, "Service disconnected " + name); 122 } 123 }; 124 bindService(intent, conn, Context.BIND_AUTO_CREATE); 125 return true; 126 } 127 }); 128 menu.add("Start!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { 129 @Override public boolean onMenuItemClick(MenuItem item) { 130 Intent intent = new Intent(ActivityTestMain.this, SingleUserService.class); 131 startService(intent); 132 return true; 133 } 134 }); 135 menu.add("Send!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { 136 @Override public boolean onMenuItemClick(MenuItem item) { 137 Intent intent = new Intent(ActivityTestMain.this, UserTarget.class); 138 sendBroadcastToUser(intent, 1); 139 return true; 140 } 141 }); 142 return true; 143 } 144 145 @Override 146 protected void onStart() { 147 super.onStart(); 148 buildUi(); 149 } 150 151 private View scrollWrap(View view) { 152 ScrollView scroller = new ScrollView(this); 153 scroller.addView(view, new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, 154 ScrollView.LayoutParams.MATCH_PARENT)); 155 return scroller; 156 } 157 158 private void buildUi() { 159 LinearLayout top = new LinearLayout(this); 160 top.setOrientation(LinearLayout.VERTICAL); 161 162 List<ActivityManager.RecentTaskInfo> recents = mAm.getRecentTasks(10, 163 ActivityManager.RECENT_WITH_EXCLUDED); 164 if (recents != null) { 165 for (int i=0; i<recents.size(); i++) { 166 ActivityManager.RecentTaskInfo r = recents.get(i); 167 ActivityManager.TaskThumbnails tt = mAm.getTaskThumbnails(r.persistentId); 168 TextView tv = new TextView(this); 169 tv.setText(r.baseIntent.getComponent().flattenToShortString()); 170 top.addView(tv, new LinearLayout.LayoutParams( 171 LinearLayout.LayoutParams.WRAP_CONTENT, 172 LinearLayout.LayoutParams.WRAP_CONTENT)); 173 LinearLayout item = new LinearLayout(this); 174 item.setOrientation(LinearLayout.HORIZONTAL); 175 addThumbnail(item, tt != null ? tt.mainThumbnail : null, r, tt, -1); 176 for (int j=0; j<tt.numSubThumbbails; j++) { 177 addThumbnail(item, tt.getSubThumbnail(j), r, tt, j); 178 } 179 top.addView(item, new LinearLayout.LayoutParams( 180 LinearLayout.LayoutParams.WRAP_CONTENT, 181 LinearLayout.LayoutParams.WRAP_CONTENT)); 182 } 183 } 184 185 setContentView(scrollWrap(top)); 186 } 187} 188