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