mirror of https://github.com/ThmmyNoLife/mTHMMY
Apostolos Fanakis
5 years ago
9 changed files with 326 additions and 63 deletions
@ -0,0 +1,103 @@ |
|||||
|
package gr.thmmy.mthmmy.activities.widget; |
||||
|
|
||||
|
import android.app.PendingIntent; |
||||
|
import android.appwidget.AppWidgetManager; |
||||
|
import android.appwidget.AppWidgetProvider; |
||||
|
import android.content.Context; |
||||
|
import android.content.Intent; |
||||
|
import android.content.SharedPreferences; |
||||
|
import android.os.Bundle; |
||||
|
import android.view.View; |
||||
|
import android.widget.RemoteViews; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
|
||||
|
import gr.thmmy.mthmmy.R; |
||||
|
import gr.thmmy.mthmmy.activities.board.BoardActivity; |
||||
|
import gr.thmmy.mthmmy.activities.topic.TopicActivity; |
||||
|
import gr.thmmy.mthmmy.model.Bookmark; |
||||
|
|
||||
|
import static gr.thmmy.mthmmy.activities.board.BoardActivity.BUNDLE_BOARD_TITLE; |
||||
|
import static gr.thmmy.mthmmy.activities.board.BoardActivity.BUNDLE_BOARD_URL; |
||||
|
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_TITLE; |
||||
|
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL; |
||||
|
import static gr.thmmy.mthmmy.session.SessionManager.boardUrl; |
||||
|
import static gr.thmmy.mthmmy.session.SessionManager.topicUrl; |
||||
|
|
||||
|
public class WidgetProvider extends AppWidgetProvider { |
||||
|
public static final String BOOKMARK_WIDGET_SHARED_PREFS = "bookmarkWidgetSharedPrefs"; |
||||
|
public static final String BOOKMARK_WIDGETS_KEY = "bookmarkWidgetsKey"; |
||||
|
|
||||
|
enum Type {TOPIC, BOARD} |
||||
|
|
||||
|
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { |
||||
|
// Perform this loop procedure for each App Widget that belongs to this provider
|
||||
|
for (int appWidgetId : appWidgetIds) { |
||||
|
updateAppWidget(context, appWidgetManager, appWidgetId, 0); // Todo: check if there are already notifications available
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void onDeleted(Context context, int[] appWidgetIds) { |
||||
|
SharedPreferences widgetSharedPrefs = context.getSharedPreferences(BOOKMARK_WIDGET_SHARED_PREFS, Context.MODE_PRIVATE); |
||||
|
SharedPreferences.Editor widgetSharedPrefsEditor = widgetSharedPrefs.edit(); |
||||
|
|
||||
|
for (int appWidgetId : appWidgetIds) { |
||||
|
widgetSharedPrefsEditor.remove(BOOKMARK_WIDGETS_KEY + "_t_" + appWidgetId).remove(BOOKMARK_WIDGETS_KEY + "_b_" + appWidgetId); |
||||
|
} |
||||
|
widgetSharedPrefsEditor.apply(); |
||||
|
} |
||||
|
|
||||
|
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, int notifications) { |
||||
|
SharedPreferences widgetSharedPrefs = context.getSharedPreferences(BOOKMARK_WIDGET_SHARED_PREFS, Context.MODE_PRIVATE); |
||||
|
ArrayList<Bookmark> tmpArrayList; |
||||
|
|
||||
|
// Gets the bookmark saved in shared prefs
|
||||
|
String tmpBookmarkString = widgetSharedPrefs.getString(BOOKMARK_WIDGETS_KEY + "_t_" + appWidgetId, null); |
||||
|
Type type; |
||||
|
|
||||
|
if (tmpBookmarkString != null) { |
||||
|
// It's a topic bookmark
|
||||
|
tmpArrayList = Bookmark.stringToArrayList(tmpBookmarkString); |
||||
|
type = Type.TOPIC; |
||||
|
} else { |
||||
|
tmpBookmarkString = widgetSharedPrefs.getString(BOOKMARK_WIDGETS_KEY + "_b_" + appWidgetId, null); |
||||
|
|
||||
|
if (tmpBookmarkString != null) { |
||||
|
// It's a board bookmark
|
||||
|
tmpArrayList = Bookmark.stringToArrayList(tmpBookmarkString); |
||||
|
type = Type.BOARD; |
||||
|
} else { |
||||
|
// Error? TODO: Log on Timber
|
||||
|
return; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Creates an Intent to launch TopicActivity
|
||||
|
Intent intent; |
||||
|
Bundle extras = new Bundle(); |
||||
|
if (type == Type.TOPIC) { |
||||
|
intent = new Intent(context, TopicActivity.class); |
||||
|
extras.putString(BUNDLE_TOPIC_URL, topicUrl + tmpArrayList.get(0).getId() + "." + 2147483647); |
||||
|
extras.putString(BUNDLE_TOPIC_TITLE, tmpArrayList.get(0).getTitle()); |
||||
|
} else { |
||||
|
intent = new Intent(context, BoardActivity.class); |
||||
|
extras.putString(BUNDLE_BOARD_URL, boardUrl + tmpArrayList.get(0).getId() + ".0"); |
||||
|
extras.putString(BUNDLE_BOARD_TITLE, tmpArrayList.get(0).getTitle()); |
||||
|
} |
||||
|
intent.putExtras(extras); |
||||
|
PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT); |
||||
|
|
||||
|
// Gets the layout for the Topic Widget and attach an on-click listener to the button
|
||||
|
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget); |
||||
|
views.setOnClickPendingIntent(R.id.widget_button, pendingIntent); |
||||
|
if (notifications > 0) { |
||||
|
views.setViewVisibility(R.id.widget_notifications_number, View.VISIBLE); |
||||
|
views.setTextViewText(R.id.widget_notifications_number, "" + notifications); |
||||
|
} else { |
||||
|
views.setViewVisibility(R.id.widget_notifications_number, View.GONE); |
||||
|
} |
||||
|
|
||||
|
// Tells the AppWidgetManager to perform an update on the current app widget
|
||||
|
appWidgetManager.updateAppWidget(appWidgetId, views); |
||||
|
} |
||||
|
} |
@ -0,0 +1,38 @@ |
|||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||
|
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
|
xmlns:tools="http://schemas.android.com/tools" |
||||
|
android:layout_width="wrap_content" |
||||
|
android:layout_height="wrap_content" |
||||
|
android:paddingLeft="0dp" |
||||
|
android:paddingTop="0dp" |
||||
|
android:paddingRight="0dp" |
||||
|
android:paddingBottom="0dp"> |
||||
|
|
||||
|
<ImageView |
||||
|
android:id="@+id/widget_button" |
||||
|
android:layout_width="wrap_content" |
||||
|
android:layout_height="wrap_content" |
||||
|
android:contentDescription="@string/widget_button" |
||||
|
android:paddingLeft="0dp" |
||||
|
android:paddingTop="0dp" |
||||
|
android:paddingRight="0dp" |
||||
|
android:paddingBottom="0dp" |
||||
|
android:scaleType="fitCenter" |
||||
|
android:src="@mipmap/ic_launcher" /> |
||||
|
|
||||
|
<TextView |
||||
|
android:id="@+id/widget_notifications_number" |
||||
|
android:layout_width="wrap_content" |
||||
|
android:layout_height="wrap_content" |
||||
|
android:layout_gravity="end|bottom" |
||||
|
android:background="@color/accent" |
||||
|
android:paddingLeft="5dp" |
||||
|
android:paddingTop="0.5dp" |
||||
|
android:paddingRight="5dp" |
||||
|
android:paddingBottom="0.5dp" |
||||
|
android:textColor="#ffffff" |
||||
|
android:textSize="12dp" |
||||
|
android:visibility="gone" |
||||
|
tools:ignore="SpUsage" /> |
||||
|
|
||||
|
</FrameLayout> |
@ -0,0 +1,10 @@ |
|||||
|
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" |
||||
|
android:minWidth="40dp" |
||||
|
android:minHeight="40dp" |
||||
|
android:updatePeriodMillis="86400000" |
||||
|
android:previewImage="@drawable/ic_bookmark_true_accent_24dp" |
||||
|
android:initialLayout="@layout/widget" |
||||
|
android:configure="gr.thmmy.mthmmy.activities.bookmarks.BookmarksActivity" |
||||
|
android:resizeMode="none" |
||||
|
android:widgetCategory="home_screen"> |
||||
|
</appwidget-provider> |
Loading…
Reference in new issue