OSDN Git Service

Add work-only-category controller
authorFan Zhang <zhfan@google.com>
Tue, 17 Oct 2017 01:21:00 +0000 (18:21 -0700)
committerFan Zhang <zhfan@google.com>
Tue, 17 Oct 2017 17:34:36 +0000 (10:34 -0700)
The controller will hide category if there is no child in it.

Change-Id: I9bbcc4115b991574e608933f98857b0b4eb2ed7d
Fixes: 62185871
Test: robotests

res/xml/app_default_settings.xml
res/xml/sound_settings.xml
res/xml/sound_settings_new_advance_button.xml
src/com/android/settings/applications/DefaultAppSettings.java
src/com/android/settings/applications/defaultapps/DefaultWorkBrowserPreferenceController.java
src/com/android/settings/applications/defaultapps/DefaultWorkPhonePreferenceController.java
src/com/android/settings/widget/WorkOnlyCategory.java [moved from src/com/android/settings/WorkOnlyCategory.java with 90% similarity]
src/com/android/settings/widget/WorkOnlyCategoryPreferenceController.java [new file with mode: 0644]
tests/robotests/src/com/android/settings/widget/ValidatedEditTextPreferenceTest.java
tests/robotests/src/com/android/settings/widget/WorkOnlyCategoryPreferenceControllerTest.java [new file with mode: 0644]

index 196ed9f..d2a1644 100644 (file)
@@ -74,8 +74,8 @@
         android:title="@string/domain_urls_title"
         android:fragment="com.android.settings.applications.ManageDomainUrls" />
 
-    <com.android.settings.WorkOnlyCategory
-        android:key="work_defaults"
+    <com.android.settings.widget.WorkOnlyCategory
+        android:key="work_app_defaults"
         android:title="@string/default_for_work">
 
         <Preference
@@ -93,6 +93,6 @@
             <extra android:name="for_work" android:value="true" />
         </Preference>
 
-    </com.android.settings.WorkOnlyCategory>
+    </com.android.settings.widget.WorkOnlyCategory>
 
 </PreferenceScreen>
index b0aff02..2470aeb 100644 (file)
               android:targetClass="com.android.cellbroadcastreceiver.CellBroadcastSettings" />
     </com.android.settingslib.RestrictedPreference>
 
-    <com.android.settings.WorkOnlyCategory
+    <com.android.settings.widget.WorkOnlyCategory
         android:key="sound_work_settings_section"
         android:title="@string/sound_work_settings"
         android:order="100">
                     android:ringtoneType="alarm"
                     android:dependency="work_use_personal_sounds" />
 
-    </com.android.settings.WorkOnlyCategory>
+    </com.android.settings.widget.WorkOnlyCategory>
 </PreferenceScreen>
index 6b0ba0e..a1e1a32 100644 (file)
               android:targetClass="com.android.cellbroadcastreceiver.CellBroadcastSettings" />
     </com.android.settingslib.RestrictedPreference>
 
-    <com.android.settings.WorkOnlyCategory
+    <com.android.settings.widget.WorkOnlyCategory
         android:key="sound_work_settings_section"
         android:title="@string/sound_work_settings"
         android:order="100">
                     android:ringtoneType="alarm"
                     android:dependency="work_use_personal_sounds" />
 
-    </com.android.settings.WorkOnlyCategory>
+    </com.android.settings.widget.WorkOnlyCategory>
 </PreferenceScreen>
index 5b15e51..7633bf5 100644 (file)
@@ -26,11 +26,12 @@ import com.android.settings.applications.assist.DefaultAssistPreferenceControlle
 import com.android.settings.applications.defaultapps.DefaultBrowserPreferenceController;
 import com.android.settings.applications.defaultapps.DefaultEmergencyPreferenceController;
 import com.android.settings.applications.defaultapps.DefaultHomePreferenceController;
