diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index e1191f8f5..00d4a5506 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -10,11 +10,11 @@
+ android:theme="@style/AppBaseTheme">
+ android:value="AEdPqrEAAAAI6aeWncbnMNo8E5GWeZ44dlc5cQ7tCROwFhOtiw"/>
+ android:name=".HabitBroadcastReceiver"/>
-
+
-
+
+ android:resource="@xml/small_widget_info"/>
+
+
+
+
+
+
+
+
diff --git a/app/src/main/java/org/isoron/uhabits/HabitWidgetConfigure.java b/app/src/main/java/org/isoron/uhabits/HabitWidgetConfigure.java
new file mode 100644
index 000000000..8aa15caed
--- /dev/null
+++ b/app/src/main/java/org/isoron/uhabits/HabitWidgetConfigure.java
@@ -0,0 +1,91 @@
+/* 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 .
+ */
+
+package org.isoron.uhabits;
+
+import android.app.Activity;
+import android.appwidget.AppWidgetManager;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.os.Bundle;
+import android.preference.PreferenceManager;
+import android.view.View;
+import android.widget.AdapterView;
+import android.widget.ArrayAdapter;
+import android.widget.ListView;
+
+import org.isoron.helpers.DialogHelper;
+import org.isoron.uhabits.models.Habit;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class HabitWidgetConfigure extends Activity implements AdapterView.OnItemClickListener
+{
+
+ private Integer widgetId;
+ private ListView listView;
+
+ private ArrayList habitIds;
+ private ArrayList habitNames;
+ private ArrayAdapter adapter;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState)
+ {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.widget_configure_activity);
+
+ Intent intent = getIntent();
+ Bundle extras = intent.getExtras();
+
+ if (extras != null) widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
+ AppWidgetManager.INVALID_APPWIDGET_ID);
+
+ listView = (ListView) findViewById(R.id.listView);
+
+ habitIds = new ArrayList<>();
+ habitNames = new ArrayList<>();
+
+ List habits = Habit.getAll(false);
+ for(Habit h : habits)
+ {
+ habitIds.add(h.getId());
+ habitNames.add(h.name);
+ }
+
+ adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, habitNames);
+ listView.setAdapter(adapter);
+ listView.setOnItemClickListener(this);
+ }
+
+ @Override
+ public void onItemClick(AdapterView> parent, View view, int position, long id)
+ {
+ Long habitId = habitIds.get(position);
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
+ getApplicationContext());
+ prefs.edit().putLong(SmallWidgetProvider.getWidgetPrefKey(widgetId), habitId).commit();
+
+ MainActivity.updateWidgets(this);
+
+ Intent resultValue = new Intent();
+ resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
+ setResult(RESULT_OK, resultValue);
+ finish();
+ }
+
+}
diff --git a/app/src/main/java/org/isoron/uhabits/SmallWidgetProvider.java b/app/src/main/java/org/isoron/uhabits/SmallWidgetProvider.java
index 2c46b05b6..3f3b965f1 100644
--- a/app/src/main/java/org/isoron/uhabits/SmallWidgetProvider.java
+++ b/app/src/main/java/org/isoron/uhabits/SmallWidgetProvider.java
@@ -19,7 +19,9 @@ package org.isoron.uhabits;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
+import android.content.SharedPreferences;
import android.graphics.Bitmap;
+import android.preference.PreferenceManager;
import android.widget.RemoteViews;
import org.isoron.uhabits.models.Habit;
@@ -27,6 +29,7 @@ import org.isoron.uhabits.views.SmallWidgetView;
public class SmallWidgetProvider extends AppWidgetProvider
{
+
@Override
public void onUpdate(Context context, AppWidgetManager manager, int[] appWidgetIds)
{
@@ -37,8 +40,13 @@ public class SmallWidgetProvider extends AppWidgetProvider
private void updateWidget(Context context, AppWidgetManager manager, int widgetId)
{
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.small_widget);
+ Context appContext = context.getApplicationContext();
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
+
+ Long habitId = prefs.getLong(getWidgetPrefKey(widgetId), -1L);
+ if(habitId < 0) return;
- Habit habit = Habit.get(1L);
+ Habit habit = Habit.get(habitId);
SmallWidgetView widgetView = new SmallWidgetView(context);
widgetView.setDrawingCacheEnabled(true);
@@ -56,4 +64,18 @@ public class SmallWidgetProvider extends AppWidgetProvider
manager.updateAppWidget(widgetId, remoteViews);
}
+ public static String getWidgetPrefKey(long widgetId)
+ {
+ return String.format("widget-%03d", widgetId);
+ }
+
+ @Override
+ public void onDeleted(Context context, int[] appWidgetIds)
+ {
+ Context appContext = context.getApplicationContext();
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
+
+ for(Integer id : appWidgetIds)
+ prefs.edit().remove(getWidgetPrefKey(id));
+ }
}
diff --git a/app/src/main/res/layout/small_widget.xml b/app/src/main/res/layout/small_widget.xml
index c13e7e1a5..55fbb5c94 100644
--- a/app/src/main/res/layout/small_widget.xml
+++ b/app/src/main/res/layout/small_widget.xml
@@ -25,6 +25,6 @@
android:textSize="12sp"
android:maxLines="1"
android:ellipsize="end"
- android:text="Meditate"/>
+ android:text="@string/main_activity_title"/>
\ No newline at end of file
diff --git a/app/src/main/res/layout/widget_configure_activity.xml b/app/src/main/res/layout/widget_configure_activity.xml
new file mode 100644
index 000000000..8c653b697
--- /dev/null
+++ b/app/src/main/res/layout/widget_configure_activity.xml
@@ -0,0 +1,8 @@
+
+
+
+
\ No newline at end of file
diff --git a/app/src/main/res/xml/small_widget_info.xml b/app/src/main/res/xml/small_widget_info.xml
index a908abd06..4ffc0e993 100644
--- a/app/src/main/res/xml/small_widget_info.xml
+++ b/app/src/main/res/xml/small_widget_info.xml
@@ -5,7 +5,8 @@
android:initialLayout="@layout/small_widget"
android:previewImage="@mipmap/ic_small_widget_preview"
android:resizeMode="none"
- android:updatePeriodMillis="60000"
+ android:updatePeriodMillis="3600000"
+ android:configure="org.isoron.uhabits.HabitWidgetConfigure"
android:widgetCategory="home_screen">