Refactor the layout for MasterSwitch to a 2 target pref

And move the layout to settingslib.

The new TwoTargetPreference is a preference object that has a vertical
divider between main panel and widget (if it exists).

Converted master switch, restricted preference, gear preference to use
the new style

Change-Id: Ia3848cebb06b8edc4e60abf9b0caf13cdb29c22c
Fix: 36511169
Test: make RunSettingsRoboTests
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 0404d0b..10b91cb 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -1014,6 +1014,8 @@
             </intent-filter>
             <meta-data android:name="com.android.settings.category"
                 android:value="com.android.settings.category.ia.development" />
+            <meta-data android:name="com.android.settings.summary"
+                android:resource="@string/summary_empty" />
             <meta-data android:name="com.android.settings.FRAGMENT_CLASS"
                 android:value="com.android.settings.applications.ProcessStatsSummary" />
         </activity>
diff --git a/res/layout/preference_master_switch.xml b/res/layout/preference_master_switch.xml
deleted file mode 100644
index c7dd6d6..0000000
--- a/res/layout/preference_master_switch.xml
+++ /dev/null
@@ -1,90 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2017 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.
--->
-
-<!-- Based off preference_material_settings.xml except that ripple on only on the left side. -->
-<LinearLayout
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="wrap_content"
-    android:minHeight="?android:attr/listPreferredItemHeight"
-    android:gravity="center_vertical"
-    android:background="@android:color/transparent"
-    android:clipToPadding="false">
-
-    <LinearLayout
-        android:layout_width="0dp"
-        android:layout_height="match_parent"
-        android:layout_weight="1"
-        android:background="?android:attr/selectableItemBackground"
-        android:gravity="start|center_vertical"
-        android:paddingStart="?android:attr/listPreferredItemPaddingStart">
-
-        <LinearLayout
-            android:id="@+id/icon_container"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:minWidth="60dp"
-            android:orientation="horizontal"
-            android:paddingEnd="12dp"
-            android:paddingTop="4dp"
-            android:paddingBottom="4dp">
-            <com.android.internal.widget.PreferenceImageView
-                android:id="@android:id/icon"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:maxWidth="48dp"
-                android:maxHeight="48dp"/>
-        </LinearLayout>
-
-        <RelativeLayout
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:layout_weight="1"
-            android:paddingTop="16dp"
-            android:paddingBottom="16dp">
-
-            <TextView
-                android:id="@android:id/title"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:singleLine="true"
-                android:textAppearance="?android:attr/textAppearanceListItem"
-                android:ellipsize="marquee"/>
-
-            <TextView
-                android:id="@android:id/summary"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:layout_below="@android:id/title"
-                android:layout_alignStart="@android:id/title"
-                android:textAppearance="?android:attr/textAppearanceListItemSecondary"
-                android:textColor="?android:attr/textColorSecondary"
-                android:maxLines="10"/>
-
-        </RelativeLayout>
-
-    </LinearLayout>
-
-    <!-- Preference should place its actual preference widget here. -->
-    <LinearLayout
-        android:id="@android:id/widget_frame"
-        android:layout_width="wrap_content"
-        android:layout_height="match_parent"
-        android:gravity="end|center_vertical"
-        android:orientation="vertical"
-        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"/>
-
-</LinearLayout>
diff --git a/res/layout/preference_widget_gear.xml b/res/layout/preference_widget_gear.xml
new file mode 100644
index 0000000..f533dcf
--- /dev/null
+++ b/res/layout/preference_widget_gear.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2011 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.
+-->
+
+<!-- Settings button -->
+<ImageView
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/settings_button"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:layout_gravity="center"
+    android:paddingStart="?android:attr/listPreferredItemPaddingEnd"
+    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
+    android:background="?android:attr/selectableItemBackground"
+    android:scaleType="center"
+    android:src="@drawable/ic_settings"
+    android:contentDescription="@string/settings_button" />
+
diff --git a/res/layout/preference_widget_master_switch.xml b/res/layout/preference_widget_master_switch.xml
index ee335f1..7e28be9 100644
--- a/res/layout/preference_widget_master_switch.xml
+++ b/res/layout/preference_widget_master_switch.xml
@@ -15,28 +15,12 @@
   limitations under the License.
   -->
 
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-  android:layout_width="wrap_content"
-  android:layout_height="match_parent">
-
-    <LinearLayout
-      android:layout_width="wrap_content"
-      android:layout_height="match_parent"
-      android:gravity="start|center_vertical"
-      android:orientation="horizontal"
-      android:paddingEnd="20dp"
-      android:paddingTop="16dp"
-      android:paddingBottom="16dp">
-        <View
-          android:layout_width="1dp"
-          android:layout_height="match_parent"
-          android:background="?android:attr/dividerVertical"/>
-    </LinearLayout>
-
-    <Switch android:id="@+id/switchWidget"
-      android:layout_width="wrap_content"
-      android:layout_height="match_parent"
-      android:gravity="center_vertical"
-      android:clickable="false"/>
-
-</LinearLayout>
+<Switch
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/switchWidget"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:gravity="center_vertical"
+    android:paddingStart="?android:attr/listPreferredItemPaddingEnd"
+    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
+    android:clickable="false" />
diff --git a/res/layout/preference_widget_settings.xml b/res/layout/preference_widget_settings.xml
deleted file mode 100644
index dfeca1a..0000000
--- a/res/layout/preference_widget_settings.xml
+++ /dev/null
@@ -1,33 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2011 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.
--->
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="wrap_content"
-    android:layout_height="wrap_content"
-    android:gravity="center_vertical"
-    android:paddingEnd="?android:attr/scrollbarSize">
-
-    <!-- Settings button -->
-    <ImageView
-        android:id="@+id/settings_button"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_gravity="center_vertical"
-        android:padding="8dip"
-        android:background="?android:attr/selectableItemBackground"
-        android:src="@drawable/ic_settings"
-        android:contentDescription="@string/settings_button" />
-
-</LinearLayout>
diff --git a/src/com/android/settings/inputmethod/SpellCheckerPreference.java b/src/com/android/settings/inputmethod/SpellCheckerPreference.java
index c730bcc..02a2e29 100644
--- a/src/com/android/settings/inputmethod/SpellCheckerPreference.java
+++ b/src/com/android/settings/inputmethod/SpellCheckerPreference.java
@@ -21,7 +21,6 @@
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
-import android.support.v7.preference.ListPreference;
 import android.support.v7.preference.PreferenceViewHolder;
 import android.text.TextUtils;
 import android.view.View;
