blob: 066c4d32ac3f12c21f93ba0cfd5f69d548a58ff5 [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
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.app.Dialog;
22import android.app.DialogFragment;
23import android.content.DialogInterface;
24import android.content.DialogInterface.OnClickListener;
25import android.content.Intent;
26import android.net.Uri;
27import android.os.Bundle;
28import android.provider.ContactsContract.Contacts;
29import android.provider.ContactsContract.Intents.Insert;
Tyler Gunn03192222014-09-10 15:20:09 -070030import android.telecom.PhoneAccount;
Chiao Cheng3483b812012-10-23 16:26:40 -070031import android.text.TextUtils;
32
Gary Mai0a49afa2016-12-05 15:53:58 -080033import com.android.contacts.activities.RequestPermissionsActivity;
34import com.android.contacts.util.ImplicitIntentsUtil;
35
Chiao Cheng3483b812012-10-23 16:26:40 -070036/**
37 * Activity that intercepts DIAL and VIEW intents for phone numbers for devices that can not
38 * be used as a phone. This allows the user to see the phone number
39 */
40public class NonPhoneActivity extends ContactsActivity {
41
42 private static final String PHONE_NUMBER_KEY = "PHONE_NUMBER";
43
44 @Override
45 protected void onCreate(Bundle savedInstanceState) {
46 super.onCreate(savedInstanceState);
Brian Attwellbdd32642015-05-08 17:03:15 -070047 if (RequestPermissionsActivity.startPermissionActivity(this)) {
48 return;
49 }
50
Chiao Cheng3483b812012-10-23 16:26:40 -070051 final String phoneNumber = getPhoneNumber();
52 if (TextUtils.isEmpty(phoneNumber)) {
53 finish();
54 return;
55 }
56
57 final NonPhoneDialogFragment fragment = new NonPhoneDialogFragment();
58 Bundle bundle = new Bundle();
59 bundle.putString(PHONE_NUMBER_KEY, phoneNumber);
60 fragment.setArguments(bundle);
61 getFragmentManager().beginTransaction().add(fragment, "Fragment").commitAllowingStateLoss();
62 }
63
64 private String getPhoneNumber() {
65 if (getIntent() == null) return null;
66 final Uri data = getIntent().getData();
67 if (data == null) return null;
68 final String scheme = data.getScheme();
Jay Shrauner1cd88e32014-09-05 15:37:55 -070069 if (!PhoneAccount.SCHEME_TEL.equals(scheme)) return null;
Chiao Cheng3483b812012-10-23 16:26:40 -070070 return getIntent().getData().getSchemeSpecificPart();
71 }
72
73 public static final class NonPhoneDialogFragment extends DialogFragment
74 implements OnClickListener {
75 @Override
76 public Dialog onCreateDialog(Bundle savedInstanceState) {
77 final AlertDialog alertDialog;
78 alertDialog = new AlertDialog.Builder(getActivity(), R.style.NonPhoneDialogTheme)
79 .create();
80 alertDialog.setTitle(R.string.non_phone_caption);
81 alertDialog.setMessage(getArgumentPhoneNumber());
82 alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
83 getActivity().getString(R.string.non_phone_add_to_contacts), this);
84 alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
85 getActivity().getString(R.string.non_phone_close), this);
86 return alertDialog;
87 }
88
89 @Override
90 public void onClick(DialogInterface dialog, int which) {
91 if (which == DialogInterface.BUTTON_POSITIVE) {
92 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
93 intent.setType(Contacts.CONTENT_ITEM_TYPE);
94 intent.putExtra(Insert.PHONE, getArgumentPhoneNumber());
Brian Attwellc6100ff2015-02-19 21:35:36 -080095 ImplicitIntentsUtil.startActivityInApp(getActivity(), intent);
Chiao Cheng3483b812012-10-23 16:26:40 -070096 }
97 dismiss();
98 }
99
100 private String getArgumentPhoneNumber() {
101 return getArguments().getString(PHONE_NUMBER_KEY);
102 }
103
104 @Override
105 public void onDismiss(DialogInterface dialog) {
106 super.onDismiss(dialog);
107 // During screen rotation, getActivity returns null. In this case we do not
108 // want to close the Activity anyway
109 final Activity activity = getActivity();
110 if (activity != null) activity.finish();
111 }
112 }
113}