Merge "Add a More item for Special app access page."

This commit is contained in:
TreeHugger Robot
2019-01-28 23:53:11 +00:00
committed by Android (Google) Code Review
4 changed files with 251 additions and 0 deletions

View File

@@ -9581,6 +9581,9 @@
<item quantity="other"><xliff:g id="count" example="10">%d</xliff:g> apps can use unrestricted data</item> <item quantity="other"><xliff:g id="count" example="10">%d</xliff:g> apps can use unrestricted data</item>
</plurals> </plurals>
<!-- Title for the More preference item in Special app access settings [CHAR LIMIT=30] -->
<string name="special_access_more">More</string>
<!-- Developer option to convert to file encryption - final warning --> <!-- Developer option to convert to file encryption - final warning -->
<string name="confirm_convert_to_fbe_warning">Really wipe user data and convert to file encryption?</string> <string name="confirm_convert_to_fbe_warning">Really wipe user data and convert to file encryption?</string>
<!-- Developer option to convert to file encryption - final button --> <!-- Developer option to convert to file encryption - final button -->

View File

@@ -135,4 +135,9 @@
android:title="@string/financial_apps_sms_access_title" android:title="@string/financial_apps_sms_access_title"
android:fragment="com.android.settings.applications.specialaccess.financialapps.FinancialAppsSmsAccess" android:fragment="com.android.settings.applications.specialaccess.financialapps.FinancialAppsSmsAccess"
settings:keywords="@string/keywords_financial_apps_sms_access" /> settings:keywords="@string/keywords_financial_apps_sms_access" />
<Preference
android:key="special_access_more"
android:title="@string/special_access_more"
settings:controller="com.android.settings.applications.specialaccess.MoreSpecialAccessPreferenceController" />
</PreferenceScreen> </PreferenceScreen>

View File

