1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.exchange.provider; 18 19 import com.android.exchange.EasSyncService; 20 import com.android.exchange.provider.GalResult.GalData; 21 22 import android.content.ContentProvider; 23 import android.content.ContentValues; 24 import android.content.UriMatcher; 25 import android.database.Cursor; 26 import android.database.MatrixCursor; 27 import android.net.Uri; 28 import android.os.Bundle; 29 30 /** 31 * ExchangeProvider provides real-time data from the Exchange server; at the moment, it is used 32 * solely to provide GAL (Global Address Lookup) service to email address adapters 33 */ 34 public class ExchangeProvider extends ContentProvider { 35 public static final String EXCHANGE_AUTHORITY = "com.android.exchange.provider"; 36 public static final Uri GAL_URI = Uri.parse("content://" + EXCHANGE_AUTHORITY + "/gal/"); 37 38 private static final int GAL_BASE = 0; 39 private static final int GAL_FILTER = GAL_BASE; 40 41 public static final String[] GAL_PROJECTION = new String[] {"_id", "displayName", "data"}; 42 public static final int GAL_COLUMN_ID = 0; 43 public static final int GAL_COLUMN_DISPLAYNAME = 1; 44 public static final int GAL_COLUMN_DATA = 2; 45 46 public static final String EXTRAS_TOTAL_RESULTS = "com.android.exchange.provider.TOTAL_RESULTS"; 47 48 private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH); 49 50 static { 51 // Exchange URI matching table 52 UriMatcher matcher = sURIMatcher; 53 // The URI for GAL lookup contains three user-supplied parameters in the path: 54 // 1) the account id of the Exchange account 55 // 2) the constraint (filter) text 56 matcher.addURI(EXCHANGE_AUTHORITY, "gal/*/*", GAL_FILTER); 57 } 58 59 private static void addGalDataRow(MatrixCursor mc, long id, String name, String address) { 60 mc.newRow().add(id).add(name).add(address); 61 } 62 63 @Override 64 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 65 String sortOrder) { 66 int match = sURIMatcher.match(uri); 67 switch (match) { 68 case GAL_FILTER: 69 long accountId = -1; 70 // Pull out our parameters 71 MatrixCursorExtras c = new MatrixCursorExtras(GAL_PROJECTION); 72 String accountIdString = uri.getPathSegments().get(1); 73 String filter = uri.getPathSegments().get(2); 74 // Make sure we get a valid id; otherwise throw an exception 75 try { 76 accountId = Long.parseLong(accountIdString); 77 } catch (NumberFormatException e) { 78 throw new IllegalArgumentException("Illegal value in URI"); 79 } 80 // Get results from the Exchange account 81 GalResult galResult = EasSyncService.searchGal(getContext(), accountId, filter); 82 if (galResult != null) { 83 for (GalData data : galResult.galData) { 84 addGalDataRow(c, data._id, data.displayName, data.emailAddress); 85 } 86 // Use cursor side channel to report metadata 87 final Bundle bundle = new Bundle(); 88 bundle.putInt(EXTRAS_TOTAL_RESULTS, galResult.total); 89 c.setExtras(bundle); 90 } 91 return c; 92 default: 93 throw new IllegalArgumentException("Unknown URI " + uri); 94 } 95 } 96 97 @Override 98 public int delete(Uri uri, String selection, String[] selectionArgs) { 99 return -1; 100 } 101 102 @Override 103 public String getType(Uri uri) { 104 return "vnd.android.cursor.dir/gal-entry"; 105 } 106 107 @Override 108 public Uri insert(Uri uri, ContentValues values) { 109 return null; 110 } 111 112 @Override 113 public boolean onCreate() { 114 return false; 115 } 116 117 @Override 118 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 119 return -1; 120 } 121 122 /** 123 * A simple extension to MatrixCursor that supports extras 124 */ 125 private static class MatrixCursorExtras extends MatrixCursor { 126 127 private Bundle mExtras; 128 129 public MatrixCursorExtras(String[] columnNames) { 130 super(columnNames); 131 mExtras = null; 132 } 133 134 public void setExtras(Bundle extras) { 135 mExtras = extras; 136 } 137 138 @Override 139 public Bundle getExtras() { 140 return mExtras; 141 } 142 } 143 } 144