1/*
2 * Copyright (C) 2011 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.calllog;
18
19import com.android.contacts.CallDetailActivity;
20
21import android.content.ContentUris;
22import android.content.Context;
23import android.content.Intent;
24import android.database.Cursor;
25import android.net.Uri;
26import android.provider.CallLog.Calls;
27import android.telephony.PhoneNumberUtils;
28
29/**
30 * Used to create an intent to attach to an action in the call log.
31 * <p>
32 * The intent is constructed lazily with the given information.
33 */
34public abstract class IntentProvider {
35    public abstract Intent getIntent(Context context);
36
37    public static IntentProvider getReturnCallIntentProvider(final String number) {
38        return new IntentProvider() {
39            @Override
40            public Intent getIntent(Context context) {
41                // Here, "number" can either be a PSTN phone number or a
42                // SIP address.  So turn it into either a tel: URI or a
43                // sip: URI, as appropriate.
44                Uri uri;
45                if (PhoneNumberUtils.isUriNumber(number)) {
46                    uri = Uri.fromParts("sip", number, null);
47                } else {
48                    uri = Uri.fromParts("tel", number, null);
49                }
50                return new Intent(Intent.ACTION_CALL_PRIVILEGED, uri);
51            }
52        };
53    }
54
55    public static IntentProvider getPlayVoicemailIntentProvider(final long rowId,
56            final String voicemailUri) {
57        return new IntentProvider() {
58            @Override
59            public Intent getIntent(Context context) {
60                Intent intent = new Intent(context, CallDetailActivity.class);
61                intent.setData(ContentUris.withAppendedId(
62                        Calls.CONTENT_URI_WITH_VOICEMAIL, rowId));
63                if (voicemailUri != null) {
64                    intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_URI,
65                            Uri.parse(voicemailUri));
66                }
67                intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_START_PLAYBACK, true);
68                return intent;
69            }
70        };
71    }
72
73    public static IntentProvider getCallDetailIntentProvider(
74            final CallLogAdapter adapter, final int position, final long id, final int groupSize) {
75        return new IntentProvider() {
76            @Override
77            public Intent getIntent(Context context) {
78                Cursor cursor = adapter.getCursor();
79                cursor.moveToPosition(position);
80                if (CallLogQuery.isSectionHeader(cursor)) {
81                    // Do nothing when a header is clicked.
82                    return null;
83                }
84                Intent intent = new Intent(context, CallDetailActivity.class);
85                // Check if the first item is a voicemail.
86                String voicemailUri = cursor.getString(CallLogQuery.VOICEMAIL_URI);
87                if (voicemailUri != null) {
88                    intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_URI,
89                            Uri.parse(voicemailUri));
90                }
91                intent.putExtra(CallDetailActivity.EXTRA_VOICEMAIL_START_PLAYBACK, false);
92
93                if (groupSize > 1) {
94                    // We want to restore the position in the cursor at the end.
95                    long[] ids = new long[groupSize];
96                    // Copy the ids of the rows in the group.
97                    for (int index = 0; index < groupSize; ++index) {
98                        ids[index] = cursor.getLong(CallLogQuery.ID);
99                        cursor.moveToNext();
100                    }
101                    intent.putExtra(CallDetailActivity.EXTRA_CALL_LOG_IDS, ids);
102                } else {
103                    // If there is a single item, use the direct URI for it.
104                    intent.setData(ContentUris.withAppendedId(
105                            Calls.CONTENT_URI_WITH_VOICEMAIL, id));
106                }
107                return intent;
108            }
109        };
110    }
111}
112