Implement dummy widget

This commit is contained in:
2016-02-27 11:52:46 -05:00
parent 88455acc76
commit c1dae021bf
12 changed files with 191 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
/* Copyright (C) 2016 Alinson Santos Xavier
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.widget.RemoteViews;
import org.isoron.uhabits.views.SmallWidgetView;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class SmallWidgetProvider extends AppWidgetProvider
{
@Override
public void onUpdate(Context context, AppWidgetManager manager, int[] appWidgetIds)
{
for(int id : appWidgetIds)
updateWidget(context, manager, id);
}
private void updateWidget(Context context, AppWidgetManager manager, int widgetId)
{
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.small_widget);
SmallWidgetView widgetView = new SmallWidgetView(context);
widgetView.setDrawingCacheEnabled(true);
widgetView.measure(200, 200);
widgetView.layout(0, 0, 200, 200);
widgetView.buildDrawingCache(true);
Bitmap drawingCache = widgetView.getDrawingCache();
try
{
drawingCache.compress(Bitmap.CompressFormat.PNG, 100,
new FileOutputStream(context.getFilesDir() + "/widget.png"));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
Log.d("SmallWidgetProvider", drawingCache.toString());
remoteViews.setImageViewBitmap(R.id.imageView, drawingCache);
manager.updateAppWidget(widgetId, remoteViews);
}
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright (C) 2016 Alinson Santos Xavier
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
package org.isoron.uhabits.views;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import org.isoron.helpers.ColorHelper;
public class SmallWidgetView extends View
{
private Paint pCircle;
public SmallWidgetView(Context context)
{
super(context);
init(context);
}
public SmallWidgetView(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context);
}
private void init(Context context)
{
pCircle = new Paint();
pCircle.setColor(ColorHelper.palette[7]);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawOval(0, 0, getMeasuredWidth(), getMeasuredHeight(), pCircle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
}
}