package com.android.settings.applications.defaultapps;
+import android.app.role.RoleManager;
+import android.app.role.RoleManagerCallback;
import android.app.settings.SettingsEnums;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
+import android.os.AsyncTask;
+import android.os.Process;
import android.provider.Settings;
import android.text.TextUtils;
+import android.util.Log;
+import com.android.internal.util.CollectionUtils;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settingslib.applications.DefaultAppInfo;
import java.util.List;
public class DefaultEmergencyPicker extends DefaultAppPickerFragment {
-
+ private static final String TAG = "DefaultEmergencyPicker";
@Override
public int getMetricsCategory() {
return SettingsEnums.DEFAULT_EMERGENCY_APP_PICKER;
@Override
protected String getDefaultKey() {
- return Settings.Secure.getString(getContext().getContentResolver(),
- Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION);
+ RoleManager roleManager = getContext().getSystemService(RoleManager.class);
+ return CollectionUtils.firstOrNull(roleManager.getRoleHolders(RoleManager.ROLE_EMERGENCY));
}
@Override
protected boolean setDefaultKey(String key) {
- final ContentResolver contentResolver = getContext().getContentResolver();
- final String previousValue = Settings.Secure.getString(contentResolver,
- Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION);
+ final String previousValue = getDefaultKey();
if (!TextUtils.isEmpty(key) && !TextUtils.equals(key, previousValue)) {
- Settings.Secure.putString(contentResolver,
- Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION,
- key);
+ getContext().getSystemService(RoleManager.class)
+ .addRoleHolderAsUser(
+ RoleManager.ROLE_EMERGENCY, key, 0, Process.myUserHandle(),
+ AsyncTask.THREAD_POOL_EXECUTOR, new RoleManagerCallback() {
+ @Override
+ public void onSuccess() {}
+
+ @Override
+ public void onFailure() {
+ Log.e(TAG, "Failed to set emergency default app.");
+ }
+ });
return true;
}
return false;
package com.android.settings.applications.defaultapps;
+import android.app.role.RoleManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.provider.Settings;
import android.telephony.TelephonyManager;
+import com.android.internal.util.CollectionUtils;
import com.android.settingslib.applications.DefaultAppInfo;
import java.util.List;
}
public static boolean isEmergencyDefault(String pkg, Context context) {
- String defaultPackage = Settings.Secure.getString(context.getContentResolver(),
- Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION);
+ String defaultPackage = CollectionUtils.firstOrNull(
+ context.getSystemService(RoleManager.class)
+ .getRoleHolders(RoleManager.ROLE_EMERGENCY));
return defaultPackage != null && defaultPackage.equals(pkg);
}
}
import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
+import android.app.role.RoleManager;
+import android.app.role.RoleManagerCallback;
import android.content.Context;
import android.content.pm.PackageManager;
+import android.os.AsyncTask;
+import android.os.Process;
+import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
+import android.util.Log;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
+import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.ReflectionHelpers;
+import java.util.Arrays;
+import java.util.concurrent.Executor;
+
@RunWith(RobolectricTestRunner.class)
public class DefaultEmergencyPickerTest {
-
+ private static final String TAG = DefaultEmergencyPickerTest.class.getSimpleName();
private static final String TEST_APP_KEY = "test_app";
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private UserManager mUserManager;
@Mock
private PackageManager mPackageManager;
+ @Mock
+ private RoleManager mRoleManager;
private DefaultEmergencyPicker mPicker;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
+ ShadowApplication shadowApplication = ShadowApplication.getInstance();
+ shadowApplication.setSystemService(Context.ROLE_SERVICE, mRoleManager);
when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
mPicker = spy(new DefaultEmergencyPicker());
mPicker.onAttach(mActivity);
ReflectionHelpers.setField(mPicker, "mPm", mPackageManager);
-
- doReturn(RuntimeEnvironment.application).when(mPicker).getContext();
+ when(mPicker.getContext()).thenReturn(RuntimeEnvironment.application);
}
@Test
public void setDefaultAppKey_shouldUpdateDefault() {
- assertThat(mPicker.setDefaultKey(TEST_APP_KEY)).isTrue();
- assertThat(mPicker.getDefaultKey()).isEqualTo(TEST_APP_KEY);
+ mPicker.setDefaultKey(TEST_APP_KEY);
+ verify(mRoleManager).addRoleHolderAsUser(
+ eq(RoleManager.ROLE_EMERGENCY),
+ eq(TEST_APP_KEY),
+ eq(0),
+ any(UserHandle.class),
+ any(Executor.class),
+ any(RoleManagerCallback.class));
}
@Test
public void getDefaultAppKey_shouldReturnDefault() {
- Settings.Secure.putString(RuntimeEnvironment.application.getContentResolver(),
- Settings.Secure.EMERGENCY_ASSISTANCE_APPLICATION,
- TEST_APP_KEY);
-
- assertThat(mPicker.getDefaultKey()).isEqualTo(TEST_APP_KEY);
+ when(mRoleManager.getRoleHolders(RoleManager.ROLE_EMERGENCY))
+ .thenReturn(Arrays.asList(TEST_APP_KEY));
+ assertThat(mPicker.getDefaultKey()).isEqualTo(TEST_APP_KEY);
}
}