diff --git a/src/com/android/settings/homepage/conditional/BackgroundDataConditionController.java b/src/com/android/settings/homepage/conditional/BackgroundDataConditionController.java index d7c06987e74..0dc3cf124f9 100644 --- a/src/com/android/settings/homepage/conditional/BackgroundDataConditionController.java +++ b/src/com/android/settings/homepage/conditional/BackgroundDataConditionController.java @@ -28,9 +28,14 @@ public class BackgroundDataConditionController implements ConditionalCardControl static final int ID = Objects.hash("BackgroundDataConditionController"); private final Context mAppContext; + private final ConditionManager mConditionManager; + private final NetworkPolicyManager mNetworkPolicyManager; - public BackgroundDataConditionController(Context appContext) { + public BackgroundDataConditionController(Context appContext, ConditionManager manager) { mAppContext = appContext; + mConditionManager = manager; + mNetworkPolicyManager = + (NetworkPolicyManager) appContext.getSystemService(Context.NETWORK_POLICY_SERVICE); } @Override @@ -40,7 +45,7 @@ public class BackgroundDataConditionController implements ConditionalCardControl @Override public boolean isDisplayable() { - return NetworkPolicyManager.from(mAppContext).getRestrictBackground(); + return mNetworkPolicyManager.getRestrictBackground(); } @Override @@ -50,7 +55,8 @@ public class BackgroundDataConditionController implements ConditionalCardControl @Override public void onActionClick() { - NetworkPolicyManager.from(mAppContext).setRestrictBackground(false); + mNetworkPolicyManager.setRestrictBackground(false); + mConditionManager.onConditionChanged(); } @Override diff --git a/src/com/android/settings/homepage/conditional/ConditionManager.java b/src/com/android/settings/homepage/conditional/ConditionManager.java index d6e50123a52..d1c9a27771c 100644 --- a/src/com/android/settings/homepage/conditional/ConditionManager.java +++ b/src/com/android/settings/homepage/conditional/ConditionManager.java @@ -100,7 +100,6 @@ public class ConditionManager { */ public void onActionClick(long id) { getController(id).onActionClick(); - onConditionChanged(); } /** @@ -155,15 +154,16 @@ public class ConditionManager { private void initCandidates() { // Initialize controllers first. mCardControllers.add(new AirplaneModeConditionController(mAppContext, this /* manager */)); - mCardControllers.add(new BackgroundDataConditionController(mAppContext)); + mCardControllers.add( + new BackgroundDataConditionController(mAppContext, this /* manager */)); mCardControllers.add(new BatterySaverConditionController(mAppContext, this /* manager */)); mCardControllers.add(new CellularDataConditionController(mAppContext, this /* manager */)); mCardControllers.add(new DndConditionCardController(mAppContext, this /* manager */)); mCardControllers.add(new HotspotConditionController(mAppContext, this /* manager */)); - mCardControllers.add(new NightDisplayConditionController(mAppContext)); + mCardControllers.add(new NightDisplayConditionController(mAppContext, this /* manager */)); mCardControllers.add(new RingerVibrateConditionController(mAppContext, this /* manager */)); mCardControllers.add(new RingerMutedConditionController(mAppContext, this /* manager */)); - mCardControllers.add(new WorkModeConditionController(mAppContext)); + mCardControllers.add(new WorkModeConditionController(mAppContext, this /* manager */)); // Initialize ui model later. UI model depends on controller. mCandidates.add(new AirplaneModeConditionCard(mAppContext)); diff --git a/src/com/android/settings/homepage/conditional/NightDisplayConditionController.java b/src/com/android/settings/homepage/conditional/NightDisplayConditionController.java index 428fe488989..b4816f15a05 100644 --- a/src/com/android/settings/homepage/conditional/NightDisplayConditionController.java +++ b/src/com/android/settings/homepage/conditional/NightDisplayConditionController.java @@ -30,10 +30,12 @@ public class NightDisplayConditionController implements ConditionalCardControlle ColorDisplayController.Callback { static final int ID = Objects.hash("NightDisplayConditionController"); + private final ConditionManager mConditionManager; private final ColorDisplayController mController; - public NightDisplayConditionController(Context appContext) { + public NightDisplayConditionController(Context appContext, ConditionManager manager) { mController = new ColorDisplayController(appContext); + mConditionManager = manager; } @Override @@ -69,4 +71,9 @@ public class NightDisplayConditionController implements ConditionalCardControlle public void stopMonitoringStateChange() { mController.setListener(null); } + + @Override + public void onActivated(boolean activated) { + mConditionManager.onConditionChanged(); + } } diff --git a/src/com/android/settings/homepage/conditional/WorkModeConditionController.java b/src/com/android/settings/homepage/conditional/WorkModeConditionController.java index 1bd227a0912..033a6a86070 100644 --- a/src/com/android/settings/homepage/conditional/WorkModeConditionController.java +++ b/src/com/android/settings/homepage/conditional/WorkModeConditionController.java @@ -16,11 +16,14 @@ package com.android.settings.homepage.conditional; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.content.pm.UserInfo; import android.os.UserHandle; import android.os.UserManager; +import android.text.TextUtils; import com.android.settings.Settings; @@ -31,13 +34,25 @@ public class WorkModeConditionController implements ConditionalCardController { static final int ID = Objects.hash("WorkModeConditionController"); + private static final IntentFilter FILTER = new IntentFilter(); + + static { + FILTER.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE); + FILTER.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); + } + private final Context mAppContext; private final UserManager mUm; + private final ConditionManager mConditionManager; + private final Receiver mReceiver; + private UserHandle mUserHandle; - public WorkModeConditionController(Context appContext) { + public WorkModeConditionController(Context appContext, ConditionManager manager) { mAppContext = appContext; mUm = mAppContext.getSystemService(UserManager.class); + mConditionManager = manager; + mReceiver = new Receiver(); } @Override @@ -66,12 +81,12 @@ public class WorkModeConditionController implements ConditionalCardController { @Override public void startMonitoringStateChange() { - + mAppContext.registerReceiver(mReceiver, FILTER); } @Override public void stopMonitoringStateChange() { - + mAppContext.unregisterReceiver(mReceiver); } private void updateUserHandle() { @@ -87,4 +102,15 @@ public class WorkModeConditionController implements ConditionalCardController { } } } + + public class Receiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + final String action = intent.getAction(); + if (TextUtils.equals(action, Intent.ACTION_MANAGED_PROFILE_AVAILABLE) + || TextUtils.equals(action, Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) { + mConditionManager.onConditionChanged(); + } + } + } } diff --git a/tests/robotests/src/com/android/settings/homepage/conditional/BackgroundDataConditionControllerTest.java b/tests/robotests/src/com/android/settings/homepage/conditional/BackgroundDataConditionControllerTest.java index 4ad7e6dee7b..e3db59b0853 100644 --- a/tests/robotests/src/com/android/settings/homepage/conditional/BackgroundDataConditionControllerTest.java +++ b/tests/robotests/src/com/android/settings/homepage/conditional/BackgroundDataConditionControllerTest.java @@ -22,28 +22,37 @@ import static org.mockito.Mockito.verify; import android.content.Context; import android.content.Intent; +import android.net.NetworkPolicyManager; import com.android.settings.Settings; -import com.android.settings.homepage.conditional.BackgroundDataConditionController; import com.android.settings.testutils.SettingsRobolectricTestRunner; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; +import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.RuntimeEnvironment; +import org.robolectric.shadows.ShadowApplication; @RunWith(SettingsRobolectricTestRunner.class) public class BackgroundDataConditionControllerTest { + + @Mock + private ConditionManager mConditionManager; + @Mock + private NetworkPolicyManager mNetworkPolicyManager; private Context mContext; private BackgroundDataConditionController mController; @Before public void setUp() { MockitoAnnotations.initMocks(this); + ShadowApplication.getInstance().setSystemService(Context.NETWORK_POLICY_SERVICE, + mNetworkPolicyManager); mContext = spy(RuntimeEnvironment.application); - mController = new BackgroundDataConditionController(mContext); + mController = new BackgroundDataConditionController(mContext, mConditionManager); } @Test @@ -56,4 +65,10 @@ public class BackgroundDataConditionControllerTest { assertThat(intent.getComponent().getClassName()).isEqualTo( Settings.DataUsageSummaryActivity.class.getName()); } + + @Test + public void onActionClick_shouldRefreshCondition() { + mController.onActionClick(); + verify(mConditionManager).onConditionChanged(); + } } diff --git a/tests/robotests/src/com/android/settings/homepage/conditional/NightDisplayConditionControllerTest.java b/tests/robotests/src/com/android/settings/homepage/conditional/NightDisplayConditionControllerTest.java new file mode 100644 index 00000000000..130df90c696 --- /dev/null +++ b/tests/robotests/src/com/android/settings/homepage/conditional/NightDisplayConditionControllerTest.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2018 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.homepage.conditional; + +import static org.mockito.Mockito.verify; + +import android.content.Context; + +import com.android.settings.testutils.SettingsRobolectricTestRunner; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RuntimeEnvironment; + +@RunWith(SettingsRobolectricTestRunner.class) +public class NightDisplayConditionControllerTest { + + @Mock + private ConditionManager mConditionManager; + + private Context mContext; + private NightDisplayConditionController mController; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = RuntimeEnvironment.application; + mController = new NightDisplayConditionController(mContext, mConditionManager); + } + + @Test + public void onActivated_shouldUpdateCondition() { + mController.onActivated(true); + + verify(mConditionManager).onConditionChanged(); + } +} diff --git a/tests/robotests/src/com/android/settings/homepage/conditional/WorkModeConditionControllerTest.java b/tests/robotests/src/com/android/settings/homepage/conditional/WorkModeConditionControllerTest.java index 52c9ffedbf0..c993e68923d 100644 --- a/tests/robotests/src/com/android/settings/homepage/conditional/WorkModeConditionControllerTest.java +++ b/tests/robotests/src/com/android/settings/homepage/conditional/WorkModeConditionControllerTest.java @@ -24,24 +24,28 @@ import android.content.ComponentName; import android.content.Context; import com.android.settings.Settings; -import com.android.settings.homepage.conditional.WorkModeConditionController; import com.android.settings.testutils.SettingsRobolectricTestRunner; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import org.robolectric.RuntimeEnvironment; @RunWith(SettingsRobolectricTestRunner.class) public class WorkModeConditionControllerTest { + @Mock + private ConditionManager mConditionManager; private Context mContext; private WorkModeConditionController mController; @Before public void setUp() { + MockitoAnnotations.initMocks(this); mContext = spy(RuntimeEnvironment.application); - mController = new WorkModeConditionController(mContext); + mController = new WorkModeConditionController(mContext, mConditionManager); } @Test