• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.contacts.socialwidget;
18 
19 import android.app.Activity;
20 import android.appwidget.AppWidgetManager;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.provider.ContactsContract.Contacts;
24 
25 public class SocialWidgetConfigureActivity extends Activity {
26     private static final String KEY_LAUNCHED = "already_launched_picker_activity";
27 
28     @Override
onCreate(Bundle savedInstanceState)29     protected void onCreate(Bundle savedInstanceState) {
30         super.onCreate(savedInstanceState);
31         // If the user presses back, we want to cancel
32         setResult(RESULT_CANCELED);
33 
34         // Don't launch contact-picker if we already launched it (for example, if
35         // we launched it in a previous onCreate() and the device orientation changes
36         // before the picker returns its result, then this activity will be recreated).
37         if (savedInstanceState != null && savedInstanceState.getBoolean(KEY_LAUNCHED)) return;
38 
39         // Forward the Intent to the picker
40         final Intent pickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
41         pickerIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
42         startActivityForResult(pickerIntent, 0);
43     }
44 
45     @Override
onSaveInstanceState(Bundle savedInstanceState)46     protected void onSaveInstanceState(Bundle savedInstanceState) {
47         super.onSaveInstanceState(savedInstanceState);
48 
49         // We know for sure that we've launched the contact-picker... see onCreate()
50         savedInstanceState.putBoolean(KEY_LAUNCHED, true);
51     }
52 
53     @Override
onActivityResult(int requestCode, int resultCode, Intent data)54     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
55         // We came back from the Picker. If the user actually selected a contact,
56         // return it now
57         if (resultCode == Activity.RESULT_OK) {
58             final Bundle extras = getIntent().getExtras();
59             if (extras == null) throw new IllegalStateException("Intent extras are null");
60             final int widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
61                     AppWidgetManager.INVALID_APPWIDGET_ID);
62 
63             // Save the setting
64             final SocialWidgetConfigureActivity context = SocialWidgetConfigureActivity.this;
65             SocialWidgetSettings.getInstance().setContactUri(context, widgetId, data.getData());
66 
67             // Update the widget
68             SocialWidgetProvider.loadWidgetData(
69                     context, AppWidgetManager.getInstance(this), widgetId, true);
70 
71             // Return OK so that the system won't remove the widget
72             final Intent resultValue = new Intent();
73             resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
74             setResult(RESULT_OK, resultValue);
75         }
76         finish();
77     }
78 }
79