From 3d1933c45fe9ba2389ebd166d96abeceab1971d1 Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Wed, 10 Jun 2015 16:25:57 -0700 Subject: Implement some control over ALLOW_WHILE_IDLE alarms. Since these alarms allow you to bypass the idle restrictions, we don't want them to be so open-ended like other alarms. This implements a policy where the alarm manager will only deliver these types of alarms every X minutes to each application. For this initial implementation, X is 1 minute under normal operation and 15 minutes when in idle mode. To do this, I needed to introduce a new internal allow-while-idle flag for system alarms, which applications can't get, and doesn't have these new restrictions. Also tweaked how the alarm manager handles the alarm window, so it doesn't change if the alarm gets rescheduld; the window is now always what as computed based on the time when the alarm was first given to it. Finally, fix TimeUtils to be able to correctly print times that are > 999 days. Change-Id: Ibad8c6a7c14b0624b54e82267be23224b4c31e84 --- tests/ActivityTests/AndroidManifest.xml | 1 + .../android/test/activity/ActivityTestMain.java | 28 +++++++++++++++++++ .../android/test/activity/AlarmSpamReceiver.java | 31 ++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 tests/ActivityTests/src/com/google/android/test/activity/AlarmSpamReceiver.java (limited to 'tests') diff --git a/tests/ActivityTests/AndroidManifest.xml b/tests/ActivityTests/AndroidManifest.xml index c105491..dae7cc5 100644 --- a/tests/ActivityTests/AndroidManifest.xml +++ b/tests/ActivityTests/AndroidManifest.xml @@ -76,5 +76,6 @@ android:authorities="com.google.android.test.activity.single_user" android:singleUser="true" android:exported="true" /> + diff --git a/tests/ActivityTests/src/com/google/android/test/activity/ActivityTestMain.java b/tests/ActivityTests/src/com/google/android/test/activity/ActivityTestMain.java index ddcfd9e..94cbabf 100644 --- a/tests/ActivityTests/src/com/google/android/test/activity/ActivityTestMain.java +++ b/tests/ActivityTests/src/com/google/android/test/activity/ActivityTestMain.java @@ -22,6 +22,7 @@ import java.util.List; import android.app.Activity; import android.app.ActivityManager; import android.app.ActivityOptions; +import android.app.AlarmManager; import android.app.AlertDialog; import android.app.PendingIntent; import android.content.ActivityNotFoundException; @@ -36,6 +37,7 @@ import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.RemoteException; +import android.os.SystemClock; import android.os.UserHandle; import android.os.UserManager; import android.graphics.Bitmap; @@ -58,6 +60,7 @@ public class ActivityTestMain extends Activity { static final String KEY_CONFIGURATION = "configuration"; ActivityManager mAm; + AlarmManager mAlarm; Configuration mOverrideConfig; int mSecondUser; @@ -66,6 +69,7 @@ public class ActivityTestMain extends Activity { ServiceConnection mIsolatedConnection; static final int MSG_SPAM = 1; + static final int MSG_SPAM_ALARM = 2; final Handler mHandler = new Handler() { @Override @@ -82,6 +86,15 @@ public class ActivityTestMain extends Activity { startActivity(intent, options); scheduleSpam(!fg); } break; + case MSG_SPAM_ALARM: { + long when = SystemClock.elapsedRealtime(); + Intent intent = new Intent(ActivityTestMain.this, AlarmSpamReceiver.class); + intent.setAction("com.example.SPAM_ALARM=" + when); + PendingIntent pi = PendingIntent.getBroadcast(ActivityTestMain.this, + 0, intent, 0); + mAlarm.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME, when+(30*1000), pi); + scheduleSpamAlarm(30*1000); + } break; } super.handleMessage(msg); } @@ -146,6 +159,7 @@ public class ActivityTestMain extends Activity { Log.i(TAG, "Referrer: " + getReferrer()); mAm = (ActivityManager)getSystemService(ACTIVITY_SERVICE); + mAlarm = (AlarmManager)getSystemService(ALARM_SERVICE); if (savedInstanceState != null) { mOverrideConfig = savedInstanceState.getParcelable(KEY_CONFIGURATION); if (mOverrideConfig != null) { @@ -436,6 +450,13 @@ public class ActivityTestMain extends Activity { return true; } }); + menu.add("Spam idle alarm").setOnMenuItemClickListener( + new MenuItem.OnMenuItemClickListener() { + @Override public boolean onMenuItemClick(MenuItem item) { + scheduleSpamAlarm(0); + return true; + } + }); return true; } @@ -467,6 +488,7 @@ public class ActivityTestMain extends Activity { @Override protected void onStop() { super.onStop(); + mHandler.removeMessages(MSG_SPAM_ALARM); for (ServiceConnection conn : mConnections) { unbindService(conn); } @@ -536,6 +558,12 @@ public class ActivityTestMain extends Activity { mHandler.sendMessageDelayed(msg, 500); } + void scheduleSpamAlarm(long delay) { + mHandler.removeMessages(MSG_SPAM_ALARM); + Message msg = mHandler.obtainMessage(MSG_SPAM_ALARM); + mHandler.sendMessageDelayed(msg, delay); + } + private View scrollWrap(View view) { ScrollView scroller = new ScrollView(this); scroller.addView(view, new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, diff --git a/tests/ActivityTests/src/com/google/android/test/activity/AlarmSpamReceiver.java b/tests/ActivityTests/src/com/google/android/test/activity/AlarmSpamReceiver.java new file mode 100644 index 0000000..0cb1ffb --- /dev/null +++ b/tests/ActivityTests/src/com/google/android/test/activity/AlarmSpamReceiver.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.android.test.activity; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.os.UserHandle; +import android.util.Log; + +public class AlarmSpamReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + Log.i("AlarmSpamReceiver", "Received spam = " + intent); + } +} -- cgit v1.1