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 /**
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  */
37 public class NonPhoneActivity extends ContactsActivity {
38 
39     private static final String PHONE_NUMBER_KEY = "PHONE_NUMBER";
40 
41     @Override
onCreate(Bundle savedInstanceState)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 
getPhoneNumber()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();
62         if (!PhoneAccount.SCHEME_TEL.equals(scheme)) return null;
63         return getIntent().getData().getSchemeSpecificPart();
64     }
65 
66     public static final class NonPhoneDialogFragment extends DialogFragment
67             implements OnClickListener {
68         @Override
onCreateDialog(Bundle savedInstanceState)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
onClick(DialogInterface dialog, int which)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 
getArgumentPhoneNumber()93         private String getArgumentPhoneNumber() {
94             return getArguments().getString(PHONE_NUMBER_KEY);
95         }
96 
97         @Override
onDismiss(DialogInterface dialog)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 }
107