Move over to the new AttachmentService.

Change-Id: I4a687b003884ea6a92a755fcbf394001bfc96a38
This commit is contained in:
Anthony Lee 2014-06-25 09:56:29 -07:00
parent ff52eef8e6
commit 3d16e5d4b9
9 changed files with 20 additions and 1466 deletions

View File

@ -424,7 +424,7 @@
<!-- From Email application -->
<receiver
android:name=".service.AttachmentDownloadService$Watchdog"
android:name=".service.AttachmentService$AttachmentWatchdog"
android:enabled="true"/>
<!-- Handles app upgrade. This disables itself after running once. -->
@ -482,7 +482,7 @@
</receiver>
<service
android:name=".service.AttachmentDownloadService"
android:name=".service.AttachmentService"
android:enabled="false"
>
</service>

View File

@ -1407,7 +1407,7 @@ public abstract class EmailContent {
+ ")";
// Bits used in mFlags
// WARNING: AttachmentDownloadService relies on the fact that ALL of the flags below
// WARNING: AttachmentService relies on the fact that ALL of the flags below
// disqualify attachments for precaching. If you add a flag that does NOT disqualify an
// attachment for precaching, you MUST change the PRECACHE_SELECTION definition above

View File

@ -60,14 +60,6 @@
*** createUniqueFile(java.lang.String);
}
-keepclasseswithmembers class com.android.email.service.AttachmentDownloadService {
*** addServiceClass(long, java.lang.Class);
}
-keepclasseswithmembers class com.android.email.service.AttachmentDownloadService$AccountManagerStub {
*** setNumberOfAccounts(int);
}
-keepclasseswithmembers class com.android.email.Preferences {
*** getAccountByContentUri(android.net.Uri);
}
@ -130,21 +122,6 @@
-keep class com.android.emailcommon.mail.Flag
-keepclasseswithmembers class com.android.email.service.AttachmentService$DownloadQueue {
*** addRequest(com.android.email.service.AttachmentService$DownloadRequest);
*** removeRequest(com.android.email.service.AttachmentService$DownloadRequest);
*** getNextRequest();
*** findRequestById(long);
*** getSize();
*** isEmpty();
}
-keepclasseswithmembers class com.android.email.service.AttachmentService$DownloadRequest {
<init>(int, long);
*** hashCode();
*** equals(java.lang.Object);
}
-keepclasseswithmembers class com.android.emailcommon.mail.Folder {
*** getUnreadMessageCount();
*** delete(boolean);

View File

@ -68,7 +68,7 @@ import com.android.email.R;
import com.android.email.SecurityPolicy;
import com.android.email.activity.setup.AccountSettingsFragment;
import com.android.email.activity.setup.AccountSettingsUtils;
import com.android.email.service.AttachmentDownloadService;
import com.android.email.service.AttachmentService;
import com.android.email.service.EmailServiceUtils;
import com.android.email.service.EmailServiceUtils.EmailServiceInfo;
import com.android.email2.ui.MailActivityEmail;
@ -2439,24 +2439,24 @@ public class EmailProvider extends ContentProvider
}
}
public static interface AttachmentService {
public static interface EmailAttachmentService {
/**
* Notify the service that an attachment has changed.
*/
void attachmentChanged(Context context, long id, int flags);
void attachmentChanged(final Context context, final long id, final int flags);
}
private final AttachmentService DEFAULT_ATTACHMENT_SERVICE = new AttachmentService() {
private final EmailAttachmentService DEFAULT_ATTACHMENT_SERVICE = new EmailAttachmentService() {
@Override
public void attachmentChanged(Context context, long id, int flags) {
public void attachmentChanged(final Context context, final long id, final int flags) {
// The default implementation delegates to the real service.
AttachmentDownloadService.attachmentChanged(context, id, flags);
AttachmentService.attachmentChanged(context, id, flags);
}
};
private AttachmentService mAttachmentService = DEFAULT_ATTACHMENT_SERVICE;
private EmailAttachmentService mAttachmentService = DEFAULT_ATTACHMENT_SERVICE;
// exposed for testing
public void injectAttachmentService(AttachmentService attachmentService) {
public void injectAttachmentService(final EmailAttachmentService attachmentService) {
mAttachmentService =
attachmentService == null ? DEFAULT_ATTACHMENT_SERVICE : attachmentService;
}

File diff suppressed because it is too large Load Diff

View File

@ -219,7 +219,7 @@ public abstract class EmailServiceStub extends IEmailService.Stub implements IEm
new MessageRetrievalListenerBridge(messageId, attachmentId, cb));
// If we failed to load the attachment, throw an Exception here, so that
// AttachmentDownloadService knows that we failed
// AttachmentService knows that we failed
if (storePart.getBody() == null) {
throw new MessagingException("Attachment not loaded.");
}

View File

@ -29,7 +29,7 @@ import android.os.Bundle;
import com.android.email.NotificationController;
import com.android.email.Preferences;
import com.android.email.provider.EmailProvider;
import com.android.email.service.AttachmentDownloadService;
import com.android.email.service.AttachmentService;
import com.android.email.service.EmailServiceUtils;
import com.android.emailcommon.Logging;
import com.android.emailcommon.TempDirectory;
@ -124,13 +124,15 @@ public class MailActivityEmail extends com.android.mail.ui.MailActivity {
private static void setServicesEnabled(Context context, boolean enabled) {
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(
new ComponentName(context, AttachmentDownloadService.class),
new ComponentName(context, AttachmentService.class),
enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
// Start/stop the various services depending on whether there are any accounts
startOrStopService(enabled, context, new Intent(context, AttachmentDownloadService.class));
// TODO: Make sure that the AttachmentService responds to this request as it
// expects a particular set of data in the intents that it receives or it ignores.
startOrStopService(enabled, context, new Intent(context, AttachmentService.class));
NotificationController.getInstance(context).watchForMessages();
}

View File

@ -34,7 +34,7 @@ import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.test.suitebuilder.annotation.Suppress;
import com.android.email.provider.EmailProvider.AttachmentService;
import com.android.email.provider.EmailProvider.EmailAttachmentService;
import com.android.emailcommon.provider.Account;
import com.android.emailcommon.provider.EmailContent;
import com.android.emailcommon.provider.EmailContent.AccountColumns;
@ -110,7 +110,8 @@ public class ProviderTests extends ProviderTestCase2<EmailProvider> {
}
}
private static final AttachmentService MOCK_ATTACHMENT_SERVICE = new AttachmentService() {
private static final EmailAttachmentService MOCK_ATTACHMENT_SERVICE =
new EmailAttachmentService() {
@Override
public void attachmentChanged(Context context, long id, int flags) {
// Noop. Don't download attachments.

View File

@ -1,283 +0,0 @@
/*
* Copyright (C) 2010 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.email.service;
import android.content.Context;
import android.test.suitebuilder.annotation.Suppress;
import com.android.email.AccountTestCase;
import com.android.email.EmailConnectivityManager;
import com.android.email.provider.ProviderTestUtils;
import com.android.email.service.AttachmentDownloadService.DownloadRequest;
import com.android.email.service.AttachmentDownloadService.DownloadSet;
import com.android.emailcommon.provider.Account;
import com.android.emailcommon.provider.EmailContent.Attachment;
import com.android.emailcommon.provider.EmailContent.Message;
import com.android.emailcommon.provider.Mailbox;
import com.android.emailcommon.service.EmailServiceStatus;
import java.io.File;
import java.util.Iterator;
/**
* Tests of the AttachmentDownloadService
*
* You can run this entire test case with:
* runtest -c com.android.email.service.AttachmentDownloadServiceTests email
*/
@Suppress
public class AttachmentDownloadServiceTests extends AccountTestCase {
private AttachmentDownloadService mService;
private Context mMockContext;
private Account mAccount;
private Mailbox mMailbox;
private long mAccountId;
private long mMailboxId;
private AttachmentDownloadService.AccountManagerStub mAccountManagerStub;
private MockDirectory mMockDirectory;
private DownloadSet mDownloadSet;
@Override
public void setUp() throws Exception {
super.setUp();
mMockContext = getMockContext();
// Set up an account and mailbox
mAccount = ProviderTestUtils.setupAccount("account", false, mMockContext);
mAccount.mFlags |= Account.FLAGS_BACKGROUND_ATTACHMENTS;
mAccount.save(mMockContext);
mAccountId = mAccount.mId;
mMailbox = ProviderTestUtils.setupMailbox("mailbox", mAccountId, true, mMockContext);
mMailboxId = mMailbox.mId;
// Set up our download service to simulate a running environment
// Use the NullEmailService so that the loadAttachment calls become no-ops
mService = new AttachmentDownloadService();
mService.mContext = mMockContext;
// there's no NullEmailService class
/*mService.addServiceIntentForTest(mAccountId, new Intent(mContext,
NullEmailService.class));*/
mAccountManagerStub = new AttachmentDownloadService.AccountManagerStub(null);
mService.mAccountManagerStub = mAccountManagerStub;
mService.mConnectivityManager = new MockConnectivityManager(mContext, "mock");
mDownloadSet = mService.mDownloadSet;
mMockDirectory =
new MockDirectory(mService.mContext.getCacheDir().getAbsolutePath());
}
@Override
public void tearDown() throws Exception {
super.tearDown();
}
/**
* This test creates attachments and places them in the DownloadSet; we then do various checks
* that exercise its functionality.
*/
public void testDownloadSet() {
// TODO: Make sure that this doesn't interfere with the "real" ADS that might be running
// on device
Message message = ProviderTestUtils.setupMessage("message", mAccountId, mMailboxId, false,
true, mMockContext);
Attachment att1 = ProviderTestUtils.setupAttachment(message.mId, "filename1", 1000,
Attachment.FLAG_DOWNLOAD_USER_REQUEST, true, mMockContext);
Attachment att2 = ProviderTestUtils.setupAttachment(message.mId, "filename2", 1000,
Attachment.FLAG_DOWNLOAD_FORWARD, true, mMockContext);
Attachment att3 = ProviderTestUtils.setupAttachment(message.mId, "filename3", 1000,
Attachment.FLAG_DOWNLOAD_FORWARD, true, mMockContext);
Attachment att4 = ProviderTestUtils.setupAttachment(message.mId, "filename4", 1000,
Attachment.FLAG_DOWNLOAD_USER_REQUEST, true, mMockContext);
// Indicate that these attachments have changed; they will be added to the queue
mDownloadSet.onChange(mMockContext, att1);
mDownloadSet.onChange(mMockContext, att2);
mDownloadSet.onChange(mMockContext, att3);
mDownloadSet.onChange(mMockContext, att4);
Iterator<DownloadRequest> iterator = mDownloadSet.descendingIterator();
// Check the expected ordering; 1 & 4 are higher priority than 2 & 3
// 1 and 3 were created earlier than their priority equals
long[] expectedAttachmentIds = new long[] {att1.mId, att4.mId, att2.mId, att3.mId};
for (int i = 0; i < expectedAttachmentIds.length; i++) {
assertTrue(iterator.hasNext());
DownloadRequest req = iterator.next();
assertEquals(expectedAttachmentIds[i], req.attachmentId);
}
// Process the queue; attachment 1 should be marked "in progress", and should be in
// the in-progress map
mDownloadSet.processQueue();
DownloadRequest req = mDownloadSet.findDownloadRequest(att1.mId);
assertNotNull(req);
assertTrue(req.inProgress);
assertTrue(mDownloadSet.mDownloadsInProgress.containsKey(att1.mId));
// There should also be only one download in progress (testing the per-account limitation)
assertEquals(1, mDownloadSet.mDownloadsInProgress.size());
// End the "download" with a connection error; we should still have this in the queue,
// but it should no longer be in-progress
mDownloadSet.endDownload(att1.mId, EmailServiceStatus.CONNECTION_ERROR);
assertFalse(req.inProgress);
assertEquals(0, mDownloadSet.mDownloadsInProgress.size());
mDownloadSet.processQueue();
// Things should be as they were earlier; att1 should be an in-progress download
req = mDownloadSet.findDownloadRequest(att1.mId);
assertNotNull(req);
assertTrue(req.inProgress);
assertTrue(mDownloadSet.mDownloadsInProgress.containsKey(att1.mId));
// Successfully download the attachment; there should be no downloads in progress, and
// att1 should no longer be in the queue
mDownloadSet.endDownload(att1.mId, EmailServiceStatus.SUCCESS);
assertEquals(0, mDownloadSet.mDownloadsInProgress.size());
assertNull(mDownloadSet.findDownloadRequest(att1.mId));
// Test dequeue and isQueued
assertEquals(3, mDownloadSet.size());
mService.dequeue(att2.mId);
assertEquals(2, mDownloadSet.size());
assertTrue(mService.isQueued(att4.mId));
assertTrue(mService.isQueued(att3.mId));
mDownloadSet.processQueue();
// att4 should be the download in progress
req = mDownloadSet.findDownloadRequest(att4.mId);
assertNotNull(req);
assertTrue(req.inProgress);
assertTrue(mDownloadSet.mDownloadsInProgress.containsKey(att4.mId));
}
/**
* A mock file directory containing a single (Mock)File. The total space, usable space, and
* length of the single file can be set
*/
private static class MockDirectory extends File {
private static final long serialVersionUID = 1L;
private long mTotalSpace;
private long mUsableSpace;
private MockFile[] mFiles;
private final MockFile mMockFile = new MockFile();
public MockDirectory(String path) {
super(path);
mFiles = new MockFile[1];
mFiles[0] = mMockFile;
}
private void setTotalAndUsableSpace(long total, long usable) {
mTotalSpace = total;
mUsableSpace = usable;
}
@Override
public long getTotalSpace() {
return mTotalSpace;
}
@Override
public long getUsableSpace() {
return mUsableSpace;
}
public void setFileLength(long length) {
mMockFile.mLength = length;
}
@Override
public File[] listFiles() {
return mFiles;
}
}
/**
* A mock file that reports back a pre-set length
*/
private static class MockFile extends File {
private static final long serialVersionUID = 1L;
private long mLength = 0;
public MockFile() {
super("_mock");
}
@Override
public long length() {
return mLength;
}
}
private static class MockConnectivityManager extends EmailConnectivityManager {
public MockConnectivityManager(Context context, String name) {
super(context, name);
}
@Override
public void waitForConnectivity() {
}
@Override
public boolean isAutoSyncAllowed() {
return true;
}
}
public void testCanPrefetchForAccount() {
// First, test our "global" limits (based on free storage)
// Mock storage @ 100 total and 26 available
// Note that all file lengths in this test are in arbitrary units
mMockDirectory.setTotalAndUsableSpace(100L, 26L);
// Mock 2 accounts in total
mAccountManagerStub.setNumberOfAccounts(2);
// With 26% available, we should be ok to prefetch
assertTrue(mService.canPrefetchForAccount(mAccount, mMockDirectory));
// Now change to 24 available
mMockDirectory.setTotalAndUsableSpace(100L, 24L);
// With 24% available, we should NOT be ok to prefetch
assertFalse(mService.canPrefetchForAccount(mAccount, mMockDirectory));
// Now, test per-account storage
// Mock storage @ 100 total and 50 available
mMockDirectory.setTotalAndUsableSpace(100L, 50L);
// Mock a file of length 12, but need to uncache previous amount first
mService.mAttachmentStorageMap.remove(mAccountId);
mMockDirectory.setFileLength(11);
// We can prefetch since 11 < 50/4
assertTrue(mService.canPrefetchForAccount(mAccount, mMockDirectory));
// Mock a file of length 13, but need to uncache previous amount first
mService.mAttachmentStorageMap.remove(mAccountId);
mMockDirectory.setFileLength(13);
// We can't prefetch since 13 > 50/4
assertFalse(mService.canPrefetchForAccount(mAccount, mMockDirectory));
}
public void testCanPrefetchForAccountNoBackgroundDownload() {
Account account = ProviderTestUtils.setupAccount("account2", false, mMockContext);
account.mFlags &= ~Account.FLAGS_BACKGROUND_ATTACHMENTS;
account.save(mMockContext);
// First, test our "global" limits (based on free storage)
// Mock storage @ 100 total and 26 available
// Note that all file lengths in this test are in arbitrary units
mMockDirectory.setTotalAndUsableSpace(100L, 26L);
// Mock 2 accounts in total
mAccountManagerStub.setNumberOfAccounts(2);
// With 26% available, we should be ok to prefetch,
// *but* bg download is disabled on the account.
assertFalse(mService.canPrefetchForAccount(account, mMockDirectory));
}
}