1 /**
2  * Copyright (c) 2013, Google Inc.
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.mail.providers.protos.mock;
18 
19 import android.os.Bundle;
20 import android.os.Parcelable;
21 
22 import com.android.mail.utils.LogUtils;
23 import com.android.mail.utils.MatrixCursorWithCachedColumns;
24 
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28 
29 public class MockRespondMatrixCursor extends MatrixCursorWithCachedColumns{
30     private static final String LOG_TAG = "MockProvider";
31 
32     static final String MOCK_RESPOND_PREFIX = "respond_";
33 
34     final List<Map<String, Object>> mResultList;
35 
MockRespondMatrixCursor(final String[] columnNames, final int initialCapacity, List<Map<String, Object>> queryResults)36     public MockRespondMatrixCursor(final String[] columnNames, final int initialCapacity,
37             List<Map<String, Object>> queryResults) {
38         super(columnNames, initialCapacity);
39         mResultList = queryResults;
40     }
41 
42     @Override
respond(Bundle request)43     public Bundle respond(Bundle request) {
44         final Bundle response = new Bundle();
45 
46         final int pos = getPosition();
47         if (pos >= mResultList.size()) {
48             LogUtils.wtf(LOG_TAG, "Unexpected position");
49             return response;
50         }
51 
52         final Map<String, Object> rowData = mResultList.get(pos);
53 
54         // For each of the keys in the request, we want to see if there is a mock response for the
55         // request
56         final Set<String> bundleKeys = request.keySet();
57         for (String key : bundleKeys) {
58             final Object responseData = rowData.get(MOCK_RESPOND_PREFIX + key);
59             if (responseData != null) {
60                 response.putParcelable(key, (Parcelable)responseData);
61             }
62         }
63 
64         return response;
65     }
66 }
67