Add setting for allowing Google to use location for better search results.
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 26d7588..3d1b5d5 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1130,7 +1130,7 @@
     <string name="network_settings_summary">Set options for roaming, networks, APNs</string>
 
     <!-- Security & location settings screen, section header for settings relating to location -->
-    <string name="location_title">My Location sources</string>
+    <string name="location_title">My Location</string>
     <!-- Security & location settings screen, setting check box label if the user wants to use wireless network-based positioning (cell ID, wifi, etc.) -->
     <string name="location_network_based">Use wireless networks</string>
     <!-- Security & location settings screen, setting summary when Use wireless networks check box is clear -->
@@ -1143,9 +1143,19 @@
     <string name="location_street_level">When locating, accurate to street level (deselect to conserve battery)</string>
     <!-- Security & location settings screen, setting summary when Enable GPS satellites check box is clear -->
     <string name="location_gps_disabled">Locate to street-level (requires more battery plus view of sky)</string>
-    <!-- Title of warning dialog to user that location information will be logged -->
+    <!-- Setting title for allow sending location to google -->
+    <string name="use_location_title">Share with Google</string>
+    <!-- Title of dialog to user requesting use of location information to improve services -->
+    <string name="use_location_summary">Allow Google to use location for improved search results and other services</string>
+    <!-- Message of dialog to user requesting use of location information -->
+    <string name="use_location_warning_message">Allow Google to use location for improved search results and other services</string>
+    <!-- Agree -->
+    <string name="agree">Agree</string>
+    <!-- Disagree -->
+    <string name="disagree">Disagree</string>
 
-    <!-- About -->
+
+    <!-- About --> <skip />
     <!-- Main settings screen, setting title for the user to go into the About phone screen -->
     <string name="about_settings">About phone</string>
     <!-- Main settings screen, setting summary for the user to go into the About phone screen-->
diff --git a/res/xml/security_settings.xml b/res/xml/security_settings.xml
index 788aae3..8dd9d89 100644
--- a/res/xml/security_settings.xml
+++ b/res/xml/security_settings.xml
@@ -17,11 +17,12 @@
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
 
     <PreferenceCategory
+        android:key="location_category"
         android:title="@string/location_title">
         
         <CheckBoxPreference 
             android:key="location_network" 
-            android:title="@string/location_network_based" 
+            android:title="@string/location_network_based"
             android:summaryOn="@string/location_neighborhood_level"
             android:summaryOff="@string/location_networks_disabled"/>
         
@@ -30,7 +31,13 @@
             android:title="@string/location_gps"
             android:summaryOn="@string/location_street_level"
             android:summaryOff="@string/location_gps_disabled"/>
-            
+
+        <CheckBoxPreference
+            android:key="use_location"
+            android:title="@string/use_location_title"
+            android:persistent="false"
+            android:summary="@string/use_location_summary"/>
+
     </PreferenceCategory>
             
 </PreferenceScreen>
diff --git a/src/com/android/settings/SecuritySettings.java b/src/com/android/settings/SecuritySettings.java
index ccf360a..41b4148 100644
--- a/src/com/android/settings/SecuritySettings.java
+++ b/src/com/android/settings/SecuritySettings.java
@@ -18,10 +18,14 @@
 
 
 import android.app.Activity;
+import android.app.AlertDialog;
+import android.app.Dialog;
 import android.content.ContentQueryMap;
+import android.content.ContentResolver;
 import android.content.Context;
+import android.content.DialogInterface;
 import android.content.Intent;
-import android.content.SharedPreferences;
+import android.content.pm.PackageManager.NameNotFoundException;
 import android.database.Cursor;
 import android.location.LocationManager;
 import android.net.vpn.VpnManager;
@@ -30,20 +34,22 @@
 import android.preference.Preference;
 import android.preference.PreferenceActivity;
 import android.preference.PreferenceCategory;
+import android.preference.PreferenceGroup;
 import android.preference.PreferenceScreen;
 import android.provider.Settings;
