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 | |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 19 | import java.util.List; |
| 20 | |
| 21 | import android.app.Activity; |
| 22 | import android.app.ActivityManager; |
Dianne Hackborn | 8a59b7f | 2012-01-09 12:05:26 -0800 | [diff] [blame] | 23 | import android.app.AlertDialog; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 24 | import android.content.ActivityNotFoundException; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 25 | import android.content.BroadcastReceiver; |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 26 | import android.content.ComponentName; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 27 | import android.content.ContentProviderClient; |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 28 | import android.content.Intent; |
| 29 | import android.content.ServiceConnection; |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 30 | import android.os.Bundle; |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 31 | import android.os.IBinder; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 32 | import android.os.RemoteException; |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame^] | 33 | import android.os.UserHandle; |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 34 | import android.graphics.Bitmap; |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 35 | import android.widget.ImageView; |
| 36 | import android.widget.LinearLayout; |
| 37 | import android.widget.TextView; |
| 38 | import android.widget.ScrollView; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 39 | import android.widget.Toast; |
Dianne Hackborn | 8a59b7f | 2012-01-09 12:05:26 -0800 | [diff] [blame] | 40 | import android.view.Menu; |
| 41 | import android.view.MenuItem; |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 42 | import android.view.View; |
| 43 | import android.content.Context; |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 44 | import android.content.res.Configuration; |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 45 | import android.util.Log; |
| 46 | |
| 47 | public class ActivityTestMain extends Activity { |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 48 | static final String TAG = "ActivityTest"; |
| 49 | |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 50 | static final String KEY_CONFIGURATION = "configuration"; |
| 51 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 52 | ActivityManager mAm; |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 53 | Configuration mOverrideConfig; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 54 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 55 | class BroadcastResultReceiver extends BroadcastReceiver { |
| 56 | @Override |
| 57 | public void onReceive(Context context, Intent intent) { |
| 58 | Bundle res = getResultExtras(true); |
| 59 | int user = res.getInt("user", -1); |
| 60 | Toast.makeText(ActivityTestMain.this, |
| 61 | "Receiver executed as user " |
| 62 | + (user >= 0 ? Integer.toString(user) : "unknown"), |
| 63 | Toast.LENGTH_LONG).show(); |
| 64 | } |
| 65 | } |
| 66 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 67 | private void addThumbnail(LinearLayout container, Bitmap bm, |
| 68 | final ActivityManager.RecentTaskInfo task, |
| 69 | final ActivityManager.TaskThumbnails thumbs, final int subIndex) { |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 70 | ImageView iv = new ImageView(this); |
| 71 | if (bm != null) { |
| 72 | iv.setImageBitmap(bm); |
| 73 | } |
| 74 | iv.setBackgroundResource(android.R.drawable.gallery_thumb); |
| 75 | int w = getResources().getDimensionPixelSize(android.R.dimen.thumbnail_width); |
| 76 | int h = getResources().getDimensionPixelSize(android.R.dimen.thumbnail_height); |
| 77 | container.addView(iv, new LinearLayout.LayoutParams(w, h)); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 78 | |
| 79 | iv.setOnClickListener(new View.OnClickListener() { |
| 80 | @Override |
| 81 | public void onClick(View v) { |
| 82 | if (task.id >= 0 && thumbs != null) { |
| 83 | if (subIndex < (thumbs.numSubThumbbails-1)) { |
| 84 | mAm.removeSubTask(task.id, subIndex+1); |
| 85 | } |
| 86 | mAm.moveTaskToFront(task.id, ActivityManager.MOVE_TASK_WITH_HOME); |
| 87 | } else { |
| 88 | try { |
| 89 | startActivity(task.baseIntent); |
| 90 | } catch (ActivityNotFoundException e) { |
| 91 | Log.w("foo", "Unable to start task: " + e); |
| 92 | } |
| 93 | } |
| 94 | buildUi(); |
| 95 | } |
| 96 | }); |
| 97 | iv.setOnLongClickListener(new View.OnLongClickListener() { |
| 98 | @Override |
| 99 | public boolean onLongClick(View v) { |
| 100 | if (task.id >= 0 && thumbs != null) { |
| 101 | if (subIndex < 0) { |
| 102 | mAm.removeTask(task.id, ActivityManager.REMOVE_TASK_KILL_PROCESS); |
| 103 | } else { |
| 104 | mAm.removeSubTask(task.id, subIndex); |
| 105 | } |
| 106 | buildUi(); |
| 107 | return true; |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | }); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | protected void onCreate(Bundle savedInstanceState) { |
| 116 | super.onCreate(savedInstanceState); |
| 117 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 118 | mAm = (ActivityManager)getSystemService(ACTIVITY_SERVICE); |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 119 | if (savedInstanceState != null) { |
| 120 | mOverrideConfig = savedInstanceState.getParcelable(KEY_CONFIGURATION); |
| 121 | if (mOverrideConfig != null) { |
| 122 | applyOverrideConfiguration(mOverrideConfig); |
| 123 | } |
| 124 | } |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 125 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 126 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 127 | @Override |
Dianne Hackborn | 8a59b7f | 2012-01-09 12:05:26 -0800 | [diff] [blame] | 128 | public boolean onCreateOptionsMenu(Menu menu) { |
| 129 | menu.add("Animate!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 130 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 131 | AlertDialog.Builder builder = new AlertDialog.Builder(ActivityTestMain.this, |
| 132 | R.style.SlowDialog); |
| 133 | builder.setTitle("This is a title"); |
| 134 | builder.show(); |
| 135 | return true; |
| 136 | } |
| 137 | }); |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 138 | menu.add("Bind!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 139 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 140 | Intent intent = new Intent(ActivityTestMain.this, SingleUserService.class); |
| 141 | ServiceConnection conn = new ServiceConnection() { |
| 142 | @Override |
| 143 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 144 | Log.i(TAG, "Service connected " + name + " " + service); |
| 145 | } |
| 146 | @Override |
| 147 | public void onServiceDisconnected(ComponentName name) { |
| 148 | Log.i(TAG, "Service disconnected " + name); |
| 149 | } |
| 150 | }; |
| 151 | bindService(intent, conn, Context.BIND_AUTO_CREATE); |
| 152 | return true; |
| 153 | } |
| 154 | }); |
| 155 | menu.add("Start!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 156 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 157 | Intent intent = new Intent(ActivityTestMain.this, SingleUserService.class); |
| 158 | startService(intent); |
| 159 | return true; |
| 160 | } |
| 161 | }); |
| 162 | menu.add("Send!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 163 | @Override public boolean onMenuItemClick(MenuItem item) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 164 | Intent intent = new Intent(ActivityTestMain.this, SingleUserReceiver.class); |
| 165 | sendOrderedBroadcast(intent, null, new BroadcastResultReceiver(), |
| 166 | null, Activity.RESULT_OK, null, null); |
| 167 | return true; |
| 168 | } |
| 169 | }); |
| 170 | menu.add("Call!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 171 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 172 | ContentProviderClient cpl = getContentResolver().acquireContentProviderClient( |
| 173 | SingleUserProvider.AUTHORITY); |
| 174 | Bundle res = null; |
| 175 | try { |
| 176 | res = cpl.call("getuser", null, null); |
| 177 | } catch (RemoteException e) { |
| 178 | } |
| 179 | int user = res != null ? res.getInt("user", -1) : -1; |
| 180 | Toast.makeText(ActivityTestMain.this, |
| 181 | "Provider executed as user " |
| 182 | + (user >= 0 ? Integer.toString(user) : "unknown"), |
| 183 | Toast.LENGTH_LONG).show(); |
| 184 | cpl.release(); |
| 185 | return true; |
| 186 | } |
| 187 | }); |
| 188 | menu.add("Send to user 1!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 189 | @Override public boolean onMenuItemClick(MenuItem item) { |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 190 | Intent intent = new Intent(ActivityTestMain.this, UserTarget.class); |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame^] | 191 | sendOrderedBroadcastAsUser(intent, new UserHandle(1), new BroadcastResultReceiver(), |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 192 | null, Activity.RESULT_OK, null, null); |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 193 | return true; |
| 194 | } |
| 195 | }); |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 196 | menu.add("Density!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 197 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 198 | if (mOverrideConfig == null) { |
| 199 | mOverrideConfig = new Configuration(); |
| 200 | } |
| 201 | if (mOverrideConfig.densityDpi == Configuration.DENSITY_DPI_UNDEFINED) { |
| 202 | mOverrideConfig.densityDpi = (getApplicationContext().getResources() |
| 203 | .getConfiguration().densityDpi*2)/3; |
| 204 | } else { |
| 205 | mOverrideConfig.densityDpi = Configuration.DENSITY_DPI_UNDEFINED; |
| 206 | } |
| 207 | recreate(); |
| 208 | return true; |
| 209 | } |
| 210 | }); |
Dianne Hackborn | 8a59b7f | 2012-01-09 12:05:26 -0800 | [diff] [blame] | 211 | return true; |
| 212 | } |
| 213 | |
| 214 | @Override |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 215 | protected void onStart() { |
| 216 | super.onStart(); |
| 217 | buildUi(); |
| 218 | } |
| 219 | |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 220 | @Override |
| 221 | protected void onSaveInstanceState(Bundle outState) { |
| 222 | super.onSaveInstanceState(outState); |
| 223 | if (mOverrideConfig != null) { |
| 224 | outState.putParcelable(KEY_CONFIGURATION, mOverrideConfig); |
| 225 | } |
| 226 | } |
| 227 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 228 | private View scrollWrap(View view) { |
| 229 | ScrollView scroller = new ScrollView(this); |
| 230 | scroller.addView(view, new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, |
| 231 | ScrollView.LayoutParams.MATCH_PARENT)); |
| 232 | return scroller; |
| 233 | } |
| 234 | |
| 235 | private void buildUi() { |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 236 | LinearLayout top = new LinearLayout(this); |
| 237 | top.setOrientation(LinearLayout.VERTICAL); |
| 238 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 239 | List<ActivityManager.RecentTaskInfo> recents = mAm.getRecentTasks(10, |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 240 | ActivityManager.RECENT_WITH_EXCLUDED); |
| 241 | if (recents != null) { |
| 242 | for (int i=0; i<recents.size(); i++) { |
| 243 | ActivityManager.RecentTaskInfo r = recents.get(i); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 244 | ActivityManager.TaskThumbnails tt = mAm.getTaskThumbnails(r.persistentId); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 245 | TextView tv = new TextView(this); |
| 246 | tv.setText(r.baseIntent.getComponent().flattenToShortString()); |
| 247 | top.addView(tv, new LinearLayout.LayoutParams( |
| 248 | LinearLayout.LayoutParams.WRAP_CONTENT, |
| 249 | LinearLayout.LayoutParams.WRAP_CONTENT)); |
| 250 | LinearLayout item = new LinearLayout(this); |
| 251 | item.setOrientation(LinearLayout.HORIZONTAL); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 252 | addThumbnail(item, tt != null ? tt.mainThumbnail : null, r, tt, -1); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 253 | for (int j=0; j<tt.numSubThumbbails; j++) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 254 | addThumbnail(item, tt.getSubThumbnail(j), r, tt, j); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 255 | } |
| 256 | top.addView(item, new LinearLayout.LayoutParams( |
| 257 | LinearLayout.LayoutParams.WRAP_CONTENT, |
| 258 | LinearLayout.LayoutParams.WRAP_CONTENT)); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | setContentView(scrollWrap(top)); |
| 263 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 264 | } |