12d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann/*
22d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann * Copyright (C) 2010 The Android Open Source Project
32d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann *
42d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann * Licensed under the Apache License, Version 2.0 (the "License");
52d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann * you may not use this file except in compliance with the License.
62d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann * You may obtain a copy of the License at
72d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann *
82d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann *      http://www.apache.org/licenses/LICENSE-2.0
92d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann *
102d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann * Unless required by applicable law or agreed to in writing, software
112d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann * distributed under the License is distributed on an "AS IS" BASIS,
122d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann * See the License for the specific language governing permissions and
142d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann * limitations under the License.
152d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann */
162d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann
17b93a26c62aef357e01080b574b9103babe6435b0Daniel Lehmannpackage com.android.contacts.socialwidget;
182d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann
192d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmannimport android.app.Activity;
202d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmannimport android.appwidget.AppWidgetManager;
212d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmannimport android.content.Intent;
222d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmannimport android.os.Bundle;
232d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmannimport android.provider.ContactsContract.Contacts;
242d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann
25b93a26c62aef357e01080b574b9103babe6435b0Daniel Lehmannpublic class SocialWidgetConfigureActivity extends Activity {
26b7cf3f0f1c8a843dad3258466b6a182fa74fba43Josh Gargus    private static final String KEY_LAUNCHED = "already_launched_picker_activity";
27b7cf3f0f1c8a843dad3258466b6a182fa74fba43Josh Gargus
282d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann    @Override
292d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann    protected void onCreate(Bundle savedInstanceState) {
302d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann        super.onCreate(savedInstanceState);
312d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann        // If the user presses back, we want to cancel
322d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann        setResult(RESULT_CANCELED);
332d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann
34b7cf3f0f1c8a843dad3258466b6a182fa74fba43Josh Gargus        // Don't launch contact-picker if we already launched it (for example, if
35b7cf3f0f1c8a843dad3258466b6a182fa74fba43Josh Gargus        // we launched it in a previous onCreate() and the device orientation changes
36b7cf3f0f1c8a843dad3258466b6a182fa74fba43Josh Gargus        // before the picker returns its result, then this activity will be recreated).
37b7cf3f0f1c8a843dad3258466b6a182fa74fba43Josh Gargus        if (savedInstanceState != null && savedInstanceState.getBoolean(KEY_LAUNCHED)) return;
38b7cf3f0f1c8a843dad3258466b6a182fa74fba43Josh Gargus
392d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann        // Forward the Intent to the picker
402d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann        final Intent pickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
41a32cce4b90d60df9dc487e82a9566746e17f1989Dmitri Plotnikov        pickerIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
422d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann        startActivityForResult(pickerIntent, 0);
432d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann    }
442d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann
452d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann    @Override
46b7cf3f0f1c8a843dad3258466b6a182fa74fba43Josh Gargus    protected void onSaveInstanceState(Bundle savedInstanceState) {
47b7cf3f0f1c8a843dad3258466b6a182fa74fba43Josh Gargus        super.onSaveInstanceState(savedInstanceState);
48b7cf3f0f1c8a843dad3258466b6a182fa74fba43Josh Gargus
49b7cf3f0f1c8a843dad3258466b6a182fa74fba43Josh Gargus        // We know for sure that we've launched the contact-picker... see onCreate()
50b7cf3f0f1c8a843dad3258466b6a182fa74fba43Josh Gargus        savedInstanceState.putBoolean(KEY_LAUNCHED, true);
51b7cf3f0f1c8a843dad3258466b6a182fa74fba43Josh Gargus    }
52b7cf3f0f1c8a843dad3258466b6a182fa74fba43Josh Gargus
53b7cf3f0f1c8a843dad3258466b6a182fa74fba43Josh Gargus    @Override
542d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
552d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann        // We came back from the Picker. If the user actually selected a contact,
562d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann        // return it now
572d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann        if (resultCode == Activity.RESULT_OK) {
582d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann            final Bundle extras = getIntent().getExtras();
592d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann            if (extras == null) throw new IllegalStateException("Intent extras are null");
602d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann            final int widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
612d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann                    AppWidgetManager.INVALID_APPWIDGET_ID);
622d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann
632d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann            // Save the setting
64b93a26c62aef357e01080b574b9103babe6435b0Daniel Lehmann            final SocialWidgetConfigureActivity context = SocialWidgetConfigureActivity.this;
653cceb3f9311d0f1c523fe264238aff5f0394ab32Daniel Lehmann            SocialWidgetSettings.getInstance().setContactUri(context, widgetId, data.getData());
662d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann
672d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann            // Update the widget
68192a1387fb364963ff313bdbc866e914d4188790Dmitri Plotnikov            SocialWidgetProvider.loadWidgetData(
6948f4b487a2b03a5f12796ebe94923fd7decb04d7Dmitri Plotnikov                    context, AppWidgetManager.getInstance(this), widgetId, true);
702d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann
712d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann            // Return OK so that the system won't remove the widget
722d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann            final Intent resultValue = new Intent();
732d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
742d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann            setResult(RESULT_OK, resultValue);
752d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann        }
762d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann        finish();
772d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann    }
782d4f759e29edd983cf6436c06f2a87d847616ed5Daniel Lehmann}
79