Added Basic Pic Of the Day App Widget (#1523)

* Added Basic Pic Of the Day App Widget

* Added Java Lib for XML to JSON

* Added missing json library from xml to json

* Undone formatting

* Consolidate the networking libraries - drop volley in favor of OkHttp

* Extracted a few networking related items into a new Dagger module and finished the process of mocking the main component for tests.

* Refactoring to extract GpsCategoryModel and ensure single-responsibility-principle is maintained in CategoryApi.

* Updated PicOfDayAppWidget class to parse HTML

* fixed featured image back bug

* Localisation updates from https://translatewiki.net.

* Javadocs added

* Add option to set image as wallpaper (#1535)

* Add option to set image as wallpaper

* Added java docs

* Toast message on setting the wallpaper successfully

* Localisation updates from https://translatewiki.net.

* Add dependencies to com.android.support.test.rules and runner

Needed for ActivityTestRule used in SettingsActivityTest

* Added Basic Pic Of the Day App Widget

* Added Java Lib for XML to JSON

* Added missing json library from xml to json

* Undone formatting

* Updated PicOfDayAppWidget class to parse HTML
This commit is contained in:
Madhur Gupta 2018-06-14 14:40:52 +05:30 committed by Josephine Lim
parent a0fe8b0745
commit 2cfbb67f1c
10 changed files with 192 additions and 53 deletions

View file

@ -0,0 +1,80 @@
package fr.free.nrw.commons.widget;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.widget.RemoteViews;
import com.prof.rssparser.Article;
import com.prof.rssparser.Parser;
import com.squareup.picasso.Picasso;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.ArrayList;
import fr.free.nrw.commons.BuildConfig;
import fr.free.nrw.commons.R;
/**
* Implementation of App Widget functionality.
*/
public class PicOfDayAppWidget extends AppWidgetProvider {
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.pic_of_day_app_widget);
String urlString = BuildConfig.WIKIMEDIA_API_POTD;
Parser parser = new Parser();
parser.execute(urlString);
parser.onFinish(new Parser.OnTaskCompleted() {
@Override
public void onTaskCompleted(ArrayList<Article> list) {
String desc = list.get(list.size() - 1).getDescription();
if (desc != null) {
Document document = Jsoup.parse(desc);
Elements elements = document.select("img");
String imageUrl = elements.get(0).attr("src");
if (imageUrl != null && imageUrl.length() > 0) {
Picasso.get().load(imageUrl).into(views, R.id.appwidget_image, new int[]{appWidgetId});
}
}
}
@Override
public void onError() {
}
});
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
}