Dialog for showing phone numbers on devices that can not be used as phones

Bug:3116684

Change-Id: I000df5fa6d83812a60fa5eb4c7e621cca2adb4fe
diff --git a/src/com/android/contacts/DialtactsActivity.java b/src/com/android/contacts/DialtactsActivity.java
index ce8d3f4..dc69194 100644
--- a/src/com/android/contacts/DialtactsActivity.java
+++ b/src/com/android/contacts/DialtactsActivity.java
@@ -43,12 +43,6 @@
  */
 public class DialtactsActivity extends TabActivity implements TabHost.OnTabChangeListener {
     private static final String TAG = "Dailtacts";
-    private static final String FAVORITES_ENTRY_COMPONENT =
-            "com.android.contacts.DialtactsFavoritesEntryActivity";
-
-    /** Opens the Contacts app in the state the user has last set it to */
-    private static final String CONTACTS_LAUNCH_ACTIVITY =
-            "com.android.contacts.ContactsLaunchActivity";
 
     private static final int TAB_INDEX_DIALER = 0;
     private static final int TAB_INDEX_CALL_LOG = 1;
@@ -231,8 +225,16 @@
 
         // Choose the tab based on the inbound intent
         if (intent.getBooleanExtra(ContactsFrontDoor.EXTRA_FRONT_DOOR, false)) {
-            // Launched through the contacts front door, set the proper contacts tab
-            setContactsTab();
+            // Launched through the contacts front door, set the proper contacts tab (sticky
+            // between favorites and contacts)
+            SharedPreferences prefs = getSharedPreferences(PREFS_DIALTACTS, MODE_PRIVATE);
+            boolean favoritesAsContacts = prefs.getBoolean(PREF_FAVORITES_AS_CONTACTS,
+                    PREF_FAVORITES_AS_CONTACTS_DEFAULT);
+            if (favoritesAsContacts) {
+                mTabHost.setCurrentTab(TAB_INDEX_FAVORITES);
+            } else {
+                mTabHost.setCurrentTab(TAB_INDEX_CONTACTS);
+            }
         } else {
             // Not launched through the front door, look at the component to determine the tab
             String componentName = intent.getComponent().getClassName();
@@ -242,12 +244,8 @@
                 } else {
                     mTabHost.setCurrentTab(TAB_INDEX_DIALER);
                 }
-            } else if (FAVORITES_ENTRY_COMPONENT.equals(componentName)) {
-                mTabHost.setCurrentTab(TAB_INDEX_FAVORITES);
-            } else if (CONTACTS_LAUNCH_ACTIVITY.equals(componentName)) {
-                mTabHost.setCurrentTab(mLastManuallySelectedTab);
             } else {
-                setContactsTab();
+                mTabHost.setCurrentTab(mLastManuallySelectedTab);
             }
         }
 
@@ -259,17 +257,6 @@
         intent.putExtra(EXTRA_IGNORE_STATE, false);
     }
 
