Commit 770f4abf authored by Doris Ling's avatar Doris Ling

Disable changing lock when device is not provisioned.

When the device is not yet provisioned and settings is launched:
- disable the entry point for changing device lock
- remove the search panel from settings home page
- remove the search menu

Bug: 110034419
Test: make RunSettingsRoboTests
Change-Id: Ieb7eb0e8699229ec0824ccc19d7b958ac44965a2
Merged-In: Ieb7eb0e8699229ec0824ccc19d7b958ac44965a2
parent 0a8de9d7
......@@ -291,8 +291,10 @@ public class SettingsActivity extends SettingsDrawerActivity
launchSettingFragment(initialFragmentName, isSubSettings, intent);
}
final boolean deviceProvisioned = Utils.isDeviceProvisioned(this);
if (mIsShowingDashboard) {
findViewById(R.id.search_bar).setVisibility(View.VISIBLE);
findViewById(R.id.search_bar).setVisibility(
deviceProvisioned ? View.VISIBLE : View.INVISIBLE);
findViewById(R.id.action_bar).setVisibility(View.GONE);
final Toolbar toolbar = findViewById(R.id.search_action_bar);
FeatureFactory.getFactory(this).getSearchFeatureProvider()
......@@ -311,7 +313,6 @@ public class SettingsActivity extends SettingsDrawerActivity
ActionBar actionBar = getActionBar();
if (actionBar != null) {
boolean deviceProvisioned = Utils.isDeviceProvisioned(this);
actionBar.setDisplayHomeAsUpEnabled(deviceProvisioned);
actionBar.setHomeButtonEnabled(deviceProvisioned);
actionBar.setDisplayShowTitleEnabled(!mIsShowingDashboard);
......
......@@ -164,6 +164,11 @@ public class ChooseLockGeneric extends SettingsActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Activity activity = getActivity();
if (!Utils.isDeviceProvisioned(activity) && !canRunBeforeDeviceProvisioned()) {
activity.finish();
return;
}
String chooseLockAction = getActivity().getIntent().getAction();
mFingerprintManager = Utils.getFingerprintManagerOrNull(getActivity());
......@@ -249,6 +254,10 @@ public class ChooseLockGeneric extends SettingsActivity {
addHeaderView();
}
protected boolean canRunBeforeDeviceProvisioned() {
return false;
}
protected void addHeaderView() {
if (mForFingerprint) {
setHeaderView(R.layout.choose_lock_generic_fingerprint_header);
......
......@@ -129,6 +129,11 @@ public class SetupChooseLockGeneric extends ChooseLockGeneric {
return layout.onCreateRecyclerView(inflater, parent, savedInstanceState);
}
@Override
protected boolean canRunBeforeDeviceProvisioned() {
return true;
}
/***
* Disables preferences that are less secure than required quality and shows only secure
* screen lock options here.
......
......@@ -25,6 +25,7 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.search.SearchFeatureProvider;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
......@@ -52,6 +53,9 @@ public class SearchMenuController implements LifecycleObserver, OnCreateOptionsM
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (!Utils.isDeviceProvisioned(mHost.getContext())) {
return;
}
if (menu == null) {
return;
}
......
......@@ -16,6 +16,7 @@
package com.android.settings;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.doReturn;
......@@ -27,17 +28,25 @@ import static org.mockito.Mockito.when;
import android.app.ActivityManager;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.Settings.Global;
import android.view.View;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowResources;
import com.android.settings.testutils.shadow.SettingsShadowResourcesImpl;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
public class SettingsActivityTest {
......@@ -49,15 +58,46 @@ public class SettingsActivityTest {
@Mock
private Bitmap mBitmap;
private SettingsActivity mActivity;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mActivity = spy(new SettingsActivity());
doReturn(mBitmap).when(mActivity).getBitmapFromXmlResource(anyInt());
}
@Test
@Config(shadows = {
SettingsShadowResourcesImpl.class,
SettingsShadowResources.SettingsShadowTheme.class,
})
public void onCreate_deviceNotProvisioned_shouldDisableSearch() {
Global.putInt(mContext.getContentResolver(), Global.DEVICE_PROVISIONED, 0);
final Intent intent = new Intent(mContext, Settings.class);
final SettingsActivity activity =
Robolectric.buildActivity(SettingsActivity.class, intent).create(Bundle.EMPTY).get();
assertThat(activity.findViewById(R.id.search_bar).getVisibility())
.isEqualTo(View.INVISIBLE);
}
@Test
@Config(shadows = {
SettingsShadowResourcesImpl.class,
SettingsShadowResources.SettingsShadowTheme.class,
})
public void onCreate_deviceProvisioned_shouldEnableSearch() {
Global.putInt(mContext.getContentResolver(), Global.DEVICE_PROVISIONED, 1);
final Intent intent = new Intent(mContext, Settings.class);
final SettingsActivity activity =
Robolectric.buildActivity(SettingsActivity.class, intent).create(Bundle.EMPTY).get();
assertThat(activity.findViewById(R.id.search_bar).getVisibility()).isEqualTo(View.VISIBLE);
}
@Test
public void launchSettingFragment_nullExtraShowFragment_shouldNotCrash() {
when(mActivity.getFragmentManager()).thenReturn(mFragmentManager);
......
/*
* 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.password;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings.Global;
import com.android.settings.password.ChooseLockGeneric.ChooseLockGenericFragment;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowResources;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
public class ChooseLockGenericTest {
@After
public void tearDown() {
Global.putInt(RuntimeEnvironment.application.getContentResolver(),
Global.DEVICE_PROVISIONED, 1);
}
@Test
@Config(shadows = SettingsShadowResources.SettingsShadowTheme.class)
public void onCreate_deviceNotProvisioned_shouldFinishActivity() {
final Context context = RuntimeEnvironment.application;
Global.putInt(context.getContentResolver(), Global.DEVICE_PROVISIONED, 0);
final Activity activity = mock(Activity.class);
when(activity.getContentResolver()).thenReturn(context.getContentResolver());
when(activity.getTheme()).thenReturn(context.getTheme());
final ChooseLockGenericFragment fragment = spy(new ChooseLockGenericFragment());
when(fragment.getActivity()).thenReturn(activity);
when(fragment.getArguments()).thenReturn(Bundle.EMPTY);
fragment.onCreate(Bundle.EMPTY);
verify(activity).finish();
}
}
......@@ -17,11 +17,14 @@
package com.android.settings.search.actionbar;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings.Global;
import android.view.Menu;
import android.view.MenuItem;
......@@ -35,6 +38,7 @@ 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 SearchMenuControllerTest {
......@@ -43,12 +47,16 @@ public class SearchMenuControllerTest {
private Menu mMenu;
private TestPreferenceFragment mPreferenceHost;
private ObservableFragment mHost;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mHost = new ObservableFragment();
mContext = RuntimeEnvironment.application;
mHost = spy(new ObservableFragment());
when(mHost.getContext()).thenReturn(mContext);
mPreferenceHost = new TestPreferenceFragment();
Global.putInt(mContext.getContentResolver(), Global.DEVICE_PROVISIONED, 1);
when(mMenu.add(Menu.NONE, Menu.NONE, 0 /* order */, R.string.search_menu))
.thenReturn(mock(MenuItem.class));
......@@ -81,9 +89,23 @@ public class SearchMenuControllerTest {
verifyZeroInteractions(mMenu);
}
@Test
public void init_deviceNotProvisioned_shouldNotAddMenu() {
Global.putInt(mContext.getContentResolver(), Global.DEVICE_PROVISIONED, 0);
SearchMenuController.init(mHost);
mHost.getLifecycle().onCreateOptionsMenu(mMenu, null /* inflater */);
verifyZeroInteractions(mMenu);
}
private static class TestPreferenceFragment extends ObservablePreferenceFragment {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
}
@Override
public Context getContext() {
return RuntimeEnvironment.application;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment