[Wi-Fi] Add IMSI privacy protection warning summary

If a SIM based Wi-Fi network is not provided with IMSI
protection, Wi-Fi detail UI shows the warning summary if
the Wi-Fi network is connected.

Bug: 148283447
Test: make RunSettingsRoboTests ROBOTEST_FILTER=WifiSecondSummaryController2Test
      make RunSettingsRoboTests ROBOTEST_FILTER=LinkifySummaryPreferenceTest

Change-Id: I689a75d2f0a2ae6196b2ed5985b8ff141fbac8b4
diff --git a/res/xml/wifi_network_details_fragment2.xml b/res/xml/wifi_network_details_fragment2.xml
index 30a7f67..92f68cc 100644
--- a/res/xml/wifi_network_details_fragment2.xml
+++ b/res/xml/wifi_network_details_fragment2.xml
@@ -31,10 +31,18 @@
         android:selectable="false"
         settings:isPreferenceVisible="false"/>
 
+    <!-- The preference to display the second summary -->
+    <com.android.settings.widget.LinkifySummaryPreference
+        android:key="second_summary"
+        android:icon="@drawable/ic_info_outline_24dp"
+        android:selectable="false"
+        settings:allowDividerAbove="false"/>
+
     <!-- Buttons -->
     <com.android.settingslib.widget.ActionButtonsPreference
         android:key="buttons"
-        android:selectable="false" />
+        android:selectable="false"
+        settings:allowDividerAbove="true"/>
 
     <!-- General Details Preferences -->
     <Preference
diff --git a/src/com/android/settings/widget/EntityHeaderController.java b/src/com/android/settings/widget/EntityHeaderController.java
index 654d50b..91a20b3 100644
--- a/src/com/android/settings/widget/EntityHeaderController.java
+++ b/src/com/android/settings/widget/EntityHeaderController.java
@@ -374,7 +374,6 @@
         }
     }
 
