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 | 3992645 | 2012-09-25 14:53:52 -0700 | [diff] [blame] | 19 | import java.util.ArrayList; |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 20 | import java.util.List; |
| 21 | |
| 22 | import android.app.Activity; |
| 23 | import android.app.ActivityManager; |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 24 | import android.app.ActivityOptions; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 25 | import android.app.AlarmManager; |
Dianne Hackborn | 8a59b7f | 2012-01-09 12:05:26 -0800 | [diff] [blame] | 26 | import android.app.AlertDialog; |
Dianne Hackborn | b5a380d | 2015-05-20 18:18:46 -0700 | [diff] [blame] | 27 | import android.app.PendingIntent; |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 28 | import android.content.ActivityNotFoundException; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 29 | import android.content.BroadcastReceiver; |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 30 | import android.content.ComponentName; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 31 | import android.content.ContentProviderClient; |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 32 | import android.content.Intent; |
| 33 | import android.content.ServiceConnection; |
Dianne Hackborn | aec68bb | 2014-08-20 15:25:13 -0700 | [diff] [blame] | 34 | import android.graphics.BitmapFactory; |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 35 | import android.net.Uri; |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 36 | import android.os.Bundle; |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 37 | import android.os.Handler; |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 38 | import android.os.IBinder; |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 39 | import android.os.Message; |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 40 | import android.os.PowerManager; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 41 | import android.os.RemoteException; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 42 | import android.os.SystemClock; |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 43 | import android.os.UserHandle; |
Dianne Hackborn | 3992645 | 2012-09-25 14:53:52 -0700 | [diff] [blame] | 44 | import android.os.UserManager; |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 45 | import android.graphics.Bitmap; |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 46 | import android.provider.Settings; |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 47 | import android.widget.ImageView; |
| 48 | import android.widget.LinearLayout; |
| 49 | import android.widget.TextView; |
| 50 | import android.widget.ScrollView; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 51 | import android.widget.Toast; |
Dianne Hackborn | 8a59b7f | 2012-01-09 12:05:26 -0800 | [diff] [blame] | 52 | import android.view.Menu; |
| 53 | import android.view.MenuItem; |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 54 | import android.view.View; |
| 55 | import android.content.Context; |
Dianne Hackborn | 3992645 | 2012-09-25 14:53:52 -0700 | [diff] [blame] | 56 | import android.content.pm.UserInfo; |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 57 | import android.content.res.Configuration; |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 58 | import android.util.Log; |
| 59 | |
Xiaohui Chen | e4de5a0 | 2015-09-22 15:33:31 -0700 | [diff] [blame] | 60 | |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 61 | public class ActivityTestMain extends Activity { |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 62 | static final String TAG = "ActivityTest"; |
| 63 | |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 64 | static final String KEY_CONFIGURATION = "configuration"; |
| 65 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 66 | ActivityManager mAm; |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 67 | PowerManager mPower; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 68 | AlarmManager mAlarm; |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 69 | Configuration mOverrideConfig; |
Dianne Hackborn | 3992645 | 2012-09-25 14:53:52 -0700 | [diff] [blame] | 70 | int mSecondUser; |
| 71 | |
| 72 | ArrayList<ServiceConnection> mConnections = new ArrayList<ServiceConnection>(); |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 73 | |
Dianne Hackborn | 875e464 | 2014-12-01 16:22:14 -0800 | [diff] [blame] | 74 | ServiceConnection mIsolatedConnection; |
| 75 | |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 76 | static final int MSG_SPAM = 1; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 77 | static final int MSG_SPAM_ALARM = 2; |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 78 | |
| 79 | final Handler mHandler = new Handler() { |
| 80 | @Override |
| 81 | public void handleMessage(Message msg) { |
| 82 | switch (msg.what) { |
| 83 | case MSG_SPAM: { |
| 84 | boolean fg = msg.arg1 != 0; |
| 85 | Intent intent = new Intent(ActivityTestMain.this, SpamActivity.class); |
| 86 | Bundle options = null; |
| 87 | if (fg) { |
Craig Mautner | 0321573 | 2014-09-09 08:53:56 -0700 | [diff] [blame] | 88 | ActivityOptions opts = ActivityOptions.makeTaskLaunchBehind(); |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 89 | options = opts.toBundle(); |
| 90 | } |
| 91 | startActivity(intent, options); |
| 92 | scheduleSpam(!fg); |
| 93 | } break; |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 94 | case MSG_SPAM_ALARM: { |
| 95 | long when = SystemClock.elapsedRealtime(); |
| 96 | Intent intent = new Intent(ActivityTestMain.this, AlarmSpamReceiver.class); |
| 97 | intent.setAction("com.example.SPAM_ALARM=" + when); |
| 98 | PendingIntent pi = PendingIntent.getBroadcast(ActivityTestMain.this, |
| 99 | 0, intent, 0); |
| 100 | mAlarm.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME, when+(30*1000), pi); |
| 101 | scheduleSpamAlarm(30*1000); |
| 102 | } break; |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 103 | } |
| 104 | super.handleMessage(msg); |
| 105 | } |
| 106 | }; |
| 107 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 108 | class BroadcastResultReceiver extends BroadcastReceiver { |
| 109 | @Override |
| 110 | public void onReceive(Context context, Intent intent) { |
| 111 | Bundle res = getResultExtras(true); |
| 112 | int user = res.getInt("user", -1); |
| 113 | Toast.makeText(ActivityTestMain.this, |
| 114 | "Receiver executed as user " |
| 115 | + (user >= 0 ? Integer.toString(user) : "unknown"), |
| 116 | Toast.LENGTH_LONG).show(); |
| 117 | } |
| 118 | } |
| 119 | |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 120 | @Override |
| 121 | protected void onCreate(Bundle savedInstanceState) { |
| 122 | super.onCreate(savedInstanceState); |
| 123 | |
Dianne Hackborn | 85d558c | 2014-11-04 10:31:54 -0800 | [diff] [blame] | 124 | Log.i(TAG, "Referrer: " + getReferrer()); |
| 125 | |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 126 | mAm = getSystemService(ActivityManager.class); |
| 127 | mPower = getSystemService(PowerManager.class); |
| 128 | mAlarm = getSystemService(AlarmManager.class); |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 129 | if (savedInstanceState != null) { |
| 130 | mOverrideConfig = savedInstanceState.getParcelable(KEY_CONFIGURATION); |
| 131 | if (mOverrideConfig != null) { |
| 132 | applyOverrideConfiguration(mOverrideConfig); |
| 133 | } |
| 134 | } |
Dianne Hackborn | 3992645 | 2012-09-25 14:53:52 -0700 | [diff] [blame] | 135 | |
| 136 | UserManager um = (UserManager)getSystemService(Context.USER_SERVICE); |
| 137 | List<UserInfo> users = um.getUsers(); |
| 138 | mSecondUser = Integer.MAX_VALUE; |
| 139 | for (UserInfo ui : users) { |
| 140 | if (ui.id != 0 && mSecondUser > ui.id) { |
| 141 | mSecondUser = ui.id; |
| 142 | } |
| 143 | } |
Dianne Hackborn | 15ed275 | 2014-08-18 15:08:33 -0700 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | AlertDialog ad = new AlertDialog.Builder(this).setTitle("title").setMessage("message").create(); |
| 147 | ad.getWindow().getAttributes().type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR; |
| 148 | ad.show(); |
| 149 | */ |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 150 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 151 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 152 | @Override |
Dianne Hackborn | 8a59b7f | 2012-01-09 12:05:26 -0800 | [diff] [blame] | 153 | public boolean onCreateOptionsMenu(Menu menu) { |
| 154 | menu.add("Animate!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 155 | @Override |
| 156 | public boolean onMenuItemClick(MenuItem item) { |
Dianne Hackborn | 8a59b7f | 2012-01-09 12:05:26 -0800 | [diff] [blame] | 157 | AlertDialog.Builder builder = new AlertDialog.Builder(ActivityTestMain.this, |
| 158 | R.style.SlowDialog); |
| 159 | builder.setTitle("This is a title"); |
| 160 | builder.show(); |
| 161 | return true; |
| 162 | } |
| 163 | }); |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 164 | menu.add("Bind!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 165 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 166 | Intent intent = new Intent(ActivityTestMain.this, SingleUserService.class); |
| 167 | ServiceConnection conn = new ServiceConnection() { |
| 168 | @Override |
| 169 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 170 | Log.i(TAG, "Service connected " + name + " " + service); |
| 171 | } |
| 172 | @Override |
| 173 | public void onServiceDisconnected(ComponentName name) { |
| 174 | Log.i(TAG, "Service disconnected " + name); |
| 175 | } |
| 176 | }; |
Dianne Hackborn | 3992645 | 2012-09-25 14:53:52 -0700 | [diff] [blame] | 177 | if (bindService(intent, conn, Context.BIND_AUTO_CREATE)) { |
| 178 | mConnections.add(conn); |
| 179 | } else { |
| 180 | Toast.makeText(ActivityTestMain.this, "Failed to bind", |
| 181 | Toast.LENGTH_LONG).show(); |
| 182 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 183 | return true; |
| 184 | } |
| 185 | }); |
| 186 | menu.add("Start!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 187 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 188 | Intent intent = new Intent(ActivityTestMain.this, SingleUserService.class); |
| 189 | startService(intent); |
| 190 | return true; |
| 191 | } |
| 192 | }); |
Dianne Hackborn | 875e464 | 2014-12-01 16:22:14 -0800 | [diff] [blame] | 193 | menu.add("Rebind Isolated!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 194 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 195 | Intent intent = new Intent(ActivityTestMain.this, IsolatedService.class); |
| 196 | ServiceConnection conn = new ServiceConnection() { |
| 197 | @Override |
| 198 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 199 | Log.i(TAG, "Isolated service connected " + name + " " + service); |
| 200 | } |
| 201 | @Override |
| 202 | public void onServiceDisconnected(ComponentName name) { |
| 203 | Log.i(TAG, "Isolated service disconnected " + name); |
| 204 | } |
| 205 | }; |
| 206 | if (mIsolatedConnection != null) { |
| 207 | Log.i(TAG, "Unbinding existing service: " + mIsolatedConnection); |
| 208 | unbindService(mIsolatedConnection); |
| 209 | mIsolatedConnection = null; |
| 210 | } |
| 211 | Log.i(TAG, "Binding new service: " + conn); |
| 212 | if (bindService(intent, conn, Context.BIND_AUTO_CREATE)) { |
| 213 | mIsolatedConnection = conn; |
| 214 | } else { |
| 215 | Toast.makeText(ActivityTestMain.this, "Failed to bind", |
| 216 | Toast.LENGTH_LONG).show(); |
| 217 | } |
| 218 | return true; |
| 219 | } |
| 220 | }); |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 221 | menu.add("Send!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 222 | @Override public boolean onMenuItemClick(MenuItem item) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 223 | Intent intent = new Intent(ActivityTestMain.this, SingleUserReceiver.class); |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 224 | sendOrderedBroadcast(intent, null, new BroadcastResultReceiver(), |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 225 | null, Activity.RESULT_OK, null, null); |
| 226 | return true; |
| 227 | } |
| 228 | }); |
| 229 | menu.add("Call!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 230 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 231 | ContentProviderClient cpl = getContentResolver().acquireContentProviderClient( |
| 232 | SingleUserProvider.AUTHORITY); |
| 233 | Bundle res = null; |
| 234 | try { |
| 235 | res = cpl.call("getuser", null, null); |
| 236 | } catch (RemoteException e) { |
| 237 | } |
| 238 | int user = res != null ? res.getInt("user", -1) : -1; |
| 239 | Toast.makeText(ActivityTestMain.this, |
| 240 | "Provider executed as user " |
| 241 | + (user >= 0 ? Integer.toString(user) : "unknown"), |
| 242 | Toast.LENGTH_LONG).show(); |
| 243 | cpl.release(); |
| 244 | return true; |
| 245 | } |
| 246 | }); |
Dianne Hackborn | 3992645 | 2012-09-25 14:53:52 -0700 | [diff] [blame] | 247 | menu.add("Send to user 0!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 248 | @Override public boolean onMenuItemClick(MenuItem item) { |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 249 | Intent intent = new Intent(ActivityTestMain.this, UserTarget.class); |
Dianne Hackborn | 3992645 | 2012-09-25 14:53:52 -0700 | [diff] [blame] | 250 | sendOrderedBroadcastAsUser(intent, new UserHandle(0), null, |
| 251 | new BroadcastResultReceiver(), |
| 252 | null, Activity.RESULT_OK, null, null); |
| 253 | return true; |
| 254 | } |
| 255 | }); |
| 256 | menu.add("Send to user " + mSecondUser + "!").setOnMenuItemClickListener( |
| 257 | new MenuItem.OnMenuItemClickListener() { |
| 258 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 259 | Intent intent = new Intent(ActivityTestMain.this, UserTarget.class); |
| 260 | sendOrderedBroadcastAsUser(intent, new UserHandle(mSecondUser), null, |
Dianne Hackborn | aec68bb | 2014-08-20 15:25:13 -0700 | [diff] [blame] | 261 | new BroadcastResultReceiver(), |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 262 | null, Activity.RESULT_OK, null, null); |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 263 | return true; |
| 264 | } |
| 265 | }); |
Dianne Hackborn | 3992645 | 2012-09-25 14:53:52 -0700 | [diff] [blame] | 266 | menu.add("Bind to user 0!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 267 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 268 | Intent intent = new Intent(ActivityTestMain.this, ServiceUserTarget.class); |
| 269 | ServiceConnection conn = new ServiceConnection() { |
| 270 | @Override |
| 271 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 272 | Log.i(TAG, "Service connected " + name + " " + service); |
| 273 | } |
| 274 | @Override |
| 275 | public void onServiceDisconnected(ComponentName name) { |
| 276 | Log.i(TAG, "Service disconnected " + name); |
| 277 | } |
| 278 | }; |
Xiaohui Chen | e4de5a0 | 2015-09-22 15:33:31 -0700 | [diff] [blame] | 279 | if (bindServiceAsUser(intent, conn, Context.BIND_AUTO_CREATE, UserHandle.SYSTEM)) { |
Dianne Hackborn | 3992645 | 2012-09-25 14:53:52 -0700 | [diff] [blame] | 280 | mConnections.add(conn); |
| 281 | } else { |
| 282 | Toast.makeText(ActivityTestMain.this, "Failed to bind", |
| 283 | Toast.LENGTH_LONG).show(); |
| 284 | } |
| 285 | return true; |
| 286 | } |
| 287 | }); |
| 288 | menu.add("Bind to user " + mSecondUser + "!").setOnMenuItemClickListener( |
| 289 | new MenuItem.OnMenuItemClickListener() { |
| 290 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 291 | Intent intent = new Intent(ActivityTestMain.this, ServiceUserTarget.class); |
| 292 | ServiceConnection conn = new ServiceConnection() { |
| 293 | @Override |
| 294 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 295 | Log.i(TAG, "Service connected " + name + " " + service); |
| 296 | } |
| 297 | @Override |
| 298 | public void onServiceDisconnected(ComponentName name) { |
| 299 | Log.i(TAG, "Service disconnected " + name); |
| 300 | } |
| 301 | }; |
Amith Yamasani | 45c0924 | 2013-01-16 16:04:00 -0800 | [diff] [blame] | 302 | if (bindServiceAsUser(intent, conn, Context.BIND_AUTO_CREATE, |
| 303 | new UserHandle(mSecondUser))) { |
Dianne Hackborn | 3992645 | 2012-09-25 14:53:52 -0700 | [diff] [blame] | 304 | mConnections.add(conn); |
| 305 | } else { |
| 306 | Toast.makeText(ActivityTestMain.this, "Failed to bind", |
| 307 | Toast.LENGTH_LONG).show(); |
| 308 | } |
| 309 | return true; |
| 310 | } |
| 311 | }); |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 312 | menu.add("Density!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 313 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 314 | if (mOverrideConfig == null) { |
| 315 | mOverrideConfig = new Configuration(); |
| 316 | } |
| 317 | if (mOverrideConfig.densityDpi == Configuration.DENSITY_DPI_UNDEFINED) { |
| 318 | mOverrideConfig.densityDpi = (getApplicationContext().getResources() |
| 319 | .getConfiguration().densityDpi*2)/3; |
| 320 | } else { |
| 321 | mOverrideConfig.densityDpi = Configuration.DENSITY_DPI_UNDEFINED; |
| 322 | } |
| 323 | recreate(); |
| 324 | return true; |
| 325 | } |
| 326 | }); |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 327 | menu.add("HashArray").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 328 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 329 | ArrayMapTests.run(); |
| 330 | return true; |
| 331 | } |
| 332 | }); |
Dianne Hackborn | aec68bb | 2014-08-20 15:25:13 -0700 | [diff] [blame] | 333 | menu.add("Add App Recent").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 334 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 335 | addAppRecents(1); |
| 336 | return true; |
| 337 | } |
| 338 | }); |
| 339 | menu.add("Add App 10x Recent").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 340 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 341 | addAppRecents(10); |
| 342 | return true; |
| 343 | } |
| 344 | }); |
| 345 | menu.add("Exclude!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 346 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 347 | setExclude(true); |
| 348 | return true; |
| 349 | } |
| 350 | }); |
| 351 | menu.add("Include!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 352 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 353 | setExclude(false); |
| 354 | return true; |
| 355 | } |
| 356 | }); |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 357 | menu.add("Open Doc").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 358 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 359 | ActivityManager.AppTask task = findDocTask(); |
| 360 | if (task == null) { |
| 361 | Intent intent = new Intent(ActivityTestMain.this, DocActivity.class); |
| 362 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT |
| 363 | | Intent.FLAG_ACTIVITY_MULTIPLE_TASK |
| 364 | | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS); |
| 365 | startActivity(intent); |
| 366 | } else { |
| 367 | task.moveToFront(); |
| 368 | } |
| 369 | return true; |
| 370 | } |
| 371 | }); |
| 372 | menu.add("Stack Doc").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 373 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 374 | ActivityManager.AppTask task = findDocTask(); |
| 375 | if (task != null) { |
| 376 | ActivityManager.RecentTaskInfo recent = task.getTaskInfo(); |
| 377 | Intent intent = new Intent(ActivityTestMain.this, DocActivity.class); |
| 378 | if (recent.id >= 0) { |
| 379 | // Stack on top. |
| 380 | intent.putExtra(DocActivity.LABEL, "Stacked"); |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 381 | } else { |
| 382 | // Start root activity. |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 383 | intent.putExtra(DocActivity.LABEL, "New Root"); |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 384 | } |
Dianne Hackborn | d7c9289 | 2014-08-27 16:44:24 -0700 | [diff] [blame] | 385 | task.startActivity(ActivityTestMain.this, intent, null); |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 386 | } |
| 387 | return true; |
| 388 | } |
| 389 | }); |
| 390 | menu.add("Spam!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 391 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 392 | scheduleSpam(false); |
| 393 | return true; |
| 394 | } |
| 395 | }); |
Dianne Hackborn | b5a380d | 2015-05-20 18:18:46 -0700 | [diff] [blame] | 396 | menu.add("Track time").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 397 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 398 | Intent intent = new Intent(Intent.ACTION_SEND); |
| 399 | intent.setType("text/plain"); |
| 400 | intent.putExtra(Intent.EXTRA_TEXT, "We are sharing this with you!"); |
| 401 | ActivityOptions options = ActivityOptions.makeBasic(); |
| 402 | Intent receiveIntent = new Intent(ActivityTestMain.this, TrackTimeReceiver.class); |
| 403 | receiveIntent.putExtra("something", "yeah, this is us!"); |
| 404 | options.requestUsageTimeReport(PendingIntent.getBroadcast(ActivityTestMain.this, |
| 405 | 0, receiveIntent, PendingIntent.FLAG_CANCEL_CURRENT)); |
| 406 | startActivity(Intent.createChooser(intent, "Who do you love?"), options.toBundle()); |
| 407 | return true; |
| 408 | } |
| 409 | }); |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 410 | menu.add("Transaction fail").setOnMenuItemClickListener( |
| 411 | new MenuItem.OnMenuItemClickListener() { |
| 412 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 413 | Intent intent = new Intent(Intent.ACTION_MAIN); |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 414 | intent.putExtra("gulp", new int[1024 * 1024]); |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 415 | startActivity(intent); |
| 416 | return true; |
| 417 | } |
| 418 | }); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 419 | menu.add("Spam idle alarm").setOnMenuItemClickListener( |
| 420 | new MenuItem.OnMenuItemClickListener() { |
| 421 | @Override public boolean onMenuItemClick(MenuItem item) { |
| 422 | scheduleSpamAlarm(0); |
| 423 | return true; |
| 424 | } |
| 425 | }); |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 426 | menu.add("Ignore battery optimizations").setOnMenuItemClickListener( |
| 427 | new MenuItem.OnMenuItemClickListener() { |
| 428 | @Override public boolean onMenuItemClick(MenuItem item) { |
Dianne Hackborn | c7b57b1 | 2015-08-10 14:52:52 -0700 | [diff] [blame] | 429 | Intent intent; |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 430 | if (!mPower.isIgnoringBatteryOptimizations(getPackageName())) { |
Dianne Hackborn | c7b57b1 | 2015-08-10 14:52:52 -0700 | [diff] [blame] | 431 | intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 432 | intent.setData(Uri.fromParts("package", getPackageName(), null)); |
Dianne Hackborn | c7b57b1 | 2015-08-10 14:52:52 -0700 | [diff] [blame] | 433 | } else { |
| 434 | intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); |
Dianne Hackborn | 1958e5e | 2015-06-12 18:11:41 -0700 | [diff] [blame] | 435 | } |
| 436 | startActivity(intent); |
| 437 | return true; |
| 438 | } |
| 439 | }); |
Dianne Hackborn | 8a59b7f | 2012-01-09 12:05:26 -0800 | [diff] [blame] | 440 | return true; |
| 441 | } |
| 442 | |
| 443 | @Override |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 444 | protected void onStart() { |
| 445 | super.onStart(); |
| 446 | buildUi(); |
| 447 | } |
| 448 | |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 449 | @Override |
Dianne Hackborn | a4e102e | 2014-09-04 22:52:27 -0700 | [diff] [blame] | 450 | protected void onPause() { |
| 451 | super.onPause(); |
| 452 | Log.i(TAG, "I'm such a slooow poor loser"); |
| 453 | try { |
| 454 | Thread.sleep(500); |
| 455 | } catch (InterruptedException e) { |
| 456 | } |
| 457 | Log.i(TAG, "See?"); |
| 458 | } |
| 459 | |
| 460 | @Override |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 461 | protected void onSaveInstanceState(Bundle outState) { |
| 462 | super.onSaveInstanceState(outState); |
| 463 | if (mOverrideConfig != null) { |
| 464 | outState.putParcelable(KEY_CONFIGURATION, mOverrideConfig); |
| 465 | } |
| 466 | } |
| 467 | |
Dianne Hackborn | 3992645 | 2012-09-25 14:53:52 -0700 | [diff] [blame] | 468 | @Override |
| 469 | protected void onStop() { |
| 470 | super.onStop(); |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 471 | mHandler.removeMessages(MSG_SPAM_ALARM); |
Dianne Hackborn | 3992645 | 2012-09-25 14:53:52 -0700 | [diff] [blame] | 472 | for (ServiceConnection conn : mConnections) { |
| 473 | unbindService(conn); |
| 474 | } |
| 475 | mConnections.clear(); |
Dianne Hackborn | 875e464 | 2014-12-01 16:22:14 -0800 | [diff] [blame] | 476 | if (mIsolatedConnection != null) { |
| 477 | unbindService(mIsolatedConnection); |
| 478 | mIsolatedConnection = null; |
| 479 | } |
Dianne Hackborn | 3992645 | 2012-09-25 14:53:52 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 482 | @Override |
| 483 | protected void onDestroy() { |
| 484 | super.onDestroy(); |
| 485 | mHandler.removeMessages(MSG_SPAM); |
| 486 | } |
| 487 | |
Dianne Hackborn | aec68bb | 2014-08-20 15:25:13 -0700 | [diff] [blame] | 488 | void addAppRecents(int count) { |
| 489 | ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); |
| 490 | Intent intent = new Intent(Intent.ACTION_MAIN); |
| 491 | intent.addCategory(Intent.CATEGORY_LAUNCHER); |
| 492 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); |
| 493 | intent.setComponent(new ComponentName(this, ActivityTestMain.class)); |
| 494 | for (int i=0; i<count; i++) { |
| 495 | ActivityManager.TaskDescription desc = new ActivityManager.TaskDescription(); |
| 496 | desc.setLabel("Added #" + i); |
| 497 | Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon); |
| 498 | if ((i&1) == 0) { |
| 499 | desc.setIcon(bitmap); |
| 500 | } |
| 501 | int taskId = am.addAppTask(this, intent, desc, bitmap); |
| 502 | Log.i(TAG, "Added new task id #" + taskId); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | void setExclude(boolean exclude) { |
| 507 | ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); |
| 508 | List<ActivityManager.AppTask> tasks = am.getAppTasks(); |
| 509 | int taskId = getTaskId(); |
| 510 | for (int i=0; i<tasks.size(); i++) { |
| 511 | ActivityManager.AppTask task = tasks.get(i); |
| 512 | if (task.getTaskInfo().id == taskId) { |
| 513 | task.setExcludeFromRecents(exclude); |
| 514 | } |
| 515 | } |
| 516 | } |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 517 | |
| 518 | ActivityManager.AppTask findDocTask() { |
| 519 | ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); |
| 520 | List<ActivityManager.AppTask> tasks = am.getAppTasks(); |
| 521 | if (tasks != null) { |
| 522 | for (int i=0; i<tasks.size(); i++) { |
| 523 | ActivityManager.AppTask task = tasks.get(i); |
| 524 | ActivityManager.RecentTaskInfo recent = task.getTaskInfo(); |
| 525 | if (recent.baseIntent != null |
| 526 | && recent.baseIntent.getComponent().getClassName().equals( |
| 527 | DocActivity.class.getCanonicalName())) { |
| 528 | return task; |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | return null; |
| 533 | } |
| 534 | |
| 535 | void scheduleSpam(boolean fg) { |
| 536 | mHandler.removeMessages(MSG_SPAM); |
| 537 | Message msg = mHandler.obtainMessage(MSG_SPAM, fg ? 1 : 0, 0); |
| 538 | mHandler.sendMessageDelayed(msg, 500); |
| 539 | } |
| 540 | |
Dianne Hackborn | 3d1933c4 | 2015-06-10 16:25:57 -0700 | [diff] [blame] | 541 | void scheduleSpamAlarm(long delay) { |
| 542 | mHandler.removeMessages(MSG_SPAM_ALARM); |
| 543 | Message msg = mHandler.obtainMessage(MSG_SPAM_ALARM); |
| 544 | mHandler.sendMessageDelayed(msg, delay); |
| 545 | } |
| 546 | |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 547 | private View scrollWrap(View view) { |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 548 | ScrollView scroller = new ScrollView(this); |
| 549 | scroller.addView(view, new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, |
| 550 | ScrollView.LayoutParams.MATCH_PARENT)); |
| 551 | return scroller; |
| 552 | } |
| 553 | |
| 554 | private void buildUi() { |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 555 | LinearLayout top = new LinearLayout(this); |
| 556 | top.setOrientation(LinearLayout.VERTICAL); |
| 557 | |
Dianne Hackborn | 0c5001d | 2011-04-12 18:16:08 -0700 | [diff] [blame] | 558 | List<ActivityManager.RecentTaskInfo> recents = mAm.getRecentTasks(10, |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 559 | ActivityManager.RECENT_WITH_EXCLUDED); |
| 560 | if (recents != null) { |
| 561 | for (int i=0; i<recents.size(); i++) { |
| 562 | ActivityManager.RecentTaskInfo r = recents.get(i); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 563 | TextView tv = new TextView(this); |
| 564 | tv.setText(r.baseIntent.getComponent().flattenToShortString()); |
| 565 | top.addView(tv, new LinearLayout.LayoutParams( |
| 566 | LinearLayout.LayoutParams.WRAP_CONTENT, |
| 567 | LinearLayout.LayoutParams.WRAP_CONTENT)); |
| 568 | LinearLayout item = new LinearLayout(this); |
| 569 | item.setOrientation(LinearLayout.HORIZONTAL); |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 570 | top.addView(item, new LinearLayout.LayoutParams( |
| 571 | LinearLayout.LayoutParams.WRAP_CONTENT, |
| 572 | LinearLayout.LayoutParams.WRAP_CONTENT)); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | setContentView(scrollWrap(top)); |
| 577 | } |
Dianne Hackborn | f26fd99 | 2011-04-08 18:14:09 -0700 | [diff] [blame] | 578 | } |