mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
Add Timeout Service.
This commit is contained in:
1
bin/.gitignore
vendored
1
bin/.gitignore
vendored
@@ -1 +0,0 @@
|
||||
[^.]
|
||||
1
src/com/android/keepass/.gitignore
vendored
Normal file
1
src/com/android/keepass/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
R.java
|
||||
70
src/com/android/keepass/services/TimeoutService.java
Normal file
70
src/com/android/keepass/services/TimeoutService.java
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user