Samples
Samples
Wiktionary
Language: Oxygene, Platform: Cooper, Category: Android
https://github.com/remobjects/ElementsSamples/tree/master/Oxygene/Cooper/Android/Wiktionary
-
com.example.android.simplewiktionary
-
References
- android
- Source Files
-
Other Files
- res\values\strings.android-xml
- Properties\AndroidManifest.android-xml
- NOTICE
- Readme.txt
- res\anim\slide_in.android-xml
- res\anim\slide_out.android-xml
- res\drawable\app_icon.png
- res\drawable\ic_menu_shuffle.png
- res\drawable\logo_overlay.9.png
- res\drawable\lookup_bg.android-xml
- res\drawable\progress_spin.android-xml
- res\drawable\star_logo.png
- res\drawable\widget_bg.android-xml
- res\drawable\widget_bg_normal.9.png
- res\drawable\widget_bg_pressed.9.png
- res\drawable\widget_bg_selected.9.png
- res\layout\about.layout-xml
- res\layout\lookup.layout-xml
- res\layout\widget_message.layout-xml
- res\layout\widget_word.layout-xml
- res\menu\lookup.android-xml
- res\values\styles.android-xml
- res\values\themes.android-xml
- res\xml\searchable.android-xml
- res\xml\widget_word.android-xml
-
References
WordWidget.pas
namespace com.example.android.wiktionary;
{*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*}
interface
uses
android.app,
android.appwidget,
android.content,
android.content.res,
android.net,
android.os,
android.text.format,
android.util,
android.widget,
java.util.regex;
type
/// <summary>
/// Define a simple widget that shows the Wiktionary "Word of the day." To build
/// an update we spawn a background Service to perform the API queries.
/// </summary>
WordWidget = public class(AppWidgetProvider)
public
method onUpdate(aContext: Context; appWidgetManager: AppWidgetManager; appWidgetIds: array of Integer); override;
end;
UpdateService nested in WordWidget = public class(Service)
private
//Regular expression that splits "Word of the day" entry into word
//name, word type, and the first description bullet point.
const WOTD_PATTERN = '(?s)\{\{wotd\|(.+?)\|(.+?)\|([^#\|]+).*?\}\}';
public
method onStart(aIntent: Intent; startId: Integer); override;
method buildUpdate(aContext: Context): RemoteViews;
method onBind(aIntent: Intent): IBinder; override;
end;
implementation
method WordWidget.onUpdate(aContext: Context; appWidgetManager: AppWidgetManager; appWidgetIds: array of Integer);
begin
// To prevent any ANR timeouts, we perform the update in a service
aContext.startService(new Intent(aContext, typeOf(UpdateService)))
end;
method WordWidget.UpdateService.onStart(aIntent: Intent; startId: Integer);
begin
// Build the widget update for today
var updateViews: RemoteViews := buildUpdate(self);
// Push update for this widget to the home screen
var thisWidget: ComponentName := new ComponentName(self, typeOf(WordWidget));
var manager: AppWidgetManager := AppWidgetManager.getInstance(self);
manager.updateAppWidget(thisWidget, updateViews)
end;
/// <summary>
/// Build a widget update to show the current Wiktionary
/// "Word of the day." Will block until the online API returns.
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
method WordWidget.UpdateService.buildUpdate(aContext: Context): RemoteViews;
begin
// Pick out month names from resources
var res: Resources := aContext.Resources;
var monthNames: array of String := res.StringArray[R.&array.month_names];
// Find current month and day
var today: Time := new Time();
today.setToNow;
// Build the page title for today, such as "March 21"
var pageName: String := res.getString(R.string.template_wotd_title, monthNames[today.month], today.monthDay);
var pageContent: String := nil;
try
// Try querying the Wiktionary API for today's word
pageContent := SimpleWikiHelper.getPageContent(pageName, false);
except
on e: SimpleWikiHelper.ApiException do
Log.e('WordWidget', 'Couldn''t contact API', e);
on e: ParseException do
Log.e('WordWidget', 'Couldn''t parse API response', e)
end;
var views: RemoteViews := nil;
// Use a regular expression to parse out the word and its definition
var matcher: Matcher := Pattern.compile(WOTD_PATTERN).matcher(pageContent);
if matcher.find then
begin
// Build an update that holds the updated widget contents
views := new RemoteViews(aContext.PackageName, R.layout.widget_word);
var wordTitle: String := matcher.&group(1);
views.setTextViewText(R.id.word_title, wordTitle);
views.setTextViewText(R.id.word_type, matcher.&group(2));
views.setTextViewText(R.id.definition, matcher.&group(3).trim);
// When user clicks on widget, launch to Wiktionary definition page
var definePage: String := WideString.format('%s://%s/%s', ExtendedWikiHelper.WIKI_AUTHORITY, ExtendedWikiHelper.WIKI_LOOKUP_HOST, wordTitle);
var defineIntent: Intent := new Intent(Intent.ACTION_VIEW, Uri.parse(definePage));
var lPendingIntent: PendingIntent := PendingIntent.getActivity(aContext, 0, defineIntent, 0);
views.setOnClickPendingIntent(R.id.widget, lPendingIntent)
end
else
begin
// Didn't find word of day, so show error message
views := new RemoteViews(aContext.PackageName, R.layout.widget_message);
views.setTextViewText(R.id.message, aContext.String[R.string.widget_error])
end;
exit views
end;
method WordWidget.UpdateService.onBind(aIntent: Intent): IBinder;
begin
// We don't need to bind to this service
exit nil
end;
end.
