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.socialwidget;
18
19import android.app.Activity;
20import android.appwidget.AppWidgetManager;
21import android.content.Intent;
22import android.os.Bundle;
23import android.provider.ContactsContract.Contacts;
24
25public class SocialWidgetConfigureActivity extends Activity {
26    @Override
27    protected void onCreate(Bundle savedInstanceState) {
28        super.onCreate(savedInstanceState);
29        // If the user presses back, we want to cancel
30        setResult(RESULT_CANCELED);
31
32        // Forward the Intent to the picker
33        final Intent pickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
34        pickerIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
35        startActivityForResult(pickerIntent, 0);
36    }
37
38    @Override
39    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
40        // We came back from the Picker. If the user actually selected a contact,
41        // return it now
42        if (resultCode == Activity.RESULT_OK) {
43            final Bundle extras = getIntent().getExtras();
44            if (extras == null) throw new IllegalStateException("Intent extras are null");
45            final int widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
46                    AppWidgetManager.INVALID_APPWIDGET_ID);
47
48            // Save the setting
49            final SocialWidgetConfigureActivity context = SocialWidgetConfigureActivity.this;
50            SocialWidgetSettings.getInstance().setContactUri(context, widgetId, data.getData());
51
52            // Update the widget
53            SocialWidgetProvider.loadWidgetData(
54                    context, AppWidgetManager.getInstance(this), widgetId, true);
55
56            // Return OK so that the system won't remove the widget
57            final Intent resultValue = new Intent();
58            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
59            setResult(RESULT_OK, resultValue);
60        }
61        finish();
62    }
63}
64