+import com.android.settings.applications.defaultapps.DefaultPaymentSettingsPreferenceController;
 import com.android.settings.applications.defaultapps.DefaultPhonePreferenceController;
 import com.android.settings.applications.defaultapps.DefaultSmsPreferenceController;
 import com.android.settings.applications.defaultapps.DefaultWorkBrowserPreferenceController;
+import com.android.settings.widget.WorkOnlyCategoryPreferenceController;
 import com.android.settings.applications.defaultapps.DefaultWorkPhonePreferenceController;
-import com.android.settings.applications.defaultapps.DefaultPaymentSettingsPreferenceController;
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.dashboard.SummaryLoader;
 import com.android.settings.search.BaseSearchIndexProvider;
@@ -45,6 +46,7 @@ public class DefaultAppSettings extends DashboardFragment {
 
     static final String TAG = "DefaultAppSettings";
 
+    private static final String KEY_DEFAULT_WORK_CATEGORY = "work_app_defaults";
     private static final String KEY_ASSIST_VOICE_INPUT = "assist_and_voice_input";
 
     @Override
@@ -69,12 +71,16 @@ public class DefaultAppSettings extends DashboardFragment {
 
     private static List<AbstractPreferenceController> buildPreferenceControllers(Context context) {
         final List<AbstractPreferenceController> controllers = new ArrayList<>();
+        final List<AbstractPreferenceController> workControllers = new ArrayList<>();
+        workControllers.add(new DefaultWorkPhonePreferenceController(context));
+        workControllers.add(new DefaultWorkBrowserPreferenceController(context));
+        controllers.addAll(workControllers);
+        controllers.add(new WorkOnlyCategoryPreferenceController(
+                context, KEY_DEFAULT_WORK_CATEGORY, workControllers));
         controllers.add(new DefaultAssistPreferenceController(context, KEY_ASSIST_VOICE_INPUT,
                 false /* showSetting */));
         controllers.add(new DefaultBrowserPreferenceController(context));
-        controllers.add(new DefaultWorkBrowserPreferenceController(context));
         controllers.add(new DefaultPhonePreferenceController(context));
-        controllers.add(new DefaultWorkPhonePreferenceController(context));
         controllers.add(new DefaultSmsPreferenceController(context));
         controllers.add(new DefaultEmergencyPreferenceController(context));
         controllers.add(new DefaultHomePreferenceController(context));
@@ -97,10 +103,8 @@ public class DefaultAppSettings extends DashboardFragment {
                     List<String> keys = super.getNonIndexableKeys(context);
                     keys.add(KEY_ASSIST_VOICE_INPUT);
                     // TODO (b/38230148) Remove these keys when we can differentiate work results
-                    keys.add((new DefaultWorkPhonePreferenceController(context))
-                            .getPreferenceKey());
-                    keys.add((new DefaultWorkBrowserPreferenceController(context))
-                            .getPreferenceKey());
+                    keys.add(DefaultWorkPhonePreferenceController.KEY);
+                    keys.add(DefaultWorkBrowserPreferenceController.KEY);
                     return keys;
                 }
 
index 0792ff9..4dc984b 100644 (file)
@@ -23,6 +23,7 @@ import com.android.settings.Utils;
 
 public class DefaultWorkBrowserPreferenceController extends DefaultBrowserPreferenceController {
 
+    public static final String KEY = "work_default_browser";
     private final UserHandle mUserHandle;
 
     public DefaultWorkBrowserPreferenceController(Context context) {
@@ -35,7 +36,7 @@ public class DefaultWorkBrowserPreferenceController extends DefaultBrowserPrefer
 
     @Override
     public String getPreferenceKey() {
-        return "work_default_browser";
+        return KEY;
     }
 
     @Override
index c6d04fa..decff06 100644 (file)
@@ -23,6 +23,7 @@ import com.android.settings.Utils;
 
 public class DefaultWorkPhonePreferenceController extends DefaultPhonePreferenceController {
 
+    public static final String KEY = "work_default_phone_app";
     private final UserHandle mUserHandle;
 
     public DefaultWorkPhonePreferenceController(Context context) {
@@ -43,6 +44,6 @@ public class DefaultWorkPhonePreferenceController extends DefaultPhonePreference
 
     @Override
     public String getPreferenceKey() {
-        return "work_default_phone_app";
+        return KEY;
     }
 }
  * permissions and limitations under the License.
  */
 
-package com.android.settings;
+package com.android.settings.widget;
 
 import android.content.Context;
 import android.os.UserManager;
 import android.support.v7.preference.PreferenceCategory;
 import android.util.AttributeSet;
 
+import com.android.settings.SelfAvailablePreference;
+import com.android.settings.Utils;
+
 /**
  * A PreferenceCategory that is only visible when the device has a work profile.
  */
diff --git a/src/com/android/settings/widget/WorkOnlyCategoryPreferenceController.java b/src/com/android/settings/widget/WorkOnlyCategoryPreferenceController.java
new file mode 100644 (file)
index 0000000..a308b2f
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * 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.
+ */
+
+package com.android.settings.widget;
+
+import android.content.Context;
+
+import com.android.settings.core.PreferenceControllerMixin;
+import com.android.settingslib.core.AbstractPreferenceController;
+
+import java.util.List;
+
+public class WorkOnlyCategoryPreferenceController extends AbstractPreferenceController
+        implements PreferenceControllerMixin {
+
+    private final String mKey;
+    private final List<AbstractPreferenceController> mChildren;
+
+    public WorkOnlyCategoryPreferenceController(Context context,
+            String key, List<AbstractPreferenceController> childrenControllers) {
+        super(context);
+        mKey = key;
+        mChildren = childrenControllers;
+    }
+
+    @Override
+    public boolean isAvailable() {
+        if (mChildren == null || mChildren.isEmpty()) {
+            return true;
+        }
+        // Category is available if any child is available
+        for (AbstractPreferenceController controller : mChildren) {
+            if (controller.isAvailable()) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    @Override
+    public String getPreferenceKey() {
+        return mKey;
+    }
+}
index e061787..59be160 100644 (file)
 package com.android.settings.widget;
 
 
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
+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.text.InputType;
 import android.text.TextWatcher;
 import android.view.View;
@@ -33,14 +41,6 @@ import org.mockito.MockitoAnnotations;
 import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
 
-import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyInt;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
 @RunWith(SettingsRobolectricTestRunner.class)
 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
 public class ValidatedEditTextPreferenceTest {
diff --git a/tests/robotests/src/com/android/settings/widget/WorkOnlyCategoryPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/widget/WorkOnlyCategoryPreferenceControllerTest.java
new file mode 100644 (file)
index 0000000..9c7e8fc
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * 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.
+ */
+
+package com.android.settings.widget;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+
+import com.android.settings.TestConfig;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settingslib.core.AbstractPreferenceController;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class WorkOnlyCategoryPreferenceControllerTest {
+
+    private Context mContext;
+    private WorkOnlyCategoryPreferenceController mController;
+    private List<AbstractPreferenceController> mChildren;
+
+    @Before
+    public void setUp() {
+        mContext = RuntimeEnvironment.application;
+        mChildren = new ArrayList<>();
+        mController = new WorkOnlyCategoryPreferenceController(mContext, "pref_key", mChildren);
+    }
+
+    @Test
+    public void isAvailable_noChildren_true() {
+        assertThat(mController.isAvailable()).isTrue();
+    }
+
+    @Test
+    public void isAvailable_childrenAvailable_true() {
+        final AbstractPreferenceController child = mock(AbstractPreferenceController.class);
+        when(child.isAvailable()).thenReturn(true);
+        mChildren.add(child);
+
+        assertThat(mController.isAvailable()).isTrue();
+    }
+
+    @Test
+    public void isAvailable_childrenUnavailable_false() {
+        final AbstractPreferenceController child = mock(AbstractPreferenceController.class);
+        when(child.isAvailable()).thenReturn(false);
+        mChildren.add(child);
+
+        assertThat(mController.isAvailable()).isFalse();
+    }
+}