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 17 package com.android.contacts; 18 19 import com.android.contacts.common.activity.RequestPermissionsActivity; 20 import com.android.contacts.common.util.ImplicitIntentsUtil; 21 22 import android.app.Activity; 23 import android.app.AlertDialog; 24 import android.app.Dialog; 25 import android.app.DialogFragment; 26 import android.content.DialogInterface; 27 import android.content.DialogInterface.OnClickListener; 28 import android.content.Intent; 29 import android.net.Uri; 30 import android.os.Bundle; 31 import android.provider.ContactsContract.Contacts; 32 import android.provider.ContactsContract.Intents.Insert; 33 import android.telecom.PhoneAccount; 34 import android.text.TextUtils; 35 36 /** 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 */ 40 public class NonPhoneActivity extends ContactsActivity { 41 42 private static final String PHONE_NUMBER_KEY = "PHONE_NUMBER"; 43 44 @Override onCreate(Bundle savedInstanceState)45 protected void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 if (RequestPermissionsActivity.startPermissionActivity(this)) { 48 return; 49 } 50 51 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 getPhoneNumber()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(); 69 if (!PhoneAccount.SCHEME_TEL.equals(scheme)) return null; 70 return getIntent().getData().getSchemeSpecificPart(); 71 } 72 73 public static final class NonPhoneDialogFragment extends DialogFragment 74 implements OnClickListener { 75 @Override onCreateDialog(Bundle savedInstanceState)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 onClick(DialogInterface dialog, int which)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()); 95 ImplicitIntentsUtil.startActivityInApp(getActivity(), intent); 96 } 97 dismiss(); 98 } 99 getArgumentPhoneNumber()100 private String getArgumentPhoneNumber() { 101 return getArguments().getString(PHONE_NUMBER_KEY); 102 } 103 104 @Override onDismiss(DialogInterface dialog)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 } 114