From 75a754660e33c5e18cacffff193983ba22a7b9b0 Mon Sep 17 00:00:00 2001 From: Marc Blank Date: Thu, 2 Jun 2011 12:06:22 -0700 Subject: [PATCH] Update searchMessages API * Store various search parameters in a new parcelable class Change-Id: Iadec6a803b1bf17d89cd401c3fca1cb0ad3340d4 --- .../service/EmailServiceProxy.java | 17 +--- .../emailcommon/service/IEmailService.aidl | 4 +- .../emailcommon/service/SearchParams.aidl | 18 ++++ .../emailcommon/service/SearchParams.java | 99 +++++++++++++++++++ src/com/android/email/Controller.java | 13 ++- src/com/android/email/ExchangeUtils.java | 4 +- .../android/email/activity/EmailActivity.java | 23 +++-- .../service/SearchParamsTests.java | 38 +++++++ 8 files changed, 186 insertions(+), 30 deletions(-) create mode 100644 emailcommon/src/com/android/emailcommon/service/SearchParams.aidl create mode 100644 emailcommon/src/com/android/emailcommon/service/SearchParams.java create mode 100644 tests/src/com/android/emailcommon/service/SearchParamsTests.java diff --git a/emailcommon/src/com/android/emailcommon/service/EmailServiceProxy.java b/emailcommon/src/com/android/emailcommon/service/EmailServiceProxy.java index 5d03660d2..76477db1e 100644 --- a/emailcommon/src/com/android/emailcommon/service/EmailServiceProxy.java +++ b/emailcommon/src/com/android/emailcommon/service/EmailServiceProxy.java @@ -391,25 +391,16 @@ public class EmailServiceProxy extends ServiceProxy implements IEmailService { * for the continuation of a previous search) * * @param accountId the id of the account to be searched - * @param mailboxId the id of the mailbox to be searched; if -1, all mailboxes should be - * searched - * @param includeSubfolders if true, all subfolders of the specified mailbox should be searched - * @param query the terms to be searched (the search MUST find messages whose contents - * include all of the search terms in the query - * @param numResults specifies the maximum number of results returned in this request - * @param firstResult if zero, specifies a "new" query; otherwise, asks for a continuation of - * the previous query(ies) starting with the firstResult'th match (0 based) + * @param searchParams the search specification * @param destMailboxId the id of the mailbox into which search results are appended * @return the total number of matches for this search (regardless of how many were requested) */ - public int searchMessages(final long accountId, final long mailboxId, - final boolean includeSubfolders, final String query, final int numResults, - final int firstResult, final long destMailboxId) throws RemoteException { + public int searchMessages(final long accountId, final SearchParams searchParams, + final long destMailboxId) throws RemoteException { setTask(new ProxyTask() { public void run() throws RemoteException{ if (mCallback != null) mService.setCallback(mCallback); - mReturn = mService.searchMessages(accountId, mailboxId, includeSubfolders, query, - numResults, firstResult, destMailboxId); + mReturn = mService.searchMessages(accountId, searchParams, destMailboxId); } }, "searchMessages"); waitForCompletion(); diff --git a/emailcommon/src/com/android/emailcommon/service/IEmailService.aidl b/emailcommon/src/com/android/emailcommon/service/IEmailService.aidl index 05956cd81..dc5fbf62a 100644 --- a/emailcommon/src/com/android/emailcommon/service/IEmailService.aidl +++ b/emailcommon/src/com/android/emailcommon/service/IEmailService.aidl @@ -18,6 +18,7 @@ package com.android.emailcommon.service; import com.android.emailcommon.service.IEmailServiceCallback; +import com.android.emailcommon.service.SearchParams; import android.os.Bundle; interface IEmailService { @@ -55,6 +56,5 @@ interface IEmailService { int getApiLevel(); // API level 2 - int searchMessages(long accountId, long mailboxId, boolean includeSubfolders, String query, - int numResults, int firstResult, long destMailboxId); + int searchMessages(long accountId, in SearchParams params, long destMailboxId); } \ No newline at end of file diff --git a/emailcommon/src/com/android/emailcommon/service/SearchParams.aidl b/emailcommon/src/com/android/emailcommon/service/SearchParams.aidl new file mode 100644 index 000000000..77dedaedb --- /dev/null +++ b/emailcommon/src/com/android/emailcommon/service/SearchParams.aidl @@ -0,0 +1,18 @@ +/* + * Copyright (C) 2011 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.emailcommon.service; + +parcelable SearchParams; \ No newline at end of file diff --git a/emailcommon/src/com/android/emailcommon/service/SearchParams.java b/emailcommon/src/com/android/emailcommon/service/SearchParams.java new file mode 100644 index 000000000..1107aca29 --- /dev/null +++ b/emailcommon/src/com/android/emailcommon/service/SearchParams.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2011 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.emailcommon.service; + +import com.android.emailcommon.provider.Mailbox; + +import android.os.Parcel; +import android.os.Parcelable; + +public class SearchParams implements Parcelable { + public static final long ALL_MAILBOXES = Mailbox.NO_MAILBOX; + + private static final int DEFAULT_LIMIT = 20; + private static final int DEFAULT_OFFSET = 0; + + // The id of the mailbox to be searched; if -1, all mailboxes MUST be searched + public final long mMailboxId; + // If true, all subfolders of the specified mailbox MUST be searched + public boolean mIncludeChildren = true; + // The search terms (the search MUST only select messages whose contents include all of the + // search terms in the query) + public final String mFilter; + // The maximum number of results to be created by this search + public int mLimit = DEFAULT_LIMIT; + // If zero, specifies a "new" search; otherwise, asks for a continuation of the previous + // query(ies) starting with the mOffset'th match (0 based) + public int mOffset = DEFAULT_OFFSET; + + /** + * Error codes returned by the searchMessages API + */ + public static class SearchParamsError { + public static final int CANT_SEARCH_ALL_MAILBOXES = -1; + public static final int CANT_SEARCH_CHILDREN = -2; + } + + public SearchParams(long mailboxId, String filter) { + mMailboxId = mailboxId; + mFilter = filter; + } + + @Override + public int describeContents() { + return 0; + } + + /** + * Supports Parcelable + */ + public static final Parcelable.Creator CREATOR + = new Parcelable.Creator() { + @Override + public SearchParams createFromParcel(Parcel in) { + return new SearchParams(in); + } + + @Override + public SearchParams[] newArray(int size) { + return new SearchParams[size]; + } + }; + + /** + * Supports Parcelable + */ + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeLong(mMailboxId); + dest.writeInt(mIncludeChildren ? 1 : 0); + dest.writeString(mFilter); + dest.writeInt(mLimit); + dest.writeInt(mOffset); + } + + /** + * Supports Parcelable + */ + public SearchParams(Parcel in) { + mMailboxId = in.readLong(); + mIncludeChildren = in.readInt() == 1; + mFilter = in.readString(); + mLimit = in.readInt(); + mOffset = in.readInt(); + } +} diff --git a/src/com/android/email/Controller.java b/src/com/android/email/Controller.java index cfe313889..3de1263fd 100644 --- a/src/com/android/email/Controller.java +++ b/src/com/android/email/Controller.java @@ -35,6 +35,7 @@ import com.android.emailcommon.provider.Mailbox; import com.android.emailcommon.service.EmailServiceStatus; import com.android.emailcommon.service.IEmailService; import com.android.emailcommon.service.IEmailServiceCallback; +import com.android.emailcommon.service.SearchParams; import com.android.emailcommon.utility.AttachmentUtilities; import com.android.emailcommon.utility.EmailAsyncTask; import com.android.emailcommon.utility.Utility; @@ -890,15 +891,13 @@ public class Controller { * Search for messages on the server; see {@Link EmailServiceProxy#searchMessages(long, long, * boolean, String, int, int, long)} for a complete description of this method's arguments. */ - public void searchMessages(final long accountId, final long mailboxId, - final boolean includeSubfolders, final String query, final int numResults, - final int firstResult, final long destMailboxId) { + public void searchMessages(final long accountId, final SearchParams searchParams, + final long destMailboxId) { IEmailService service = getServiceForAccount(accountId); if (service != null) { // Service implementation try { - service.searchMessages(accountId, mailboxId, includeSubfolders, query, numResults, - firstResult, destMailboxId); + service.searchMessages(accountId, searchParams, destMailboxId); } catch (RemoteException e) { // TODO Change exception handling to be consistent with however this method // is implemented for other protocols @@ -1774,8 +1773,8 @@ public class Controller { public void deleteAccountPIMData(long accountId) { } - public int searchMessages(long accountId, long mailboxId, boolean includeSubfolders, - String query, int numResults, int firstResult, long destMailboxId) { + public int searchMessages(long accountId, SearchParams searchParams, + long destMailboxId) { return 0; } diff --git a/src/com/android/email/ExchangeUtils.java b/src/com/android/email/ExchangeUtils.java index db757d146..57180e8d1 100644 --- a/src/com/android/email/ExchangeUtils.java +++ b/src/com/android/email/ExchangeUtils.java @@ -20,6 +20,7 @@ import com.android.emailcommon.Api; import com.android.emailcommon.service.EmailServiceProxy; import com.android.emailcommon.service.IEmailService; import com.android.emailcommon.service.IEmailServiceCallback; +import com.android.emailcommon.service.SearchParams; import android.app.Service; import android.content.Context; @@ -137,8 +138,7 @@ public class ExchangeUtils { public void deleteAccountPIMData(long accountId) throws RemoteException { } - public int searchMessages(long accountId, long mailboxId, boolean includeSubfolders, - String query, int numResults, int firstResult, long destMailboxId) { + public int searchMessages(long accountId, SearchParams searchParams, long destMailboxId) { return 0; } diff --git a/src/com/android/email/activity/EmailActivity.java b/src/com/android/email/activity/EmailActivity.java index dd4619d84..aefd79693 100644 --- a/src/com/android/email/activity/EmailActivity.java +++ b/src/com/android/email/activity/EmailActivity.java @@ -27,8 +27,8 @@ import com.android.emailcommon.provider.EmailContent.Account; import com.android.emailcommon.provider.EmailContent.MailboxColumns; import com.android.emailcommon.provider.EmailContent.Message; import com.android.emailcommon.provider.Mailbox; +import com.android.emailcommon.service.SearchParams; import com.android.emailcommon.utility.EmailAsyncTask; -import com.android.emailcommon.utility.Utility; import android.app.Activity; import android.app.AlertDialog; @@ -280,11 +280,12 @@ public class EmailActivity extends Activity implements View.OnClickListener { startActivity(createOpenMessageIntent(EmailActivity.this, accountId, searchMailbox.mId, msg.mId)); - Utility.runAsync(new Runnable() { + EmailAsyncTask.runAsyncParallel(new Runnable() { @Override public void run() { - controller.searchMessages(accountId, mailboxId, true, queryString, 10, 0, - searchMailbox.mId); + SearchParams searchSpec = new SearchParams(SearchParams.ALL_MAILBOXES, + queryString); + controller.searchMessages(accountId, searchSpec, searchMailbox.mId); }}); return; } @@ -361,16 +362,26 @@ public class EmailActivity extends Activity implements View.OnClickListener { @Override public boolean onPrepareOptionsMenu(Menu menu) { // STOPSHIP Temporary search/sync options UI - // Only show search/sync options for EAS + // Only show search/sync options for EAS 12.0 and later boolean isEas = false; + boolean canSearch = false; long accountId = mUIController.getActualAccountId(); if (accountId > 0) { + // Move database operations out of the UI thread if ("eas".equals(Account.getProtocol(mContext, accountId))) { isEas = true; + Account account = Account.restoreAccountWithId(mContext, accountId); + if (account != null) { + // We should set a flag in the account indicating ability to handle search + String protocolVersion = account.mProtocolVersion; + if (Double.parseDouble(protocolVersion) >= 12.0) { + canSearch = true; + } + } } } // Should use an isSearchable call to prevent search on inappropriate accounts/boxes - menu.findItem(R.id.search).setVisible(isEas); + menu.findItem(R.id.search).setVisible(canSearch); // Should use an isSyncable call to prevent drafts/outbox from allowing this menu.findItem(R.id.sync_lookback).setVisible(isEas); menu.findItem(R.id.sync_frequency).setVisible(isEas); diff --git a/tests/src/com/android/emailcommon/service/SearchParamsTests.java b/tests/src/com/android/emailcommon/service/SearchParamsTests.java new file mode 100644 index 000000000..36ce130da --- /dev/null +++ b/tests/src/com/android/emailcommon/service/SearchParamsTests.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2011 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.emailcommon.service; + +import android.os.Parcel; +import android.test.AndroidTestCase; + +public class SearchParamsTests extends AndroidTestCase { + public void testParcel() { + SearchParams params = new SearchParams(1, "query"); + params.mIncludeChildren = true; + params.mLimit = 66; + params.mOffset = 99; + Parcel parcel = Parcel.obtain(); + params.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + SearchParams readParams = SearchParams.CREATOR.createFromParcel(parcel); + assertEquals(params.mFilter, readParams.mFilter); + assertEquals(params.mIncludeChildren, readParams.mIncludeChildren); + assertEquals(params.mLimit, readParams.mLimit); + assertEquals(params.mOffset, readParams.mOffset); + assertEquals(params.mMailboxId, readParams.mMailboxId); + } +}