Introduce LogPersistPreferenceControllerV2

- Create new LogPersistPreferenceControllerV2
 - Deprecate LogPersistPreferenceController
 - Add DisableLogPersistWarningDialog
 - Create controller inside the DashboardFragment
 - Port logic from DevelopmentSettings into the controller

Bug: 34203528
Test: make RunSettingsRoboTests -j40
Change-Id: I8ff49ec4ece15cad2d0c60bd21488e3f5d55ee98
This commit is contained in:
jeffreyhuang
2017-10-09 16:22:40 -07:00
parent cafce68bc2
commit 60ab063ea3
8 changed files with 333 additions and 2 deletions

View File

@@ -52,7 +52,7 @@ import java.util.List;
public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFragment public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFragment
implements SwitchBar.OnSwitchChangeListener, OemUnlockDialogHost, AdbDialogHost, implements SwitchBar.OnSwitchChangeListener, OemUnlockDialogHost, AdbDialogHost,
AdbClearKeysDialogHost { AdbClearKeysDialogHost, LogPersistDialogHost {
private static final String TAG = "DevSettingsDashboard"; private static final String TAG = "DevSettingsDashboard";
@@ -179,6 +179,20 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
controller.onClearAdbKeysConfirmed(); controller.onClearAdbKeysConfirmed();
} }
@Override
public void onDisableLogPersistDialogConfirmed() {
final LogPersistPreferenceControllerV2 controller = getDevelopmentOptionsController(
LogPersistPreferenceControllerV2.class);
controller.onDisableLogPersistDialogConfirmed();
}
@Override
public void onDisableLogPersistDialogRejected() {
final LogPersistPreferenceControllerV2 controller = getDevelopmentOptionsController(
LogPersistPreferenceControllerV2.class);
controller.onDisableLogPersistDialogRejected();
}
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
boolean handledResult = false; boolean handledResult = false;
@@ -270,7 +284,7 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
controllers.add(new WaitForDebuggerPreferenceController(context)); controllers.add(new WaitForDebuggerPreferenceController(context));
controllers.add(new VerifyAppsOverUsbPreferenceControllerV2(context)); controllers.add(new VerifyAppsOverUsbPreferenceControllerV2(context));
controllers.add(new LogdSizePreferenceControllerV2(context)); controllers.add(new LogdSizePreferenceControllerV2(context));
// store logger data persistently on device controllers.add(new LogPersistPreferenceControllerV2(context, fragment, lifecycle));
controllers.add(new ConnectivityMonitorPreferenceControllerV2(context)); controllers.add(new ConnectivityMonitorPreferenceControllerV2(context));
controllers.add(new CameraLaserSensorPreferenceControllerV2(context)); controllers.add(new CameraLaserSensorPreferenceControllerV2(context));
controllers.add(new CameraHalHdrPlusPreferenceControllerV2(context)); controllers.add(new CameraHalHdrPlusPreferenceControllerV2(context));

View File

@@ -0,0 +1,76 @@
/*
* 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.development;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.os.Bundle;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
public class DisableLogPersistWarningDialog extends InstrumentedDialogFragment implements
DialogInterface.OnClickListener, DialogInterface.OnDismissListener {
public static final String TAG = "DisableLogPersistDlg";
public static void show(LogPersistDialogHost host) {
if (!(host instanceof Fragment)) {
return;
}
final Fragment hostFragment = (Fragment) host;
final FragmentManager manager = hostFragment.getActivity().getFragmentManager();
if (manager.findFragmentByTag(TAG) == null) {
final DisableLogPersistWarningDialog dialog =
new DisableLogPersistWarningDialog();
dialog.setTargetFragment(hostFragment, 0 /* requestCode */);
dialog.show(manager, TAG);
}
}
@Override
public int getMetricsCategory() {
return MetricsProto.MetricsEvent.DIALOG_LOG_PERSIST;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle(R.string.dev_logpersist_clear_warning_title)
.setMessage(R.string.dev_logpersist_clear_warning_message)
.setPositiveButton(android.R.string.yes, this /* onClickListener */)
.setNegativeButton(android.R.string.no, this /* onClickListener */)
.create();
}
@Override
public void onClick(DialogInterface dialog, int which) {
final LogPersistDialogHost host = (LogPersistDialogHost) getTargetFragment();
if (host == null) {
return;
}
if (which == DialogInterface.BUTTON_POSITIVE) {
host.onDisableLogPersistDialogConfirmed();
} else {
host.onDisableLogPersistDialogRejected();
}
}
}

View File

@@ -0,0 +1,33 @@
/*
* 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.development;
/**
* Interface for LogPersistDialogFragment callbacks.
*/
public interface LogPersistDialogHost {
/**
* Called when the user presses yes on the warning dialog.
*/
void onDisableLogPersistDialogConfirmed();
/**
* Called when the user presses no on the warning dialog.
*/
void onDisableLogPersistDialogRejected();
}