@@ -46,7 +45,7 @@
     public SpellCheckerPreference(final Context context, final SpellCheckerInfo[] scis) {
         super(context, null);
         mScis = scis;
-        setWidgetLayoutResource(R.layout.preference_widget_settings);
+        setWidgetLayoutResource(R.layout.preference_widget_gear);
         CharSequence[] labels = new CharSequence[scis.length];
         CharSequence[] values = new CharSequence[scis.length];
         for (int i = 0 ; i < scis.length; i++) {
diff --git a/src/com/android/settings/nfc/NfcPaymentPreference.java b/src/com/android/settings/nfc/NfcPaymentPreference.java
index 4738728..072d454 100644
--- a/src/com/android/settings/nfc/NfcPaymentPreference.java
+++ b/src/com/android/settings/nfc/NfcPaymentPreference.java
@@ -15,7 +15,6 @@
  */
 package com.android.settings.nfc;
 
-import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.ActivityNotFoundException;
 import android.content.Context;
@@ -33,7 +32,6 @@
 
 import com.android.settings.CustomDialogPreference;
 import com.android.settings.R;
-import com.android.settings.dashboard.SummaryLoader;
 import com.android.settings.nfc.PaymentBackend.PaymentAppInfo;
 
 import java.util.List;
@@ -59,7 +57,7 @@
         mAdapter = new NfcPaymentAdapter();
         setDialogTitle(context.getString(R.string.nfc_payment_pay_with));
         mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
-        setWidgetLayoutResource(R.layout.preference_widget_settings);
+        setWidgetLayoutResource(R.layout.preference_widget_gear);
 
         refresh();
     }
diff --git a/src/com/android/settings/widget/GearPreference.java b/src/com/android/settings/widget/GearPreference.java
index 7659f0f..9593798 100644
--- a/src/com/android/settings/widget/GearPreference.java
+++ b/src/com/android/settings/widget/GearPreference.java
@@ -33,7 +33,6 @@
 
     public GearPreference(Context context, AttributeSet attrs) {
         super(context, attrs);
-        setWidgetLayoutResource(R.layout.preference_widget_settings);
     }
 
     public void setOnGearClickListener(OnGearClickListener l) {
@@ -42,6 +41,16 @@
     }
 
     @Override
+    protected int getSecondTargetResId() {
+        return R.layout.preference_widget_gear;
+    }
+
+    @Override
+    protected boolean shouldHideSecondTarget() {
+        return mOnGearClickListener == null;
+    }
+
+    @Override
     public void onBindViewHolder(PreferenceViewHolder holder) {
         super.onBindViewHolder(holder);
         final View gear = holder.findViewById(R.id.settings_button);
diff --git a/src/com/android/settings/widget/MasterSwitchPreference.java b/src/com/android/settings/widget/MasterSwitchPreference.java
index 84f1532..3a771bb 100644
--- a/src/com/android/settings/widget/MasterSwitchPreference.java
+++ b/src/com/android/settings/widget/MasterSwitchPreference.java
@@ -17,7 +17,6 @@
 package com.android.settings.widget;
 
 import android.content.Context;
-import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceViewHolder;
 import android.util.AttributeSet;
 import android.view.View;
@@ -27,12 +26,13 @@
 
 import com.android.settings.R;
 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
+import com.android.settingslib.TwoTargetPreference;
 
 /**
  * A custom preference that provides inline switch toggle. It has a mandatory field for title, and
  * optional fields for icon and sub-text.
  */
-public class MasterSwitchPreference extends Preference {
+public class MasterSwitchPreference extends TwoTargetPreference {
 
     private Switch mSwitch;
     private boolean mChecked;
@@ -41,28 +41,29 @@
     public MasterSwitchPreference(Context context, AttributeSet attrs,
             int defStyleAttr, int defStyleRes) {
         super(context, attrs, defStyleAttr, defStyleRes);
-        init();
     }
 
     public MasterSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
         super(context, attrs, defStyleAttr);
-        init();
     }
 
     public MasterSwitchPreference(Context context, AttributeSet attrs) {
         super(context, attrs);
-        init();
     }
 
     public MasterSwitchPreference(Context context) {
         super(context);
-        init();
+    }
+
+    @Override
+    protected int getSecondTargetResId() {
+        return R.layout.preference_widget_master_switch;
     }
 
     @Override
     public void onBindViewHolder(PreferenceViewHolder holder) {
         super.onBindViewHolder(holder);
-        final View widgetView = holder.itemView.findViewById(android.R.id.widget_frame);
+        final View widgetView = holder.findViewById(android.R.id.widget_frame);
         if (widgetView != null) {
             widgetView.setOnClickListener(new OnClickListener() {
                 @Override
@@ -76,12 +77,12 @@
                 }
             });
         }
-        mSwitch = (Switch) holder.itemView.findViewById(R.id.switchWidget);
+        mSwitch = (Switch) holder.findViewById(R.id.switchWidget);
         if (mSwitch != null) {
             mSwitch.setChecked(mChecked);
         }
         if (mMultiLine) {
-            TextView textView = (TextView)holder.findViewById(android.R.id.title);
+            TextView textView = (TextView) holder.findViewById(android.R.id.title);
             if (textView != null) {
                 textView.setSingleLine(false);
             }
@@ -99,10 +100,6 @@
         }
     }
 
-    public boolean isSwitchEnabled() {
-        return mSwitch != null && mSwitch.isEnabled();
-    }
-
     public void setSwitchEnabled(boolean enabled) {
         if (mSwitch != null) {
             mSwitch.setEnabled(enabled);
@@ -128,9 +125,4 @@
     public Switch getSwitch() {
         return mSwitch;
     }
-
-    private void init() {
-        setLayoutResource(R.layout.preference_master_switch);
-        setWidgetLayoutResource(R.layout.preference_widget_master_switch);
-    }
 }
diff --git a/tests/robotests/src/com/android/settings/widget/MasterSwitchPreferenceTest.java b/tests/robotests/src/com/android/settings/widget/MasterSwitchPreferenceTest.java
index 7d2e240..3936d14 100644
--- a/tests/robotests/src/com/android/settings/widget/MasterSwitchPreferenceTest.java
+++ b/tests/robotests/src/com/android/settings/widget/MasterSwitchPreferenceTest.java
@@ -17,13 +17,10 @@
 package com.android.settings.widget;
 
 import android.content.Context;
-import android.support.v7.preference.Preference;
-import android.support.v7.preference.PreferenceViewHolder;
 import android.support.v7.preference.Preference.OnPreferenceChangeListener;
+import android.support.v7.preference.PreferenceViewHolder;
 import android.view.LayoutInflater;
 import android.view.View;
-import android.view.View.OnClickListener;
-import android.widget.CompoundButton.OnCheckedChangeListener;
 import android.widget.LinearLayout;
 import android.widget.Switch;
 
@@ -35,14 +32,11 @@
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
-import org.robolectric.shadows.ShadowApplication;
 
 import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 
 @RunWith(SettingsRobolectricTestRunner.class)
@@ -53,23 +47,23 @@
 
     @Before
     public void setUp() {
-        mContext = ShadowApplication.getInstance().getApplicationContext();
+        mContext = RuntimeEnvironment.application;
     }
 
     @Test
     public void createNewPreference_shouldSetLayout() {
         final MasterSwitchPreference preference = new MasterSwitchPreference(mContext);
 
-        assertThat(preference.getWidgetLayoutResource()).isEqualTo(
-            R.layout.preference_widget_master_switch);
+        assertThat(preference.getWidgetLayoutResource())
+                .isEqualTo(R.layout.preference_widget_master_switch);
     }
 
     @Test
     public void setChecked_shouldUpdateButtonCheckedState() {
         final MasterSwitchPreference preference = new MasterSwitchPreference(mContext);
         final PreferenceViewHolder holder = new PreferenceViewHolder(LayoutInflater.from(mContext)
-            .inflate(R.layout.preference_widget_master_switch, null));
-        final Switch toggle = (Switch) holder.itemView.findViewById(R.id.switchWidget);
+                .inflate(R.layout.preference_widget_master_switch, null));
+        final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget);
         preference.onBindViewHolder(holder);
 
         preference.setChecked(true);
@@ -82,9 +76,9 @@
     @Test
     public void setSwitchEnabled_shouldUpdateButtonEnabledState() {
         final MasterSwitchPreference preference = new MasterSwitchPreference(mContext);
-        final PreferenceViewHolder holder = new PreferenceViewHolder(
-            LayoutInflater.from(mContext).inflate(R.layout.preference_widget_master_switch, null));
-        final Switch toggle = (Switch) holder.itemView.findViewById(R.id.switchWidget);
+        final PreferenceViewHolder holder = new PreferenceViewHolder(LayoutInflater.from(mContext)
+                .inflate(R.layout.preference_widget_master_switch, null));
+        final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget);
         preference.onBindViewHolder(holder);
 
         preference.setSwitchEnabled(true);
@@ -99,10 +93,10 @@
         final MasterSwitchPreference preference = new MasterSwitchPreference(mContext);
         final LayoutInflater inflater = LayoutInflater.from(mContext);
         final PreferenceViewHolder holder = new PreferenceViewHolder(
-            inflater.inflate(R.layout.preference_master_switch, null));
+                inflater.inflate(R.layout.preference_two_target, null));
         final LinearLayout widgetView = holder.itemView.findViewById(android.R.id.widget_frame);
         inflater.inflate(R.layout.preference_widget_master_switch, widgetView, true);
-        final Switch toggle = (Switch) holder.itemView.findViewById(R.id.switchWidget);
+        final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget);
         preference.onBindViewHolder(holder);
 
         widgetView.performClick();