-    private void setContactsTab() {
-        SharedPreferences prefs = getSharedPreferences(PREFS_DIALTACTS, MODE_PRIVATE);
-        boolean favoritesAsContacts = prefs.getBoolean(PREF_FAVORITES_AS_CONTACTS,
-                PREF_FAVORITES_AS_CONTACTS_DEFAULT);
-        if (favoritesAsContacts) {
-            mTabHost.setCurrentTab(TAB_INDEX_FAVORITES);
-        } else {
-            mTabHost.setCurrentTab(TAB_INDEX_CONTACTS);
-        }
-    }
-
     @Override
     public void onNewIntent(Intent newIntent) {
         setIntent(newIntent);
diff --git a/src/com/android/contacts/activities/NonPhoneActivity.java b/src/com/android/contacts/activities/NonPhoneActivity.java
new file mode 100644
index 0000000..d963cd5
--- /dev/null
+++ b/src/com/android/contacts/activities/NonPhoneActivity.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.contacts.activities;
+
+import com.android.contacts.R;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.app.DialogFragment;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.DialogInterface.OnClickListener;
+import android.net.Uri;
+import android.os.Bundle;
+import android.provider.ContactsContract.Contacts;
+import android.provider.ContactsContract.Intents.Insert;
+import android.text.TextUtils;
+
+/**
+ * Activity that intercepts DIAL and VIEW intents for phone numbers for devices that can not
+ * be used as a phone. This allows the user to see the phone number
+ */
+public class NonPhoneActivity extends Activity {
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        final String phoneNumber = getPhoneNumber();
+        if (TextUtils.isEmpty(phoneNumber)) {
+            finish();
+            return;
+        }
+
+        final NonPhoneDialogFragment fragment = new NonPhoneDialogFragment();
+        fragment.setArguments(Bundle.forPair("PHONE_NUMBER", phoneNumber));
+        getFragmentManager().openTransaction().add(fragment, "Fragment").commit();
+    }
+
+    private String getPhoneNumber() {
+        if (getIntent() == null) return null;
+        final Uri data = getIntent().getData();
+        if (data == null) return null;
+        final String scheme = data.getScheme();
+        if (!"tel".equals(scheme)) return null;
+        return getIntent().getData().getSchemeSpecificPart();
+    }
+
+    public static final class NonPhoneDialogFragment extends DialogFragment
+            implements OnClickListener {
+        @Override
+        public Dialog onCreateDialog(Bundle savedInstanceState) {
+            final AlertDialog alertDialog;
+            alertDialog = new AlertDialog.Builder(getActivity()).create();
+            alertDialog.setTitle(R.string.non_phone_caption);
+            alertDialog.setMessage(getArgumentPhoneNumber());
+            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
+                    getActivity().getString(R.string.non_phone_add_to_contacts), this);
+            alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
+                    getActivity().getString(R.string.non_phone_close), this);
+            alertDialog.setOnDismissListener(this);
+            return alertDialog;
+        }
+
+        @Override
+        public void onClick(DialogInterface dialog, int which) {
+            if (which == DialogInterface.BUTTON_POSITIVE) {
+                final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
+                intent.setType(Contacts.CONTENT_ITEM_TYPE);
+                intent.putExtra(Insert.PHONE, getArgumentPhoneNumber());
+                intent.setFlags(
+                        Intent.FLAG_ACTIVITY_FORWARD_RESULT | Intent.FLAG_ACTIVITY_NO_HISTORY);
+                startActivity(intent);
+            }
+            dismiss();
+        }
+
+        private String getArgumentPhoneNumber() {
+            return getArguments().getPairValue();
+        }
+
+        @Override
+        public void onDismiss(DialogInterface dialog) {
+            super.onDismiss(dialog);
+            // During screen rotation, getActivity returns null. In this case we do not
+            // want to close the Activity anyway
+            final Activity activity = getActivity();
+            if (activity != null) activity.finish();
+        }
+    }
+}
diff --git a/src/com/android/contacts/util/PhoneCapabilityTester.java b/src/com/android/contacts/util/PhoneCapabilityTester.java
index fa34b92..961e3c9 100644
--- a/src/com/android/contacts/util/PhoneCapabilityTester.java
+++ b/src/com/android/contacts/util/PhoneCapabilityTester.java
@@ -22,10 +22,19 @@
 import android.content.pm.ResolveInfo;
 import android.net.Uri;
 import android.net.sip.SipManager;
+import android.telephony.TelephonyManager;
 
 import java.util.List;
 
+/**
+ * Provides static functions to quickly test the capabilities of this device. The static
+ * members are not safe for threading
+ */
 public final class PhoneCapabilityTester {
+    private static boolean sIsInitialized;
+    private static boolean sIsPhone;
+    private static boolean sIsSipPhone;
+
     /**
      * Tests whether the Intent has a receiver registered. This can be used to show/hide
      * functionality (like Phone, SMS)
@@ -41,28 +50,31 @@
      * Returns true if this device can be used to make phone calls
      */
     public static boolean isPhone(Context context) {
+        if (!sIsInitialized) initialize(context);
         // Is the device physically capabable of making phone calls?
-        if (!context.getResources().getBoolean(com.android.internal.R.bool.config_voice_capable)) {
-            return false;
-        }
+        return sIsPhone;
+    }
 
-        // Is there an app registered that accepts the call intent?
-        final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
-                Uri.fromParts(Constants.SCHEME_TEL, "", null));
-        return isIntentRegistered(context, intent);
+    private static void initialize(Context context) {
+        final TelephonyManager telephonyManager = new TelephonyManager(context);
+        sIsPhone = telephonyManager.isVoiceCapable();
+        sIsSipPhone = SipManager.isVoipSupported(context);
+        sIsInitialized = true;
     }
 
     /**
      * Returns true if this device can be used to make sip calls
      */
     public static boolean isSipPhone(Context context) {
-        return SipManager.isVoipSupported(context);
+        if (!sIsInitialized) initialize(context);
+        return sIsSipPhone;
     }
 
     /**
      * Returns true if the device has an SMS application installed.
      */
     public static boolean isSmsIntentRegistered(Context context) {
+        // Don't cache the result as the user might install third party apps to send SMS
         final Intent intent = new Intent(Intent.ACTION_SENDTO,
                 Uri.fromParts(Constants.SCHEME_SMSTO, "", null));
         return isIntentRegistered(context, intent);