Before Width: | Height: | Size: 8.6 KiB After Width: | Height: | Size: 8.5 KiB |
Before Width: | Height: | Size: 8.5 KiB After Width: | Height: | Size: 9.4 KiB |
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 8.6 KiB After Width: | Height: | Size: 9.6 KiB |
@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker 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.
|
||||
*
|
||||
* Loop Habit Tracker 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.ui.widgets;
|
||||
|
||||
import android.content.*;
|
||||
import android.support.test.runner.*;
|
||||
import android.test.suitebuilder.annotation.*;
|
||||
import android.widget.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.widgets.*;
|
||||
import org.junit.*;
|
||||
import org.junit.runner.*;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.isoron.uhabits.models.Checkmark.*;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@MediumTest
|
||||
public class CheckmarkWidgetTest extends BaseViewTest
|
||||
{
|
||||
|
||||
private static final String PATH = "widgets/CheckmarkWidgetView/";
|
||||
|
||||
private Habit habit;
|
||||
|
||||
private CheckmarkList checkmarks;
|
||||
|
||||
private FrameLayout view;
|
||||
|
||||
@Override
|
||||
public void setUp()
|
||||
{
|
||||
super.setUp();
|
||||
habit = fixtures.createShortHabit();
|
||||
checkmarks = habit.getCheckmarks();
|
||||
CheckmarkWidget widget = new CheckmarkWidget(targetContext, 0, habit);
|
||||
view = convertToView(widget, 200, 250);
|
||||
|
||||
assertThat(checkmarks.getTodayValue(), equalTo(CHECKED_EXPLICITLY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClick() throws Exception
|
||||
{
|
||||
Button button = (Button) view.findViewById(R.id.button);
|
||||
assertThat(button, is(not(nullValue())));
|
||||
|
||||
// A better test would be to capture the intent, but it doesn't seem
|
||||
// possible to capture intents sent to BroadcastReceivers.
|
||||
button.performClick();
|
||||
sleep(1000);
|
||||
|
||||
assertThat(checkmarks.getTodayValue(), equalTo(CHECKED_IMPLICITLY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsInstalled()
|
||||
{
|
||||
ComponentName provider =
|
||||
new ComponentName(targetContext, CheckmarkWidgetProvider.class);
|
||||
|
||||
assertWidgetProviderIsInstalled(provider);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRender() throws Exception
|
||||
{
|
||||
assertRenders(view, PATH + "checked.png");
|
||||
}
|
||||
}
|
@ -0,0 +1,196 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker 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.
|
||||
*
|
||||
* Loop Habit Tracker 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.ui.widgets;
|
||||
|
||||
import android.app.*;
|
||||
import android.content.*;
|
||||
import android.graphics.*;
|
||||
import android.support.annotation.*;
|
||||
import android.view.*;
|
||||
import android.widget.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
|
||||
import javax.inject.*;
|
||||
|
||||
import static android.os.Build.VERSION.*;
|
||||
import static android.os.Build.VERSION_CODES.*;
|
||||
import static android.view.View.MeasureSpec.*;
|
||||
|
||||
public abstract class BaseWidget
|
||||
{
|
||||
@Inject
|
||||
WidgetPreferences preferences;
|
||||
|
||||
private final int id;
|
||||
|
||||
@NonNull
|
||||
private WidgetDimensions dimensions;
|
||||
|
||||
@NonNull
|
||||
private final Context context;
|
||||
|
||||
public BaseWidget(@NonNull Context context, int id)
|
||||
{
|
||||
this.id = id;
|
||||
this.context = context;
|
||||
HabitsApplication.getComponent().inject(this);
|
||||
dimensions = new WidgetDimensions(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
public void delete()
|
||||
{
|
||||
preferences.removeWidget(id);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public Context getContext()
|
||||
{
|
||||
return context;
|
||||
}
|
||||
|
||||
public int getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public RemoteViews getLandscapeRemoteViews()
|
||||
{
|
||||
return getRemoteViews(dimensions.getLandscapeWidth(),
|
||||
dimensions.getLandscapeHeight());
|
||||
}
|
||||
|
||||
public abstract int getLayoutId();
|
||||
|
||||
public abstract PendingIntent getOnClickPendingIntent(Context context);
|
||||
|
||||
@NonNull
|
||||
public RemoteViews getPortraitRemoteViews()
|
||||
{
|
||||
return getRemoteViews(dimensions.getPortraitWidth(),
|
||||
dimensions.getPortraitHeight());
|
||||
}
|
||||
|
||||
public abstract void refreshData(View widgetView);
|
||||
|
||||
public void setDimensions(@NonNull WidgetDimensions dimensions)
|
||||
{
|
||||
this.dimensions = dimensions;
|
||||
}
|
||||
|
||||
protected abstract View buildView();
|
||||
|
||||
protected abstract int getDefaultHeight();
|
||||
|
||||
protected abstract int getDefaultWidth();
|
||||
|
||||
protected abstract String getTitle();
|
||||
|
||||
private void adjustRemoteViewsPadding(RemoteViews remoteViews,
|
||||
View view,
|
||||
int width,
|
||||
int height)
|
||||
{
|
||||
int imageWidth = view.getMeasuredWidth();
|
||||
int imageHeight = view.getMeasuredHeight();
|
||||
int p[] = calculatePadding(width, height, imageWidth, imageHeight);
|
||||
remoteViews.setViewPadding(R.id.buttonOverlay, p[0], p[1], p[2], p[3]);
|
||||
}
|
||||
|
||||
private void buildRemoteViews(View view,
|
||||
RemoteViews remoteViews,
|
||||
int width,
|
||||
int height)
|
||||
{
|
||||
Bitmap bitmap = getBitmapFromView(view);
|
||||
remoteViews.setTextViewText(R.id.label, getTitle());
|
||||
remoteViews.setImageViewBitmap(R.id.imageView, bitmap);
|
||||
|
||||
if (SDK_INT >= JELLY_BEAN)
|
||||
adjustRemoteViewsPadding(remoteViews, view, width, height);
|
||||
|
||||
PendingIntent onClickIntent = getOnClickPendingIntent(context);
|
||||
if (onClickIntent != null)
|
||||
remoteViews.setOnClickPendingIntent(R.id.button, onClickIntent);
|
||||
}
|
||||
|
||||
private int[] calculatePadding(int entireWidth,
|
||||
int entireHeight,
|
||||
int imageWidth,
|
||||
int imageHeight)
|
||||
{
|
||||
int w = (int) (((float) entireWidth - imageWidth) / 2);
|
||||
int h = (int) (((float) entireHeight - imageHeight) / 2);
|
||||
|
||||
return new int[]{ w, h, w, h };
|
||||
}
|
||||
|
||||
private Bitmap getBitmapFromView(View view)
|
||||
{
|
||||
view.invalidate();
|
||||
view.setDrawingCacheEnabled(true);
|
||||
view.buildDrawingCache(true);
|
||||
return view.getDrawingCache();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private RemoteViews getRemoteViews(int width, int height)
|
||||
{
|
||||
View view = buildView();
|
||||
measureView(view, width, height);
|
||||
|
||||
refreshData(view);
|
||||
|
||||
if(view.isLayoutRequested())
|
||||
measureView(view, width, height);
|
||||
|
||||
RemoteViews remoteViews =
|
||||
new RemoteViews(context.getPackageName(), getLayoutId());
|
||||
|
||||
buildRemoteViews(view, remoteViews, width, height);
|
||||
|
||||
return remoteViews;
|
||||
}
|
||||
|
||||
private void measureView(View view, int width, int height)
|
||||
{
|
||||
LayoutInflater inflater = LayoutInflater.from(context);
|
||||
View entireView = inflater.inflate(getLayoutId(), null);
|
||||
|
||||
int specWidth = makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
|
||||
int specHeight = makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
|
||||
|
||||
entireView.measure(specWidth, specHeight);
|
||||
entireView.layout(0, 0, entireView.getMeasuredWidth(),
|
||||
entireView.getMeasuredHeight());
|
||||
|
||||
View imageView = entireView.findViewById(R.id.imageView);
|
||||
width = imageView.getMeasuredWidth();
|
||||
height = imageView.getMeasuredHeight();
|
||||
|
||||
specWidth = makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
|
||||
specHeight = makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
|
||||
|
||||
view.measure(specWidth, specHeight);
|
||||
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
|
||||
}
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker 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.
|
||||
*
|
||||
* Loop Habit Tracker 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.ui.widgets;
|
||||
|
||||
import android.app.*;
|
||||
import android.content.*;
|
||||
import android.support.annotation.*;
|
||||
import android.view.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.ui.widgets.views.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
|
||||
public class CheckmarkWidget extends BaseWidget
|
||||
{
|
||||
@NonNull
|
||||
private final Habit habit;
|
||||
|
||||
public CheckmarkWidget(@NonNull Context context,
|
||||
int widgetId,
|
||||
@NonNull Habit habit)
|
||||
{
|
||||
super(context, widgetId);
|
||||
this.habit = habit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLayoutId()
|
||||
{
|
||||
return R.layout.widget_wrapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PendingIntent getOnClickPendingIntent(Context context)
|
||||
{
|
||||
return HabitBroadcastReceiver.buildCheckIntent(context, habit, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refreshData(View v)
|
||||
{
|
||||
CheckmarkWidgetView view = (CheckmarkWidgetView) v;
|
||||
int color = ColorUtils.getColor(getContext(), habit.getColor());
|
||||
int score = habit.getScores().getTodayValue();
|
||||
float percentage = (float) score / Score.MAX_VALUE;
|
||||
int checkmark = habit.getCheckmarks().getTodayValue();
|
||||
|
||||
view.setPercentage(percentage);
|
||||
view.setActiveColor(color);
|
||||
view.setName(habit.getName());
|
||||
view.setCheckmarkValue(checkmark);
|
||||
view.refresh();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected View buildView()
|
||||
{
|
||||
return new CheckmarkWidgetView(getContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getDefaultHeight()
|
||||
{
|
||||
return 125;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getDefaultWidth()
|
||||
{
|
||||
return 125;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTitle()
|
||||
{
|
||||
return habit.getName();
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker 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.
|
||||
*
|
||||
* Loop Habit Tracker 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.ui.widgets;
|
||||
|
||||
public class WidgetDimensions
|
||||
{
|
||||
private final int portraitWidth;
|
||||
|
||||
private final int portraitHeight;
|
||||
|
||||
private final int landscapeWidth;
|
||||
|
||||
private final int landscapeHeight;
|
||||
|
||||
public WidgetDimensions(int portraitWidth,
|
||||
int portraitHeight,
|
||||
int landscapeWidth,
|
||||
int landscapeHeight)
|
||||
{
|
||||
this.portraitWidth = portraitWidth;
|
||||
this.portraitHeight = portraitHeight;
|
||||
this.landscapeWidth = landscapeWidth;
|
||||
this.landscapeHeight = landscapeHeight;
|
||||
}
|
||||
|
||||
public int getLandscapeHeight()
|
||||
{
|
||||
return landscapeHeight;
|
||||
}
|
||||
|
||||
public int getLandscapeWidth()
|
||||
{
|
||||
return landscapeWidth;
|
||||
}
|
||||
|
||||
public int getPortraitHeight()
|
||||
{
|
||||
return portraitHeight;
|
||||
}
|
||||
|
||||
public int getPortraitWidth()
|
||||
{
|
||||
return portraitWidth;
|
||||
}
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker 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.
|
||||
*
|
||||
* Loop Habit Tracker 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.ui.widgets;
|
||||
|
||||
import android.appwidget.*;
|
||||
import android.content.*;
|
||||
import android.support.annotation.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.commands.*;
|
||||
import org.isoron.uhabits.widgets.*;
|
||||
|
||||
import javax.inject.*;
|
||||
|
||||
/**
|
||||
* A WidgetUpdater listens to the commands being executed by the application and
|
||||
* updates the home-screen widgets accordingly.
|
||||
* <p>
|
||||
* There should be only one instance of this class, created when the application
|
||||
* starts. To access it, call HabitApplication.getWidgetUpdater().
|
||||
*/
|
||||
public class WidgetUpdater implements CommandRunner.Listener
|
||||
{
|
||||
@Inject
|
||||
CommandRunner commandRunner;
|
||||
|
||||
@NonNull
|
||||
private final Context context;
|
||||
|
||||
public WidgetUpdater(@NonNull Context context)
|
||||
{
|
||||
this.context = context;
|
||||
HabitsApplication.getComponent().inject(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommandExecuted(@NonNull Command command,
|
||||
@Nullable Long refreshKey)
|
||||
{
|
||||
updateWidgets(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instructs the updater to start listening to commands. If any relevant
|
||||
* commands are executed after this method is called, the corresponding
|
||||
* widgets will get updated.
|
||||
*/
|
||||
public void startListening()
|
||||
{
|
||||
commandRunner.addListener(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instructs the updater to stop listening to commands. Every command
|
||||
* executed after this method is called will be ignored by the updater.
|
||||
*/
|
||||
public void stopListening()
|
||||
{
|
||||
commandRunner.removeListener(this);
|
||||
}
|
||||
|
||||
void updateWidgets(Context context)
|
||||
{
|
||||
updateWidgets(context, CheckmarkWidgetProvider.class);
|
||||
updateWidgets(context, HistoryWidgetProvider.class);
|
||||
updateWidgets(context, ScoreWidgetProvider.class);
|
||||
updateWidgets(context, StreakWidgetProvider.class);
|
||||
updateWidgets(context, FrequencyWidgetProvider.class);
|
||||
}
|
||||
|
||||
private void updateWidgets(Context context, Class providerClass)
|
||||
{
|
||||
ComponentName provider = new ComponentName(context, providerClass);
|
||||
Intent intent = new Intent(context, providerClass);
|
||||
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
|
||||
int ids[] =
|
||||
AppWidgetManager.getInstance(context).getAppWidgetIds(provider);
|
||||
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
|
||||
context.sendBroadcast(intent);
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker 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.
|
||||
*
|
||||
* Loop Habit Tracker 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.utils;
|
||||
|
||||
import android.content.*;
|
||||
import android.preference.*;
|
||||
|
||||
import org.isoron.uhabits.*;
|
||||
|
||||
public class WidgetPreferences
|
||||
{
|
||||
private Context context;
|
||||
|
||||
private SharedPreferences prefs;
|
||||
|
||||
public WidgetPreferences()
|
||||
{
|
||||
this.context = HabitsApplication.getContext();
|
||||
prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
}
|
||||
|
||||
public void addWidget(int widgetId, long habitId)
|
||||
{
|
||||
prefs
|
||||
.edit()
|
||||
.putLong(getHabitIdKey(widgetId), habitId)
|
||||
.commit();
|
||||
}
|
||||
|
||||
public long getHabitIdFromWidgetId(int widgetId)
|
||||
{
|
||||
Long habitId = prefs.getLong(getHabitIdKey(widgetId), -1);
|
||||
if (habitId < 0) throw new RuntimeException("widget not found");
|
||||
|
||||
return habitId;
|
||||
}
|
||||
|
||||
public void removeWidget(int id)
|
||||
{
|
||||
String habitIdKey = getHabitIdKey(id);
|
||||
prefs.edit().remove(habitIdKey).apply();
|
||||
}
|
||||
|
||||
private String getHabitIdKey(int id)
|
||||
{
|
||||
return String.format("widget-%06d-habit", id);
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker 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.
|
||||
*
|
||||
* Loop Habit Tracker 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.utils;
|
||||
|
||||
import android.appwidget.*;
|
||||
import android.content.*;
|
||||
import android.os.*;
|
||||
import android.support.annotation.*;
|
||||
import android.widget.*;
|
||||
|
||||
import org.isoron.uhabits.ui.widgets.*;
|
||||
|
||||
import static android.os.Build.VERSION.*;
|
||||
import static android.os.Build.VERSION_CODES.*;
|
||||
|
||||
public abstract class WidgetUtils
|
||||
{
|
||||
@NonNull
|
||||
public static WidgetDimensions getDimensionsFromOptions(
|
||||
@NonNull Context context, @NonNull Bundle options)
|
||||
{
|
||||
if (SDK_INT < JELLY_BEAN)
|
||||
throw new AssertionError("method requires jelly-bean");
|
||||
|
||||
int maxWidth = (int) InterfaceUtils.dpToPixels(context,
|
||||
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH));
|
||||
int maxHeight = (int) InterfaceUtils.dpToPixels(context,
|
||||
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT));
|
||||
int minWidth = (int) InterfaceUtils.dpToPixels(context,
|
||||
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH));
|
||||
int minHeight = (int) InterfaceUtils.dpToPixels(context,
|
||||
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT));
|
||||
|
||||
return new WidgetDimensions(minWidth, maxHeight, maxWidth, minHeight);
|
||||
}
|
||||
|
||||
public static void updateAppWidget(@NonNull AppWidgetManager manager,
|
||||
@NonNull BaseWidget widget)
|
||||
{
|
||||
if (SDK_INT < JELLY_BEAN)
|
||||
{
|
||||
RemoteViews portrait = widget.getPortraitRemoteViews();
|
||||
manager.updateAppWidget(widget.getId(), portrait);
|
||||
}
|
||||
else
|
||||
{
|
||||
RemoteViews landscape = widget.getLandscapeRemoteViews();
|
||||
RemoteViews portrait = widget.getPortraitRemoteViews();
|
||||
RemoteViews views = new RemoteViews(landscape, portrait);
|
||||
manager.updateAppWidget(widget.getId(), views);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker 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.
|
||||
*
|
||||
* Loop Habit Tracker 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.widgets;
|
||||
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
public class WidgetManager
|
||||
{
|
||||
public static void updateWidgets(Context context)
|
||||
{
|
||||
updateWidgets(context, CheckmarkWidgetProvider.class);
|
||||
updateWidgets(context, HistoryWidgetProvider.class);
|
||||
updateWidgets(context, ScoreWidgetProvider.class);
|
||||
updateWidgets(context, StreakWidgetProvider.class);
|
||||
updateWidgets(context, FrequencyWidgetProvider.class);
|
||||
}
|
||||
|
||||
private static void updateWidgets(Context context, Class providerClass)
|
||||
{
|
||||
ComponentName provider = new ComponentName(context, providerClass);
|
||||
Intent intent = new Intent(context, providerClass);
|
||||
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
|
||||
int ids[] = AppWidgetManager.getInstance(context).getAppWidgetIds(provider);
|
||||
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
|
||||
context.sendBroadcast(intent);
|
||||
}
|
||||
}
|