Merge "feat(HCT): Notification 'open settings' action behavoir" into main
This commit is contained in:
@@ -54,6 +54,9 @@ public class HighContrastTextMigrationReceiver extends BroadcastReceiver {
|
|||||||
static final String ACTION_RESTORED =
|
static final String ACTION_RESTORED =
|
||||||
"com.android.settings.accessibility.ACTION_HIGH_CONTRAST_TEXT_RESTORED";
|
"com.android.settings.accessibility.ACTION_HIGH_CONTRAST_TEXT_RESTORED";
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
|
static final String ACTION_OPEN_SETTINGS =
|
||||||
|
"com.android.settings.accessibility.ACTION_OPEN_HIGH_CONTRAST_TEXT_SETTINGS";
|
||||||
|
@VisibleForTesting
|
||||||
static final int NOTIFICATION_ID = 1;
|
static final int NOTIFICATION_ID = 1;
|
||||||
|
|
||||||
@Retention(RetentionPolicy.SOURCE)
|
@Retention(RetentionPolicy.SOURCE)
|
||||||
@@ -74,7 +77,16 @@ public class HighContrastTextMigrationReceiver extends BroadcastReceiver {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ACTION_RESTORED.equals(intent.getAction())) {
|
if (ACTION_OPEN_SETTINGS.equals(intent.getAction())) {
|
||||||
|
// Close notification drawer before opening the HCT setting.
|
||||||
|
context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
|
||||||
|
|
||||||
|
Intent settingsIntent = createHighContrastTextSettingsIntent(context);
|
||||||
|
settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
|
context.startActivity(settingsIntent);
|
||||||
|
|
||||||
|
context.getSystemService(NotificationManager.class).cancel(NOTIFICATION_ID);
|
||||||
|
} else if (ACTION_RESTORED.equals(intent.getAction())) {
|
||||||
Log.i(TAG, "HCT attempted to be restored from backup; showing notification for userId: "
|
Log.i(TAG, "HCT attempted to be restored from backup; showing notification for userId: "
|
||||||
+ context.getUserId());
|
+ context.getUserId());
|
||||||
Settings.Secure.putInt(context.getContentResolver(),
|
Settings.Secure.putInt(context.getContentResolver(),
|
||||||
@@ -125,21 +137,20 @@ public class HighContrastTextMigrationReceiver extends BroadcastReceiver {
|
|||||||
R.string.accessibility_notification_high_contrast_text_content))
|
R.string.accessibility_notification_high_contrast_text_content))
|
||||||
.setFlag(Notification.FLAG_NO_CLEAR, true);
|
.setFlag(Notification.FLAG_NO_CLEAR, true);
|
||||||
|
|
||||||
Intent settingsIntent = new Intent(Settings.ACTION_TEXT_READING_SETTINGS);
|
Intent settingsIntent = createHighContrastTextSettingsIntent(context);
|
||||||
settingsIntent.setPackage(context.getPackageName());
|
|
||||||
if (settingsIntent.resolveActivity(context.getPackageManager()) != null) {
|
if (settingsIntent.resolveActivity(context.getPackageManager()) != null) {
|
||||||
Bundle fragmentArgs = new Bundle();
|
|
||||||
fragmentArgs.putString(EXTRA_FRAGMENT_ARG_KEY,
|
|
||||||
TextReadingPreferenceFragment.HIGH_TEXT_CONTRAST_KEY);
|
|
||||||
settingsIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, fragmentArgs);
|
|
||||||
PendingIntent settingsPendingIntent = PendingIntent.getActivity(context,
|
PendingIntent settingsPendingIntent = PendingIntent.getActivity(context,
|
||||||
/* requestCode = */ 0, settingsIntent, PendingIntent.FLAG_IMMUTABLE);
|
/* requestCode = */ 0, settingsIntent, PendingIntent.FLAG_IMMUTABLE);
|
||||||
|
|
||||||
|
Intent actionIntent = new Intent(context, HighContrastTextMigrationReceiver.class);
|
||||||
|
actionIntent.setAction(ACTION_OPEN_SETTINGS);
|
||||||
|
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0,
|
||||||
|
actionIntent, PendingIntent.FLAG_IMMUTABLE);
|
||||||
Notification.Action settingsAction = new Notification.Action.Builder(
|
Notification.Action settingsAction = new Notification.Action.Builder(
|
||||||
/* icon= */ null,
|
/* icon= */ null,
|
||||||
context.getString(
|
context.getString(
|
||||||
R.string.accessibility_notification_high_contrast_text_action),
|
R.string.accessibility_notification_high_contrast_text_action),
|
||||||
settingsPendingIntent
|
actionPendingIntent
|
||||||
).build();
|
).build();
|
||||||
|
|
||||||
notificationBuilder
|
notificationBuilder
|
||||||
@@ -156,4 +167,14 @@ public class HighContrastTextMigrationReceiver extends BroadcastReceiver {
|
|||||||
notificationManager.createNotificationChannel(notificationChannel);
|
notificationManager.createNotificationChannel(notificationChannel);
|
||||||
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
|
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Intent createHighContrastTextSettingsIntent(Context context) {
|
||||||
|
Intent settingsIntent = new Intent(Settings.ACTION_TEXT_READING_SETTINGS);
|
||||||
|
settingsIntent.setPackage(context.getPackageName());
|
||||||
|
Bundle fragmentArgs = new Bundle();
|
||||||
|
fragmentArgs.putString(EXTRA_FRAGMENT_ARG_KEY,
|
||||||
|
TextReadingPreferenceFragment.HIGH_TEXT_CONTRAST_KEY);
|
||||||
|
settingsIntent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, fragmentArgs);
|
||||||
|
return settingsIntent;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -20,6 +20,7 @@ import static com.android.settings.SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENT
|
|||||||
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
|
import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
|
||||||
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
|
import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
|
||||||
import static com.android.settings.accessibility.HighContrastTextMigrationReceiver.ACTION_RESTORED;
|
import static com.android.settings.accessibility.HighContrastTextMigrationReceiver.ACTION_RESTORED;
|
||||||
|
import static com.android.settings.accessibility.HighContrastTextMigrationReceiver.ACTION_OPEN_SETTINGS;
|
||||||
import static com.android.settings.accessibility.HighContrastTextMigrationReceiver.NOTIFICATION_CHANNEL;
|
import static com.android.settings.accessibility.HighContrastTextMigrationReceiver.NOTIFICATION_CHANNEL;
|
||||||
import static com.android.settings.accessibility.HighContrastTextMigrationReceiver.NOTIFICATION_ID;
|
import static com.android.settings.accessibility.HighContrastTextMigrationReceiver.NOTIFICATION_ID;
|
||||||
import static com.android.settings.accessibility.HighContrastTextMigrationReceiver.PromptState.PROMPT_SHOWN;
|
import static com.android.settings.accessibility.HighContrastTextMigrationReceiver.PromptState.PROMPT_SHOWN;
|
||||||
@@ -28,9 +29,9 @@ import static com.android.settings.accessibility.HighContrastTextMigrationReceiv
|
|||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import android.app.Application;
|
||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.app.PendingIntent;
|
|
||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
@@ -54,10 +55,13 @@ import org.junit.Test;
|
|||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.Shadows;
|
import org.robolectric.Shadows;
|
||||||
|
import org.robolectric.shadows.ShadowApplication;
|
||||||
import org.robolectric.shadows.ShadowNotification;
|
import org.robolectric.shadows.ShadowNotification;
|
||||||
import org.robolectric.shadows.ShadowNotificationManager;
|
import org.robolectric.shadows.ShadowNotificationManager;
|
||||||
import org.robolectric.shadows.ShadowPackageManager;
|
import org.robolectric.shadows.ShadowPackageManager;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/** Tests for {@link HighContrastTextMigrationReceiver}. */
|
/** Tests for {@link HighContrastTextMigrationReceiver}. */
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
public class HighContrastTextMigrationReceiverTest {
|
public class HighContrastTextMigrationReceiverTest {
|
||||||
@@ -66,6 +70,7 @@ public class HighContrastTextMigrationReceiverTest {
|
|||||||
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
|
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
|
||||||
private final Context mContext = ApplicationProvider.getApplicationContext();
|
private final Context mContext = ApplicationProvider.getApplicationContext();
|
||||||
private HighContrastTextMigrationReceiver mReceiver;
|
private HighContrastTextMigrationReceiver mReceiver;
|
||||||
|
private ShadowApplication mShadowApplication;
|
||||||
private ShadowNotificationManager mShadowNotificationManager;
|
private ShadowNotificationManager mShadowNotificationManager;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
@@ -73,6 +78,7 @@ public class HighContrastTextMigrationReceiverTest {
|
|||||||
NotificationManager notificationManager =
|
NotificationManager notificationManager =
|
||||||
mContext.getSystemService(NotificationManager.class);
|
mContext.getSystemService(NotificationManager.class);
|
||||||
mShadowNotificationManager = Shadows.shadowOf(notificationManager);
|
mShadowNotificationManager = Shadows.shadowOf(notificationManager);
|
||||||
|
mShadowApplication = Shadows.shadowOf((Application) mContext);
|
||||||
|
|
||||||
// Setup Settings app as a system app
|
// Setup Settings app as a system app
|
||||||
ShadowPackageManager shadowPm = Shadows.shadowOf(mContext.getPackageManager());
|
ShadowPackageManager shadowPm = Shadows.shadowOf(mContext.getPackageManager());
|
||||||
@@ -187,6 +193,28 @@ public class HighContrastTextMigrationReceiverTest {
|
|||||||
verifyNotificationNotSent();
|
verifyNotificationNotSent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@EnableFlags(Flags.FLAG_HIGH_CONTRAST_TEXT_SMALL_TEXT_RECT)
|
||||||
|
public void onReceive_openSettingsIntent_openHighContrastTextPreference() {
|
||||||
|
Intent intent = new Intent(ACTION_OPEN_SETTINGS);
|
||||||
|
mReceiver.onReceive(mContext, intent);
|
||||||
|
|
||||||
|
List<Intent> broadcastIntents = mShadowApplication.getBroadcastIntents();
|
||||||
|
assertThat(broadcastIntents.size()).isEqualTo(1);
|
||||||
|
assertThat(broadcastIntents.get(0).getAction())
|
||||||
|
.isEqualTo(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
|
||||||
|
|
||||||
|
Intent startedActivitie = mShadowApplication.getNextStartedActivity();
|
||||||
|
assertThat(startedActivitie).isNotNull();
|
||||||
|
Bundle fragmentArgs = startedActivitie.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
|
||||||
|
assertThat(fragmentArgs).isNotNull();
|
||||||
|
assertThat(fragmentArgs.getString(EXTRA_FRAGMENT_ARG_KEY))
|
||||||
|
.isEqualTo(TextReadingPreferenceFragment.HIGH_TEXT_CONTRAST_KEY);
|
||||||
|
|
||||||
|
Notification notification = mShadowNotificationManager.getNotification(NOTIFICATION_ID);
|
||||||
|
assertThat(notification).isNull();
|
||||||
|
}
|
||||||
|
|
||||||
private void verifyNotificationNotSent() {
|
private void verifyNotificationNotSent() {
|
||||||
Notification notification = mShadowNotificationManager.getNotification(NOTIFICATION_ID);
|
Notification notification = mShadowNotificationManager.getNotification(NOTIFICATION_ID);
|
||||||
assertThat(notification).isNull();
|
assertThat(notification).isNull();
|
||||||
@@ -210,13 +238,6 @@ public class HighContrastTextMigrationReceiverTest {
|
|||||||
assertThat(notification.actions.length).isEqualTo(1);
|
assertThat(notification.actions.length).isEqualTo(1);
|
||||||
assertThat(notification.actions[0].title.toString()).isEqualTo(
|
assertThat(notification.actions[0].title.toString()).isEqualTo(
|
||||||
mContext.getString(R.string.accessibility_notification_high_contrast_text_action));
|
mContext.getString(R.string.accessibility_notification_high_contrast_text_action));
|
||||||
|
|
||||||
PendingIntent pendingIntent = notification.actions[0].actionIntent;
|
|
||||||
Intent settingsIntent = Shadows.shadowOf(pendingIntent).getSavedIntent();
|
|
||||||
Bundle fragmentArgs = settingsIntent.getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS);
|
|
||||||
assertThat(fragmentArgs).isNotNull();
|
|
||||||
assertThat(fragmentArgs.getString(EXTRA_FRAGMENT_ARG_KEY))
|
|
||||||
.isEqualTo(TextReadingPreferenceFragment.HIGH_TEXT_CONTRAST_KEY);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertPromptStateAndHctState(
|
private void assertPromptStateAndHctState(
|
||||||
|
Reference in New Issue
Block a user