-import android.util.Config;
-import android.util.Log;
+import android.text.method.LinkMovementMethod;
+import android.widget.TextView;
+
+import com.android.internal.widget.LockPatternUtils;
 
 import java.util.Observable;
 import java.util.Observer;
 
-import com.android.internal.widget.LockPatternUtils;
-
 /**
  * Gesture lock pattern settings.
  */
-public class SecuritySettings extends PreferenceActivity {
+public class SecuritySettings extends PreferenceActivity implements
+        DialogInterface.OnDismissListener, DialogInterface.OnClickListener {
 
     // Lock Settings
     
@@ -61,10 +67,18 @@
     private CheckBoxPreference mShowPassword;
     
     // Location Settings
-    
+    private static final String LOCATION_CATEGORY = "location_category";
     private static final String LOCATION_NETWORK = "location_network";
     private static final String LOCATION_GPS = "location_gps";
 
+    // Vendor specific
+    private static final String GSETTINGS_PROVIDER = "com.google.android.providers.settings";
+    private static final String USE_LOCATION = "use_location";
+    private static final String KEY_DONE_USE_LOCATION = "doneLocation";
+    private CheckBoxPreference mUseLocation;
+    private boolean mOkClicked;
+    private Dialog mUseLocationDialog;
+
     private CheckBoxPreference mNetwork;
     private CheckBoxPreference mGps;
 
@@ -89,6 +103,17 @@
 
         mNetwork = (CheckBoxPreference) getPreferenceScreen().findPreference(LOCATION_NETWORK);
         mGps = (CheckBoxPreference) getPreferenceScreen().findPreference(LOCATION_GPS);
+        mUseLocation = (CheckBoxPreference) getPreferenceScreen().findPreference(USE_LOCATION);
+
+        // Vendor specific
+        try {
+            if (mUseLocation != null
+                    && getPackageManager().getPackageInfo(GSETTINGS_PROVIDER, 0) == null) {
+                ((PreferenceGroup)findPreference(LOCATION_CATEGORY))
+                        .removePreference(mUseLocation);
+            }
+        } catch (NameNotFoundException nnfe) {
+        }
         updateToggles();
 
         // listen for Location Manager settings changes
@@ -98,6 +123,11 @@
                 null);
         mContentQueryMap = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, null);
         mContentQueryMap.addObserver(new SettingsObserver());
+        boolean doneUseLocation = savedInstanceState != null
+                && savedInstanceState.getBoolean(KEY_DONE_USE_LOCATION, true);
+        if (getIntent().getBooleanExtra("SHOW_USE_LOCATION", false) && !doneUseLocation) {
+            showUseLocationDialog(true);
+        }
     }
 
     private PreferenceScreen createPreferenceHierarchy() {
@@ -199,6 +229,23 @@
     }
 
     @Override
+    public void onPause() {
+        if (mUseLocationDialog != null && mUseLocationDialog.isShowing()) {
+            mUseLocationDialog.dismiss();
+        }
+        mUseLocationDialog = null;
+        super.onPause();
+    }
+
+    @Override
+    public void onSaveInstanceState(Bundle icicle) {
+        if (mUseLocationDialog != null && mUseLocationDialog.isShowing()) {
+            icicle.putBoolean(KEY_DONE_USE_LOCATION, false);
+        }
+        super.onSaveInstanceState(icicle);
+    }
+
+    @Override
     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
             Preference preference) {
         final String key = preference.getKey();
@@ -218,25 +265,84 @@
         } else if (preference == mGps) {
             Settings.Secure.setLocationProviderEnabled(getContentResolver(),
                     LocationManager.GPS_PROVIDER, mGps.isChecked());
+        } else if (preference == mUseLocation) {
+            //normally called on the toggle click
+            if (mUseLocation.isChecked()) {
+                showUseLocationDialog(false);
+            } else {
+                updateUseLocation();
+            }
         }
 
         return false;
     }
 
