mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Add code to save widget preview to file
This commit is contained in:
@@ -21,19 +21,25 @@ import android.appwidget.AppWidgetManager;
|
|||||||
import android.appwidget.AppWidgetProvider;
|
import android.appwidget.AppWidgetProvider;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.res.Configuration;
|
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
|
import android.media.Image;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.widget.ImageView;
|
||||||
|
import android.widget.LinearLayout;
|
||||||
import android.widget.RemoteViews;
|
import android.widget.RemoteViews;
|
||||||
|
|
||||||
import org.isoron.helpers.DialogHelper;
|
import org.isoron.helpers.DialogHelper;
|
||||||
import org.isoron.uhabits.R;
|
import org.isoron.uhabits.R;
|
||||||
import org.isoron.uhabits.models.Habit;
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
public abstract class BaseWidgetProvider extends AppWidgetProvider
|
public abstract class BaseWidgetProvider extends AppWidgetProvider
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -107,12 +113,45 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
|
|||||||
remoteViews.setTextViewText(R.id.label, habit.name);
|
remoteViews.setTextViewText(R.id.label, habit.name);
|
||||||
remoteViews.setImageViewBitmap(R.id.imageView, drawingCache);
|
remoteViews.setImageViewBitmap(R.id.imageView, drawingCache);
|
||||||
|
|
||||||
|
//savePreview(context, widgetId, drawingCache);
|
||||||
|
|
||||||
PendingIntent onClickIntent = getOnClickPendingIntent(context, habit);
|
PendingIntent onClickIntent = getOnClickPendingIntent(context, habit);
|
||||||
if(onClickIntent != null) remoteViews.setOnClickPendingIntent(R.id.imageView, onClickIntent);
|
if(onClickIntent != null) remoteViews.setOnClickPendingIntent(R.id.imageView, onClickIntent);
|
||||||
|
|
||||||
manager.updateAppWidget(widgetId, remoteViews);
|
manager.updateAppWidget(widgetId, remoteViews);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void savePreview(Context context, int widgetId, Bitmap widgetCache)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
LayoutInflater inflater = LayoutInflater.from(context);
|
||||||
|
View view = inflater.inflate(getLayoutId(), null);
|
||||||
|
|
||||||
|
ImageView iv = (ImageView) view.findViewById(R.id.imageView);
|
||||||
|
iv.setImageBitmap(widgetCache);
|
||||||
|
|
||||||
|
view.measure(width, height);
|
||||||
|
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
|
||||||
|
view.setDrawingCacheEnabled(true);
|
||||||
|
view.buildDrawingCache();
|
||||||
|
Bitmap previewCache = view.getDrawingCache();
|
||||||
|
|
||||||
|
String filename = String.format("%s/%d.png", context.getExternalCacheDir(), widgetId);
|
||||||
|
Log.d("BaseWidgetProvider", String.format("Writing %s", filename));
|
||||||
|
FileOutputStream out = new FileOutputStream(filename);
|
||||||
|
|
||||||
|
if(previewCache != null)
|
||||||
|
previewCache.compress(Bitmap.CompressFormat.PNG, 100, out);
|
||||||
|
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void updateWidgetSize(Context context, Bundle options)
|
private void updateWidgetSize(Context context, Bundle options)
|
||||||
{
|
{
|
||||||
int maxWidth = getDefaultWidth();
|
int maxWidth = getDefaultWidth();
|
||||||
|
|||||||
Reference in New Issue
Block a user