@@ -0,0 +1,64 @@
/*
* 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.applications.specialaccess;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.text.TextUtils;
import androidx.preference.Preference;
import com.android.settings.core.BasePreferenceController;
public class MoreSpecialAccessPreferenceController extends BasePreferenceController {
private final Intent mIntent;
public MoreSpecialAccessPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
final PackageManager packageManager = context.getPackageManager();
final String packageName = packageManager.getPermissionControllerPackageName();
if (packageName != null) {
Intent intent = new Intent(Intent.ACTION_MANAGE_SPECIAL_APP_ACCESSES)
.setPackage(packageName);
ResolveInfo resolveInfo = packageManager.resolveActivity(intent,
PackageManager.MATCH_DEFAULT_ONLY);
mIntent = resolveInfo != null ? intent : null;
} else {
mIntent = null;
}
}
@Override
public int getAvailabilityStatus() {
return mIntent != null ? AVAILABLE_UNSEARCHABLE : UNSUPPORTED_ON_DEVICE;
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (TextUtils.equals(preference.getKey(), mPreferenceKey)) {
if (mIntent != null) {
mContext.startActivity(mIntent);
}
return true;
}
return false;
}
}

View File

@@ -0,0 +1,179 @@
/*
* 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.applications.specialaccess;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import androidx.preference.Preference;
import com.android.settings.core.BasePreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class MoreSpecialAccessPreferenceControllerTest {
private static final String PREFERENCE_KEY = "more";
private static final String DIFFERENT_PREFERENCE_KEY = "different";
private static final String PERMISSION_CONTROLLER_PACKAGE_NAME =
"com.android.permissioncontroller";
@Mock
private Context mContext;
@Mock
private PackageManager mPackageManager;
@Before
public void setUp() throws PackageManager.NameNotFoundException {
MockitoAnnotations.initMocks(this);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
}
@Test
public void constructor_shouldResolveActivityWithPermissionControllerPackageName() {
when(mPackageManager.getPermissionControllerPackageName()).thenReturn(
PERMISSION_CONTROLLER_PACKAGE_NAME);
new MoreSpecialAccessPreferenceController(mContext, PREFERENCE_KEY);
ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
verify(mPackageManager).resolveActivity(intentCaptor.capture(), anyInt());
final Intent intent = intentCaptor.getValue();
assertThat(intent.getPackage()).isEqualTo(PERMISSION_CONTROLLER_PACKAGE_NAME);
}
@Test
public void getAvailabilityStatus_noPermissionController_shouldReturnUnsupportedOnDevice() {
when(mPackageManager.getPermissionControllerPackageName()).thenReturn(null);
MoreSpecialAccessPreferenceController preferenceController =
new MoreSpecialAccessPreferenceController(mContext, PREFERENCE_KEY);
assertThat(preferenceController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.UNSUPPORTED_ON_DEVICE);
}
@Test
public void getAvailabilityStatus_canNotResolveActivity_shouldReturnUnsupportedOnDevice() {
when(mPackageManager.getPermissionControllerPackageName()).thenReturn(
PERMISSION_CONTROLLER_PACKAGE_NAME);
MoreSpecialAccessPreferenceController preferenceController =
new MoreSpecialAccessPreferenceController(mContext, PREFERENCE_KEY);
assertThat(preferenceController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.UNSUPPORTED_ON_DEVICE);
}
@Test
public void getAvailabilityStatus_canResolveActivity_shouldReturnAvailableUnsearchable() {
when(mPackageManager.getPermissionControllerPackageName()).thenReturn(
PERMISSION_CONTROLLER_PACKAGE_NAME);
when(mPackageManager.resolveActivity(any(Intent.class), anyInt())).thenReturn(
new ResolveInfo());
MoreSpecialAccessPreferenceController preferenceController =
new MoreSpecialAccessPreferenceController(mContext, PREFERENCE_KEY);
assertThat(preferenceController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE_UNSEARCHABLE);
}
@Test
public void handlePreferenceTreeClick_differentKey_shouldReturnFalse() {
when(mPackageManager.getPermissionControllerPackageName())
.thenReturn(PERMISSION_CONTROLLER_PACKAGE_NAME);
MoreSpecialAccessPreferenceController preferenceController =
new MoreSpecialAccessPreferenceController(mContext, PREFERENCE_KEY);
Preference preference = mock(Preference.class);
when(preference.getKey()).thenReturn(DIFFERENT_PREFERENCE_KEY);
assertThat(preferenceController.handlePreferenceTreeClick(preference)).isFalse();
}
@Test
public void handlePreferenceTreeClick_sameKey_shouldReturnTrue() {
when(mPackageManager.getPermissionControllerPackageName())
.thenReturn(PERMISSION_CONTROLLER_PACKAGE_NAME);
MoreSpecialAccessPreferenceController preferenceController =
new MoreSpecialAccessPreferenceController(mContext, PREFERENCE_KEY);
Preference preference = mock(Preference.class);
when(preference.getKey()).thenReturn(PREFERENCE_KEY);
assertThat(preferenceController.handlePreferenceTreeClick(preference)).isTrue();
}
@Test
public void handlePreferenceTreeClick_noPermissionController_shouldNotStartActivity() {
when(mPackageManager.getPermissionControllerPackageName()).thenReturn(null);
MoreSpecialAccessPreferenceController preferenceController =
new MoreSpecialAccessPreferenceController(mContext, PREFERENCE_KEY);
Preference preference = mock(Preference.class);
when(preference.getKey()).thenReturn(PREFERENCE_KEY);
preferenceController.handlePreferenceTreeClick(preference);
verify(mContext, never()).startActivity(any(Intent.class));
}
@Test
public void handlePreferenceTreeClick_canNotResolveActivity_shouldNotStartActivity() {
when(mPackageManager.getPermissionControllerPackageName()).thenReturn(
PERMISSION_CONTROLLER_PACKAGE_NAME);
MoreSpecialAccessPreferenceController preferenceController =
new MoreSpecialAccessPreferenceController(mContext, PREFERENCE_KEY);
Preference preference = mock(Preference.class);
when(preference.getKey()).thenReturn(PREFERENCE_KEY);
preferenceController.handlePreferenceTreeClick(preference);
verify(mContext, never()).startActivity(any(Intent.class));
}
@Test
public void handlePreferenceTreeClick_canResolveActivity_shouldStartActivityWithIntent() {
when(mPackageManager.getPermissionControllerPackageName())
.thenReturn(PERMISSION_CONTROLLER_PACKAGE_NAME);
when(mPackageManager.resolveActivity(any(Intent.class), anyInt())).thenReturn(
new ResolveInfo());
MoreSpecialAccessPreferenceController preferenceController =
new MoreSpecialAccessPreferenceController(mContext, PREFERENCE_KEY);
Preference preference = mock(Preference.class);
when(preference.getKey()).thenReturn(PREFERENCE_KEY);
preferenceController.handlePreferenceTreeClick(preference);
ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
verify(mContext).startActivity(intentCaptor.capture());
final Intent intent = intentCaptor.getValue();
assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MANAGE_SPECIAL_APP_ACCESSES);
assertThat(intent.getPackage()).isEqualTo(PERMISSION_CONTROLLER_PACKAGE_NAME);
}
}