-
     private void setText(@IdRes int id, CharSequence text) {
         TextView textView = mHeader.findViewById(id);
         if (textView != null) {
diff --git a/src/com/android/settings/widget/LinkifySummaryPreference.java b/src/com/android/settings/widget/LinkifySummaryPreference.java
new file mode 100644
index 0000000..2683873
--- /dev/null
+++ b/src/com/android/settings/widget/LinkifySummaryPreference.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2020 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.settings.widget;
+
+import android.content.Context;
+import android.text.SpannableString;
+import android.text.TextUtils;
+import android.text.method.LinkMovementMethod;
+import android.text.style.ClickableSpan;
+import android.util.AttributeSet;
+import android.view.View;
+import android.widget.TextView;
+
+import androidx.preference.Preference;
+import androidx.preference.PreferenceViewHolder;
+
+/** A preference which supports linkify text in the summary **/
+public class LinkifySummaryPreference extends Preference {
+
+    public LinkifySummaryPreference(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    public LinkifySummaryPreference(Context context) {
+        super(context);
+    }
+
+    @Override
+    public void onBindViewHolder(PreferenceViewHolder holder) {
+        super.onBindViewHolder(holder);
+
+        final TextView summaryView = (TextView) holder.findViewById(android.R.id.summary);
+        if (summaryView == null || summaryView.getVisibility() != View.VISIBLE) {
+            return;
+        }
+
+        final CharSequence summary = getSummary();
+        if (!TextUtils.isEmpty(summary)) {
+            final SpannableString spannableSummary = new SpannableString(summary);
+            if (spannableSummary.getSpans(0, spannableSummary.length(), ClickableSpan.class)
+                    .length > 0) {
+                summaryView.setMovementMethod(LinkMovementMethod.getInstance());
+            }
+        }
+    }
+}
diff --git a/src/com/android/settings/wifi/details2/WifiDetailPreferenceController2.java b/src/com/android/settings/wifi/details2/WifiDetailPreferenceController2.java
index 2228f7b..58299c8 100644
--- a/src/com/android/settings/wifi/details2/WifiDetailPreferenceController2.java
+++ b/src/com/android/settings/wifi/details2/WifiDetailPreferenceController2.java
@@ -446,10 +446,8 @@
         if (usingDataUsageHeader(mContext)) {
             mSummaryHeaderController.updateState(mDataUsageSummaryPref);
         } else {
-            String summary = mWifiEntry.getSummary();
-
             mEntityHeaderController
-                    .setSummary(summary)
+                    .setSummary(mWifiEntry.getSummary())
                     .setSecondSummary(getExpiryTimeSummary())
                     .setRecyclerView(mFragment.getListView(), mLifecycle)
                     .done(mFragment.getActivity(), true /* rebind */);
diff --git a/src/com/android/settings/wifi/details2/WifiNetworkDetailsFragment2.java b/src/com/android/settings/wifi/details2/WifiNetworkDetailsFragment2.java
index 394bab6..1a8ac1d 100644
--- a/src/com/android/settings/wifi/details2/WifiNetworkDetailsFragment2.java
+++ b/src/com/android/settings/wifi/details2/WifiNetworkDetailsFragment2.java
@@ -140,7 +140,7 @@
                                 getContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
                         final UserManager um = (UserManager)
                                 getContext().getSystemService(Context.USER_SERVICE);
-                        int profileOwnerUserId = Utils.getManagedProfileId(
+                        final int profileOwnerUserId = Utils.getManagedProfileId(
                                 um, UserHandle.myUserId());
                         admin = new EnforcedAdmin(dpm.getProfileOwnerAsUser(profileOwnerUserId),
                                 null, UserHandle.of(profileOwnerUserId));
@@ -162,6 +162,11 @@
         setupNetworksDetailTracker();
         final WifiEntry wifiEntry = mNetworkDetailsTracker.getWifiEntry();
 
+        final WifiSecondSummaryController2 wifiSecondSummaryController2 =
+                new WifiSecondSummaryController2(context);
+        wifiSecondSummaryController2.setWifiEntry(wifiEntry);
+        mControllers.add(wifiSecondSummaryController2);
+
         mWifiDetailPreferenceController2 = WifiDetailPreferenceController2.newInstance(
                 wifiEntry,
                 cm,
diff --git a/src/com/android/settings/wifi/details2/WifiSecondSummaryController2.java b/src/com/android/settings/wifi/details2/WifiSecondSummaryController2.java
new file mode 100644
index 0000000..93bb2c4
--- /dev/null
+++ b/src/com/android/settings/wifi/details2/WifiSecondSummaryController2.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2020 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.settings.wifi.details2;
+
+import android.content.Context;
+import android.text.TextUtils;
+
+import com.android.settings.core.BasePreferenceController;
+import com.android.wifitrackerlib.WifiEntry;
+
+/**
+ * {@link BasePreferenceController} that display the second summary. If users click the preference,
+ * @link ClickableSpan#onClick} of the first {@link ClickableSpan} in the summary will be called.
+ */
+public class WifiSecondSummaryController2 extends BasePreferenceController {
+
+    private static final String KEY_WIFI_SECOND_SUMMARY = "second_summary";
+    private CharSequence mSecondSummary;
+
+    public WifiSecondSummaryController2(Context context) {
+        super(context, KEY_WIFI_SECOND_SUMMARY);
+    }
+
+    public void setWifiEntry(WifiEntry wifiEntry) {
+        mSecondSummary = wifiEntry.getSecondSummary();
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return TextUtils.isEmpty(mSecondSummary) ? CONDITIONALLY_UNAVAILABLE : AVAILABLE;
+    }
+
+    @Override
+    public CharSequence getSummary() {
+        return mSecondSummary;
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/widget/LinkifySummaryPreferenceTest.java b/tests/robotests/src/com/android/settings/widget/LinkifySummaryPreferenceTest.java
new file mode 100644
index 0000000..e5ae840
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/widget/LinkifySummaryPreferenceTest.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2020 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.settings.widget;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.text.SpannableStringBuilder;
+import android.text.Spanned;
+import android.text.method.LinkMovementMethod;
+import android.text.style.URLSpan;
+import android.view.View;
+import android.widget.TextView;
+
+import androidx.preference.PreferenceViewHolder;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.mockito.Spy;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+
+@RunWith(RobolectricTestRunner.class)
+public class LinkifySummaryPreferenceTest {
+    @Spy
+    private PreferenceViewHolder mViewHolder;
+    @Mock
+    private TextView mSummaryTextView;
+    private LinkifySummaryPreference mPreference;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
+        final Context context = RuntimeEnvironment.application;
+        mPreference = new LinkifySummaryPreference(context, null /* attrs */);
+
+        final View view = spy(View.inflate(context, mPreference.getLayoutResource(),
+                null /* root */));
+        mViewHolder = spy(PreferenceViewHolder.createInstanceForTests(view));
+        doReturn(mSummaryTextView).when(mViewHolder).findViewById(android.R.id.summary);
+    }
+
+    @Test
+    public void onBindViewHolder_summaryTextViewGone_shouldNotSetMovementMethod() {
+        when(mSummaryTextView.getVisibility()).thenReturn(View.GONE);
+
+        mPreference.onBindViewHolder(mViewHolder);
+
+        verify(mSummaryTextView, never()).setMovementMethod(LinkMovementMethod.getInstance());
+    }
+
+    @Test
+    public void onBindViewHolder_noLinkSummary_shouldNotSetMovementMethod() {
+        when(mSummaryTextView.getVisibility()).thenReturn(View.VISIBLE);
+        final CharSequence seondSummary = "secondSummary";
+        mPreference.setSummary(seondSummary);
+
+        mPreference.onBindViewHolder(mViewHolder);
+
+        verify(mSummaryTextView, never()).setMovementMethod(LinkMovementMethod.getInstance());
+    }
+
+    @Test
+    public void onBindViewHolder_linkedSummary_shouldSetMovementMethod() {
+        when(mSummaryTextView.getVisibility()).thenReturn(View.VISIBLE);
+        final CharSequence seondSummary = "secondSummary";
+        final SpannableStringBuilder summaryBuilder = new SpannableStringBuilder();
+        summaryBuilder.append(seondSummary, new URLSpan("" /* url */),
+                Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+        mPreference.setSummary(summaryBuilder);
+
+        mPreference.onBindViewHolder(mViewHolder);
+
+        verify(mSummaryTextView).setMovementMethod(any(LinkMovementMethod.class));
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/wifi/details2/WifiSecondSummaryController2Test.java b/tests/robotests/src/com/android/settings/wifi/details2/WifiSecondSummaryController2Test.java
new file mode 100644
index 0000000..439fd7d
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/wifi/details2/WifiSecondSummaryController2Test.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2020 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.settings.wifi.details2;
+
+import static com.android.settings.core.BasePreferenceController.AVAILABLE;
+import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.when;
+
+import com.android.wifitrackerlib.WifiEntry;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+
+@RunWith(RobolectricTestRunner.class)
+public class WifiSecondSummaryController2Test {
+
+    private WifiSecondSummaryController2 mController;
+    @Mock
+    private WifiEntry mWifiEntry;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mController = new WifiSecondSummaryController2(RuntimeEnvironment.application);
+    }
+
+    @Test
+    public void getAvailabilityStatus_showWhenSummaryAvailable() {
+        // Visible when summary is not empty.
+        when(mWifiEntry.getSecondSummary()).thenReturn("test");
+        mController.setWifiEntry(mWifiEntry);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
+
+        // Invisible when summary is empty.
+        when(mWifiEntry.getSecondSummary()).thenReturn("");
+        mController.setWifiEntry(mWifiEntry);
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
+    }
+}