1/*
2 * Copyright (C) 2009 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.activities;
18
19
20import com.android.contacts.ContactsActivity;
21import com.android.contacts.R;
22import com.android.contacts.list.ContactEntryListFragment;
23import com.android.contacts.list.JoinContactListFragment;
24import com.android.contacts.list.OnContactPickerActionListener;
25
26import android.app.ActionBar;
27import android.app.Fragment;
28import android.content.Intent;
29import android.net.Uri;
30import android.os.Bundle;
31import android.provider.ContactsContract;
32import android.util.Log;
33import android.view.MenuItem;
34
35/**
36 * An activity that shows a list of contacts that can be joined with the target contact.
37 */
38public class JoinContactActivity extends ContactsActivity {
39
40    private static final String TAG = "JoinContactActivity";
41
42    /**
43     * The action for the join contact activity.
44     * <p>
45     * Input: extra field {@link #EXTRA_TARGET_CONTACT_ID} is the aggregate ID.
46     * TODO: move to {@link ContactsContract}.
47     */
48    public static final String JOIN_CONTACT = "com.android.contacts.action.JOIN_CONTACT";
49
50    /**
51     * Used with {@link #JOIN_CONTACT} to give it the target for aggregation.
52     * <p>
53     * Type: LONG
54     */
55    public static final String EXTRA_TARGET_CONTACT_ID = "com.android.contacts.action.CONTACT_ID";
56
57    private static final String KEY_TARGET_CONTACT_ID = "targetContactId";
58
59    private long mTargetContactId;
60
61    private JoinContactListFragment mListFragment;
62
63    @Override
64    public void onAttachFragment(Fragment fragment) {
65        if (fragment instanceof JoinContactListFragment) {
66            mListFragment = (JoinContactListFragment) fragment;
67            setupActionListener();
68        }
69    }
70
71    @Override
72    protected void onCreate(Bundle savedInstanceState) {
73        super.onCreate(savedInstanceState);
74
75        Intent intent = getIntent();
76        mTargetContactId = intent.getLongExtra(EXTRA_TARGET_CONTACT_ID, -1);
77        if (mTargetContactId == -1) {
78            Log.e(TAG, "Intent " + intent.getAction() + " is missing required extra: "
79                    + EXTRA_TARGET_CONTACT_ID);
80            setResult(RESULT_CANCELED);
81            finish();
82            return;
83        }
84
85        setContentView(R.layout.join_contact_picker);
86        setTitle(R.string.titleJoinContactDataWith);
87
88        if (mListFragment == null) {
89            mListFragment = new JoinContactListFragment();
90
91            getFragmentManager().beginTransaction()
92                    .replace(R.id.list_container, mListFragment)
93                    .commitAllowingStateLoss();
94        }
95
96        final ActionBar actionBar = getActionBar();
97        if (actionBar != null) {
98            actionBar.setDisplayShowHomeEnabled(true);
99            actionBar.setDisplayHomeAsUpEnabled(true);
100            actionBar.setDisplayShowTitleEnabled(true);
101        }
102    }
103
104    private void setupActionListener() {
105        mListFragment.setTargetContactId(mTargetContactId);
106        mListFragment.setOnContactPickerActionListener(new OnContactPickerActionListener() {
107            @Override
108            public void onPickContactAction(Uri contactUri) {
109                Intent intent = new Intent(null, contactUri);
110                setResult(RESULT_OK, intent);
111                finish();
112            }
113
114            @Override
115            public void onShortcutIntentCreated(Intent intent) {
116            }
117
118            @Override
119            public void onCreateNewContactAction() {
120            }
121
122            @Override
123            public void onEditContactAction(Uri contactLookupUri) {
124            }
125        });
126    }
127
128    @Override
129    public boolean onOptionsItemSelected(MenuItem item) {
130        switch (item.getItemId()) {
131            case android.R.id.home:
132                // Go back to previous screen, intending "cancel"
133                setResult(RESULT_CANCELED);
134                finish();
135                return true;
136        }
137        return super.onOptionsItemSelected(item);
138    }
139
140    @Override
141    protected void onSaveInstanceState(Bundle outState) {
142        super.onSaveInstanceState(outState);
143        outState.putLong(KEY_TARGET_CONTACT_ID, mTargetContactId);
144    }
145
146    @Override
147    protected void onRestoreInstanceState(Bundle savedInstanceState) {
148        super.onRestoreInstanceState(savedInstanceState);
149        mTargetContactId = savedInstanceState.getLong(KEY_TARGET_CONTACT_ID);
150    }
151
152    @Override
153    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
154        if (requestCode == ContactEntryListFragment.ACTIVITY_REQUEST_CODE_PICKER
155                && resultCode == RESULT_OK) {
156            mListFragment.onPickerResult(data);
157        }
158    }
159}
160