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