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 android.app.Activity;
20 import android.app.AlertDialog;
21 import android.app.Dialog;
22 import android.app.DialogFragment;
23 import android.content.DialogInterface;
24 import android.content.DialogInterface.OnClickListener;
25 import android.content.Intent;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.provider.ContactsContract.Contacts;
29 import android.provider.ContactsContract.Intents.Insert;
30 import android.telecom.PhoneAccount;
31 import android.text.TextUtils;
32 
33 import com.android.contacts.activities.RequestPermissionsActivity;
34 import com.android.contacts.util.ImplicitIntentsUtil;
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         RequestPermissionsActivity.startPermissionActivityIfNeeded(this);
48 
49         final String phoneNumber = getPhoneNumber();
50         if (TextUtils.isEmpty(phoneNumber)) {
51             finish();
52             return;
53         }
54 
55         final NonPhoneDialogFragment fragment = new NonPhoneDialogFragment();
56         Bundle bundle = new Bundle();
57         bundle.putString(PHONE_NUMBER_KEY, phoneNumber);
58         fragment.setArguments(bundle);
59         getFragmentManager().beginTransaction().add(fragment, "Fragment").commitAllowingStateLoss();
60     }
61 
getPhoneNumber()62     private String getPhoneNumber() {
63         if (getIntent() == null) return null;
64         final Uri data = getIntent().getData();
65         if (data == null) return null;
66         final String scheme = data.getScheme();
67         if (!PhoneAccount.SCHEME_TEL.equals(scheme)) return null;
68         return getIntent().getData().getSchemeSpecificPart();
69     }
70 
71     public static final class NonPhoneDialogFragment extends DialogFragment
72             implements OnClickListener {
73         @Override
onCreateDialog(Bundle savedInstanceState)74         public Dialog onCreateDialog(Bundle savedInstanceState) {
75             final AlertDialog alertDialog;
76             alertDialog = new AlertDialog.Builder(getActivity(), R.style.NonPhoneDialogTheme)
77                     .create();
78             alertDialog.setTitle(R.string.non_phone_caption);
79             alertDialog.setMessage(getArgumentPhoneNumber());
80             alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
81                     getActivity().getString(R.string.non_phone_add_to_contacts), this);
82             alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
83                     getActivity().getString(R.string.non_phone_close), this);
84             return alertDialog;
85         }
86 
87         @Override
onClick(DialogInterface dialog, int which)88         public void onClick(DialogInterface dialog, int which) {
89             if (which == DialogInterface.BUTTON_POSITIVE) {
90                 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
91                 intent.setType(Contacts.CONTENT_ITEM_TYPE);
92                 intent.putExtra(Insert.PHONE, getArgumentPhoneNumber());
93                 ImplicitIntentsUtil.startActivityInApp(getActivity(), intent);
94             }
95             dismiss();
96         }
97 
getArgumentPhoneNumber()98         private String getArgumentPhoneNumber() {
99             return getArguments().getString(PHONE_NUMBER_KEY);
100         }
101 
102         @Override
onDismiss(DialogInterface dialog)103         public void onDismiss(DialogInterface dialog) {
104             super.onDismiss(dialog);
105             // During screen rotation, getActivity returns null. In this case we do not
106             // want to close the Activity anyway
107             final Activity activity = getActivity();
108             if (activity != null) activity.finish();
109         }
110     }
111 }
112