Replace intent with pendingIntent in the ScheduleInfo

The pendingIntent can launch the management UI when users click on the item
(even the private activity in theschedules-provided app).

Fixes: 150754969
Test: make RunSettingsLibRoboTests -j
Change-Id: I23a95c37993f61d4a3dfcd70b11225b61633f7a6
This commit is contained in:
Sunny Shao
2020-03-04 21:18:21 +08:00
parent d071b2526e
commit 9844eae18d
3 changed files with 82 additions and 62 deletions

View File

@@ -16,6 +16,8 @@
package com.android.settingslib.schedulesprovider;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Parcel;
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
* summary of the Settings preference and an {@link Intent} which Settings will launch when the
* user clicks on the preference.
* summary of the Settings preference and a {@link PendingIntent} which Settings will launch
* when the user clicks on the preference.
*/
public class ScheduleInfo implements Parcelable {
private static final String TAG = "ScheduleInfo";
private final String mTitle;
private final String mSummary;
private final Intent mIntent;
private final PendingIntent mPendingIntent;
public ScheduleInfo(Builder builder) {
mTitle = builder.mTitle;
mSummary = builder.mSummary;
mIntent = builder.mIntent;
mPendingIntent = builder.mPendingIntent;
}
private ScheduleInfo(Parcel in) {
mTitle = 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
* preference.
* Returns a {@link PendingIntent} which Settings will launch when the user clicks on a
* schedule preference.
*/
public Intent getIntent() {
return mIntent;
public PendingIntent getPendingIntent() {
return mPendingIntent;
}
/**
@@ -74,14 +76,15 @@ public class ScheduleInfo implements Parcelable {
* @return {@code true} if all member variables are valid.
*/
public boolean isValid() {
return !TextUtils.isEmpty(mTitle) && !TextUtils.isEmpty(mSummary) && (mIntent != null);
return !TextUtils.isEmpty(mTitle) && !TextUtils.isEmpty(mSummary)
&& (mPendingIntent != null);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mTitle);
dest.writeString(mSummary);
dest.writeParcelable(mIntent, flags);
dest.writeParcelable(mPendingIntent, flags);
}
@Override
@@ -104,7 +107,7 @@ public class ScheduleInfo implements Parcelable {
@NonNull
@Override
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 {
private String mTitle;
private String mSummary;
private Intent mIntent;
private PendingIntent mPendingIntent;
/**
* 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.
*/
public Builder setIntent(@NonNull Intent intent) {
mIntent = intent;
public Builder setPendingIntent(@NonNull PendingIntent pendingIntent) {
mPendingIntent = pendingIntent;
return this;
}

View File

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

View File

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