Prevent NPE's in calls to database utility methods

* Before the Email/Exchange split, EmailProvider was guaranteed not to return a null
  cursor; this is no longer true
* We now throw a new RuntimeException (ProviderUnavailableException) when calling
  commonly-used utility methods (e.g. restoreXWithId) and the cursor as returned from
  EmailProvider is null (which implies that the provider isn't available).

Change-Id: I152d13bff0129c23586cd570d23c87d69cfce372
This commit is contained in:
Marc Blank 2011-05-05 08:29:30 -07:00
parent 335a724ee6
commit 74143e89d5
3 changed files with 33 additions and 6 deletions

View File

@ -138,7 +138,7 @@ public abstract class EmailContent {
Class<T> klass, Uri contentUri, String[] contentProjection, long id) {
Uri u = ContentUris.withAppendedId(contentUri, id);
Cursor c = context.getContentResolver().query(u, contentProjection, null, null, null);
if (c == null) throw new ProviderUnavailableException();
try {
if (c.moveToFirst()) {
return (T)getContent(c, klass);
@ -322,6 +322,11 @@ public abstract class EmailContent {
return values;
}
/**
* Given a cursor, restore a Body from it
* @param cursor a cursor which must NOT be null
* @return the Body as restored from the cursor
*/
private static Body restoreBodyWithCursor(Cursor cursor) {
try {
if (cursor.moveToFirst()) {
@ -338,6 +343,7 @@ public abstract class EmailContent {
Uri u = ContentUris.withAppendedId(Body.CONTENT_URI, id);
Cursor c = context.getContentResolver().query(u, Body.CONTENT_PROJECTION,
null, null, null);
if (c == null) throw new ProviderUnavailableException();
return restoreBodyWithCursor(c);
}
@ -345,6 +351,7 @@ public abstract class EmailContent {
Cursor c = context.getContentResolver().query(Body.CONTENT_URI,
Body.CONTENT_PROJECTION, Body.MESSAGE_KEY + "=?",
new String[] {Long.toString(messageId)}, null);
if (c == null) throw new ProviderUnavailableException();
return restoreBodyWithCursor(c);
}
@ -387,6 +394,7 @@ public abstract class EmailContent {
String[] projection) {
Cursor c = context.getContentResolver().query(Body.CONTENT_URI, projection,
Body.MESSAGE_KEY + "=?", new String[] {Long.toString(messageId)}, null);
if (c == null) throw new ProviderUnavailableException();
try {
if (c.moveToFirst()) {
return c.getString(COMMON_PROJECTION_COLUMN_TEXT);
@ -2429,8 +2437,7 @@ public abstract class EmailContent {
Mailbox.PATH_AND_ACCOUNT_SELECTION,
new String[] { path, Long.toString(accountId) },
null);
// TODO for mblank; uncomment when you submit CL Iab059f9a68eecd797914a6229f1ff9c03d0f0800
// if (c == null) throw new ProviderUnavailableException();
if (c == null) throw new ProviderUnavailableException();
try {
Mailbox mailbox = null;
if (c.moveToFirst()) {

View File

@ -0,0 +1,21 @@
/*
* 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.provider;
public class ProviderUnavailableException extends RuntimeException {
private static final long serialVersionUID = 1L;
}

View File

@ -28,6 +28,7 @@ import com.android.emailcommon.provider.EmailContent.Mailbox;
import com.android.emailcommon.provider.EmailContent.MailboxColumns;
import com.android.emailcommon.provider.EmailContent.Message;
import com.android.emailcommon.provider.EmailContent.MessageColumns;
import com.android.emailcommon.provider.ProviderUnavailableException;
import android.app.Activity;
import android.content.ContentResolver;
@ -42,8 +43,6 @@ import android.os.AsyncTask;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.StrictMode;
import android.provider.OpenableColumns;
import android.text.Spannable;
@ -53,7 +52,6 @@ import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.util.Base64;
import android.util.Log;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
@ -325,6 +323,7 @@ public class Utility {
ContentResolver resolver = context.getContentResolver();
Cursor c = resolver.query(HostAuth.CONTENT_URI, HostAuth.ID_PROJECTION,
HOSTAUTH_WHERE_CREDENTIALS, new String[] { hostName, userLogin }, null);
if (c == null) throw new ProviderUnavailableException();
try {
while (c.moveToNext()) {
long hostAuthId = c.getLong(HostAuth.ID_PROJECTION_COLUMN);