blob: 9599b19fb708627049b23a3ac8fc0db902893406 [file] [log] [blame]
Dianne Hackbornf26fd992011-04-08 18:14:09 -07001/*
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
Dianne Hackbornf26fd992011-04-08 18:14:09 -070019import java.util.List;
20
21import android.app.Activity;
22import android.app.ActivityManager;
Dianne Hackborn8a59b7f2012-01-09 12:05:26 -080023import android.app.AlertDialog;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070024import android.content.ActivityNotFoundException;
Dianne Hackborn7d19e022012-08-07 19:12:33 -070025import android.content.BroadcastReceiver;
Dianne Hackbornb4163a62012-08-02 18:31:26 -070026import android.content.ComponentName;
Dianne Hackborn7d19e022012-08-07 19:12:33 -070027import android.content.ContentProviderClient;
Dianne Hackbornb4163a62012-08-02 18:31:26 -070028import android.content.Intent;
29import android.content.ServiceConnection;
Dianne Hackbornf26fd992011-04-08 18:14:09 -070030import android.os.Bundle;
Dianne Hackbornb4163a62012-08-02 18:31:26 -070031import android.os.IBinder;
Dianne Hackborn7d19e022012-08-07 19:12:33 -070032import android.os.RemoteException;
Dianne Hackborn79af1dd2012-08-16 16:42:52 -070033import android.os.UserHandle;
Dianne Hackbornf26fd992011-04-08 18:14:09 -070034import android.graphics.Bitmap;
Dianne Hackbornf26fd992011-04-08 18:14:09 -070035import android.widget.ImageView;
36import android.widget.LinearLayout;
37import android.widget.TextView;
38import android.widget.ScrollView;
Dianne Hackborn7d19e022012-08-07 19:12:33 -070039import android.widget.Toast;
Dianne Hackborn8a59b7f2012-01-09 12:05:26 -080040import android.view.Menu;
41import android.view.MenuItem;
Dianne Hackbornf26fd992011-04-08 18:14:09 -070042import android.view.View;
43import android.content.Context;
Dianne Hackborn756220b2012-08-14 16:45:30 -070044import android.content.res.Configuration;
Dianne Hackbornf26fd992011-04-08 18:14:09 -070045import android.util.Log;
46
47public class ActivityTestMain extends Activity {
Dianne Hackbornb4163a62012-08-02 18:31:26 -070048 static final String TAG = "ActivityTest";
49
Dianne Hackborn756220b2012-08-14 16:45:30 -070050 static final String KEY_CONFIGURATION = "configuration";
51
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070052 ActivityManager mAm;
Dianne Hackborn756220b2012-08-14 16:45:30 -070053 Configuration mOverrideConfig;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070054
Dianne Hackborn7d19e022012-08-07 19:12:33 -070055 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 Hackborn0c5001d2011-04-12 18:16:08 -070067 private void addThumbnail(LinearLayout container, Bitmap bm,
68 final ActivityManager.RecentTaskInfo task,
69 final ActivityManager.TaskThumbnails thumbs, final int subIndex) {
Dianne Hackbornf26fd992011-04-08 18:14:09 -070070 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 Hackborn0c5001d2011-04-12 18:16:08 -070078
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 Hackbornf26fd992011-04-08 18:14:09 -0700112 }
113
114 @Override
115 protected void onCreate(Bundle savedInstanceState) {
116 super.onCreate(savedInstanceState);
117
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700118 mAm = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
Dianne Hackborn756220b2012-08-14 16:45:30 -0700119 if (savedInstanceState != null) {
120 mOverrideConfig = savedInstanceState.getParcelable(KEY_CONFIGURATION);
121 if (mOverrideConfig != null) {
122 applyOverrideConfiguration(mOverrideConfig);
123 }
124 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700125 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700126
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700127 @Override
Dianne Hackborn8a59b7f2012-01-09 12:05:26 -0800128 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 Hackbornb4163a62012-08-02 18:31:26 -0700138 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 Hackborn7d19e022012-08-07 19:12:33 -0700164 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 Hackbornb4163a62012-08-02 18:31:26 -0700190 Intent intent = new Intent(ActivityTestMain.this, UserTarget.class);
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700191 sendOrderedBroadcastAsUser(intent, new UserHandle(1), new BroadcastResultReceiver(),
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700192 null, Activity.RESULT_OK, null, null);
Dianne Hackbornb4163a62012-08-02 18:31:26 -0700193 return true;
194 }
195 });
Dianne Hackborn756220b2012-08-14 16:45:30 -0700196 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 Hackborn8a59b7f2012-01-09 12:05:26 -0800211 return true;
212 }
213
214 @Override
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700215 protected void onStart() {
216 super.onStart();
217 buildUi();
218 }
219
Dianne Hackborn756220b2012-08-14 16:45:30 -0700220 @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 Hackborn0c5001d2011-04-12 18:16:08 -0700228 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 Hackbornf26fd992011-04-08 18:14:09 -0700236 LinearLayout top = new LinearLayout(this);
237 top.setOrientation(LinearLayout.VERTICAL);
238
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700239 List<ActivityManager.RecentTaskInfo> recents = mAm.getRecentTasks(10,
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700240 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 Hackborn0c5001d2011-04-12 18:16:08 -0700244 ActivityManager.TaskThumbnails tt = mAm.getTaskThumbnails(r.persistentId);
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700245 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 Hackborn0c5001d2011-04-12 18:16:08 -0700252 addThumbnail(item, tt != null ? tt.mainThumbnail : null, r, tt, -1);
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700253 for (int j=0; j<tt.numSubThumbbails; j++) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700254 addThumbnail(item, tt.getSubThumbnail(j), r, tt, j);
Dianne Hackbornf26fd992011-04-08 18:14:09 -0700255 }
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 Hackbornf26fd992011-04-08 18:14:09 -0700264}