mirror of https://github.com/iSoron/uhabits.git
parent
b8f7d4fad2
commit
82972d6e47
@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* 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.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.view.View
|
||||||
|
import android.widget.RemoteViews
|
||||||
|
import org.isoron.uhabits.R
|
||||||
|
import org.isoron.uhabits.widgets.views.CheckmarkWidgetView
|
||||||
|
|
||||||
|
class StackGroupWidget(
|
||||||
|
context: Context,
|
||||||
|
widgetId: Int
|
||||||
|
) : BaseWidget(context, widgetId) {
|
||||||
|
|
||||||
|
override fun getOnClickPendingIntent(context: Context) = null
|
||||||
|
|
||||||
|
override fun refreshData(v: View) {
|
||||||
|
// unused
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getRemoteViews(width: Int, height: Int): RemoteViews {
|
||||||
|
val remoteViews = RemoteViews(context.packageName, R.layout.widget_stackview)
|
||||||
|
val serviceIntent = Intent(context, StackWidgetService::class.java)
|
||||||
|
serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id)
|
||||||
|
// TODO this commented line is taken from official examples, but adding it seems to make the widget not update immediately when completing the habit
|
||||||
|
// serviceIntent.data = Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME)) // embed extras so they don't get ignored
|
||||||
|
remoteViews.setRemoteAdapter(R.id.stackWidgetView, serviceIntent)
|
||||||
|
AppWidgetManager.getInstance(context).notifyAppWidgetViewDataChanged(id, R.id.stackWidgetView)
|
||||||
|
// TODO what should the empty view look like?
|
||||||
|
remoteViews.setEmptyView(R.id.stackWidgetView, R.id.stackWidgetEmptyView)
|
||||||
|
return remoteViews
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun buildView() = null // unused
|
||||||
|
override fun getDefaultHeight() = 0 // unused
|
||||||
|
override fun getDefaultWidth() = 0 // unused
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package org.isoron.uhabits.widgets
|
||||||
|
|
||||||
|
import android.appwidget.AppWidgetManager
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.util.Log
|
||||||
|
import org.isoron.uhabits.R
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by victoryu on 9/30/17.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class StackWidgetProvider : BaseWidgetProvider() {
|
||||||
|
override fun getWidgetFromId(context: Context, id: Int): StackGroupWidget {
|
||||||
|
return StackGroupWidget(context, id)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,110 @@
|
|||||||
|
package org.isoron.uhabits.widgets;
|
||||||
|
|
||||||
|
import android.appwidget.AppWidgetManager;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.widget.RemoteViews;
|
||||||
|
import android.widget.RemoteViewsService;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.HabitsApplication;
|
||||||
|
import org.isoron.uhabits.core.models.Habit;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT;
|
||||||
|
import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH;
|
||||||
|
import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT;
|
||||||
|
import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH;
|
||||||
|
import static org.isoron.androidbase.utils.InterfaceUtils.dpToPixels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by victoryu on 9/30/17.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class StackWidgetService extends RemoteViewsService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RemoteViewsFactory onGetViewFactory(Intent intent) {
|
||||||
|
return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
|
||||||
|
private Context mContext;
|
||||||
|
private int mAppWidgetId;
|
||||||
|
private ArrayList<Habit> mHabitList;
|
||||||
|
|
||||||
|
public StackRemoteViewsFactory(Context context, Intent intent) {
|
||||||
|
mContext = context;
|
||||||
|
mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onCreate() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onDestroy() {
|
||||||
|
mHabitList.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCount() {
|
||||||
|
return mHabitList.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public WidgetDimensions getDimensionsFromOptions(@NonNull Context ctx,
|
||||||
|
@NonNull Bundle options) {
|
||||||
|
int maxWidth =
|
||||||
|
(int) dpToPixels(ctx, options.getInt(OPTION_APPWIDGET_MAX_WIDTH));
|
||||||
|
int maxHeight =
|
||||||
|
(int) dpToPixels(ctx, options.getInt(OPTION_APPWIDGET_MAX_HEIGHT));
|
||||||
|
int minWidth =
|
||||||
|
(int) dpToPixels(ctx, options.getInt(OPTION_APPWIDGET_MIN_WIDTH));
|
||||||
|
int minHeight =
|
||||||
|
(int) dpToPixels(ctx, options.getInt(OPTION_APPWIDGET_MIN_HEIGHT));
|
||||||
|
|
||||||
|
return new WidgetDimensions(minWidth, maxHeight, maxWidth, minHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RemoteViews getViewAt(int position) {
|
||||||
|
RemoteViews rv = null;
|
||||||
|
|
||||||
|
if (position < getCount()) {
|
||||||
|
Habit habit = mHabitList.get(position);
|
||||||
|
CheckmarkWidget checkmarkWidget = new CheckmarkWidget(mContext, mAppWidgetId, habit);
|
||||||
|
Bundle options = AppWidgetManager.getInstance(mContext).getAppWidgetOptions(mAppWidgetId);
|
||||||
|
checkmarkWidget.setDimensions(getDimensionsFromOptions(mContext, options));
|
||||||
|
RemoteViews landscape = checkmarkWidget.getLandscapeRemoteViews();
|
||||||
|
RemoteViews portrait = checkmarkWidget.getPortraitRemoteViews();
|
||||||
|
rv = new RemoteViews(landscape, portrait);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RemoteViews getLoadingView() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getViewTypeCount() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getItemId(int position) {
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasStableIds() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onDataSetChanged() {
|
||||||
|
mHabitList = new ArrayList<>();
|
||||||
|
HabitsApplication app = (HabitsApplication) mContext.getApplicationContext();
|
||||||
|
for (Habit h : app.getComponent().getHabitList()) {
|
||||||
|
mHabitList.add(h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent">
|
||||||
|
<StackView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/stackWidgetView"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:loopViews="true" />
|
||||||
|
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/stackWidgetEmptyView"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:text="@string/stacked_checkmark"
|
||||||
|
android:gravity="center"
|
||||||
|
android:textColor="#ffffff"
|
||||||
|
android:textStyle="bold"
|
||||||
|
android:textSize="16sp" />
|
||||||
|
</FrameLayout>
|
@ -0,0 +1,33 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!--
|
||||||
|
~ 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/>.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<!-- TODO need to change preview image here -->
|
||||||
|
|
||||||
|
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:minHeight="40dp"
|
||||||
|
android:minWidth="40dp"
|
||||||
|
android:initialLayout="@layout/widget_wrapper"
|
||||||
|
android:previewImage="@drawable/widget_preview_checkmark"
|
||||||
|
android:resizeMode="none"
|
||||||
|
android:updatePeriodMillis="3600000"
|
||||||
|
android:configure="org.isoron.uhabits.widgets.HabitPickerDialog"
|
||||||
|
android:widgetCategory="home_screen">
|
||||||
|
|
||||||
|
</appwidget-provider>
|
Loading…
Reference in new issue