Add Timeout Service.

This commit is contained in:
Brian Pellin
2009-03-04 23:27:17 -06:00
parent af04bb11b8
commit 24a73d1606
4 changed files with 138 additions and 1 deletions

1
bin/.gitignore vendored
View File

@@ -1 +0,0 @@
[^.]

1
src/com/android/keepass/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
R.java

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2009 Brian Pellin.
*
* This file is part of KeePassDroid.
*
* KeePassDroid is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePassDroid is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePassDroid. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.android.keepass.services;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class TimeoutService extends Service {
private boolean timeout = false;
private Timer mTimer = new Timer();
public class TimeoutBinder extends Binder {
public TimeoutService getService() {
return TimeoutService.this;
}
}
private final IBinder mBinder = new TimeoutBinder();
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return mBinder;
}
private class TimeoutTask extends TimerTask {
@Override
public void run() {
timeout = true;
}
}
public void startTimeout(long seconds) {
mTimer.schedule(new TimeoutTask(), seconds);
}
public void cancel() {
mTimer.cancel();
timeout = false;
}
public boolean HasTimedOut() {
return timeout;
}
}

View File

@@ -0,0 +1,67 @@
/* Copyright 2009 Brian Pellin.
*
* This file is part of KeePassDroid.
*
* KeePassDroid is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePassDroid is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePassDroid. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.android.keepass.tests.services;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.test.ServiceTestCase;
import com.android.keepass.services.TimeoutService;
public class TimeoutServiceTest extends ServiceTestCase<TimeoutService> {
public TimeoutServiceTest() {
super(TimeoutService.class);
}
private TimeoutService mService;
@Override
protected void setUp() throws Exception {
super.setUp();
startService(new Intent(getContext(), TimeoutService.class));
mService = getService();
assertNotNull(mService);
}
public void testTimeout() {
assertFalse("Timeout is not set at the beginning.", mService.HasTimedOut() );
mService.startTimeout(1000);
assertFalse("Timeout too early.", mService.HasTimedOut() );
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
assertTrue("Thread interrupted.", false);
}
assertTrue("Timeout was not set.", mService.HasTimedOut());
}
}