+    private void showPrivacyPolicy() {
+        Intent intent = new Intent("android.settings.TERMS");
+        startActivity(intent);
+    }
+
+    private void showUseLocationDialog(boolean force) {
+        // Show a warning to the user that location data will be shared
+        mOkClicked = false;
+        if (force) {
+            mUseLocation.setChecked(true);
+        }
+        /*
+        Resources res = getResources();
+        CharSequence msg = new SpannableString(
+                res.getText(R.string.use_location_warning_message));
+        SpannableString spannable = (SpannableString) msg;
+        Annotation[] spans = spannable.getSpans(0, spannable.length(), Annotation.class);
+        if (spans != null && spans.length > 0) {
+            SpannableStringBuilder builder = new SpannableStringBuilder(spannable);
+            int start = spannable.getSpanStart(spans[0]);
+            int end = spannable.getSpanEnd(spans[0]);
+            ClickableSpan link = new ClickableSpan() {
+                @Override
+                public void onClick(View view) {
+                    showPrivacyPolicy();
+                }
+            };
+            builder.setSpan(link, start, end, spannable.getSpanFlags(link));
+            msg = builder;
+        }
+        */
+        CharSequence msg = getResources().getText(R.string.use_location_warning_message);
+        mUseLocationDialog = new AlertDialog.Builder(this).setMessage(msg)
+                .setTitle(R.string.use_location_title)
+                .setIcon(android.R.drawable.ic_dialog_alert)
+                .setPositiveButton(R.string.agree, this)
+                .setNegativeButton(R.string.disagree, this)
+                .show();
+        ((TextView)mUseLocationDialog.findViewById(android.R.id.message))
+                .setMovementMethod(LinkMovementMethod.getInstance());
+        mUseLocationDialog.setOnDismissListener(this);
+    }
+
     /*
      * Creates toggles for each available location provider
      */
     private void updateToggles() {
+        ContentResolver res = getContentResolver();
         mNetwork.setChecked(Settings.Secure.isLocationProviderEnabled(
-                getContentResolver(), LocationManager.NETWORK_PROVIDER));
+                res, LocationManager.NETWORK_PROVIDER));
         mGps.setChecked(Settings.Secure.isLocationProviderEnabled(
-                getContentResolver(), LocationManager.GPS_PROVIDER));
+                res, LocationManager.GPS_PROVIDER));
+        mUseLocation.setChecked(Settings.Gservices.getInt(res,
+                Settings.Gservices.USE_LOCATION_FOR_SERVICES, 2) == 1);
     }
 
     private boolean isToggled(Preference pref) {
         return ((CheckBoxPreference) pref).isChecked();
     }
 
+    private void updateUseLocation() {
+        boolean use = mUseLocation.isChecked();
+        Settings.Gservices.putString(getContentResolver(),
+                Settings.Gservices.USE_LOCATION_FOR_SERVICES, use ? "1" : "0");
+    }
+
 
     /**
      * For the user to disable keyguard, we first make them verify their
@@ -272,15 +378,33 @@
      * @see #confirmPatternThenDisableAndClear
      */
     @Override
-    protected void onActivityResult(int requestCode, int resultCode,
-            Intent data) {
+    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
 
         final boolean resultOk = resultCode == Activity.RESULT_OK;
 
-        if ((requestCode == CONFIRM_PATTERN_THEN_DISABLE_AND_CLEAR_REQUEST_CODE) && resultOk) {
+        if ((requestCode == CONFIRM_PATTERN_THEN_DISABLE_AND_CLEAR_REQUEST_CODE)
+                && resultOk) {
             mLockPatternUtils.setLockPatternEnabled(false);
             mLockPatternUtils.saveLockPattern(null);
         }
     }
+
+    public void onClick(DialogInterface dialog, int which) {
+        if (which == DialogInterface.BUTTON_POSITIVE) {
+            //updateProviders();
+            mOkClicked = true;
+        } else {
+            // Reset the toggle
+            mUseLocation.setChecked(false);
+        }
+        updateUseLocation();
+    }
+
+    public void onDismiss(DialogInterface dialog) {
+        // Assuming that onClick gets called first
+        if (!mOkClicked) {
+            mUseLocation.setChecked(false);
+        }
+    }
 }