diff options
author | Guang Zhu <guangzhu@google.com> | 2014-06-02 19:56:48 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2014-06-02 19:56:48 +0000 |
commit | 6907f6da99e07017f0ba613f9556f7f18b2484d5 (patch) | |
tree | 33a0aac4d154ddf9cdf17b9ed168928f24153c1b | |
parent | fbfc72f3b192d2e76fdfd64c428cc1928c3822b1 (diff) | |
parent | 85c70eeb6cd10cc556373e64db50c6592b5278af (diff) | |
download | testing-6907f6da99e07017f0ba613f9556f7f18b2484d5.tar.gz |
am 85c70eeb: add a new service for sleep/wakeup loop
* commit '85c70eeb6cd10cc556373e64db50c6592b5278af':
add a new service for sleep/wakeup loop
5 files changed, 319 insertions, 0 deletions
diff --git a/uiautomator/utils/SleepUtils/WakeLoopService/Android.mk b/uiautomator/utils/SleepUtils/WakeLoopService/Android.mk new file mode 100644 index 0000000..a8a944b --- /dev/null +++ b/uiautomator/utils/SleepUtils/WakeLoopService/Android.mk @@ -0,0 +1,24 @@ +# +# Copyright (C) 2014 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. +# + +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := tests +LOCAL_SRC_FILES := $(call all-java-files-under, src) +LOCAL_PACKAGE_NAME := WakeupLoopService +LOCAL_SDK_VERSION := 7 +include $(BUILD_PACKAGE) diff --git a/uiautomator/utils/SleepUtils/WakeLoopService/AndroidManifest.xml b/uiautomator/utils/SleepUtils/WakeLoopService/AndroidManifest.xml new file mode 100644 index 0000000..a7028c4 --- /dev/null +++ b/uiautomator/utils/SleepUtils/WakeLoopService/AndroidManifest.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2014 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. --> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="android.test.wakeuploop" > + + <uses-sdk android:minSdkVersion="7" /> + <uses-permission android:name="android.permission.WAKE_LOCK" /> + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> + + <application android:label="Auto Wakeup Loop"> + <service android:name=".WakeLoopService" + android:label="Wakup Loop Service" + android:exported="true" + android:enabled="true"> + <intent-filter> + <action android:name="android.test.wakeuploop.WAKEUP_SERVICE" /> + </intent-filter> + </service> + <receiver android:name=".WakeUpCall"> + <intent-filter> + <action android:name="android.test.wakeuploop.WAKEUP" /> + </intent-filter> + </receiver> + </application> +</manifest> diff --git a/uiautomator/utils/SleepUtils/WakeLoopService/src/android/test/wakeuploop/FileUtil.java b/uiautomator/utils/SleepUtils/WakeLoopService/src/android/test/wakeuploop/FileUtil.java new file mode 100644 index 0000000..c8b075b --- /dev/null +++ b/uiautomator/utils/SleepUtils/WakeLoopService/src/android/test/wakeuploop/FileUtil.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2014 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 android.test.wakeuploop; + +import android.util.Log; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class FileUtil { + + private static FileUtil sInst = null; + private static DateFormat sDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + + private FileUtil() {}; + + public static FileUtil get() { + if (sInst == null) { + sInst = new FileUtil(); + } + return sInst; + } + + public void writeDateToFile(File file) { + try { + FileOutputStream fos = new FileOutputStream(file); + fos.write(sDateFormat.format(new Date()).getBytes()); + fos.write('\n'); + fos.flush(); + fos.close(); + } catch (IOException ioe) { + Log.e("FileUtil", "exception writing date to file", ioe); + } + } +} diff --git a/uiautomator/utils/SleepUtils/WakeLoopService/src/android/test/wakeuploop/WakeLoopService.java b/uiautomator/utils/SleepUtils/WakeLoopService/src/android/test/wakeuploop/WakeLoopService.java new file mode 100644 index 0000000..4f557b8 --- /dev/null +++ b/uiautomator/utils/SleepUtils/WakeLoopService/src/android/test/wakeuploop/WakeLoopService.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2014 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 android.test.wakeuploop; + +import android.app.AlarmManager; +import android.app.PendingIntent; +import android.app.Service; +import android.content.Context; +import android.content.Intent; +import android.os.Environment; +import android.os.Handler; +import android.os.IBinder; +import android.os.Message; +import android.os.Messenger; +import android.os.SystemClock; +import android.util.Log; + +import java.io.File; + +public class WakeLoopService extends Service { + + private static final String LOG_TAG = WakeLoopService.class.getSimpleName(); + static final String WAKEUP_INTERNAL = "WAKEUP_INTERVAL"; + static final String MAX_LOOP = "MAX_LOOP"; + static final String STOP_CALLBACK = "STOP_CALLBACK"; + static final String THIS_LOOP = "THIS_LOOP"; + static final int MSG_STOP_SERVICE = 0xd1ed1e; + + private final Handler mHandler = new Handler() { + public void handleMessage(Message msg) { + if (msg.what == MSG_STOP_SERVICE) { + stopSelf(); + } else { + super.handleMessage(msg); + } + }; + }; + + @Override + public IBinder onBind(Intent intent) { + // no binding, just start via intent + return null; + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + // get wakeup interval from intent + long wakeupInterval = intent.getLongExtra(WAKEUP_INTERNAL, 0); + long maxLoop = intent.getLongExtra(MAX_LOOP, 0); + + if (wakeupInterval == 0) { + // stop and error + Log.e(LOG_TAG, "No wakeup interval specified, not starting the service"); + stopSelf(); + return START_NOT_STICKY; + } + FileUtil.get().writeDateToFile(new File(Environment.getExternalStorageDirectory(), + "wakeup-loop-start.txt")); + Log.d(LOG_TAG, String.format("WakeLoop: STARTED interval = %d, total loop = %d", + wakeupInterval, maxLoop)); + // calculate when device should be waken up + long atTime = SystemClock.elapsedRealtime() + wakeupInterval; + AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE); + Intent wakupIntent = new Intent(WakeUpCall.WAKEUP_CALL) + .putExtra(WAKEUP_INTERNAL, wakeupInterval) + .putExtra(MAX_LOOP, maxLoop) + .putExtra(THIS_LOOP, 0L) + .putExtra(STOP_CALLBACK, new Messenger(mHandler)); + PendingIntent pi = PendingIntent.getBroadcast(this, 0, wakupIntent, + PendingIntent.FLAG_UPDATE_CURRENT); + // set alarm, which will be delivered in form of the wakeupIntent + am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, atTime, pi); + return START_NOT_STICKY; + } + + @Override + public void onDestroy() { + Log.d(LOG_TAG, "WakeLoop: STOPPED"); + // cancel alarms first + Intent intent = new Intent(WakeUpCall.WAKEUP_CALL) + .putExtra(WakeUpCall.CANCEL, "true"); + sendBroadcast(intent); + } +} diff --git a/uiautomator/utils/SleepUtils/WakeLoopService/src/android/test/wakeuploop/WakeUpCall.java b/uiautomator/utils/SleepUtils/WakeLoopService/src/android/test/wakeuploop/WakeUpCall.java new file mode 100644 index 0000000..8347bbf --- /dev/null +++ b/uiautomator/utils/SleepUtils/WakeLoopService/src/android/test/wakeuploop/WakeUpCall.java @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2014 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 android.test.wakeuploop; + +import android.app.AlarmManager; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.Environment; +import android.os.Message; +import android.os.Messenger; +import android.os.PowerManager; +import android.os.PowerManager.WakeLock; +import android.os.RemoteException; +import android.os.SystemClock; +import android.util.Log; + +import java.io.File; + +/** + * The receiver for the alarm we set + * + */ +public class WakeUpCall extends BroadcastReceiver { + private static final String LOG_TAG = WakeUpCall.class.getSimpleName(); + static final String WAKEUP_CALL = "android.test.wakeuploop.WAKEUP"; + static final String CANCEL = "CANCEL"; + + @Override + public void onReceive(Context context, Intent intent) { + AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); + boolean cancel = intent.hasExtra(CANCEL); + if (!cancel) { + long maxLoop = intent.getLongExtra(WakeLoopService.MAX_LOOP, 0); + long wakeupInterval = intent.getLongExtra(WakeLoopService.WAKEUP_INTERNAL, 0); + long thisLoop = intent.getLongExtra(WakeLoopService.THIS_LOOP, -1); + Log.d(LOG_TAG, String.format("incoming: interval = %d, max loop = %d, this loop = %d", + wakeupInterval, maxLoop, thisLoop)); + if (thisLoop == -1) { + Log.e(LOG_TAG, "no valid loop count received, trying to stop service"); + stopService(intent); + return; + } + if (wakeupInterval == 0) { + Log.e(LOG_TAG, "no valid wakeup interval received, trying to stop service"); + stopService(intent); + return; + } + thisLoop++; + Log.d(LOG_TAG, String.format("WakeLoop - iteration %d of %d", thisLoop, maxLoop)); + if (thisLoop == maxLoop) { + // when maxLoop is 0, we loop forever, so not checking that case + // here + Log.d(LOG_TAG, "reached max loop count, stopping service"); + stopService(intent); + return; + } + screenOn(context); + FileUtil.get().writeDateToFile( + new File(Environment.getExternalStorageDirectory(), "wakeup-loop.txt")); + // calculate when device should be waken up + long atTime = SystemClock.elapsedRealtime() + wakeupInterval; + intent.putExtra(WakeLoopService.THIS_LOOP, thisLoop); + PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, + PendingIntent.FLAG_UPDATE_CURRENT); + // set alarm, which will be delivered in form of the wakeupIntent + am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, atTime, pi); + } else { + // cancel alarms + Log.d(LOG_TAG, "cancelling future alarms on request"); + am.cancel(PendingIntent.getBroadcast(context, 0, intent, 0)); + } + } + + private void stopService(Intent i) { + Messenger msgr = i.getParcelableExtra(WakeLoopService.STOP_CALLBACK); + if (msgr == null) { + Log.e(LOG_TAG, "no stop service callback found, cannot stop"); + } else { + Message msg = new Message(); + msg.what = WakeLoopService.MSG_STOP_SERVICE; + try { + msgr.send(msg); + } catch (RemoteException e) { + Log.e(LOG_TAG, "ignored remoted exception while attempting to stop service", e); + } + } + } + + private void screenOn(Context context) { + PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); + @SuppressWarnings("deprecation") + WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | + PowerManager.ACQUIRE_CAUSES_WAKEUP, LOG_TAG); + wl.acquire(500); + } +} |