blob: bab39775a7801be1e42a6c09e631166268cb7fe8 [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;
Jay Shrauner1cd88e32014-09-05 15:37:55 -070030import android.telecomm.PhoneAccount;
Chiao Cheng3483b812012-10-23 16:26:40 -070031import android.text.TextUtils;
32
Chiao Cheng3483b812012-10-23 16:26:40 -070033/**
34 * Activity that intercepts DIAL and VIEW intents for phone numbers for devices that can not
35 * be used as a phone. This allows the user to see the phone number
36 */
37public class NonPhoneActivity extends ContactsActivity {
38
39 private static final String PHONE_NUMBER_KEY = "PHONE_NUMBER";
40
41 @Override
42 protected void onCreate(Bundle savedInstanceState) {
43 super.onCreate(savedInstanceState);
44 final String phoneNumber = getPhoneNumber();
45 if (TextUtils.isEmpty(phoneNumber)) {
46 finish();
47 return;
48 }
49
50 final NonPhoneDialogFragment fragment = new NonPhoneDialogFragment();
51 Bundle bundle = new Bundle();
52 bundle.putString(PHONE_NUMBER_KEY, phoneNumber);
53 fragment.setArguments(bundle);
54 getFragmentManager().beginTransaction().add(fragment, "Fragment").commitAllowingStateLoss();
55 }
56
57 private String getPhoneNumber() {
58 if (getIntent() == null) return null;
59 final Uri data = getIntent().getData();
60 if (data == null) return null;
61 final String scheme = data.getScheme();
Jay Shrauner1cd88e32014-09-05 15:37:55 -070062 if (!PhoneAccount.SCHEME_TEL.equals(scheme)) return null;
Chiao Cheng3483b812012-10-23 16:26:40 -070063 return getIntent().getData().getSchemeSpecificPart();
64 }
65
66 public static final class NonPhoneDialogFragment extends DialogFragment
67 implements OnClickListener {
68 @Override
69 public Dialog onCreateDialog(Bundle savedInstanceState) {
70 final AlertDialog alertDialog;
71 alertDialog = new AlertDialog.Builder(getActivity(), R.style.NonPhoneDialogTheme)
72 .create();
73 alertDialog.setTitle(R.string.non_phone_caption);
74 alertDialog.setMessage(getArgumentPhoneNumber());
75 alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
76 getActivity().getString(R.string.non_phone_add_to_contacts), this);
77 alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
78 getActivity().getString(R.string.non_phone_close), this);
79 return alertDialog;
80 }
81
82 @Override
83 public void onClick(DialogInterface dialog, int which) {
84 if (which == DialogInterface.BUTTON_POSITIVE) {
85 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
86 intent.setType(Contacts.CONTENT_ITEM_TYPE);
87 intent.putExtra(Insert.PHONE, getArgumentPhoneNumber());
88 startActivity(intent);
89 }
90 dismiss();
91 }
92
93 private String getArgumentPhoneNumber() {
94 return getArguments().getString(PHONE_NUMBER_KEY);
95 }
96
97 @Override
98 public void onDismiss(DialogInterface dialog) {
99 super.onDismiss(dialog);
100 // During screen rotation, getActivity returns null. In this case we do not
101 // want to close the Activity anyway
102 final Activity activity = getActivity();
103 if (activity != null) activity.finish();
104 }
105 }
106}