Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2683549e authored by Wilson Wu's avatar Wilson Wu Committed by Android (Google) Code Review
Browse files

Merge "Fix NPE if keyboard settings is launched by work apps"

parents 78ba8d0b ddb51476
Loading
Loading
Loading
Loading
+28 −9
Original line number Original line Diff line number Diff line
@@ -68,16 +68,35 @@ public class AvailableVirtualKeyboardFragment extends DashboardFragment
    public void onAttach(Context context) {
    public void onAttach(Context context) {
        super.onAttach(context);
        super.onAttach(context);
        final int profileType = getArguments().getInt(ProfileSelectFragment.EXTRA_PROFILE);
        final int profileType = getArguments().getInt(ProfileSelectFragment.EXTRA_PROFILE);
        if (profileType == ProfileSelectFragment.ProfileType.WORK) {
        final UserManager userManager = context.getSystemService(UserManager.class);
            final UserManager userManager = UserManager.get(context);
        final int currentUserId = UserHandle.myUserId();
            final UserHandle workUser = Utils.getManagedProfile(userManager);
        final int newUserId;
            // get work userId
        final Context newUserAwareContext;
            mUserId = Utils.getManagedProfileId(userManager, UserHandle.myUserId());
        switch (profileType) {
            mUserAwareContext = context.createContextAsUser(workUser, 0);
            case ProfileSelectFragment.ProfileType.WORK: {
                final UserHandle workUser;
                if (currentUserId == UserHandle.MIN_SECONDARY_USER_ID) {
                    newUserId = currentUserId;
                    workUser = UserHandle.of(currentUserId);
                } else {
                } else {
            mUserId = UserHandle.myUserId();
                    newUserId = Utils.getManagedProfileId(userManager, currentUserId);
            mUserAwareContext = context;
                    workUser = Utils.getManagedProfile(userManager);
                }
                }
                newUserAwareContext = context.createContextAsUser(workUser, 0);
                break;
            }
            case ProfileSelectFragment.ProfileType.PERSONAL: {
                final UserHandle primaryUser = userManager.getPrimaryUser().getUserHandle();
                newUserId = primaryUser.getIdentifier();
                newUserAwareContext = context.createContextAsUser(primaryUser, 0);
                break;
            }
            default:
                newUserId = currentUserId;
                newUserAwareContext = context;
        }
        mUserId = newUserId;
        mUserAwareContext = newUserAwareContext;
    }
    }


    @Override
    @Override
+7 −1
Original line number Original line Diff line number Diff line
@@ -20,7 +20,9 @@ import static com.android.settings.dashboard.profileselector.ProfileSelectFragme


import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertThat;


import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verify;
@@ -31,6 +33,7 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.content.pm.ServiceInfo;
import android.os.Bundle;
import android.os.Bundle;
import android.os.UserHandle;
import android.provider.SearchIndexableResource;
import android.provider.SearchIndexableResource;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodManager;
@@ -43,6 +46,7 @@ import com.android.settings.R;
import com.android.settings.dashboard.profileselector.ProfileSelectFragment;
import com.android.settings.dashboard.profileselector.ProfileSelectFragment;
import com.android.settings.testutils.shadow.ShadowInputMethodManagerWithMethodList;
import com.android.settings.testutils.shadow.ShadowInputMethodManagerWithMethodList;
import com.android.settings.testutils.shadow.ShadowSecureSettings;
import com.android.settings.testutils.shadow.ShadowSecureSettings;
import com.android.settings.testutils.shadow.ShadowUserManager;
import com.android.settingslib.inputmethod.InputMethodPreference;
import com.android.settingslib.inputmethod.InputMethodPreference;
import com.android.settingslib.inputmethod.InputMethodSettingValuesWrapper;
import com.android.settingslib.inputmethod.InputMethodSettingValuesWrapper;


@@ -62,7 +66,8 @@ import java.util.List;
@RunWith(RobolectricTestRunner.class)
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {
@Config(shadows = {
        ShadowSecureSettings.class,
        ShadowSecureSettings.class,
        ShadowInputMethodManagerWithMethodList.class
        ShadowInputMethodManagerWithMethodList.class,
        ShadowUserManager.class
})
})
public class AvailableVirtualKeyboardFragmentTest {
public class AvailableVirtualKeyboardFragmentTest {


@@ -170,6 +175,7 @@ public class AvailableVirtualKeyboardFragmentTest {
        when(mFragment.getPreferenceScreen()).thenReturn(mPreferenceScreen);
        when(mFragment.getPreferenceScreen()).thenReturn(mPreferenceScreen);
        when(mPreferenceManager.getContext()).thenReturn(mContext);
        when(mPreferenceManager.getContext()).thenReturn(mContext);
        when(mContext.getSystemService(InputMethodManager.class)).thenReturn(mInputMethodManager);
        when(mContext.getSystemService(InputMethodManager.class)).thenReturn(mInputMethodManager);
        doReturn(mContext).when(mContext).createContextAsUser(any(UserHandle.class), anyInt());
    }
    }


    private List<InputMethodInfo> createFakeInputMethodInfoList(final String name, int num) {
    private List<InputMethodInfo> createFakeInputMethodInfoList(final String name, int num) {
+8 −0
Original line number Original line Diff line number Diff line
@@ -44,6 +44,8 @@ public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager


    private static boolean sIsSupportsMultipleUsers;
    private static boolean sIsSupportsMultipleUsers;


    private static final int PRIMARY_USER_ID = 0;

    private final List<String> mBaseRestrictions = new ArrayList<>();
    private final List<String> mBaseRestrictions = new ArrayList<>();
    private final List<String> mGuestRestrictions = new ArrayList<>();
    private final List<String> mGuestRestrictions = new ArrayList<>();
    private final Map<String, List<EnforcingUser>> mRestrictionSources = new HashMap<>();
    private final Map<String, List<EnforcingUser>> mRestrictionSources = new HashMap<>();
@@ -218,4 +220,10 @@ public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager
            mEnabledTypes.remove(type);
            mEnabledTypes.remove(type);
        }
        }
    }
    }

    @Implementation
    protected UserInfo getPrimaryUser() {
        return new UserInfo(PRIMARY_USER_ID, null, null,
                UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY);
    }
}
}