JoinContactActivity.java revision d8f84e076b762f063ae498c297d6f02574099dd2
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.Fragment;
27import android.content.Intent;
28import android.net.Uri;
29import android.os.Bundle;
30import android.provider.ContactsContract;
31import android.util.Log;
32import android.view.Menu;
33import android.view.MenuInflater;
34import android.view.MenuItem;
35import android.view.View;
36import android.view.View.OnClickListener;
37
38/**
39 * An activity that shows a list of contacts that can be joined with the target contact.
40 */
41public class JoinContactActivity extends ContactsActivity implements OnClickListener {
42
43    private static final String TAG = "JoinContactActivity";
44
45    /**
46     * The action for the join contact activity.
47     * <p>
48     * Input: extra field {@link #EXTRA_TARGET_CONTACT_ID} is the aggregate ID.
49     * TODO: move to {@link ContactsContract}.
50     */
51    public static final String JOIN_CONTACT = "com.android.contacts.action.JOIN_CONTACT";
52
53    /**
54     * Used with {@link #JOIN_CONTACT} to give it the target for aggregation.
55     * <p>
56     * Type: LONG
57     */
58    public static final String EXTRA_TARGET_CONTACT_ID = "com.android.contacts.action.CONTACT_ID";
59
60    private static final String KEY_TARGET_CONTACT_ID = "targetContactId";
61
62    private long mTargetContactId;
63
64    private JoinContactListFragment mListFragment;
65
66    @Override
67    public void onAttachFragment(Fragment fragment) {
68        if (fragment instanceof JoinContactListFragment) {
69            mListFragment = (JoinContactListFragment) fragment;
70            setupActionListener();
71        }
72    }
73
74    @Override
75    protected void onCreate(Bundle savedInstanceState) {
76        super.onCreate(savedInstanceState);
77
78        Intent intent = getIntent();
79        mTargetContactId = intent.getLongExtra(EXTRA_TARGET_CONTACT_ID, -1);
80        if (mTargetContactId == -1) {
81            Log.e(TAG, "Intent " + intent.getAction() + " is missing required extra: "
82                    + EXTRA_TARGET_CONTACT_ID);
83            setResult(RESULT_CANCELED);
84            finish();
85            return;
86        }
87
88        setContentView(R.layout.join_contact_picker);
89        setTitle(R.string.titleJoinContactDataWith);
90
91        findViewById(R.id.cancel).setOnClickListener(this);
92
93        if (mListFragment == null) {
94            mListFragment = new JoinContactListFragment();
95
96            getFragmentManager().beginTransaction()
97                    .replace(R.id.list_container, mListFragment)
98                    .commit();
99        }
100    }
101
102    public void setupActionListener() {
103        mListFragment.setTargetContactId(mTargetContactId);
104        mListFragment.setOnContactPickerActionListener(new OnContactPickerActionListener() {
105            @Override
106            public void onPickContactAction(Uri contactUri) {
107                Intent intent = new Intent(null, contactUri);
108                setResult(RESULT_OK, intent);
109                finish();
110            }
111
112            @Override
113            public void onShortcutIntentCreated(Intent intent) {
114            }
115
116            @Override
117            public void onCreateNewContactAction() {
118            }
119
120            @Override
121            public void onEditContactAction(Uri contactLookupUri) {
122            }
123        });
124    }
125
126    @Override
127    protected void onSaveInstanceState(Bundle outState) {
128        super.onSaveInstanceState(outState);
129        outState.putLong(KEY_TARGET_CONTACT_ID, mTargetContactId);
130    }
131
132    @Override
133    protected void onRestoreInstanceState(Bundle savedInstanceState) {
134        super.onRestoreInstanceState(savedInstanceState);
135        mTargetContactId = savedInstanceState.getLong(KEY_TARGET_CONTACT_ID);
136    }
137
138    @Override
139    public boolean onCreateOptionsMenu(Menu menu) {
140        MenuInflater inflater = getMenuInflater();
141        inflater.inflate(R.menu.search, menu);
142        return true;
143    }
144
145    @Override
146    public boolean onOptionsItemSelected(MenuItem item) {
147        switch (item.getItemId()) {
148            case R.id.menu_search: {
149                onSearchRequested();
150                return true;
151            }
152        }
153        return false;
154    }
155
156    @Override
157    public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
158            boolean globalSearch) {
159        if (globalSearch) {
160            super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
161        } else {
162            mListFragment.startSearch(initialQuery);
163        }
164    }
165
166    @Override
167    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
168        if (requestCode == ContactEntryListFragment.ACTIVITY_REQUEST_CODE_PICKER
169                && resultCode == RESULT_OK) {
170            mListFragment.onPickerResult(data);
171        }
172    }
173
174    @Override
175    public void onClick(View v) {
176        if (v.getId() == R.id.cancel) {
177            finish();
178        }
179    }
180}
181