Permalink
Browse files

Remove the example-contract-widget module

As the app widget was incorporated into Muzei itself (with improvements), this separate example is no longer useful.
  • Loading branch information...
ianhanniballake committed Feb 24, 2017
1 parent 057062f commit fa62a6c974b339a13386ca81c027c9065007e2b7
@@ -1,45 +0,0 @@
/*
* Copyright 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:$rootProject.ext.toolsPluginVersion"
}
}
apply plugin: 'com.android.application'
repositories {
jcenter()
}
dependencies {
//compile "com.google.android.apps.muzei:muzei-api:2.+"
compile project(':api')
}
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion 18
targetSdkVersion rootProject.ext.targetSdkVersion
}
}
@@ -1,45 +0,0 @@
<!--
Copyright 2014 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.muzei.examplecontractwidget">
<application
android:allowBackup="true"
android:icon="@drawable/preview"
android:label="@string/app_name">
<receiver android:name=".MuzeiAppWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_info"/>
</receiver>
<!-- Receive artwork update broadcasts. Disabled by default until you add at least one widget instance -->
<receiver
android:name=".ArtworkUpdateReceiver"
android:enabled="false"
tools:ignore="ExportedReceiver">
<intent-filter>
<action android:name="com.google.android.apps.muzei.ACTION_ARTWORK_CHANGED"/>
</intent-filter>
</receiver>
<!-- Service which actually updates the widget(s) -->
<service android:name=".ArtworkUpdateService"/>
</application>
</manifest>
@@ -1,32 +0,0 @@
/*
* Copyright 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.muzei.examplecontractwidget;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* Receive artwork update broadcasts from Muzei and reloads our artwork. This approach allows our process to only be
* started as needed rather than rely on a ContentObserver being constantly active
*/
public class ArtworkUpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
context.startService(new Intent(context, ArtworkUpdateService.class));
}
}
@@ -1,122 +0,0 @@
/*
* Copyright 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.muzei.examplecontractwidget;
import android.app.IntentService;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.widget.RemoteViews;
import com.google.android.apps.muzei.api.Artwork;
import com.google.android.apps.muzei.api.MuzeiContract;
import java.io.FileNotFoundException;
/**
* Handle updating all widgets with the latest artwork
*/
public class ArtworkUpdateService extends IntentService {
private static final String TAG = "ArtworkUpdateService";
public ArtworkUpdateService() {
super(TAG);
}
@Override
protected void onHandleIntent(final Intent intent) {
ComponentName widget = new ComponentName(this, MuzeiAppWidgetProvider.class);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(widget);
if (appWidgetIds.length == 0) {
// No app widgets, nothing to do
Log.w(TAG, "No AppWidgets found");
return;
}
Artwork currentArtwork = MuzeiContract.Artwork.getCurrentArtwork(this);
if (currentArtwork == null) {
Log.w(TAG, "No current artwork found");
return;
}
String contentDescription = !TextUtils.isEmpty(currentArtwork.getTitle())
? currentArtwork.getTitle()
: currentArtwork.getByline();
Bitmap image;
try {
image = MuzeiContract.Artwork.getCurrentArtworkBitmap(this);
} catch (FileNotFoundException e) {
Log.w(TAG, "Could not find current artwork image", e);
return;
}
if (image == null) {
Log.w(TAG, "No current artwork bitmap found");
return;
}
// Update the widget(s) with the new artwork information
PackageManager packageManager = getPackageManager();
Intent launchIntent = packageManager.getLaunchIntentForPackage("net.nurik.roman.muzei");
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
for (int widgetId : appWidgetIds) {
Bundle extras = appWidgetManager.getAppWidgetOptions(widgetId);
int widgetWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
extras.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH), displayMetrics);
int widgetHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
extras.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT), displayMetrics);
// getCurrentArtworkBitmap is a naive method so we manually reduce the size based on the
// widget's actual size. Consider using
// BitmapFactory.decodeStream(contentResolver.openInputStream(CONTENT_URI), options)
// where options subsamples the image to the appropriate size
Bitmap scaledImage = scaleBitmap(image, widgetWidth, widgetHeight);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget);
remoteViews.setContentDescription(R.id.background, contentDescription);
remoteViews.setImageViewBitmap(R.id.background, scaledImage);
remoteViews.setOnClickPendingIntent(R.id.background, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
private Bitmap scaleBitmap(Bitmap image, int widgetWidth, int widgetHeight) {
int largestDimension = Math.max(widgetWidth, widgetHeight);
int width = image.getWidth();
int height = image.getHeight();
if (width > height) {
// landscape
float ratio = (float) width / largestDimension;
width = largestDimension;
height = (int) (height / ratio);
} else if (height > width) {
// portrait
float ratio = (float) height / largestDimension;
height = largestDimension;
width = (int) (width / ratio);
} else {
height = largestDimension;
width = largestDimension;
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
}
@@ -1,54 +0,0 @@
/*
* Copyright 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.muzei.examplecontractwidget;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
/**
* Handles widget lifecycle events. Ensures that the @ArtworkUpdateReceiver is only enabled when widgets are in use.
* This prevents us from receiving artwork update broadcasts when the user does not have a widget added.
*/
public class MuzeiAppWidgetProvider extends AppWidgetProvider {
@Override
public void onEnabled(final Context context) {
// Enable the ArtworkUpdateReceiver as we now have widgets that need to be updated
PackageManager packageManager = context.getPackageManager();
ComponentName componentName = new ComponentName(context, ArtworkUpdateReceiver.class);
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
@Override
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
// Ensure that new widgets are updated with the current artwork
context.startService(new Intent(context, ArtworkUpdateService.class));
}
@Override
public void onDisabled(final Context context) {
// Disable the ArtworkUpdateReceiver as we no longer have widgets that need to be updated
PackageManager packageManager = context.getPackageManager();
ComponentName componentName = new ComponentName(context, ArtworkUpdateReceiver.class);
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
}
@@ -1,24 +0,0 @@
<!--
Copyright 2014 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:background="@android:color/black"
tools:ignore="ContentDescription"/>
@@ -1,19 +0,0 @@
<!--
Copyright 2014 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources>
<string name="app_name">Muzei Example Widget</string>
</resources>
@@ -1,27 +0,0 @@
<!--
Copyright 2014 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/widget"
android:initialLayout="@layout/widget"
android:minHeight="110dp"
android:minWidth="180dp"
android:minResizeHeight="40dp"
android:minResizeWidth="40dp"
android:previewImage="@drawable/preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="0"
android:widgetCategory="home_screen|keyguard" />
View
@@ -1 +1 @@
include ':api', ':android-client-common', ':source-featured-art', ':source-gallery', ':main', ':wearable', ':example-source-500px', 'example-contract-widget', 'example-watchface'
include ':api', ':android-client-common', ':source-featured-art', ':source-gallery', ':main', ':wearable', ':example-source-500px', 'example-watchface'

0 comments on commit fa62a6c

Please sign in to comment.