1/*
2 * Copyright (C) 2010 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
19import com.android.contacts.ContactsActivity;
20import com.android.contacts.R;
21
22import android.app.Activity;
23import android.app.AlertDialog;
24import android.app.Dialog;
25import android.app.DialogFragment;
26import android.content.DialogInterface;
27import android.content.DialogInterface.OnClickListener;
28import android.content.Intent;
29import android.net.Uri;
30import android.os.Bundle;
31import android.provider.ContactsContract.Contacts;
32import android.provider.ContactsContract.Intents.Insert;
33import android.text.TextUtils;
34
35/**
36 * Activity that intercepts DIAL and VIEW intents for phone numbers for devices that can not
37 * be used as a phone. This allows the user to see the phone number
38 */
39public class NonPhoneActivity extends ContactsActivity {
40    @Override
41    protected void onCreate(Bundle savedInstanceState) {
42        super.onCreate(savedInstanceState);
43        final String phoneNumber = getPhoneNumber();
44        if (TextUtils.isEmpty(phoneNumber)) {
45            finish();
46            return;
47        }
48
49        final NonPhoneDialogFragment fragment = new NonPhoneDialogFragment();
50        fragment.setArguments(Bundle.forPair("PHONE_NUMBER", phoneNumber));
51        getFragmentManager().beginTransaction().add(fragment, "Fragment").commitAllowingStateLoss();
52    }
53
54    private String getPhoneNumber() {
55        if (getIntent() == null) return null;
56        final Uri data = getIntent().getData();
57        if (data == null) return null;
58        final String scheme = data.getScheme();
59        if (!"tel".equals(scheme)) return null;
60        return getIntent().getData().getSchemeSpecificPart();
61    }
62
63    public static final class NonPhoneDialogFragment extends DialogFragment
64            implements OnClickListener {
65        @Override
66        public Dialog onCreateDialog(Bundle savedInstanceState) {
67            final AlertDialog alertDialog;
68            alertDialog = new AlertDialog.Builder(getActivity(), R.style.NonPhoneDialogTheme)
69                    .create();
70            alertDialog.setTitle(R.string.non_phone_caption);
71            alertDialog.setMessage(getArgumentPhoneNumber());
72            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
73                    getActivity().getString(R.string.non_phone_add_to_contacts), this);
74            alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
75                    getActivity().getString(R.string.non_phone_close), this);
76            return alertDialog;
77        }
78
79        @Override
80        public void onClick(DialogInterface dialog, int which) {
81            if (which == DialogInterface.BUTTON_POSITIVE) {
82                final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
83                intent.setType(Contacts.CONTENT_ITEM_TYPE);
84                intent.putExtra(Insert.PHONE, getArgumentPhoneNumber());
85                startActivity(intent);
86            }
87            dismiss();
88        }
89
90        private String getArgumentPhoneNumber() {
91            return getArguments().getPairValue();
92        }
93
94        @Override
95        public void onDismiss(DialogInterface dialog) {
96            super.onDismiss(dialog);
97            // During screen rotation, getActivity returns null. In this case we do not
98            // want to close the Activity anyway
99            final Activity activity = getActivity();
100            if (activity != null) activity.finish();
101        }
102    }
103}
104