View File

@@ -0,0 +1,89 @@
/*
* 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.development;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.development.AbstractLogpersistPreferenceController;
public class LogPersistPreferenceControllerV2 extends
AbstractLogpersistPreferenceController implements PreferenceControllerMixin {
private final DevelopmentSettingsDashboardFragment mFragment;
private ListPreference mPreference;
public LogPersistPreferenceControllerV2(Context context,
DevelopmentSettingsDashboardFragment fragment, Lifecycle lifecycle) {
super(context, lifecycle);
mFragment = fragment;
}
@Override
public void showConfirmationDialog(@Nullable Preference preference) {
DisableLogPersistWarningDialog.show(mFragment);
}
@Override
public void dismissConfirmationDialog() {
// intentional no-op
}
@Override
public boolean isConfirmationDialogShowing() {
return false;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = (ListPreference) screen.findPreference(getPreferenceKey());
}
@Override
public void updateState(Preference preference) {
updateLogpersistValues();
}
@Override
protected void onDeveloperOptionsSwitchEnabled() {
mPreference.setEnabled(true);
}
@Override
protected void onDeveloperOptionsSwitchDisabled() {
writeLogpersistOption(null /* new value */, true);
mPreference.setEnabled(false);
}
public void onDisableLogPersistDialogConfirmed() {
setLogpersistOff(true);
updateLogpersistValues();
}
public void onDisableLogPersistDialogRejected() {
updateLogpersistValues();
}
}

View File

@@ -27,6 +27,10 @@ import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.development.AbstractLogpersistPreferenceController; import com.android.settingslib.development.AbstractLogpersistPreferenceController;
/**
* depreacted in favor of {@link LogdSizePreferenceControllerV2}
*/
@Deprecated
public class LogpersistPreferenceController extends AbstractLogpersistPreferenceController public class LogpersistPreferenceController extends AbstractLogpersistPreferenceController
implements PreferenceControllerMixin { implements PreferenceControllerMixin {

View File

@@ -17,6 +17,7 @@
package com.android.settings.development; package com.android.settings.development;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
@@ -220,6 +221,28 @@ public class DevelopmentSettingsDashboardFragmentTest {
verify(controller).onClearAdbKeysConfirmed(); verify(controller).onClearAdbKeysConfirmed();
} }
@Test
public void onDisableLogPersistDialogConfirmed_shouldCallControllerDialogConfirmed() {
final LogPersistPreferenceControllerV2 controller = mock(
LogPersistPreferenceControllerV2.class);
doReturn(controller).when(mDashboard).getDevelopmentOptionsController(
LogPersistPreferenceControllerV2.class);
mDashboard.onDisableLogPersistDialogConfirmed();
verify(controller).onDisableLogPersistDialogConfirmed();
}
@Test
public void onDisableLogPersistDialogRejected_shouldCallControllerDialogRejected() {
final LogPersistPreferenceControllerV2 controller = mock(
LogPersistPreferenceControllerV2.class);
doReturn(controller).when(mDashboard).getDevelopmentOptionsController(
LogPersistPreferenceControllerV2.class);
mDashboard.onDisableLogPersistDialogRejected();
verify(controller).onDisableLogPersistDialogRejected();
}
@Implements(EnableDevelopmentSettingWarningDialog.class) @Implements(EnableDevelopmentSettingWarningDialog.class)
public static class ShadowEnableDevelopmentSettingWarningDialog { public static class ShadowEnableDevelopmentSettingWarningDialog {

View File

@@ -0,0 +1,88 @@
/*
* 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.development;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.SystemProperties;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowSystemProperties;
import com.android.settingslib.R;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.After;
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;
import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH,
sdk = TestConfig.SDK_VERSION,
shadows = {SettingsShadowSystemProperties.class})
public class LogPersistPreferenceControllerV2Test {
@Mock
private ListPreference mPreference;
@Mock
private PreferenceScreen mScreen;
@Mock
private DevelopmentSettingsDashboardFragment mFragment;
private Context mContext;
private LogPersistPreferenceControllerV2 mController;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = new LogPersistPreferenceControllerV2(mContext, mFragment, new Lifecycle());
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
SystemProperties.set("ro.debuggable", "1");
mController.displayPreference(mScreen);
}
@After
public void teardown() {
SettingsShadowSystemProperties.clear();
}
@Test
public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() {
mController.onDeveloperOptionsSwitchDisabled();
verify(mPreference).setEnabled(false);
}
@Test
public void onDeveloperOptionsSwitchEnabled_shouldEnablePreference() {
mController.onDeveloperOptionsSwitchEnabled();
verify(mPreference).setEnabled(true);
}
}

View File

@@ -32,6 +32,10 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
/**
* deprecated in favor of {@link LogPersistPreferenceControllerV2}
*/
@Deprecated
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class LogpersistPreferenceControllerTest { public class LogpersistPreferenceControllerTest {