Skip to content

Commit

Permalink
Update slice API usage to alpha4/beta01.
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas Sigelbaum committed Jul 10, 2018
1 parent 0796fa0 commit 05fcb0d
Show file tree
Hide file tree
Showing 7 changed files with 325 additions and 248 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0-alpha18'
classpath 'com.android.tools.build:gradle:3.2.0-beta02'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
6 changes: 3 additions & 3 deletions slice/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ dependencies {
implementation project(':shared')
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "androidx.slice:slice-builders-ktx:1.0.0-alpha3"
implementation "androidx.appcompat:appcompat:1.0.0-alpha3"
implementation "androidx.cardview:cardview:1.0.0-alpha3"
implementation "androidx.slice:slice-builders-ktx:1.0.0-alpha4"
implementation "androidx.appcompat:appcompat:1.0.0-beta01"
implementation "androidx.cardview:cardview:1.0.0-beta01"
testImplementation 'junit:junit:4.12'
}
4 changes: 4 additions & 0 deletions slice/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<intent-filter>
<action android:name="androidx.intent.SLICE_ACTION"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.app.slice.category.SLICE"/>
<data
android:host="slice.android.example.com"
android:pathPrefix="/"
Expand All @@ -39,6 +40,7 @@
<intent-filter>
<action android:name="androidx.intent.SLICE_ACTION"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.app.slice.category.SLICE"/>
<data
android:host="slice.android.example.com"
android:pathPrefix="/"
Expand All @@ -52,6 +54,7 @@
<intent-filter>
<action android:name="androidx.intent.SLICE_ACTION"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.app.slice.category.SLICE"/>
<data
android:host="slice.android.example.com"
android:pathPrefix="/"
Expand All @@ -65,6 +68,7 @@
<intent-filter>
<action android:name="androidx.intent.SLICE_ACTION"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.app.slice.category.SLICE"/>
<data
android:host="slice.android.example.com"
android:pathPrefix="/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,65 +40,78 @@ public boolean onCreateSliceProvider() {
// [START on_bind_slice]
@Override
public Slice onBindSlice(Uri sliceUri) {
// Create parent ListBuilder.
if (getContext() == null) {
return null;
}
SliceAction activityAction = createActivityAction();
ListBuilder listBuilder = new ListBuilder(getContext(), sliceUri, ListBuilder.INFINITY);

// Create RowBuilder.
ListBuilder.RowBuilder rowBuilder = new ListBuilder.RowBuilder(listBuilder);

if (sliceUri.getPath().equals("/hello")) {
rowBuilder.setTitle("URI found.");
// Create parent ListBuilder.
if ("/hello".equals(sliceUri.getPath())) {
listBuilder.addRow(new ListBuilder.RowBuilder()
.setTitle("Hello World")
.setPrimaryAction(activityAction)
);
} else {
rowBuilder.setTitle("URI not found.");
listBuilder.addRow(new ListBuilder.RowBuilder()
.setTitle("URI not recognized")
.setPrimaryAction(activityAction)
);
}

// Add Row to List.
listBuilder.addRow(rowBuilder);

// Build List.
return listBuilder.build();
}
// [END on_bind_slice]

// [START create_slice]
public Slice createSlice(Uri sliceUri) {
if (getContext() == null) {
return null;
}
SliceAction activityAction = createActivityAction();
ListBuilder listBuilder = new ListBuilder(getContext(), sliceUri, ListBuilder.INFINITY);
ListBuilder.RowBuilder rowBuilder = new ListBuilder.RowBuilder(listBuilder)
.setTitle("Perform action in app.")
.setPrimaryAction(activityAction);
listBuilder.addRow(rowBuilder);
return listBuilder.build();
return new ListBuilder(getContext(), sliceUri, ListBuilder.INFINITY)
.addRow(new ListBuilder.RowBuilder()
.setTitle("Perform action in app.")
.setPrimaryAction(activityAction)
).build();
}

public SliceAction createActivityAction() {
Intent intent = new Intent(getContext(), MainActivity.class);
return new SliceAction(PendingIntent.getActivity(getContext(), 0, intent, 0),
if (getContext() == null) {
return null;
}
return SliceAction.create(
PendingIntent.getActivity(
getContext(),
0,
new Intent(getContext(), MainActivity.class),
0
),
IconCompat.createWithResource(getContext(), R.drawable.ic_home),
"Open MainActivity");
ListBuilder.ICON_IMAGE,
"Enter app"
);
}
// [END create_slice]

// [START create_brightness_slice]
public Slice createBrightnessSlice(Uri sliceUri) {
SliceAction toggleAction = new SliceAction(createToggleIntent(),
"Toggle adaptive brightness", true);
ListBuilder listBuilder = new ListBuilder(getContext(), sliceUri, ListBuilder.INFINITY);
ListBuilder.RowBuilder rowBuilder = new ListBuilder.RowBuilder(listBuilder)
.setTitle("Adaptive brightness")
.setSubtitle("Optimizes brightness for available light.")
.setPrimaryAction(toggleAction);

listBuilder.addRow(rowBuilder);

ListBuilder.InputRangeBuilder inputRangeBuilder = new ListBuilder.InputRangeBuilder(
listBuilder)
.setInputAction(brightnessPendingIntent)
.setMax(100)
.setValue(45);

listBuilder.addInputRange(inputRangeBuilder);

if (getContext() == null) {
return null;
}
SliceAction toggleAction = SliceAction.createToggle(
createToggleIntent(),
"Toggle adaptive brightness",
true
);
ListBuilder listBuilder = new ListBuilder(getContext(), sliceUri, ListBuilder.INFINITY)
.addRow(new ListBuilder.RowBuilder()
.setTitle("Adaptive brightness")
.setSubtitle("Optimizes brightness for available light.")
.setPrimaryAction(toggleAction)
).addInputRange(new ListBuilder.InputRangeBuilder()
.setInputAction(brightnessPendingIntent)
.setMax(100)
.setValue(45)
);
return listBuilder.build();
}

Expand All @@ -110,20 +123,31 @@ public PendingIntent createToggleIntent() {

// [START create_dynamic_slice]
public Slice createDynamicSlice(Uri sliceUri) {
if (getContext() == null || sliceUri.getPath() == null) {
return null;
}
ListBuilder listBuilder = new ListBuilder(getContext(), sliceUri, ListBuilder.INFINITY);
ListBuilder.RowBuilder rowBuilder = new ListBuilder.RowBuilder(listBuilder);
switch (sliceUri.getPath()) {
case "/count":
SliceAction toastAndIncrementAction = new SliceAction(createToastAndIncrementIntent(
"Item clicked."), actionIcon, "Increment.");
rowBuilder.setPrimaryAction(toastAndIncrementAction)
.setTitle("Count: " + MyBroadcastReceiver.sReceivedCount)
.setSubtitle("Click me");
listBuilder.addRow(rowBuilder);
SliceAction toastAndIncrementAction = SliceAction.create(
createToastAndIncrementIntent("Item clicked."),
actionIcon,
ListBuilder.ICON_IMAGE,
"Increment."
);
listBuilder.addRow(
new ListBuilder.RowBuilder()
.setPrimaryAction(toastAndIncrementAction)
.setTitle("Count: " + MyBroadcastReceiver.sReceivedCount)
.setSubtitle("Click me")
);
break;
default:
rowBuilder.setTitle("URI not found.");
listBuilder.addRow(rowBuilder);
listBuilder.addRow(
new ListBuilder.RowBuilder()
.setPrimaryAction(createActivityAction())
.setTitle("URI not found.")
);
break;
}
return listBuilder.build();
Expand Down
Loading

0 comments on commit 05fcb0d

Please sign in to comment.