Refresh conditions only when it changes.
- Instead of force refresh when user tap action button, we not wait until state changes for each conditional cards. Test: robotests Change-Id: I2eca59a06b8cb332b7b99f017baefb3d5b53234b
This commit is contained in:
@@ -28,9 +28,14 @@ public class BackgroundDataConditionController implements ConditionalCardControl
|
|||||||
static final int ID = Objects.hash("BackgroundDataConditionController");
|
static final int ID = Objects.hash("BackgroundDataConditionController");
|
||||||
|
|
||||||
private final Context mAppContext;
|
private final Context mAppContext;
|
||||||
|
private final ConditionManager mConditionManager;
|
||||||
|
private final NetworkPolicyManager mNetworkPolicyManager;
|
||||||
|
|
||||||
public BackgroundDataConditionController(Context appContext) {
|
public BackgroundDataConditionController(Context appContext, ConditionManager manager) {
|
||||||
mAppContext = appContext;
|
mAppContext = appContext;
|
||||||
|
mConditionManager = manager;
|
||||||
|
mNetworkPolicyManager =
|
||||||
|
(NetworkPolicyManager) appContext.getSystemService(Context.NETWORK_POLICY_SERVICE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -40,7 +45,7 @@ public class BackgroundDataConditionController implements ConditionalCardControl
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isDisplayable() {
|
public boolean isDisplayable() {
|
||||||
return NetworkPolicyManager.from(mAppContext).getRestrictBackground();
|
return mNetworkPolicyManager.getRestrictBackground();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -50,7 +55,8 @@ public class BackgroundDataConditionController implements ConditionalCardControl
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onActionClick() {
|
public void onActionClick() {
|
||||||
NetworkPolicyManager.from(mAppContext).setRestrictBackground(false);
|
mNetworkPolicyManager.setRestrictBackground(false);
|
||||||
|
mConditionManager.onConditionChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -100,7 +100,6 @@ public class ConditionManager {
|
|||||||
*/
|
*/
|
||||||
public void onActionClick(long id) {
|
public void onActionClick(long id) {
|
||||||
getController(id).onActionClick();
|
getController(id).onActionClick();
|
||||||
onConditionChanged();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -155,15 +154,16 @@ public class ConditionManager {
|
|||||||
private void initCandidates() {
|
private void initCandidates() {
|
||||||
// Initialize controllers first.
|
// Initialize controllers first.
|
||||||
mCardControllers.add(new AirplaneModeConditionController(mAppContext, this /* manager */));
|
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 BatterySaverConditionController(mAppContext, this /* manager */));
|
||||||
mCardControllers.add(new CellularDataConditionController(mAppContext, this /* manager */));
|
mCardControllers.add(new CellularDataConditionController(mAppContext, this /* manager */));
|
||||||
mCardControllers.add(new DndConditionCardController(mAppContext, this /* manager */));
|
mCardControllers.add(new DndConditionCardController(mAppContext, this /* manager */));
|
||||||
mCardControllers.add(new HotspotConditionController(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 RingerVibrateConditionController(mAppContext, this /* manager */));
|
||||||
mCardControllers.add(new RingerMutedConditionController(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.
|
// Initialize ui model later. UI model depends on controller.
|
||||||
mCandidates.add(new AirplaneModeConditionCard(mAppContext));
|
mCandidates.add(new AirplaneModeConditionCard(mAppContext));
|
||||||
|
@@ -30,10 +30,12 @@ public class NightDisplayConditionController implements ConditionalCardControlle
|
|||||||
ColorDisplayController.Callback {
|
ColorDisplayController.Callback {
|
||||||
static final int ID = Objects.hash("NightDisplayConditionController");
|
static final int ID = Objects.hash("NightDisplayConditionController");
|
||||||
|
|
||||||
|
private final ConditionManager mConditionManager;
|
||||||
private final ColorDisplayController mController;
|
private final ColorDisplayController mController;
|
||||||
|
|
||||||
public NightDisplayConditionController(Context appContext) {
|
public NightDisplayConditionController(Context appContext, ConditionManager manager) {
|
||||||
mController = new ColorDisplayController(appContext);
|
mController = new ColorDisplayController(appContext);
|
||||||
|
mConditionManager = manager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -69,4 +71,9 @@ public class NightDisplayConditionController implements ConditionalCardControlle
|
|||||||
public void stopMonitoringStateChange() {
|
public void stopMonitoringStateChange() {
|
||||||
mController.setListener(null);
|
mController.setListener(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onActivated(boolean activated) {
|
||||||
|
mConditionManager.onConditionChanged();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -16,11 +16,14 @@
|
|||||||
|
|
||||||
package com.android.settings.homepage.conditional;
|
package com.android.settings.homepage.conditional;
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
import android.content.pm.UserInfo;
|
import android.content.pm.UserInfo;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
|
||||||
import com.android.settings.Settings;
|
import com.android.settings.Settings;
|
||||||
|
|
||||||
@@ -31,13 +34,25 @@ public class WorkModeConditionController implements ConditionalCardController {
|
|||||||
|
|
||||||
static final int ID = Objects.hash("WorkModeConditionController");
|
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 Context mAppContext;
|
||||||
private final UserManager mUm;
|
private final UserManager mUm;
|
||||||
|
private final ConditionManager mConditionManager;
|
||||||
|
private final Receiver mReceiver;
|
||||||
|
|
||||||
private UserHandle mUserHandle;
|
private UserHandle mUserHandle;
|
||||||
|
|
||||||
public WorkModeConditionController(Context appContext) {
|
public WorkModeConditionController(Context appContext, ConditionManager manager) {
|
||||||
mAppContext = appContext;
|
mAppContext = appContext;
|
||||||
mUm = mAppContext.getSystemService(UserManager.class);
|
mUm = mAppContext.getSystemService(UserManager.class);
|
||||||
|
mConditionManager = manager;
|
||||||
|
mReceiver = new Receiver();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -66,12 +81,12 @@ public class WorkModeConditionController implements ConditionalCardController {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void startMonitoringStateChange() {
|
public void startMonitoringStateChange() {
|
||||||
|
mAppContext.registerReceiver(mReceiver, FILTER);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stopMonitoringStateChange() {
|
public void stopMonitoringStateChange() {
|
||||||
|
mAppContext.unregisterReceiver(mReceiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateUserHandle() {
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -22,28 +22,37 @@ import static org.mockito.Mockito.verify;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.net.NetworkPolicyManager;
|
||||||
|
|
||||||
import com.android.settings.Settings;
|
import com.android.settings.Settings;
|
||||||
import com.android.settings.homepage.conditional.BackgroundDataConditionController;
|
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.shadows.ShadowApplication;
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
public class BackgroundDataConditionControllerTest {
|
public class BackgroundDataConditionControllerTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private ConditionManager mConditionManager;
|
||||||
|
@Mock
|
||||||
|
private NetworkPolicyManager mNetworkPolicyManager;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private BackgroundDataConditionController mController;
|
private BackgroundDataConditionController mController;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
|
ShadowApplication.getInstance().setSystemService(Context.NETWORK_POLICY_SERVICE,
|
||||||
|
mNetworkPolicyManager);
|
||||||
mContext = spy(RuntimeEnvironment.application);
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
mController = new BackgroundDataConditionController(mContext);
|
mController = new BackgroundDataConditionController(mContext, mConditionManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -56,4 +65,10 @@ public class BackgroundDataConditionControllerTest {
|
|||||||
assertThat(intent.getComponent().getClassName()).isEqualTo(
|
assertThat(intent.getComponent().getClassName()).isEqualTo(
|
||||||
Settings.DataUsageSummaryActivity.class.getName());
|
Settings.DataUsageSummaryActivity.class.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onActionClick_shouldRefreshCondition() {
|
||||||
|
mController.onActionClick();
|
||||||
|
verify(mConditionManager).onConditionChanged();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
@@ -24,24 +24,28 @@ import android.content.ComponentName;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
import com.android.settings.Settings;
|
import com.android.settings.Settings;
|
||||||
import com.android.settings.homepage.conditional.WorkModeConditionController;
|
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
public class WorkModeConditionControllerTest {
|
public class WorkModeConditionControllerTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private ConditionManager mConditionManager;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private WorkModeConditionController mController;
|
private WorkModeConditionController mController;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
mContext = spy(RuntimeEnvironment.application);
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
mController = new WorkModeConditionController(mContext);
|
mController = new WorkModeConditionController(mContext, mConditionManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Reference in New Issue
Block a user