Navigate to work tab if start intent from work profile user

Fixes: 144909960
Test: adb shell am start --user 11 -a android.settings.SYNC_SETTINGS
should launch Settings account page and focus on WORK tab

Change-Id: If30f176b6d59cd32730b1bcbc6e9d8687d7bafd2
This commit is contained in:
Raff Tsai
2019-12-04 17:58:57 +08:00
parent 74e60b1010
commit 83f7142df9
2 changed files with 167 additions and 3 deletions

View File

@@ -17,8 +17,10 @@
package com.android.settings.dashboard.profileselector;
import android.annotation.IntDef;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.UserManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -91,7 +93,22 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mContentView = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState);
final int selected = getArguments().getInt(SettingsActivity.EXTRA_SHOW_FRAGMENT_TAB, 0);
final Activity activity = getActivity();
final int intentUser = activity.getIntent().getContentUserHint();
int selectedTab = 0;
// Start intent from a specific user eg: adb shell --user 10
if (intentUser > 0 && Utils.getManagedProfile(UserManager.get(activity)).getIdentifier()
== intentUser) {
selectedTab = WORK_TAB;
}
// Set selected tab using fragment argument
final int extraTab = getArguments() != null ? getArguments().getInt(
SettingsActivity.EXTRA_SHOW_FRAGMENT_TAB, -1) : -1;
if (extraTab != -1) {
selectedTab = extraTab;
}
final View tabContainer = mContentView.findViewById(R.id.tab_container);
final ViewPager viewPager = tabContainer.findViewById(R.id.view_pager);
@@ -99,7 +116,7 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
final TabLayout tabs = tabContainer.findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
tabContainer.setVisibility(View.VISIBLE);
final TabLayout.Tab tab = tabs.getTabAt(selected);
final TabLayout.Tab tab = tabs.getTabAt(selectedTab);
tab.select();
final FrameLayout listContainer = mContentView.findViewById(android.R.id.list_container);
@@ -109,7 +126,7 @@ public abstract class ProfileSelectFragment extends DashboardFragment {
final RecyclerView recyclerView = getListView();
recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
Utils.setActionBarShadowAnimation(getActivity(), getSettingsLifecycle(), recyclerView);
Utils.setActionBarShadowAnimation(activity, getSettingsLifecycle(), recyclerView);
return mContentView;
}

View File

@@ -0,0 +1,147 @@
/*
* Copyright (C) 2019 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.dashboard.profileselector;
import static com.android.settings.dashboard.profileselector.ProfileSelectFragment.PERSONAL_TAB;
import static com.android.settings.dashboard.profileselector.ProfileSelectFragment.WORK_TAB;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.preference.PreferenceFragmentCompat;
import androidx.recyclerview.widget.RecyclerView;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.SettingsPreferenceFragmentTest;
import com.google.android.material.tabs.TabLayout;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadows.androidx.fragment.FragmentController;
@RunWith(RobolectricTestRunner.class)
public class ProfileSelectFragmentTest {
private Context mContext;
private TestProfileSelectFragment mFragment;
private FragmentActivity mActivity;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mActivity = spy(Robolectric.buildActivity(SettingsActivity.class).get());
mFragment = spy(new TestProfileSelectFragment());
when(mFragment.getContext()).thenReturn(mContext);
when(mFragment.getActivity()).thenReturn(mActivity);
}
@Test
@Config(shadows = ShadowPreferenceFragmentCompat.class)
public void onCreateView_no_setCorrectTab() {
FragmentController.of(mFragment, new Intent()).create(0 /* containerViewId*/,
null /* bundle */).start().resume().visible().get();
final TabLayout tabs = mFragment.getView().findViewById(R.id.tabs);
assertThat(tabs.getSelectedTabPosition()).isEqualTo(PERSONAL_TAB);
}
@Test
@Config(shadows = ShadowPreferenceFragmentCompat.class)
public void onCreateView_setArgument_setCorrectTab() {
final Bundle bundle = new Bundle();
bundle.putInt(SettingsActivity.EXTRA_SHOW_FRAGMENT_TAB, WORK_TAB);
mFragment.setArguments(bundle);
FragmentController.of(mFragment, new Intent()).create(0 /* containerViewId*/,
bundle).start().resume().visible().get();
final TabLayout tabs = mFragment.getView().findViewById(R.id.tabs);
assertThat(tabs.getSelectedTabPosition()).isEqualTo(WORK_TAB);
}
public static class TestProfileSelectFragment extends ProfileSelectFragment {
@Override
public Fragment[] getFragments() {
return new Fragment[]{
new SettingsPreferenceFragmentTest.TestFragment(), //0
new SettingsPreferenceFragmentTest.TestFragment()
};
}
}
@Implements(PreferenceFragmentCompat.class)
public static class ShadowPreferenceFragmentCompat {
private Context mContext;
@Implementation
protected View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
mContext = inflater.getContext();
return inflater.inflate(R.layout.preference_list_fragment, container);
}
@Implementation
protected RecyclerView getListView() {
final RecyclerView recyclerView = new RecyclerView(mContext);
recyclerView.setAdapter(new RecyclerView.Adapter() {
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup,
int i) {
return null;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
}
@Override
public int getItemCount() {
return 0;
}
});
return recyclerView;
}
}
}