Merge "Refactor status page to DashboardFragment"

This commit is contained in:
TreeHugger Robot
2017-10-11 01:00:50 +00:00
committed by Android (Google) Code Review
8 changed files with 507 additions and 92 deletions

View File

@@ -15,6 +15,9 @@
*/
package com.android.settings.core;
import android.text.TextUtils;
import android.util.Log;
import com.android.settings.search.ResultPayload;
import com.android.settings.search.SearchIndexableRaw;
import com.android.settingslib.core.AbstractPreferenceController;
@@ -26,6 +29,8 @@ import java.util.List;
*/
public interface PreferenceControllerMixin {
String TAG = "PrefControllerMixin";
/**
* Updates non-indexable keys for search provider.
*
@@ -34,7 +39,13 @@ public interface PreferenceControllerMixin {
default void updateNonIndexableKeys(List<String> keys) {
if (this instanceof AbstractPreferenceController) {
if (!((AbstractPreferenceController) this).isAvailable()) {
keys.add(((AbstractPreferenceController) this).getPreferenceKey());
final String key = ((AbstractPreferenceController) this).getPreferenceKey();
if (TextUtils.isEmpty(key)) {
Log.w(TAG,
"Skipping updateNonIndexableKeys due to empty key " + this.toString());
return;
}
keys.add(key);
}
}
}

View File

@@ -0,0 +1,104 @@
/*
* 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.deviceinfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.Utils;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
public class BatteryInfoPreferenceController extends AbstractPreferenceController
implements PreferenceControllerMixin, LifecycleObserver, OnStart, OnStop {
@VisibleForTesting
static final IntentFilter BATTERY_INFO_RECEIVER_INTENT_FILTER =
new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
@VisibleForTesting
static final String KEY_BATTERY_STATUS = "battery_status";
@VisibleForTesting
static final String KEY_BATTERY_LEVEL = "battery_level";
@VisibleForTesting
BroadcastReceiver mBatteryInfoReceiver;
private Preference mBatteryStatus;
private Preference mBatteryLevel;
public BatteryInfoPreferenceController(Context context, Lifecycle lifecycle) {
super(context);
mBatteryInfoReceiver = new BatteryInfoReceiver(context);
if (lifecycle != null) {
lifecycle.addObserver(this);
}
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public String getPreferenceKey() {
return null;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mBatteryLevel = screen.findPreference(KEY_BATTERY_LEVEL);
mBatteryStatus = screen.findPreference(KEY_BATTERY_STATUS);
}
@Override
public void onStart() {
mContext.registerReceiver(mBatteryInfoReceiver, BATTERY_INFO_RECEIVER_INTENT_FILTER);
}
@Override
public void onStop() {
mContext.unregisterReceiver(mBatteryInfoReceiver);
}
private class BatteryInfoReceiver extends BroadcastReceiver {
private final Context mContext;
public BatteryInfoReceiver(Context context) {
mContext = context;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
mBatteryLevel.setSummary(Utils.getBatteryPercentage(intent));
mBatteryStatus.setSummary(Utils.getBatteryStatus(mContext.getResources(), intent));
}
}
}
}

View File

@@ -0,0 +1,45 @@
/*
* 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.deviceinfo;
import android.content.Context;
import android.os.UserManager;
import com.android.settings.Utils;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
public class ImeiInfoPreferenceController extends AbstractPreferenceController
implements PreferenceControllerMixin {
private static final String KEY_IMEI_INFO = "imei_info";
public ImeiInfoPreferenceController(Context context) {
super(context);
}
@Override
public boolean isAvailable() {
return ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).isAdminUser()
&& !Utils.isWifiOnly(mContext);
}
@Override
public String getPreferenceKey() {
return KEY_IMEI_INFO;
}
}

View File

@@ -0,0 +1,45 @@
/*
* 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.deviceinfo;
import android.content.Context;
import android.os.UserManager;
import com.android.settings.Utils;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
public class SimStatusPreferenceController extends AbstractPreferenceController
implements PreferenceControllerMixin {
private static final String KEY_SIM_STATUS = "sim_status";
public SimStatusPreferenceController(Context context) {
super(context);
}
@Override
public boolean isAvailable() {
return ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).isAdminUser()
&& !Utils.isWifiOnly(mContext);
}
@Override
public String getPreferenceKey() {
return KEY_SIM_STATUS;
}
}

View File

@@ -16,95 +16,26 @@
package com.android.settings.deviceinfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.UserManager;
import android.provider.SearchIndexableResource;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.Utils;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Fragment for showing device hardware info, such as MAC addresses and serial numbers
*/
public class Status extends SettingsPreferenceFragment implements Indexable {
public class Status extends DashboardFragment {
private static final String KEY_BATTERY_STATUS = "battery_status";
private static final String KEY_BATTERY_LEVEL = "battery_level";
private static final String KEY_SIM_STATUS = "sim_status";
private static final String KEY_IMEI_INFO = "imei_info";
private SerialNumberPreferenceController mSerialNumberPreferenceController;
private UptimePreferenceController mUptimePreferenceController;
private Preference mBatteryStatus;
private Preference mBatteryLevel;
private BluetoothAddressPreferenceController mBluetoothAddressPreferenceController;
private IpAddressPreferenceController mIpAddressPreferenceController;
private WifiMacAddressPreferenceController mWifiMacAddressPreferenceController;
private ImsStatusPreferenceController mImsStatusPreferenceController;
private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
mBatteryLevel.setSummary(Utils.getBatteryPercentage(intent));
mBatteryStatus.setSummary(Utils.getBatteryStatus(getResources(), intent));
}
}
};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
final Context context = getContext();
final Lifecycle lifecycle = getLifecycle();
mSerialNumberPreferenceController = new SerialNumberPreferenceController(context);
mUptimePreferenceController = new UptimePreferenceController(context, lifecycle);
mBluetoothAddressPreferenceController =
new BluetoothAddressPreferenceController(context, lifecycle);
mIpAddressPreferenceController = new IpAddressPreferenceController(context, lifecycle);
mWifiMacAddressPreferenceController =
new WifiMacAddressPreferenceController(context, lifecycle);
mImsStatusPreferenceController = new ImsStatusPreferenceController(context, lifecycle);
addPreferencesFromResource(R.xml.device_info_status);
mBatteryLevel = findPreference(KEY_BATTERY_LEVEL);
mBatteryStatus = findPreference(KEY_BATTERY_STATUS);
final PreferenceScreen screen = getPreferenceScreen();
mSerialNumberPreferenceController.displayPreference(screen);
mUptimePreferenceController.displayPreference(screen);
mBluetoothAddressPreferenceController.displayPreference(screen);
mIpAddressPreferenceController.displayPreference(screen);
mWifiMacAddressPreferenceController.displayPreference(screen);
mImsStatusPreferenceController.displayPreference(screen);
// Remove SimStatus and Imei for Secondary user as it access Phone b/19165700
// Also remove on Wi-Fi only devices.
//TODO: the bug above will surface in split system user mode.
if (!UserManager.get(getContext()).isAdminUser()
|| Utils.isWifiOnly(getContext())) {
removePreferenceFromScreen(KEY_SIM_STATUS);
removePreferenceFromScreen(KEY_IMEI_INFO);
}
}
private static final String TAG = "DeviceStatus";
@Override
public int getMetricsCategory() {
@@ -112,28 +43,33 @@ public class Status extends SettingsPreferenceFragment implements Indexable {
}
@Override
public void onResume() {
super.onResume();
getContext().registerReceiver(mBatteryInfoReceiver,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
protected String getLogTag() {
return TAG;
}
@Override
public void onPause() {
super.onPause();
getContext().unregisterReceiver(mBatteryInfoReceiver);
protected int getPreferenceScreenResId() {
return R.xml.device_info_status;
}
/**
* Removes the specified preference, if it exists.
* @param key the key for the Preference item
*/
private void removePreferenceFromScreen(String key) {
Preference pref = findPreference(key);
if (pref != null) {
getPreferenceScreen().removePreference(pref);
}
@Override
protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
return buildPreferenceControllers(context, getLifecycle());
}
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
Lifecycle lifecycle) {
final List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(new SerialNumberPreferenceController(context));
controllers.add(new UptimePreferenceController(context, lifecycle));
controllers.add(new BluetoothAddressPreferenceController(context, lifecycle));
controllers.add(new IpAddressPreferenceController(context, lifecycle));
controllers.add(new WifiMacAddressPreferenceController(context, lifecycle));
controllers.add(new ImsStatusPreferenceController(context, lifecycle));
controllers.add(new SimStatusPreferenceController(context));
controllers.add(new ImeiInfoPreferenceController(context));
controllers.add(new BatteryInfoPreferenceController(context, lifecycle));
return controllers;
}
/**
@@ -149,5 +85,11 @@ public class Status extends SettingsPreferenceFragment implements Indexable {
sir.xmlResId = R.xml.device_info_status;
return Arrays.asList(sir);
}
@Override
public List<AbstractPreferenceController> getPreferenceControllers(Context
context) {
return buildPreferenceControllers(context, null /* lifecycle */);
}
};
}

View File

@@ -0,0 +1,105 @@
/*
* 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.deviceinfo;
import static com.android.settings.deviceinfo.BatteryInfoPreferenceController
.BATTERY_INFO_RECEIVER_INTENT_FILTER;
import static com.android.settings.deviceinfo.BatteryInfoPreferenceController.KEY_BATTERY_LEVEL;
import static com.android.settings.deviceinfo.BatteryInfoPreferenceController.KEY_BATTERY_STATUS;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.core.lifecycle.Lifecycle;
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;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class BatteryInfoPreferenceControllerTest {
private Context mContext;
@Mock
private PreferenceScreen mScreen;
private Preference mBatteryLevel;
private Preference mBatteryStatus;
private Lifecycle mLifecycle;
private BatteryInfoPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mLifecycle = new Lifecycle();
mController = new BatteryInfoPreferenceController(mContext, mLifecycle);
mBatteryLevel = new Preference(mContext);
mBatteryStatus = new Preference(mContext);
when(mScreen.findPreference(KEY_BATTERY_STATUS)).thenReturn(mBatteryStatus);
when(mScreen.findPreference(KEY_BATTERY_LEVEL)).thenReturn(mBatteryLevel);
}
@Test
public void isAlwaysAvailable() {
assertThat(mController.getPreferenceKey()).isNull();
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void runThroughLifecycle_shouldRegisterUnregisterBatteryInfoReceiver() {
final Context context = mock(Context.class);
mController = new BatteryInfoPreferenceController(context, mLifecycle);
mLifecycle.onStart();
mLifecycle.onStop();
verify(context).registerReceiver(mController.mBatteryInfoReceiver,
BATTERY_INFO_RECEIVER_INTENT_FILTER);
verify(context).unregisterReceiver(mController.mBatteryInfoReceiver);
}
@Test
public void onReceiveBatteryInfoBroadcast_shouldUpdatePreferences() {
mController.displayPreference(mScreen);
final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
intent.putExtra(BatteryManager.EXTRA_LEVEL, 50);
intent.putExtra(BatteryManager.EXTRA_SCALE, 100);
intent.putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_CHARGING);
mController.mBatteryInfoReceiver.onReceive(mContext, intent);
assertThat(mBatteryLevel.getSummary()).isEqualTo("50%");
assertThat(mBatteryStatus.getSummary())
.isEqualTo(mContext.getText(R.string.battery_info_status_charging));
}
}

View File

@@ -0,0 +1,81 @@
/*
* 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.deviceinfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.UserManager;
import com.android.settings.TestConfig;
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.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class ImeiInfoPreferenceControllerTest {
@Mock
private Context mContext;
@Mock
private UserManager mUserManager;
@Mock
private ConnectivityManager mConnectivityManager;
private ImeiInfoPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(
mConnectivityManager);
mController = new ImeiInfoPreferenceController(mContext);
}
@Test
public void testIsAvailable_isAdminAndHasMobile_shouldReturnTrue() {
when(mUserManager.isAdminUser()).thenReturn(true);
when(mConnectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE))
.thenReturn(true);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void testIsAvailable_isAdminButNoMobile_shouldReturnFalse() {
when(mUserManager.isAdminUser()).thenReturn(true);
when(mConnectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE))
.thenReturn(false);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void testIsAvailable_isNotAdmin_shouldReturnFalse() {
when(mUserManager.isAdminUser()).thenReturn(false);
assertThat(mController.isAvailable()).isFalse();
}
}

View File

@@ -0,0 +1,82 @@
/*
* 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.deviceinfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.UserManager;
import com.android.settings.TestConfig;
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.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class SimStatusPreferenceControllerTest {
@Mock
private Context mContext;
@Mock
private UserManager mUserManager;
@Mock
private ConnectivityManager mConnectivityManager;
private SimStatusPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(
mConnectivityManager);
mController = new SimStatusPreferenceController(mContext);
}
@Test
public void testIsAvailable_isAdminAndHasMobile_shouldReturnTrue() {
when(mUserManager.isAdminUser()).thenReturn(true);
when(mConnectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE))
.thenReturn(true);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void testIsAvailable_isAdminButNoMobile_shouldReturnFalse() {
when(mUserManager.isAdminUser()).thenReturn(true);
when(mConnectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE))
.thenReturn(false);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void testIsAvailable_isNotAdmin_shouldReturnFalse() {
when(mUserManager.isAdminUser()).thenReturn(false);
assertThat(mController.isAvailable()).isFalse();
}
}