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
17package com.android.contacts.tests.allintents;
18
19import android.app.Activity;
20import android.content.ContentUris;
21import android.content.Intent;
22import android.database.Cursor;
23import android.graphics.Bitmap;
24import android.graphics.BitmapFactory;
25import android.graphics.Color;
26import android.net.Uri;
27import android.os.Bundle;
28import android.os.Parcelable;
29import android.provider.ContactsContract.CommonDataKinds.Photo;
30import android.provider.ContactsContract.Contacts;
31import android.provider.ContactsContract.Data;
32import android.text.TextUtils;
33import android.view.View;
34import android.widget.ImageView;
35import android.widget.ImageView.ScaleType;
36import android.widget.TableLayout;
37import android.widget.TableRow;
38import android.widget.TextView;
39
40import com.android.contacts.tests.R;
41
42import java.util.Arrays;
43
44/**
45 * An activity that shows the result of a contacts activity invocation.
46 */
47public class ResultActivity extends Activity {
48
49    @Override
50    protected void onCreate(Bundle savedInstanceState) {
51        super.onCreate(savedInstanceState);
52        setContentView(R.layout.result);
53
54        Intent intent = getIntent();
55        addRowsForIntent((Intent)intent.getExtras().get("data"));
56    }
57
58    private void addRowsForIntent(Intent intent) {
59        if (intent == null) {
60            addRow("", "No data intent returned");
61        } else {
62            addRow("INTENT", intent.toString());
63            addSeparator(3);
64
65            Bundle extras = intent.getExtras();
66            if (extras != null && !extras.isEmpty()) {
67                for (String key : extras.keySet()) {
68                    Object value = extras.get(key);
69                    addRow("EXTRA", key);
70                    addRowForValue("", value);
71                }
72
73                addSeparator(3);
74            }
75
76            String dataUri = intent.getDataString();
77            if (dataUri != null) {
78                addRowsForQuery(Uri.parse(dataUri));
79            }
80        }
81    }
82
83    private void addRowForValue(String label, Object value) {
84        if (value == null) {
85            addRow(label, "null");
86        } else if (value instanceof Bitmap) {
87            addRowWithBitmap(label, (Bitmap)value);
88        } else if (value instanceof Intent) {
89            addRow(label, "INTENT");
90            addRowsForIntent((Intent)value);
91        } else if (value instanceof Uri) {
92            addRow(label, "DATA");
93            addRowsForQuery((Uri)value);
94        } else if (value.getClass().isArray()) {
95            addRow(label, "ARRAY");
96            Parcelable[] array = (Parcelable[])value;
97            for (int i = 0; i < array.length; i++) {
98                addRowForValue("[" + i + "]", String.valueOf(array[i]));
99            }
100        } else {
101            addRow(label, String.valueOf(value));
102        }
103    }
104
105    private void addRowsForQuery(Uri dataUri) {
106        Cursor cursor = getContentResolver().query(dataUri, null, null, null, null);
107        if (cursor == null) {
108            addRow("", "No data for this URI");
109        } else {
110            try {
111                while (cursor.moveToNext()) {
112                    addRow("", "DATA");
113                    String[] columnNames = cursor.getColumnNames();
114                    String[] names = new String[columnNames.length];
115                    System.arraycopy(columnNames, 0, names, 0, columnNames.length);
116                    Arrays.sort(names);
117                    for (int i = 0; i < names.length; i++) {
118                        int index = cursor.getColumnIndex(names[i]);
119                        String value = cursor.getString(index);
120                        addRow(names[i], value);
121
122                        if (names[i].equals(Contacts.PHOTO_ID) && !TextUtils.isEmpty(value)) {
123                            addRowWithPhoto(Long.parseLong(value));
124                        }
125                    }
126                }
127            } finally {
128                cursor.close();
129            }
130        }
131    }
132
133    private void addRow(String column0, String column1) {
134        TextView label = new TextView(this);
135        label.setPadding(4, 4, 4, 4);
136        label.setText(column0);
137        TextView value = new TextView(this);
138        value.setPadding(4, 4, 4, 4);
139        value.setText(column1);
140        addRow(label, value);
141    }
142
143    private void addRowWithPhoto(long photoId) {
144        byte[] data = null;
145        Cursor cursor = getContentResolver().query(
146                ContentUris.withAppendedId(Data.CONTENT_URI, photoId),
147                new String[]{Photo.PHOTO}, null, null, null);
148        try {
149            if (cursor.moveToNext()) {
150                data = cursor.getBlob(0);
151            }
152        } finally {
153            if (cursor != null) {
154                cursor.close();
155            }
156        }
157
158        if (data == null) {
159            return;
160        }
161
162        addRowWithBitmap("Photo", BitmapFactory.decodeByteArray(data, 0, data.length));
163    }
164
165    private void addRowWithBitmap(String label, Bitmap bitmap) {
166        TextView labelView = new TextView(this);
167        labelView.setPadding(4, 4, 4, 4);
168        labelView.setText(label);
169
170        ImageView imageView = new ImageView(this);
171        imageView.setImageBitmap(bitmap);
172        imageView.setPadding(4, 4, 4, 4);
173        imageView.setScaleType(ScaleType.FIT_START);
174        addRow(labelView, imageView);
175    }
176
177    private void addRow(View column0, View column1) {
178        TableLayout table = (TableLayout)findViewById(R.id.table);
179        TableRow row = new TableRow(this);
180        row.addView(column0);
181        row.addView(column1);
182        table.addView(row);
183
184        addSeparator(1);
185    }
186
187    private void addSeparator(int height) {
188        TableLayout table = (TableLayout)findViewById(R.id.table);
189        View separator = new View(this);
190        TableLayout.LayoutParams params = new TableLayout.LayoutParams();
191        params.height = height;
192        separator.setLayoutParams(params);
193        separator.setBackgroundColor(Color.rgb(33, 66, 33));
194        table.addView(separator);
195    }
196}
197