Author: kmorin Date: 2014-03-29 14:49:12 +0100 (Sat, 29 Mar 2014) New Revision: 8 Url: http://forge.chorem.org/projects/say-my-texts/repository/revisions/8 Log: fixes #1000 Widget to activate or deactivate the reading feature Modified: trunk/res/layout/say_my_texts_widget.xml trunk/src/org/chorem/android/saymytexts/SayMyTextsWidgetProvider.java trunk/src/site/fr/rst/user.rst trunk/src/site/rst/user.rst Modified: trunk/res/layout/say_my_texts_widget.xml =================================================================== --- trunk/res/layout/say_my_texts_widget.xml 2014-03-29 11:26:48 UTC (rev 7) +++ trunk/res/layout/say_my_texts_widget.xml 2014-03-29 13:49:12 UTC (rev 8) @@ -1,8 +1,21 @@ <?xml version="1.0" encoding="utf-8"?> -<ImageButton xmlns:android="http://schemas.android.com/apk/res/android" - android:id="@+id/say_my_texts_widget_id" + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@+id/say_my_texts_widget" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:src="@drawable/logo_small"> + android:orientation="vertical" + android:gravity="center" + android:background="@drawable/widget_preview_dark"> -</ImageButton> \ No newline at end of file + <ImageView android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:src="@drawable/logo_small"/> + + <ImageView android:id="@+id/say_my_texts_widget_onoff_icon" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginTop="2dp" + android:src="@android:drawable/button_onoff_indicator_on"/> + +</LinearLayout> \ No newline at end of file Modified: trunk/src/org/chorem/android/saymytexts/SayMyTextsWidgetProvider.java =================================================================== --- trunk/src/org/chorem/android/saymytexts/SayMyTextsWidgetProvider.java 2014-03-29 11:26:48 UTC (rev 7) +++ trunk/src/org/chorem/android/saymytexts/SayMyTextsWidgetProvider.java 2014-03-29 13:49:12 UTC (rev 8) @@ -20,12 +20,13 @@ protected static final String CLICK_ACTION = "org.chorem.android.saymytexts.action.APPWIDGET_CLICKED"; - public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + @Override + public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, int[] appWidgetIds) { final int N = appWidgetIds.length; // Perform this loop procedure for each App Widget that belongs to this provider - for (int i=0; i<N; i++) { - int appWidgetId = appWidgetIds[i]; + for (int i = 0; i < N; i++) { + final int appWidgetId = appWidgetIds[i]; // Create an Intent to call this onReceive method Intent intent = new Intent(context, getClass()); @@ -34,9 +35,21 @@ // Get the layout for the App Widget and attach an on-click listener // to the button - RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.say_my_texts_widget); - views.setOnClickPendingIntent(R.id.say_my_texts_widget_id, pendingIntent); + RemoteViews views = updateButtonState(context); + views.setOnClickPendingIntent(R.id.say_my_texts_widget, pendingIntent); + PreferenceManager.getDefaultSharedPreferences(context) + .registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() { + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + String enableReadingKey = context.getString(R.string.preference_enable_reading_key); + if (key.equals(enableReadingKey)) { + RemoteViews views = updateButtonState(context); + appWidgetManager.updateAppWidget(appWidgetId, views); + } + } + }); + // Tell the AppWidgetManager to perform an update on the current app widget appWidgetManager.updateAppWidget(appWidgetId, views); } @@ -54,4 +67,19 @@ sharedPref.edit().putBoolean(key, !enabled).commit(); } } + + protected RemoteViews updateButtonState(Context context) { + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + String key = context.getString(R.string.preference_enable_reading_key); + + boolean enabled = sharedPreferences.getBoolean(key, true); + int drawableId = enabled ? + android.R.drawable.button_onoff_indicator_on : + android.R.drawable.button_onoff_indicator_off; + + RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.say_my_texts_widget); + views.setImageViewResource(R.id.say_my_texts_widget_onoff_icon, drawableId); + + return views; + } } Modified: trunk/src/site/fr/rst/user.rst =================================================================== --- trunk/src/site/fr/rst/user.rst 2014-03-29 11:26:48 UTC (rev 7) +++ trunk/src/site/fr/rst/user.rst 2014-03-29 13:49:12 UTC (rev 8) @@ -72,4 +72,9 @@ Lorsque vous activez le mode Heisendroid, la lecture des SMS est précédée par "*Say my text*" et suivie de "*You're goddamn right!*". Il s'agit là aussi d'une référence à une des répliques cultes de la série. -.. _mode Heisendroid: #Mode_Heisendroid \ No newline at end of file +.. _mode Heisendroid: #Mode_Heisendroid + +Widget +~~~~~~ + +Le widget vous permet d'activer ou désactiver la lecture depuis l'écran d'accueil. \ No newline at end of file Modified: trunk/src/site/rst/user.rst =================================================================== --- trunk/src/site/rst/user.rst 2014-03-29 11:26:48 UTC (rev 7) +++ trunk/src/site/rst/user.rst 2014-03-29 13:49:12 UTC (rev 8) @@ -68,4 +68,10 @@ When you enable the Heisendroid mode, the SMS reading is preceded by "*Say my text*" and followed by "*You're goddamn right!*". It is also a reference to one of the cult line of the series. -.. _Heisendroid mode: #Heisendroid_mode \ No newline at end of file +.. _Heisendroid mode: #Heisendroid_mode + + +Widget +~~~~~~ + +The widget permits you to enable or disable the reading from the home screen. \ No newline at end of file