Skip to content

Commit

Permalink
updates for newer slice API
Browse files Browse the repository at this point in the history
  • Loading branch information
commonsguy committed Aug 4, 2018
1 parent 8545061 commit f496c72
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 37 deletions.
4 changes: 2 additions & 2 deletions Slices/SamplerX/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ android {
}

dependencies {
implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
implementation 'androidx.slice:slice-builders:1.0.0-alpha1'
implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
implementation 'androidx.slice:slice-builders:1.0.0-beta01'
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,33 @@ public Slice onBindSlice(Uri sliceUri) {
R.drawable.ic_looks_two_black_24dp));

builder
.setHeader(buildHeader(builder, ctxt))
.addRow(buildSimpleRow(builder, ctxt))
.addInputRange(buildRangeRow(builder, ctxt));
.setHeader(buildHeader(ctxt))
.addRow(buildSimpleRow(ctxt))
.addInputRange(buildRangeRow(ctxt));

return builder.build();
}

ListBuilder.HeaderBuilder buildHeader(ListBuilder builder, Context ctxt) {
return new ListBuilder.HeaderBuilder(builder)
ListBuilder.HeaderBuilder buildHeader(Context ctxt) {
return new ListBuilder.HeaderBuilder()
.setTitle("Header Title")
.setSubtitle("This is the subtitle")
.setSummary("This is the summary", false)
.setPrimaryAction(buildIconAction(ctxt, "Header Primary Action",
R.drawable.ic_looks_one_black_24dp));
}

RowBuilder buildSimpleRow(ListBuilder builder, Context ctxt) {
return new RowBuilder(builder)
.setTitleItem(buildIcon(ctxt, R.drawable.ic_looks_3_black_24dp),
ListBuilder.ICON_IMAGE)
RowBuilder buildSimpleRow(Context ctxt) {
return new RowBuilder()
.setTitle("Simple Row Title")
.setSubtitle("This is the subtitle")
.setPrimaryAction(buildIconAction(ctxt, "Simple Row Primary Action",
R.drawable.ic_looks_4_black_24dp))
.addEndItem(buildPlainAction(ctxt, "Simple Row End Item", R.id.toggle));
.addEndItem(buildToggleAction(ctxt, "Simple Row End Item", R.id.toggle));
}

ListBuilder.InputRangeBuilder buildRangeRow(ListBuilder builder, Context ctxt) {
return new ListBuilder.InputRangeBuilder(builder)
ListBuilder.InputRangeBuilder buildRangeRow(Context ctxt) {
return new ListBuilder.InputRangeBuilder()
.setTitle("Range Title")
.setSubtitle("This is the subtitle")
.setMax(10)
Expand All @@ -85,12 +83,12 @@ ListBuilder.InputRangeBuilder buildRangeRow(ListBuilder builder, Context ctxt) {
}

SliceAction buildIconAction(Context ctxt, String msg, @DrawableRes int iconRes) {
return new SliceAction(buildActionPI(ctxt, msg, iconRes),
buildIcon(ctxt, iconRes), msg);
return SliceAction.create(buildActionPI(ctxt, msg, iconRes),
buildIcon(ctxt, iconRes), ListBuilder.ICON_IMAGE, msg);
}

SliceAction buildPlainAction(Context ctxt, String msg, int id) {
return new SliceAction(buildActionPI(ctxt, msg, id), msg, false);
SliceAction buildToggleAction(Context ctxt, String msg, int id) {
return SliceAction.createToggle(buildActionPI(ctxt, msg, id), msg, false);
}

PendingIntent buildActionPI(Context ctxt, String msg, int id) {
Expand Down
2 changes: 1 addition & 1 deletion Slices/SamplerX/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-alpha16'
classpath 'com.android.tools.build:gradle:3.2.0-beta04'


// NOTE: Do not place your application dependencies here; they belong
Expand Down
8 changes: 4 additions & 4 deletions Slices/WeatherSlice/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ android {
}

dependencies {
implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'androidx.annotation:annotation:1.0.0-alpha1'
implementation 'androidx.slice:slice-builders:1.0.0-alpha1'
implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'androidx.annotation:annotation:1.0.0-beta01'
implementation 'androidx.slice:slice-builders:1.0.0-beta01'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,26 @@

package com.commonsware.android.slice.weather;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import org.threeten.bp.OffsetDateTime;
import org.threeten.bp.format.DateTimeFormatter;
import org.threeten.bp.format.FormatStyle;
import androidx.annotation.DrawableRes;
import androidx.core.graphics.drawable.IconCompat;
import androidx.slice.Slice;
import androidx.slice.SliceProvider;
import androidx.slice.builders.GridRowBuilder;
import androidx.slice.builders.ListBuilder;
import androidx.slice.builders.SliceAction;
import static androidx.slice.builders.ListBuilder.LARGE_IMAGE;

public class WeatherSliceProvider extends SliceProvider {
static final Uri NYC_FORECAST=
Uri.parse(
"https://forecast.weather.gov/MapClick.php?lat=40.7146&lon=-74.0071");
static final DateTimeFormatter FORMATTER=
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
static final Uri ME=new Uri.Builder()
Expand All @@ -47,44 +54,64 @@ public Slice onBindSlice(Uri sliceUri) {
}

ListBuilder builder=new ListBuilder(ctxt, sliceUri, ListBuilder.INFINITY)
.setAccentColor(ctxt.getResources().getColor(
R.color.colorAccent));
.setAccentColor(ctxt.getResources().getColor(R.color.colorAccent));

builder.setHeader(buildHeader(builder, ctxt));
builder.setHeader(buildHeader(ctxt));

WeatherResponse weather=Forecaster.LATEST;

if (weather==null) {
Forecaster.enqueueWork(getContext());

builder.addRange(new ListBuilder.RangeBuilder(builder)
builder.addRange(new ListBuilder.RangeBuilder()
.setTitle(ctxt.getString(R.string.downloading)));
}
else {
builder.addGridRow(buildGridRow(builder, ctxt));
builder.addGridRow(buildGridRow());
}

return builder.build();
}

ListBuilder.HeaderBuilder buildHeader(ListBuilder builder, Context ctxt) {
return new ListBuilder.HeaderBuilder(builder)
.setTitle(ctxt.getString(R.string.header_title));
ListBuilder.HeaderBuilder buildHeader(Context ctxt) {
return new ListBuilder.HeaderBuilder()
.setTitle(ctxt.getString(R.string.header_title))
.setPrimaryAction(
buildIconAction(ctxt, ctxt.getString(R.string.msg_forecast),
R.drawable.ic_wb_sunny_black_24dp));
}

GridRowBuilder buildGridRow(ListBuilder builder, Context ctxt) {
GridRowBuilder row=new GridRowBuilder(builder);
GridRowBuilder buildGridRow() {
GridRowBuilder row=new GridRowBuilder();

for (int i=0;i<Forecaster.COUNT;i++) {
for (int i=0; i<Forecaster.COUNT; i++) {
WeatherResponse.Period period=Forecaster.LATEST.properties.periods.get(i);
OffsetDateTime odt=OffsetDateTime.parse(period.startTime);

row.addCell(new GridRowBuilder.CellBuilder(row)
row.addCell(new GridRowBuilder.CellBuilder()
.addTitleText(odt.format(FORMATTER))
.addImage(IconCompat.createWithBitmap(period.iconBitmap), LARGE_IMAGE)
.addText(String.format("%d%s", period.temperature, period.temperatureUnit)));
.addText(
String.format("%d%s", period.temperature, period.temperatureUnit)));
}

return row;
}

SliceAction buildIconAction(Context ctxt, String msg,
@DrawableRes int iconRes) {
return SliceAction.create(buildActionPI(ctxt, iconRes),
buildIcon(ctxt, iconRes), ListBuilder.ICON_IMAGE, msg);
}

PendingIntent buildActionPI(Context ctxt, int id) {
Intent i=new Intent(Intent.ACTION_VIEW, NYC_FORECAST);

return PendingIntent.getActivity(ctxt, id, i,
PendingIntent.FLAG_UPDATE_CURRENT);
}

IconCompat buildIcon(Context ctxt, @DrawableRes int iconRes) {
return IconCompat.createWithResource(ctxt, iconRes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M6.76,4.84l-1.8,-1.79 -1.41,1.41 1.79,1.79 1.42,-1.41zM4,10.5L1,10.5v2h3v-2zM13,0.55h-2L11,3.5h2L13,0.55zM20.45,4.46l-1.41,-1.41 -1.79,1.79 1.41,1.41 1.79,-1.79zM17.24,18.16l1.79,1.8 1.41,-1.41 -1.8,-1.79 -1.4,1.4zM20,10.5v2h3v-2h-3zM12,5.5c-3.31,0 -6,2.69 -6,6s2.69,6 6,6 6,-2.69 6,-6 -2.69,-6 -6,-6zM11,22.45h2L13,19.5h-2v2.95zM3.55,18.54l1.41,1.41 1.79,-1.8 -1.41,-1.41 -1.79,1.8z"/>
</vector>
1 change: 1 addition & 0 deletions Slices/WeatherSlice/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<string name="app_name">Weather Slice</string>
<string name="header_title">NYC Weather</string>
<string name="downloading">Downloading forecast</string>
<string name="msg_forecast">Full Forecast</string>
</resources>
2 changes: 1 addition & 1 deletion Slices/WeatherSlice/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-alpha16'
classpath 'com.android.tools.build:gradle:3.2.0-beta04'


// NOTE: Do not place your application dependencies here; they belong
Expand Down

0 comments on commit f496c72

Please sign in to comment.