Merge "Replace intent with pendingIntent in the ScheduleInfo" into rvc-dev

This commit is contained in:
TreeHugger Robot
2020-03-06 05:14:58 +00:00
committed by Android (Google) Code Review
3 changed files with 82 additions and 62 deletions

View File

@@ -16,6 +16,8 @@
package com.android.settingslib.schedulesprovider; package com.android.settingslib.schedulesprovider;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
@@ -25,25 +27,25 @@ import androidx.annotation.NonNull;
/** /**
* Schedule data item containing the schedule title text, the summary text which is displayed on the * Schedule data item containing the schedule title text, the summary text which is displayed on the
* summary of the Settings preference and an {@link Intent} which Settings will launch when the * summary of the Settings preference and a {@link PendingIntent} which Settings will launch
* user clicks on the preference. * when the user clicks on the preference.
*/ */
public class ScheduleInfo implements Parcelable { public class ScheduleInfo implements Parcelable {
private static final String TAG = "ScheduleInfo"; private static final String TAG = "ScheduleInfo";
private final String mTitle; private final String mTitle;
private final String mSummary; private final String mSummary;
private final Intent mIntent; private final PendingIntent mPendingIntent;
public ScheduleInfo(Builder builder) { public ScheduleInfo(Builder builder) {
mTitle = builder.mTitle; mTitle = builder.mTitle;
mSummary = builder.mSummary; mSummary = builder.mSummary;
mIntent = builder.mIntent; mPendingIntent = builder.mPendingIntent;
} }
private ScheduleInfo(Parcel in) { private ScheduleInfo(Parcel in) {
mTitle = in.readString(); mTitle = in.readString();
mSummary = in.readString(); mSummary = in.readString();
mIntent = in.readParcelable(Intent.class.getClassLoader()); mPendingIntent = in.readParcelable(PendingIntent.class.getClassLoader());
} }
/** /**
@@ -61,11 +63,11 @@ public class ScheduleInfo implements Parcelable {
} }
/** /**
* Returns an {@link Intent} which Settings will launch when the user clicks on a schedule * Returns a {@link PendingIntent} which Settings will launch when the user clicks on a
* preference. * schedule preference.
*/ */
public Intent getIntent() { public PendingIntent getPendingIntent() {
return mIntent; return mPendingIntent;
} }
/** /**
@@ -74,14 +76,15 @@ public class ScheduleInfo implements Parcelable {
* @return {@code true} if all member variables are valid. * @return {@code true} if all member variables are valid.
*/ */
public boolean isValid() { public boolean isValid() {
return !TextUtils.isEmpty(mTitle) && !TextUtils.isEmpty(mSummary) && (mIntent != null); return !TextUtils.isEmpty(mTitle) && !TextUtils.isEmpty(mSummary)
&& (mPendingIntent != null);
} }
@Override @Override
public void writeToParcel(Parcel dest, int flags) { public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mTitle); dest.writeString(mTitle);
dest.writeString(mSummary); dest.writeString(mSummary);
dest.writeParcelable(mIntent, flags); dest.writeParcelable(mPendingIntent, flags);
} }
@Override @Override
@@ -104,7 +107,7 @@ public class ScheduleInfo implements Parcelable {
@NonNull @NonNull
@Override @Override
public String toString() { public String toString() {
return "title: " + mTitle + ", summary: " + mSummary + ", intent: " + mIntent; return "title: " + mTitle + ", summary: " + mSummary + ", pendingIntent: " + mPendingIntent;
} }
/** /**
@@ -113,7 +116,7 @@ public class ScheduleInfo implements Parcelable {
public static class Builder { public static class Builder {
private String mTitle; private String mTitle;
private String mSummary; private String mSummary;
private Intent mIntent; private PendingIntent mPendingIntent;
/** /**
* Sets the title. * Sets the title.
@@ -138,13 +141,15 @@ public class ScheduleInfo implements Parcelable {
} }
/** /**
* Sets the {@link Intent}. * Sets the {@link PendingIntent}.
* <p>The {@link PendingIntent} should be created with
* {@link PendingIntent#getActivity(Context, int, Intent, int)}.
* *
* @param intent The action when user clicks the preference. * @param pendingIntent The pending intent to send when the user clicks the preference.
* @return This instance. * @return This instance.
*/ */
public Builder setIntent(@NonNull Intent intent) { public Builder setPendingIntent(@NonNull PendingIntent pendingIntent) {
mIntent = intent; mPendingIntent = pendingIntent;
return this; return this;
} }

View File

@@ -17,11 +17,14 @@ package com.android.settingslib.schedulesprovider;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import org.junit.Test; 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.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class ScheduleInfoTest { public class ScheduleInfoTest {
@@ -29,10 +32,12 @@ public class ScheduleInfoTest {
private static final String TEST_SUMMARY = "Night Light summary"; private static final String TEST_SUMMARY = "Night Light summary";
private static final String TEST_EMPTY_SUMMARY = ""; private static final String TEST_EMPTY_SUMMARY = "";
private final Context mContext = RuntimeEnvironment.application;
@Test @Test
public void builder_usedValidArguments_isValid() { public void builder_usedValidArguments_isValid() {
final Intent intent = createTestIntent(); final PendingIntent pendingIntent = createTestPendingIntent(mContext);
final ScheduleInfo info = createTestScheduleInfo(TEST_TITLE, TEST_SUMMARY, intent); final ScheduleInfo info = createTestScheduleInfo(TEST_TITLE, TEST_SUMMARY, pendingIntent);
assertThat(info).isNotNull(); assertThat(info).isNotNull();
assertThat(info.isValid()).isTrue(); assertThat(info.isValid()).isTrue();
@@ -40,15 +45,16 @@ public class ScheduleInfoTest {
@Test @Test
public void builder_useEmptySummary_isInvalid() { public void builder_useEmptySummary_isInvalid() {
final Intent intent = createTestIntent(); final PendingIntent pendingIntent = createTestPendingIntent(mContext);
final ScheduleInfo info = createTestScheduleInfo(TEST_TITLE, TEST_EMPTY_SUMMARY, intent); final ScheduleInfo info = createTestScheduleInfo(TEST_TITLE, TEST_EMPTY_SUMMARY,
pendingIntent);
assertThat(info).isNotNull(); assertThat(info).isNotNull();
assertThat(info.isValid()).isFalse(); assertThat(info.isValid()).isFalse();
} }
@Test @Test
public void builder_intentIsNull_isInvalid() { public void builder_pendingIntentIsNull_isInvalid() {
final ScheduleInfo info = new ScheduleInfo.Builder() final ScheduleInfo info = new ScheduleInfo.Builder()
.setTitle(TEST_TITLE) .setTitle(TEST_TITLE)
.setSummary(TEST_SUMMARY) .setSummary(TEST_SUMMARY)
@@ -60,39 +66,40 @@ public class ScheduleInfoTest {
@Test @Test
public void getTitle_setValidTitle_shouldReturnSameCorrectTitle() { public void getTitle_setValidTitle_shouldReturnSameCorrectTitle() {
final Intent intent = createTestIntent(); final PendingIntent pendingIntent = createTestPendingIntent(mContext);
final ScheduleInfo info = createTestScheduleInfo(TEST_TITLE, TEST_SUMMARY, intent); final ScheduleInfo info = createTestScheduleInfo(TEST_TITLE, TEST_SUMMARY, pendingIntent);
assertThat(info.getTitle()).isEqualTo(TEST_TITLE); assertThat(info.getTitle()).isEqualTo(TEST_TITLE);
} }
@Test @Test
public void getSummary_setValidSummary_shouldReturnSameCorrectSummary() { public void getSummary_setValidSummary_shouldReturnSameCorrectSummary() {
final Intent intent = createTestIntent(); final PendingIntent pendingIntent = createTestPendingIntent(mContext);
final ScheduleInfo info = createTestScheduleInfo(TEST_TITLE, TEST_SUMMARY, intent); final ScheduleInfo info = createTestScheduleInfo(TEST_TITLE, TEST_SUMMARY, pendingIntent);
assertThat(info.getSummary()).isEqualTo(TEST_SUMMARY); assertThat(info.getSummary()).isEqualTo(TEST_SUMMARY);
} }
@Test @Test
public void getIntent_setValidIntent_shouldReturnSameCorrectIntent() { public void getPendingIntent_setValidPendingIntent_shouldReturnSameCorrectIntent() {
final Intent intent = createTestIntent(); final PendingIntent pendingIntent = createTestPendingIntent(mContext);
final ScheduleInfo info = createTestScheduleInfo(TEST_TITLE, TEST_SUMMARY, intent); final ScheduleInfo info = createTestScheduleInfo(TEST_TITLE, TEST_SUMMARY, pendingIntent);
assertThat(info.getIntent()).isEqualTo(intent); assertThat(info.getPendingIntent()).isEqualTo(pendingIntent);
} }
private static Intent createTestIntent() { private static PendingIntent createTestPendingIntent(Context context) {
return new Intent("android.settings.NIGHT_DISPLAY_SETTINGS").addCategory( final Intent intent = new Intent("android.settings.NIGHT_DISPLAY_SETTINGS").addCategory(
Intent.CATEGORY_DEFAULT); Intent.CATEGORY_DEFAULT);
return PendingIntent.getActivity(context, 0 /* requestCode */, intent, 0 /* flags */);
} }
private static ScheduleInfo createTestScheduleInfo(String title, String summary, private static ScheduleInfo createTestScheduleInfo(String title, String summary,
Intent intent) { PendingIntent pendingIntent) {
return new ScheduleInfo.Builder() return new ScheduleInfo.Builder()
.setTitle(title) .setTitle(title)
.setSummary(summary) .setSummary(summary)
.setIntent(intent) .setPendingIntent(pendingIntent)
.build(); .build();
} }
} }

View File

@@ -19,6 +19,8 @@ import static com.google.common.truth.Truth.assertThat;
import static org.robolectric.Shadows.shadowOf; import static org.robolectric.Shadows.shadowOf;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
@@ -27,6 +29,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.Robolectric; import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList; import java.util.ArrayList;
@@ -35,13 +38,16 @@ public class SchedulesProviderTest {
private static final String INVALID_PACKAGE = "com.android.sunny"; private static final String INVALID_PACKAGE = "com.android.sunny";
private static final String VALID_PACKAGE = "com.android.settings"; private static final String VALID_PACKAGE = "com.android.settings";
private static final String INVALID_METHOD = "queryTestData"; private static final String INVALID_METHOD = "queryTestData";
private final Context mContext = RuntimeEnvironment.application;
private TestSchedulesProvider mProvider; private TestSchedulesProvider mProvider;
@Before @Before
public void setUp() { public void setUp() {
mProvider = Robolectric.setupContentProvider(TestSchedulesProvider.class); mProvider = Robolectric.setupContentProvider(TestSchedulesProvider.class);
shadowOf(mProvider).setCallingPackage(VALID_PACKAGE); shadowOf(mProvider).setCallingPackage(VALID_PACKAGE);
mProvider.setScheduleInfos(TestSchedulesProvider.createOneValidScheduleInfo()); mProvider.setScheduleInfos(TestSchedulesProvider.createOneValidScheduleInfo(mContext));
} }
@Test @Test
@@ -76,7 +82,7 @@ public class SchedulesProviderTest {
@Test @Test
public void call_addTwoValidData_returnScheduleInfoData() { public void call_addTwoValidData_returnScheduleInfoData() {
mProvider.setScheduleInfos(TestSchedulesProvider.createTwoValidScheduleInfos()); mProvider.setScheduleInfos(TestSchedulesProvider.createTwoValidScheduleInfos(mContext));
final Bundle bundle = mProvider.call(SchedulesProvider.METHOD_GENERATE_SCHEDULE_INFO_LIST, final Bundle bundle = mProvider.call(SchedulesProvider.METHOD_GENERATE_SCHEDULE_INFO_LIST,
null /* arg */, null /* extras */); null /* arg */, null /* extras */);
@@ -89,7 +95,8 @@ public class SchedulesProviderTest {
@Test @Test
public void call_addTwoValidDataAndOneInvalidData_returnTwoScheduleInfoData() { public void call_addTwoValidDataAndOneInvalidData_returnTwoScheduleInfoData() {
mProvider.setScheduleInfos(TestSchedulesProvider.createTwoValidAndOneInvalidScheduleInfo()); mProvider.setScheduleInfos(
TestSchedulesProvider.createTwoValidAndOneInvalidScheduleInfo(mContext));
final Bundle bundle = mProvider.call(SchedulesProvider.METHOD_GENERATE_SCHEDULE_INFO_LIST, final Bundle bundle = mProvider.call(SchedulesProvider.METHOD_GENERATE_SCHEDULE_INFO_LIST,
null /* arg */, null /* extras */); null /* arg */, null /* extras */);
@@ -112,55 +119,56 @@ public class SchedulesProviderTest {
mScheduleInfos = scheduleInfos; mScheduleInfos = scheduleInfos;
} }
private static ArrayList<ScheduleInfo> createOneValidScheduleInfo() { private static ArrayList<ScheduleInfo> createOneValidScheduleInfo(Context context) {
final ArrayList<ScheduleInfo> scheduleInfos = new ArrayList<>(); final ArrayList<ScheduleInfo> scheduleInfos = new ArrayList<>();
final Intent intent = new Intent("android.settings.NIGHT_DISPLAY_SETTINGS").addCategory(
Intent.CATEGORY_DEFAULT); final ScheduleInfo info = new ScheduleInfo.Builder().setTitle("Night Light").setSummary(
final ScheduleInfo info = new ScheduleInfo.Builder().setTitle( "This a sunny test").setPendingIntent(createTestPendingIntent(context,
"Night Light").setSummary("This a sunny test").setIntent(intent).build(); "android.settings.NIGHT_DISPLAY_SETTINGS")).build();
scheduleInfos.add(info); scheduleInfos.add(info);
return scheduleInfos; return scheduleInfos;
} }
private static ArrayList<ScheduleInfo> createTwoValidScheduleInfos() { private static ArrayList<ScheduleInfo> createTwoValidScheduleInfos(Context context) {
final ArrayList<ScheduleInfo> scheduleInfos = new ArrayList<>(); final ArrayList<ScheduleInfo> scheduleInfos = new ArrayList<>();
Intent intent = new Intent("android.settings.NIGHT_DISPLAY_SETTINGS").addCategory( ScheduleInfo info = new ScheduleInfo.Builder().setTitle("Night Light").setSummary(
Intent.CATEGORY_DEFAULT); "This a sunny test").setPendingIntent(createTestPendingIntent(context,
ScheduleInfo info = new ScheduleInfo.Builder().setTitle( "android.settings.NIGHT_DISPLAY_SETTINGS")).build();
"Night Light").setSummary("This a sunny test").setIntent(intent).build();
scheduleInfos.add(info); scheduleInfos.add(info);
intent = new Intent("android.settings.DISPLAY_SETTINGS").addCategory(
Intent.CATEGORY_DEFAULT);
info = new ScheduleInfo.Builder().setTitle("Display").setSummary( info = new ScheduleInfo.Builder().setTitle("Display").setSummary(
"Display summary").setIntent(intent).build(); "Display summary").setPendingIntent(
createTestPendingIntent(context, "android.settings.DISPLAY_SETTINGS")).build();
scheduleInfos.add(info); scheduleInfos.add(info);
return scheduleInfos; return scheduleInfos;
} }
private static ArrayList<ScheduleInfo> createTwoValidAndOneInvalidScheduleInfo() { private static ArrayList<ScheduleInfo> createTwoValidAndOneInvalidScheduleInfo(
Context context) {
final ArrayList<ScheduleInfo> scheduleInfos = new ArrayList<>(); final ArrayList<ScheduleInfo> scheduleInfos = new ArrayList<>();
Intent intent = new Intent("android.settings.NIGHT_DISPLAY_SETTINGS").addCategory( ScheduleInfo info = new ScheduleInfo.Builder().setTitle("Night Light").setSummary(
Intent.CATEGORY_DEFAULT); "This a sunny test").setPendingIntent(createTestPendingIntent(context,
ScheduleInfo info = new ScheduleInfo.Builder().setTitle( "android.settings.NIGHT_DISPLAY_SETTINGS")).build();
"Night Light").setSummary("This a sunny test").setIntent(intent).build();
scheduleInfos.add(info); scheduleInfos.add(info);
intent = new Intent("android.settings.DISPLAY_SETTINGS").addCategory(
Intent.CATEGORY_DEFAULT);
info = new ScheduleInfo.Builder().setTitle("Display").setSummary( info = new ScheduleInfo.Builder().setTitle("Display").setSummary(
"Display summary").setIntent(intent).build(); "Display summary").setPendingIntent(
createTestPendingIntent(context, "android.settings.DISPLAY_SETTINGS")).build();
scheduleInfos.add(info); scheduleInfos.add(info);
intent = new Intent("android.settings.DISPLAY_SETTINGS").addCategory( info = new ScheduleInfo.Builder().setTitle("").setSummary(
Intent.CATEGORY_DEFAULT); "Display summary").setPendingIntent(
info = new ScheduleInfo.Builder().setTitle("").setSummary("Display summary").setIntent( createTestPendingIntent(context, "android.settings.DISPLAY_SETTINGS")).build();
intent).build();
scheduleInfos.add(info); scheduleInfos.add(info);
return scheduleInfos; return scheduleInfos;
} }
private static PendingIntent createTestPendingIntent(Context context, String action) {
final Intent intent = new Intent(action).addCategory(Intent.CATEGORY_DEFAULT);
return PendingIntent.getActivity(context, 0 /* requestCode */, intent, 0 /* flags */);
}
} }
} }