@@ -116,8 +110,8 @@
     public void clickWidgetView_shouldNotifyPreferenceChanged() {
         final MasterSwitchPreference preference = new MasterSwitchPreference(mContext);
         final PreferenceViewHolder holder = new PreferenceViewHolder(
-            LayoutInflater.from(mContext).inflate(R.layout.preference_master_switch, null));
-        final View widgetView = holder.itemView.findViewById(android.R.id.widget_frame);
+                LayoutInflater.from(mContext).inflate(R.layout.preference_two_target, null));
+        final View widgetView = holder.findViewById(android.R.id.widget_frame);
         final OnPreferenceChangeListener listener = mock(OnPreferenceChangeListener.class);
         preference.setOnPreferenceChangeListener(listener);
         preference.onBindViewHolder(holder);
@@ -134,9 +128,9 @@
     @Test
     public void setDisabledByAdmin_hasEnforcedAdmin_shouldDisableButton() {
         final MasterSwitchPreference preference = new MasterSwitchPreference(mContext);
-        final PreferenceViewHolder holder = new PreferenceViewHolder(
-            LayoutInflater.from(mContext).inflate(R.layout.preference_widget_master_switch, null));
-        final Switch toggle = (Switch) holder.itemView.findViewById(R.id.switchWidget);
+        final PreferenceViewHolder holder = new PreferenceViewHolder(LayoutInflater.from(mContext)
+                .inflate(R.layout.preference_widget_master_switch, null));
+        final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget);
         toggle.setEnabled(true);
         preference.onBindViewHolder(holder);
 
@@ -147,9 +141,9 @@
     @Test
     public void setDisabledByAdmin_noEnforcedAdmin_shouldEnableButton() {
         final MasterSwitchPreference preference = new MasterSwitchPreference(mContext);
-        final PreferenceViewHolder holder = new PreferenceViewHolder(
-            LayoutInflater.from(mContext).inflate(R.layout.preference_widget_master_switch, null));
-        final Switch toggle = (Switch) holder.itemView.findViewById(R.id.switchWidget);
+        final PreferenceViewHolder holder = new PreferenceViewHolder(LayoutInflater.from(mContext)
+                .inflate(R.layout.preference_widget_master_switch, null));
+        final Switch toggle = (Switch) holder.findViewById(R.id.switchWidget);
         toggle.setEnabled(false);
         preference.onBindViewHolder(holder);