121ef0f741f869669862fc3122a424edac97f4df4Flavio Lerda/*
221ef0f741f869669862fc3122a424edac97f4df4Flavio Lerda * Copyright (C) 2011 The Android Open Source Project
321ef0f741f869669862fc3122a424edac97f4df4Flavio Lerda *
421ef0f741f869669862fc3122a424edac97f4df4Flavio Lerda * Licensed under the Apache License, Version 2.0 (the "License");
521ef0f741f869669862fc3122a424edac97f4df4Flavio Lerda * you may not use this file except in compliance with the License.
621ef0f741f869669862fc3122a424edac97f4df4Flavio Lerda * You may obtain a copy of the License at
721ef0f741f869669862fc3122a424edac97f4df4Flavio Lerda *
821ef0f741f869669862fc3122a424edac97f4df4Flavio Lerda *      http://www.apache.org/licenses/LICENSE-2.0
921ef0f741f869669862fc3122a424edac97f4df4Flavio Lerda *
1021ef0f741f869669862fc3122a424edac97f4df4Flavio Lerda * Unless required by applicable law or agreed to in writing, software
1121ef0f741f869669862fc3122a424edac97f4df4Flavio Lerda * distributed under the License is distributed on an "AS IS" BASIS,
1221ef0f741f869669862fc3122a424edac97f4df4Flavio Lerda * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1321ef0f741f869669862fc3122a424edac97f4df4Flavio Lerda * See the License for the specific language governing permissions and
1421ef0f741f869669862fc3122a424edac97f4df4Flavio Lerda * limitations under the License.
1521ef0f741f869669862fc3122a424edac97f4df4Flavio Lerda */
16a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda
17a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerdapackage com.android.contacts.calllog;
18a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda
19a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerdaimport com.android.contacts.CallDetailActivity;
20fadd5e1f1845b8d98e247bbd30535928dc5e0f81Daisuke Miyakawaimport com.android.contacts.ContactsUtils;
21a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda
22a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerdaimport android.content.ContentUris;
23a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerdaimport android.content.Context;
24a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerdaimport android.content.Intent;
2582b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerdaimport android.database.Cursor;
26a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerdaimport android.net.Uri;
27a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerdaimport android.provider.CallLog.Calls;
28a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerdaimport android.telephony.PhoneNumberUtils;
29a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda
30a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda/**
31a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda * Used to create an intent to attach to an action in the call log.
32a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda * <p>
33a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda * The intent is constructed lazily with the given information.
34a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda */
35a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerdapublic abstract class IntentProvider {
36a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda    public abstract Intent getIntent(Context context);
37a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda
38a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda    public static IntentProvider getReturnCallIntentProvider(final String number) {
39a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda        return new IntentProvider() {
40a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda            @Override
41a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda            public Intent getIntent(Context context) {
42fadd5e1f1845b8d98e247bbd30535928dc5e0f81Daisuke Miyakawa                return ContactsUtils.getCallIntent(number);
43a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda            }
44a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda        };
45a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda    }
46a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda
47a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda    public static IntentProvider getPlayVoicemailIntentProvider(final long rowId,
48a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda            final String voicemailUri) {
49a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda        return new IntentProvider() {
50a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda            @Override
51a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda            public Intent getIntent(Context context) {
52a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda                Intent intent = new Intent(context, CallDetailActivity.class);
53a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda                intent.setData(ContentUris.withAppendedId(
54a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda                        Calls.CONTENT_URI_WITH_VOICEMAIL, rowId));
55a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda                if (voicemailUri != null) {
56a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda                    intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_URI,
57a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda                            Uri.parse(voicemailUri));
58a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda                }
59a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda                intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_START_PLAYBACK, true);
60a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda                return intent;
61a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda            }
62a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda        };
63a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda    }
6482b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda
6582b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda    public static IntentProvider getCallDetailIntentProvider(
6682b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda            final CallLogAdapter adapter, final int position, final long id, final int groupSize) {
6782b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda        return new IntentProvider() {
6882b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda            @Override
6982b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda            public Intent getIntent(Context context) {
7082b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                Cursor cursor = adapter.getCursor();
7182b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                cursor.moveToPosition(position);
7282b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                if (CallLogQuery.isSectionHeader(cursor)) {
7382b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                    // Do nothing when a header is clicked.
7482b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                    return null;
7582b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                }
7682b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                Intent intent = new Intent(context, CallDetailActivity.class);
77b4625e72331742bfe1291a49e409b165c6799047Flavio Lerda                // Check if the first item is a voicemail.
78b4625e72331742bfe1291a49e409b165c6799047Flavio Lerda                String voicemailUri = cursor.getString(CallLogQuery.VOICEMAIL_URI);
79b4625e72331742bfe1291a49e409b165c6799047Flavio Lerda                if (voicemailUri != null) {
80b4625e72331742bfe1291a49e409b165c6799047Flavio Lerda                    intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_URI,
81b4625e72331742bfe1291a49e409b165c6799047Flavio Lerda                            Uri.parse(voicemailUri));
82b4625e72331742bfe1291a49e409b165c6799047Flavio Lerda                }
83b4625e72331742bfe1291a49e409b165c6799047Flavio Lerda                intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_START_PLAYBACK, false);
84b4625e72331742bfe1291a49e409b165c6799047Flavio Lerda
85b4625e72331742bfe1291a49e409b165c6799047Flavio Lerda                if (groupSize > 1) {
8682b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                    // We want to restore the position in the cursor at the end.
8782b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                    long[] ids = new long[groupSize];
8882b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                    // Copy the ids of the rows in the group.
8982b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                    for (int index = 0; index < groupSize; ++index) {
9082b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                        ids[index] = cursor.getLong(CallLogQuery.ID);
9182b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                        cursor.moveToNext();
9282b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                    }
9382b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                    intent.putExtra(CallDetailActivity.EXTRA_CALL_LOG_IDS, ids);
9482b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                } else {
9582b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                    // If there is a single item, use the direct URI for it.
9682b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                    intent.setData(ContentUris.withAppendedId(
9782b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                            Calls.CONTENT_URI_WITH_VOICEMAIL, id));
9882b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                }
9982b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda                return intent;
10082b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda            }
10182b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda        };
10282b7c07b33fe5f3e2bfdee1d5f4c68060a337028Flavio Lerda    }
103a113689156ac38177fb8fdf82e5327c3f916d331Flavio Lerda}
104