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