Delete RemoveTempFileService

This commit is contained in:
Jimmy Shih
2013-10-01 14:05:01 -07:00
parent c9f1422c42
commit 7594f6b516
6 changed files with 1 additions and 231 deletions
-1
View File
@@ -251,7 +251,6 @@ limitations under the License.
<service
android:exported="false"
android:name="com.google.android.apps.mytracks.services.ControlRecordingService" />
<service android:name="com.google.android.apps.mytracks.services.RemoveTempFilesService" />
<service
android:enabled="true"
android:exported="true"
@@ -18,7 +18,6 @@ package com.google.android.apps.mytracks;
import static android.content.Intent.ACTION_BOOT_COMPLETED;
import static com.google.android.apps.mytracks.Constants.TAG;
import com.google.android.apps.mytracks.services.RemoveTempFilesService;
import com.google.android.apps.mytracks.services.TrackRecordingService;
import android.content.BroadcastReceiver;
@@ -49,10 +48,7 @@ public class BootReceiver extends BroadcastReceiver {
if (ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent startIntent = new Intent(context, TrackRecordingService.class)
.putExtra(TrackRecordingService.RESUME_TRACK_EXTRA_NAME, true);
context.startService(startIntent);
Intent removeTempFilesIntent = new Intent(context, RemoveTempFilesService.class);
context.startService(removeTempFilesIntent);
context.startService(startIntent);
} else {
Log.w(TAG, "BootReceiver: unsupported action");
}
@@ -33,7 +33,6 @@ import com.google.android.apps.mytracks.io.sendtogoogle.SendRequest;
import com.google.android.apps.mytracks.io.sync.SyncUtils;
import com.google.android.apps.mytracks.services.ITrackRecordingService;
import com.google.android.apps.mytracks.services.MyTracksLocationManager;
import com.google.android.apps.mytracks.services.RemoveTempFilesService;
import com.google.android.apps.mytracks.services.TrackRecordingServiceConnection;
import com.google.android.apps.mytracks.settings.SettingsActivity;
import com.google.android.apps.mytracks.util.AnalyticsUtils;
@@ -323,8 +322,6 @@ public class TrackListActivity extends AbstractSendToGoogleActivity
if (BuildConfig.DEBUG) {
ApiAdapterFactory.getApiAdapter().enableStrictMode();
}
Intent intent = new Intent(this, RemoveTempFilesService.class);
startService(intent);
myTracksProviderUtils = MyTracksProviderUtils.Factory.get(this);
sharedPreferences = getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
@@ -1,121 +0,0 @@
/*
* Copyright 2012 Google Inc.
*
* 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.apps.mytracks.services;
import com.google.android.apps.mytracks.io.file.TrackFileFormat;
import com.google.android.apps.mytracks.util.FileUtils;
import com.google.common.annotations.VisibleForTesting;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import java.io.File;
/**
* A service to remove My Tracks temp files older than one hour on the SD card.
*
* @author Jimmy Shih
*/
public class RemoveTempFilesService extends Service {
private static final String TAG = RemoveTempFilesService.class.getSimpleName();
private static final int ONE_HOUR_IN_MILLISECONDS = 60 * 60 * 1000;
private RemoveTempFilesAsyncTask removeTempFilesAsyncTask = null;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Setup an alarm to repeatedly call this service
Intent alarmIntent = new Intent(this, RemoveTempFilesService.class);
PendingIntent pendingIntent = PendingIntent.getService(
this, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + ONE_HOUR_IN_MILLISECONDS, AlarmManager.INTERVAL_HOUR,
pendingIntent);
// Invoke the AsyncTask to cleanup the temp files
if (removeTempFilesAsyncTask == null
|| removeTempFilesAsyncTask.getStatus().equals(AsyncTask.Status.FINISHED)) {
removeTempFilesAsyncTask = new RemoveTempFilesAsyncTask();
removeTempFilesAsyncTask.execute((Void[]) null);
}
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private class RemoveTempFilesAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
// Can't do anything
return null;
}
cleanTempDirectory(TrackFileFormat.KML.getExtension());
cleanTempDirectory(TrackFileFormat.GPX.getExtension());
cleanTempDirectory(TrackFileFormat.CSV.getExtension());
cleanTempDirectory(TrackFileFormat.TCX.getExtension());
return null;
}
@Override
protected void onPostExecute(Void result) {
stopSelf();
}
}
private void cleanTempDirectory(String name) {
cleanTempDirectory(new File(FileUtils.getDirectoryPath(name, FileUtils.TEMP_DIR)));
}
/**
* Removes temp files in a directory older than one hour.
*
* @param dir the directory
* @return the number of files removed.
*/
@VisibleForTesting
int cleanTempDirectory(File dir) {
if (!dir.exists()) {
return 0;
}
int count = 0;
long oneHourAgo = System.currentTimeMillis() - ONE_HOUR_IN_MILLISECONDS;
for (File f : dir.listFiles()) {
if (f.lastModified() < oneHourAgo) {
if (!f.delete()) {
Log.e(TAG, "Unable to delete file: " + f.getAbsolutePath());
} else {
count++;
}
}
}
return count;
}
}
@@ -30,7 +30,6 @@ public class FileUtils {
private FileUtils() {}
public static final String TEMP_DIR = "tmp";
public static final String BACKUPS_DIR = "backups";
/**
@@ -1,100 +0,0 @@
/*
* Copyright 2010 Google Inc.
*
* 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.apps.mytracks.services;
import com.google.android.testing.mocking.UsesMocks;
import android.test.AndroidTestCase;
import java.io.File;
/**
* Tests {@link RemoveTempFilesService}.
*
* @author Sandor Dornbush
*/
public class RemoveTempFilesServiceTest extends AndroidTestCase {
private static final String DIR_NAME = "/tmp";
private static final String FILE_NAME = "foo";
private RemoveTempFilesService service;
@UsesMocks({ File.class, })
protected void setUp() throws Exception {
service = new RemoveTempFilesService();
};
// /**
// * Tests when the directory doesn't exists.
// */
// public void test_noDir() {
// File dir = AndroidMock.createMock(File.class, DIR_NAME);
// expect(dir.exists()).andStubReturn(false);
// AndroidMock.replay(dir);
//
// assertEquals(0, service.cleanTempDirectory(dir));
// AndroidMock.verify(dir);
// }
//
// /**
// * Tests when the directory is empty.
// */
// public void test_emptyDir() {
// File dir = AndroidMock.createMock(File.class, DIR_NAME);
// expect(dir.exists()).andStubReturn(true);
// expect(dir.listFiles()).andStubReturn(new File[0]);
// AndroidMock.replay(dir);
//
// assertEquals(0, service.cleanTempDirectory(dir));
// AndroidMock.verify(dir);
// }
//
// /**
// * Tests when there is a new file and it shouldn't get deleted.
// */
// public void test_newFile() {
// File file = AndroidMock.createMock(File.class, DIR_NAME + FILE_NAME);
// expect(file.lastModified()).andStubReturn(System.currentTimeMillis());
//
// File dir = AndroidMock.createMock(File.class, DIR_NAME);
// expect(dir.exists()).andStubReturn(true);
// expect(dir.listFiles()).andStubReturn(new File[] { file });
// AndroidMock.replay(dir, file);
//
// assertEquals(0, service.cleanTempDirectory(dir));
// AndroidMock.verify(dir, file);
// }
//
// /**
// * Tests when there is an old file and it should get deleted.
// */
// public void test_oldFile() {
// File file = AndroidMock.createMock(File.class, DIR_NAME + FILE_NAME);
// // qSet to one hour and 1 millisecond later than the current time
// expect(file.lastModified()).andStubReturn(System.currentTimeMillis() - 3600001);
// expect(file.delete()).andStubReturn(true);
//
// File dir = AndroidMock.createMock(File.class, DIR_NAME);
// expect(dir.exists()).andStubReturn(true);
// expect(dir.listFiles()).andStubReturn(new File[] { file });
// AndroidMock.replay(dir, file);
//
// assertEquals(1, service.cleanTempDirectory(dir));
// AndroidMock.verify(dir, file);
// }
}