Example usage for android.database MatrixCursor getCount

List of usage examples for android.database MatrixCursor getCount

Introduction

In this page you can find the example usage for android.database MatrixCursor getCount.

Prototype

@Override
    public int getCount() 

Source Link

Usage

From source file:com.google.android.dialer.provider.DialerProvider.java

private Cursor buildResultCursor(String[] array, JSONArray jsonArray, int n) throws JSONException {
    int indexDisplayName = -1;
    int indexData3 = -1;
    int indexHasPhoneNumber = -1;
    int indexId = -1;
    int indexContactId = -1;
    int indexData1 = -1;
    int indexData2 = -1;
    int indexPhotoUri = -1;
    int indexPhotoThumbUri = -1;
    int indexLookup = -1;

    for (int i = 0; i < array.length; ++i) {
        String s = array[i];/*w w w  . ja  v  a  2  s. c  o  m*/

        if ("display_name".equals(s)) {
            indexDisplayName = i;
        } else if ("data3".equals(s)) {
            indexData3 = i;
        } else if ("has_phone_number".equals(s)) {
            indexHasPhoneNumber = i;
        } else if ("_id".equals(s)) {
            indexId = i;
        } else if ("contact_id".equals(s)) {
            indexContactId = i;
        } else if ("data1".equals(s)) {
            indexData1 = i;
        } else if ("data2".equals(s)) {
            indexData2 = i;
        } else if ("photo_uri".equals(s)) {
            indexPhotoUri = i;
        } else if ("photo_thumb_uri".equals(s)) {
            indexPhotoThumbUri = i;
        } else if ("lookup".equals(s)) {
            indexLookup = i;
        }
    }

    ContentResolver resolver = getContext().getContentResolver();
    boolean showNearbyDistance = Gservices.getBoolean(resolver, "dialer_debug_display_nearby_place_distance",
            false);

    int n12;
    if (Gservices.getBoolean(resolver, "dialer_enable_nearby_places_export", true)) {
        n12 = 2;
    } else {
        n12 = 0;
    }

    MatrixCursor matrixCursor = new MatrixCursor(array);
    JSONArray jsonArray2 = jsonArray.getJSONArray(1);

    int n13 = 1;
    int position = 0;

    while (position < jsonArray2.length()) {
        try {
            JSONArray jsonArray3 = jsonArray2.getJSONArray(position);
            String displayName = decodeHtml(jsonArray3.getString(0));
            JSONObject jsonObject = jsonArray3.getJSONObject(3);
            String data1 = decodeHtml(jsonObject.getString("b"));
            String data3 = decodeHtml(jsonObject.getString("g"));
            String optString = jsonObject.optString("f", null);
            String photoUri = jsonObject.optString("d", null);

            if (showNearbyDistance) {
                String miles = jsonObject.optString("c", null);
                if (miles != null) {
                    displayName = displayName + " [" + miles + " miles]";
                }
            }

            if (!data1.isEmpty()) {
                Object[] array2 = new Object[array.length];

                if (indexDisplayName >= 0) {
                    array2[indexDisplayName] = displayName;
                }
                if (indexData3 >= 0) {
                    array2[indexData3] = data3;
                }
                if (indexHasPhoneNumber >= 0) {
                    array2[indexHasPhoneNumber] = true;
                }
                if (indexContactId != -1) {
                    array2[indexContactId] = n13;
                }
                if (indexData1 >= 0) {
                    array2[indexData1] = data1;
                }
                if (indexData2 >= 0) {
                    array2[indexData2] = 12;
                }

                String photoThumbUri;
                if (photoUri == null) {
                    photoUri = new Uri.Builder().scheme("android.resource")
                            .authority("com.google.android.dialer")
                            .appendPath(String.valueOf(R.drawable.ic_places_picture_180_holo_light)).toString();
                    photoThumbUri = new Uri.Builder().scheme("android.resource")
                            .authority("com.google.android.dialer")
                            .appendPath(String.valueOf(R.drawable.ic_places_picture_holo_light)).toString();
                } else {
                    photoThumbUri = photoUri;
                }

                if (indexPhotoUri >= 0) {
                    array2[indexPhotoUri] = photoUri;
                }
                if (indexPhotoThumbUri >= 0) {
                    array2[indexPhotoThumbUri] = photoThumbUri;
                }
                if (indexLookup >= 0) {
                    JSONObject put = new JSONObject()
                            .put("vnd.android.cursor.item/name", new JSONObject().put("data1", displayName))
                            .put("vnd.android.cursor.item/phone_v2",
                                    JsonUtil.newJsonArray(
                                            new JSONObject().put("data1", data1).put("data2", 12)))
                            .put("vnd.android.cursor.item/postal-address_v2", JsonUtil.newJsonArray(
                                    new JSONObject().put("data1", displayName + ", " + data3).put("data2", 2)));

                    if (optString != null) {
                        put.put("vnd.android.cursor.item/website", JsonUtil
                                .newJsonArray(new JSONObject().put("data1", optString).put("data2", 3)));
                    }

                    array2[indexLookup] = new JSONObject().put("display_name", displayName)
                            .put("display_name_source", 30).put("exportSupport", n12).put("photo_uri", photoUri)
                            .put("vnd.android.cursor.item/contact", put).toString();
                }
                if (indexId != -1) {
                    array2[indexId] = n13;
                }
                matrixCursor.addRow(array2);
                if (n != -1 && matrixCursor.getCount() >= n) {
                    break;
                }

                n13++;
            }
        } catch (JSONException e) {
            Log.e("DialerProvider", "Skipped the suggestions at position " + position, e);
        }

        position++;
    }
    return matrixCursor;
}