Merge "Remove unused tests/utils/SleepUtils"
This commit is contained in:
@@ -1,26 +0,0 @@
|
||||
#
|
||||
# Copyright (C) 2013 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_SRC_FILES += \
|
||||
src/com/android/testing/alarmservice/Alarm.aidl
|
||||
LOCAL_PACKAGE_NAME := SleepUtilsAlarmService
|
||||
LOCAL_SDK_VERSION := 7
|
||||
include $(BUILD_PACKAGE)
|
||||
@@ -1,31 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Copyright (C) 2013 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="com.android.testing.alarmservice" >
|
||||
|
||||
<uses-sdk android:minSdkVersion="7" />
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
|
||||
<application android:label="Sleep Utils Alarm Service">
|
||||
<service android:name=".AlarmService"
|
||||
android:label="Sleep Utils Alarm Service"
|
||||
android:exported="true"
|
||||
android:enabled="true">
|
||||
<intent-filter>
|
||||
<action android:name="com.android.testing.ALARM_SERVICE" />
|
||||
</intent-filter>
|
||||
</service>
|
||||
<receiver android:name=".WakeUpCall">
|
||||
<intent-filter>
|
||||
<action android:name="com.android.testing.alarmservice.WAKEUP" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -1,23 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2013 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.android.testing.alarmservice;
|
||||
|
||||
interface Alarm {
|
||||
int prepare();
|
||||
int setAlarmAndWait(long timeoutMills);
|
||||
int done();
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2013 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.android.testing.alarmservice;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.RemoteException;
|
||||
import android.os.SystemClock;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.testing.alarmservice.Alarm.Stub;
|
||||
|
||||
public class AlarmImpl extends Stub {
|
||||
|
||||
private static final String LOG_TAG = AlarmImpl.class.getSimpleName();
|
||||
|
||||
private Context mContext;
|
||||
|
||||
public AlarmImpl(Context context) {
|
||||
super();
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int prepare() throws RemoteException {
|
||||
WakeUpController.getController().getWakeLock().acquire();
|
||||
Log.d(LOG_TAG, "AlarmService prepared, wake lock acquired");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int setAlarmAndWait(long timeoutMills) throws RemoteException {
|
||||
// calculate when device should be waken up
|
||||
long atTime = SystemClock.elapsedRealtime() + timeoutMills;
|
||||
AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
|
||||
Intent wakupIntent = new Intent(WakeUpCall.WAKEUP_CALL);
|
||||
PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, wakupIntent, 0);
|
||||
// set alarm, which will be delivered in form of the wakeupIntent
|
||||
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, atTime, pi);
|
||||
Log.d(LOG_TAG, String.format("Alarm set: %d, giving up wake lock", atTime));
|
||||
Object lock = WakeUpController.getController().getWakeSync();
|
||||
// release wakelock and wait for the lock to be poked from the broadcast receiver
|
||||
WakeUpController.getController().getWakeLock().release();
|
||||
// does not really matter if device enters suspend before we start waiting on lock
|
||||
synchronized (lock) {
|
||||
try {
|
||||
lock.wait();
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
Log.d(LOG_TAG, String.format("Alarm triggered, done waiting"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int done() throws RemoteException {
|
||||
WakeUpController.getController().getWakeLock().release();
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2013 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.android.testing.alarmservice;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.IBinder;
|
||||
|
||||
public class AlarmService extends Service {
|
||||
|
||||
private AlarmImpl mAlarmImpl = null;
|
||||
static Context sContext;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
sContext = this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return getAlarmImpl();
|
||||
}
|
||||
|
||||
private AlarmImpl getAlarmImpl() {
|
||||
if (mAlarmImpl == null) {
|
||||
mAlarmImpl = new AlarmImpl(this);
|
||||
}
|
||||
return mAlarmImpl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
sContext = null;
|
||||
super.onDestroy();
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2013 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.android.testing.alarmservice;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
/**
|
||||
* The receiver for the alarm we set
|
||||
*
|
||||
*/
|
||||
public class WakeUpCall extends BroadcastReceiver {
|
||||
|
||||
public static final String WAKEUP_CALL = "com.android.testing.alarmservice.WAKEUP";
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
// we acquire wakelock without release because user is supposed to manually release it
|
||||
WakeUpController.getController().getWakeLock().acquire();
|
||||
Object lock = WakeUpController.getController().getWakeSync();
|
||||
synchronized (lock) {
|
||||
// poke the lock so the service side can be woken from waiting on the lock
|
||||
lock.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2013 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.android.testing.alarmservice;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.PowerManager;
|
||||
import android.os.PowerManager.WakeLock;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* A singleton used for controlling and sharing of states/wakelocks
|
||||
*
|
||||
*/
|
||||
public class WakeUpController {
|
||||
|
||||
private static final String LOG_TAG = WakeUpController.class.getName();
|
||||
private static WakeUpController mController = null;
|
||||
private WakeLock mWakeLock = null;
|
||||
private Object mWakeSync = new Object();
|
||||
|
||||
private WakeUpController() {
|
||||
Log.i(LOG_TAG, "Created instance: 0x" + Integer.toHexString(this.hashCode()));
|
||||
}
|
||||
|
||||
public static synchronized WakeUpController getController() {
|
||||
if (mController == null) {
|
||||
mController = new WakeUpController();
|
||||
}
|
||||
return mController;
|
||||
}
|
||||
|
||||
public WakeLock getWakeLock() {
|
||||
if (mWakeLock == null) {
|
||||
PowerManager pm =
|
||||
(PowerManager) AlarmService.sContext.getSystemService(Context.POWER_SERVICE);
|
||||
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "testing-alarmservice");
|
||||
Log.i(LOG_TAG, "Create wakelock: 0x" + Integer.toHexString(mWakeLock.hashCode()));
|
||||
}
|
||||
return mWakeLock;
|
||||
}
|
||||
|
||||
public Object getWakeSync() {
|
||||
return mWakeSync;
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
LOCAL_PATH:= $(call my-dir)
|
||||
include $(call all-makefiles-under, $(LOCAL_PATH))
|
||||
@@ -1,23 +0,0 @@
|
||||
This folder contains utils to properly perform timed suspend and wakeup.
|
||||
|
||||
AlarmService - a service that client can bind to and perform:
|
||||
1) holding wakelock (singleton to this service)
|
||||
2) setting alarm for a specified period and releasing the wakelock; service
|
||||
call will block until alarm has been triggered and the wakelock is held
|
||||
3) releasing the wakelock
|
||||
|
||||
SleepHelper - a self instrumentation meant as a convenient way to trigger
|
||||
the service functions from command line. Corresponding to service function
|
||||
above, supported operations are:
|
||||
1) holding wakelock
|
||||
am instrument -w -e command prepare \
|
||||
com.android.testing.sleephelper/.SetAlarm
|
||||
|
||||
2) setting alarm and wait til triggered
|
||||
am instrument -w -e command set_wait \
|
||||
-e param <time in ms> com.android.testing.sleephelper/.SetAlarm
|
||||
Note: for the function to work properly, "-w" parameter is required
|
||||
|
||||
3) releasing wakelock
|
||||
am instrument -w -e command done \
|
||||
com.android.testing.sleephelper/.SetAlarm
|
||||
@@ -1,29 +0,0 @@
|
||||
#
|
||||
# Copyright (C) 2013 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
|
||||
|
||||
# Only compile source java files in this apk.
|
||||
LOCAL_SRC_FILES := $(call all-java-files-under, src)
|
||||
LOCAL_SRC_FILES += \
|
||||
../AlarmService/src/com/android/testing/alarmservice/Alarm.aidl
|
||||
LOCAL_SDK_VERSION := 7
|
||||
LOCAL_PACKAGE_NAME := SleepUtilsSleepHelper
|
||||
|
||||
include $(BUILD_PACKAGE)
|
||||
@@ -1,21 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Copyright (C) 2013 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="com.android.testing.sleephelper">
|
||||
|
||||
<uses-sdk android:minSdkVersion="7" />
|
||||
<instrumentation android:label="Sleep Helper"
|
||||
android:name="com.android.testing.sleephelper.SetAlarm"
|
||||
android:targetPackage="com.android.testing.sleephelper" />
|
||||
|
||||
<application android:label="Sleep Utils Sleep Helper">
|
||||
<uses-library android:name="android.test.runner" />
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -1,152 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2013 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.android.testing.sleephelper;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Instrumentation;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.os.Bundle;
|
||||
import android.os.Debug;
|
||||
import android.os.IBinder;
|
||||
import android.os.RemoteException;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.testing.alarmservice.Alarm;
|
||||
|
||||
public class SetAlarm extends Instrumentation {
|
||||
|
||||
private static final String COMMAND = "command";
|
||||
private static final String PARAM = "param";
|
||||
private static final String CMD_PREPARE = "prepare";
|
||||
private static final String CMD_SET = "set_wait";
|
||||
private static final String CMD_DONE = "done";
|
||||
private static final String SERVICE_ACTION = "com.android.testing.ALARM_SERVICE";
|
||||
private static final String SERVICE_PKG = "com.android.testing.alarmservice";
|
||||
private static final String LOG_TAG = SetAlarm.class.getSimpleName();
|
||||
|
||||
private Alarm mAlarmService = null;
|
||||
private Bundle mArgs = null;
|
||||
private String mCommand = null;
|
||||
private Intent mServceIntent = new Intent(SERVICE_ACTION).setPackage(SERVICE_PKG);
|
||||
|
||||
private ServiceConnection mConn = new ServiceConnection() {
|
||||
@Override
|
||||
public void onServiceDisconnected(ComponentName name) {
|
||||
Log.d(LOG_TAG, "Service disconnected.");
|
||||
mAlarmService = null;
|
||||
errorFinish("service disconnected");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||
Log.d(LOG_TAG, "Service connected.");
|
||||
mAlarmService = Alarm.Stub.asInterface(service);
|
||||
handleCommands();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
private void handleCommands() {
|
||||
if (CMD_PREPARE.equals(mCommand)) {
|
||||
callPrepare();
|
||||
} else if (CMD_SET.equals(mCommand)) {
|
||||
String paramString = mArgs.getString(PARAM);
|
||||
if (paramString == null) {
|
||||
errorFinish("argument expected for alarm time");
|
||||
}
|
||||
long timeout = -1;
|
||||
try {
|
||||
timeout = Long.parseLong(paramString);
|
||||
} catch (NumberFormatException nfe) {
|
||||
errorFinish("a number argument is expected");
|
||||
}
|
||||
callSetAndWait(timeout);
|
||||
} else if (CMD_DONE.equals(mCommand)) {
|
||||
callDone();
|
||||
} else {
|
||||
errorFinish("Unrecognized command: " + mCommand);
|
||||
}
|
||||
finish(Activity.RESULT_OK, new Bundle());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle arguments) {
|
||||
super.onCreate(arguments);
|
||||
mCommand = arguments.getString(COMMAND);
|
||||
if ("true".equals(arguments.getString("debug"))) {
|
||||
Debug.waitForDebugger();
|
||||
}
|
||||
if (mCommand == null) {
|
||||
errorFinish("No command specified");
|
||||
}
|
||||
mArgs = arguments;
|
||||
connectToAlarmService();
|
||||
}
|
||||
|
||||
private void errorFinish(String msg) {
|
||||
Bundle ret = new Bundle();
|
||||
ret.putString("error", msg);
|
||||
finish(Activity.RESULT_CANCELED, ret);
|
||||
}
|
||||
|
||||
private void connectToAlarmService() {
|
||||
// start the service with an intent, this ensures the service keeps running after unbind
|
||||
ComponentName cn = getContext().startService(mServceIntent);
|
||||
if (cn == null) {
|
||||
errorFinish("failed to start service");
|
||||
}
|
||||
if (!getContext().bindService(mServceIntent, mConn, Context.BIND_AUTO_CREATE)) {
|
||||
errorFinish("failed to bind service");
|
||||
}
|
||||
}
|
||||
|
||||
private void callPrepare() {
|
||||
try {
|
||||
mAlarmService.prepare();
|
||||
} catch (RemoteException e) {
|
||||
errorFinish("RemoteExeption in prepare()");
|
||||
} finally {
|
||||
getContext().unbindService(mConn);
|
||||
}
|
||||
}
|
||||
|
||||
private void callDone() {
|
||||
try {
|
||||
mAlarmService.done();
|
||||
} catch (RemoteException e) {
|
||||
errorFinish("RemoteExeption in prepare()");
|
||||
} finally {
|
||||
getContext().unbindService(mConn);
|
||||
}
|
||||
// explicitly stop the service (started in prepare()) so that the service is now free
|
||||
// to be reclaimed
|
||||
getContext().stopService(mServceIntent);
|
||||
}
|
||||
|
||||
private void callSetAndWait(long timeoutMills) {
|
||||
try {
|
||||
mAlarmService.setAlarmAndWait(timeoutMills);
|
||||
} catch (RemoteException e) {
|
||||
errorFinish("RemoteExeption in setAlarmAndWait()");
|
||||
} finally {
|
||||
getContext().unbindService(mConn);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
#
|
||||
# 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)
|
||||
@@ -1,32 +0,0 @@
|
||||
<?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>
|
||||
@@ -1,53 +0,0 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user