blob: 8ae96a279a8ef5881252cef5d6641102b45a714f [file] [log] [blame]
Chiao Cheng3483b812012-10-23 16:26:40 -07001/*
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;
18
Brian Attwellc6100ff2015-02-19 21:35:36 -080019import com.android.contacts.common.util.ImplicitIntentsUtil;
20
Chiao Cheng3483b812012-10-23 16:26:40 -070021import android.app.Activity;
22import android.app.AlertDialog;
23import android.app.Dialog;
24import android.app.DialogFragment;
25import android.content.DialogInterface;
26import android.content.DialogInterface.OnClickListener;
27import android.content.Intent;
28import android.net.Uri;
29import android.os.Bundle;
30import android.provider.ContactsContract.Contacts;
31import android.provider.ContactsContract.Intents.Insert;
Tyler Gunn03192222014-09-10 15:20:09 -070032import android.telecom.PhoneAccount;
Chiao Cheng3483b812012-10-23 16:26:40 -070033import android.text.TextUtils;
34
Chiao Cheng3483b812012-10-23 16:26:40 -070035/**
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
41 private static final String PHONE_NUMBER_KEY = "PHONE_NUMBER";
42
43 @Override
44 protected void onCreate(Bundle savedInstanceState) {
45 super.onCreate(savedInstanceState);
46 final String phoneNumber = getPhoneNumber();
47 if (TextUtils.isEmpty(phoneNumber)) {
48 finish();
49 return;
50 }
51
52 final NonPhoneDialogFragment fragment = new NonPhoneDialogFragment();
53 Bundle bundle = new Bundle();
54 bundle.putString(PHONE_NUMBER_KEY, phoneNumber);
55 fragment.setArguments(bundle);
56 getFragmentManager().beginTransaction().add(fragment, "Fragment").commitAllowingStateLoss();
57 }
58
59 private String getPhoneNumber() {
60 if (getIntent() == null) return null;
61 final Uri data = getIntent().getData();
62 if (data == null) return null;
63 final String scheme = data.getScheme();
Jay Shrauner1cd88e32014-09-05 15:37:55 -070064 if (!PhoneAccount.SCHEME_TEL.equals(scheme)) return null;
Chiao Cheng3483b812012-10-23 16:26:40 -070065 return getIntent().getData().getSchemeSpecificPart();
66 }
67
68 public static final class NonPhoneDialogFragment extends DialogFragment
69 implements OnClickListener {
70 @Override
71 public Dialog onCreateDialog(Bundle savedInstanceState) {
72 final AlertDialog alertDialog;
73 alertDialog = new AlertDialog.Builder(getActivity(), R.style.NonPhoneDialogTheme)
74 .create();
75 alertDialog.setTitle(R.string.non_phone_caption);
76 alertDialog.setMessage(getArgumentPhoneNumber());
77 alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
78 getActivity().getString(R.string.non_phone_add_to_contacts), this);
79 alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
80 getActivity().getString(R.string.non_phone_close), this);
81 return alertDialog;
82 }
83
84 @Override
85 public void onClick(DialogInterface dialog, int which) {
86 if (which == DialogInterface.BUTTON_POSITIVE) {
87 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
88 intent.setType(Contacts.CONTENT_ITEM_TYPE);
89 intent.putExtra(Insert.PHONE, getArgumentPhoneNumber());
Brian Attwellc6100ff2015-02-19 21:35:36 -080090 ImplicitIntentsUtil.startActivityInApp(getActivity(), intent);
Chiao Cheng3483b812012-10-23 16:26:40 -070091 }
92 dismiss();
93 }
94
95 private String getArgumentPhoneNumber() {
96 return getArguments().getString(PHONE_NUMBER_KEY);
97 }
98
99 @Override
100 public void onDismiss(DialogInterface dialog) {
101 super.onDismiss(dialog);
102 // During screen rotation, getActivity returns null. In this case we do not
103 // want to close the Activity anyway
104 final Activity activity = getActivity();
105 if (activity != null) activity.finish();
106 }
107 }
108}