/**
 * Copyright 2008 Joe LaPenna
 */

package com.joelapenna.foursquared;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

/**
 * @author Joe LaPenna (joe@joelapenna.com)
 */
public class PreferenceActivity extends android.preference.PreferenceActivity {
    private static final String TAG = "PreferenceActivity";
    private static final boolean DEBUG = Foursquared.DEBUG;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (DEBUG) Log.d(TAG, "onCreate");

        // Load the preferences from an XML resource
        this.addPreferencesFromResource(R.xml.preferences);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Look up the phone number if its not set.
        setPhoneNumber();
    }

    private void setPhoneNumber() {
        if (DEBUG) Log.d(TAG, "Setting phone number if not set.");
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        String phoneNumber = settings.getString(Foursquared.PREFERENCE_PHONE, null);

        if (phoneNumber == null || TextUtils.isEmpty(phoneNumber)) {
            TelephonyManager telephony = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
            phoneNumber = telephony.getLine1Number();
            if (phoneNumber.startsWith("1")) {
                phoneNumber = phoneNumber.substring(1);
            }

            if (DEBUG) Log.d(TAG, "Phone number not found. Setting it: " + phoneNumber);
            Editor editor = settings.edit();
            editor.putString(Foursquared.PREFERENCE_PHONE, phoneNumber);
            editor.commit();
        }
    }
}

@@ -1345,31 +1345,8 @@
 if (
-phoneNumber == null %7C%7C 
 Text
@@ -1371,24 +1371,86 @@
 eNumber)) %7B%0A
+            if (DEBUG) Log.d(TAG, %22Phone number not found.%22);%0A
             
@@ -1611,16 +1611,51 @@
     if (
+!TextUtils.isEmpty(phoneNumber) && 
 phoneNum
@@ -1741,19 +1741,8 @@
     
-        %7D%0A%0A
     
@@ -1838,16 +1838,20 @@
         
+    
 Editor e
@@ -1871,24 +1871,28 @@
 ngs.edit();%0A
+    
             
@@ -1960,24 +1960,28 @@
             
+    
 editor.commi
@@ -1981,24 +1981,38 @@
 r.commit();%0A
+            %7D%0A
         %7D%0A  

Fix prefernces activity for people without sim cards.