Don't show afw default app pref if there is no candidate.

Change-Id: I25f077662264406a8a4d819db7b4713be753a33b
Fix: 22564285
Test: make RunSettingsRoboTests
This commit is contained in:
Fan Zhang
2017-02-22 14:29:22 -08:00
parent c1a7723c17
commit c13b950b02
6 changed files with 73 additions and 21 deletions

View File

@@ -125,6 +125,10 @@ public final class SmsDefaultDialog extends AlertActivity implements
p.mOnClickListener = this;
p.mNegativeButtonText = getString(R.string.cancel);
p.mNegativeButtonListener = this;
if (p.mAdapter.isEmpty()) {
// If there is nothing to choose from, don't build the dialog.
return false;
}
}
setupAlert();

View File

@@ -39,7 +39,8 @@ public class DefaultBrowserPreferenceController extends DefaultAppPreferenceCont
@Override
public boolean isAvailable() {
return true;
final List<ResolveInfo> candidates = getCandidates();
return candidates != null && !candidates.isEmpty();
}
@Override
@@ -61,16 +62,6 @@ public class DefaultBrowserPreferenceController extends DefaultAppPreferenceCont
}
}
private String getOnlyAppLabel() {
// Resolve that intent and check that the handleAllWebDataURI boolean is set
final List<ResolveInfo> list = mPackageManager.queryIntentActivitiesAsUser(BROWSE_PROBE,
PackageManager.MATCH_ALL, mUserId);
if (list != null && list.size() == 1) {
return list.get(0).loadLabel(mPackageManager.getPackageManager()).toString();
}
return null;
}
@Override
protected DefaultAppInfo getDefaultAppInfo() {
try {
@@ -81,6 +72,20 @@ public class DefaultBrowserPreferenceController extends DefaultAppPreferenceCont
}
}
private List<ResolveInfo> getCandidates() {
return mPackageManager.queryIntentActivitiesAsUser(BROWSE_PROBE, PackageManager.MATCH_ALL,
mUserId);
}
private String getOnlyAppLabel() {
// Resolve that intent and check that the handleAllWebDataURI boolean is set
final List<ResolveInfo> list = getCandidates();
if (list != null && list.size() == 1) {
return list.get(0).loadLabel(mPackageManager.getPackageManager()).toString();
}
return null;
}
/**
* Whether or not the pkg contains browser capability
*/

View File

@@ -42,7 +42,11 @@ public class DefaultPhonePreferenceController extends DefaultAppPreferenceContro
final boolean hasUserRestriction =
um.hasUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS);
return !hasUserRestriction;
if (hasUserRestriction) {
return false;
}
final List<String> candidates = getCandidates();
return candidates != null && !candidates.isEmpty();
}
@Override
@@ -60,6 +64,10 @@ public class DefaultPhonePreferenceController extends DefaultAppPreferenceContro
}
}
private List<String> getCandidates() {
return DefaultDialerManager.getInstalledDialerApplications(mContext, mUserId);
}
public static boolean hasPhonePreference(String pkg, Context context) {
List<String> dialerPackages =
DefaultDialerManager.getInstalledDialerApplications(context, UserHandle.myUserId());

View File

@@ -23,21 +23,26 @@ import com.android.settings.Utils;
public class DefaultWorkBrowserPreferenceController extends DefaultBrowserPreferenceController {
private final UserHandle mUserHandle;
public DefaultWorkBrowserPreferenceController(Context context) {
super(context);
final UserHandle managedProfile = Utils.getManagedProfile(mUserManager);
if (managedProfile != null) {
mUserId = managedProfile.getIdentifier();
mUserHandle = Utils.getManagedProfile(mUserManager);
if (mUserHandle != null) {
mUserId = mUserHandle.getIdentifier();
}
}
@Override
public boolean isAvailable() {
return Utils.getManagedProfile(mUserManager) != null;
}
@Override
public String getPreferenceKey() {
return "work_default_browser";
}
@Override
public boolean isAvailable() {
if (mUserHandle == null) {
return false;
}
return super.isAvailable();
}
}

View File

@@ -17,11 +17,28 @@
package com.android.settings.applications.defaultapps;
import android.content.Context;
import android.os.UserHandle;
import com.android.settings.Utils;
public class DefaultWorkPhonePreferenceController extends DefaultPhonePreferenceController {
private final UserHandle mUserHandle;
public DefaultWorkPhonePreferenceController(Context context) {
super(context);
mUserHandle = Utils.getManagedProfile(mUserManager);
if (mUserHandle != null) {
mUserId = mUserHandle.getIdentifier();
}
}
@Override
public boolean isAvailable() {
if (mUserHandle == null) {
return false;
}
return super.isAvailable();
}
@Override

View File

@@ -36,7 +36,9 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
@@ -68,7 +70,18 @@ public class DefaultBrowserPreferenceControllerTest {
}
@Test
public void isAlwaysAvailable() {
public void isAvailable_noBrowser_shouldReturnFalse() {
when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt()))
.thenReturn(null);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_hasBrowser_shouldReturnTrue() {
final List<ResolveInfo> candidates = new ArrayList<>();
candidates.add(new ResolveInfo());
when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt()))
.thenReturn(candidates);
assertThat(mController.isAvailable()).isTrue();
}