Browse files

v1.4 added 24 hour, added about page, started with xmlParser

  • Loading branch information...
1 parent 02978de commit 347893a3c38fd32aed71f9a12ae5aaf268929cc7 Carles Sentis committed Jul 22, 2012
View
8 AndroidManifest.xml
@@ -1,7 +1,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.codeskraps.lolo"
- android:versionCode="4"
- android:versionName="1.3" >
+ android:versionCode="5"
+ android:versionName="1.4" >
+
<!-- Remember to update the info code version -->
<uses-sdk
@@ -25,6 +26,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
+ <activity
+ android:name=".AboutActivity"
+ android:theme="@style/Theme.Translucent" />
<receiver android:name=".LoloProvider" >
<intent-filter>
View
6 README.rdoc
@@ -4,7 +4,7 @@
* <b>Git:</b> https://github.com/091labs/lo-lo
* <b>Author:</b> 091 Labs members
* <b>License:</b> GNU GPL v3
-* <b>Latest Version:</b> 1.3
+* <b>Latest Version:</b> 1.4
* <b>Release Date:</b> 2012
== General Infromation
@@ -32,6 +32,10 @@ Install from Google Play
== ChangeLog
+<b>v1.4</b>
+* New super awesome about page
+* Added show 24-hour setting
+
<b>v1.3</b>
* Added the option to enable/disable last sync display
* Added a progress display while synchronizing
View
91 res/layout/about.xml
@@ -0,0 +1,91 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:id="@+id/layout_debate_root"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:orientation="vertical"
+ android:paddingLeft="10dp"
+ android:paddingRight="10dp" >
+
+ <LinearLayout
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:layout_weight="1"
+ android:gravity="center_vertical"
+ android:orientation="vertical" >
+
+ <LinearLayout
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:orientation="horizontal" >
+
+ <ImageView
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_margin="5dp"
+ android:background="@drawable/ic_launcher"
+ android:contentDescription="@string/iconImageDescription" />
+
+ <TextView
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginTop="15dp"
+ android:text="@string/app_name"
+ android:textColor="#AAAAAA"
+ android:textSize="25dp"
+ android:typeface="normal" />
+ </LinearLayout>
+
+ <LinearLayout
+ android:layout_width="match_parent"
+ android:layout_height="1dp"
+ android:background="#FF0000"
+ android:orientation="horizontal" />
+
+ <TextView
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginBottom="10dp"
+ android:layout_marginTop="10dp"
+ android:text="@string/about_summary"
+ android:textColor="#FFF"
+ android:textSize="23sp" />
+
+ <ScrollView
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:orientation="vertical" >
+
+ <LinearLayout
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:orientation="vertical" >
+
+ <TextView
+ android:id="@+id/txtContent"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:textColor="#FFF"
+ android:textSize="18sp" />
+ </LinearLayout>
+ </ScrollView>
+ </LinearLayout>
+
+ <LinearLayout
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:orientation="vertical" >
+
+ <TextView
+ android:id="@+id/content_ok"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:layout_weight="1"
+ android:gravity="center"
+ android:padding="10dp"
+ android:text="OK"
+ android:textColor="#AAA"
+ android:textSize="25sp" />
+ </LinearLayout>
+
+</LinearLayout>
View
23 res/layout/widget.xml
@@ -9,16 +9,26 @@
android:id="@+id/imgLolo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal|center"
+ android:layout_centerHorizontal="true"
+ android:layout_centerVertical="true"
android:contentDescription="@string/contentDescription"
android:padding="15dip"
android:src="@drawable/closed" />
+ <ProgressBar
+ android:id="@+id/prgBar"
+ style="@android:style/Widget.ProgressBar.Small"
+ android:layout_width="20dp"
+ android:layout_height="20dp"
+ android:layout_centerHorizontal="true"
+ android:layout_centerVertical="true"
+ android:visibility="gone" />
+
<TextView
android:id="@+id/txtSync"
android:layout_width="match_parent"
android:layout_height="wrap_content"
- android:layout_alignBottom="@id/imgLolo"
+ android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:shadowColor="#000000"
android:shadowDx="1"
@@ -27,13 +37,4 @@
android:textColor="#FFFFFF"
android:typeface="monospace" />
- <ProgressBar
- android:id="@+id/prgBar"
- style="@android:style/Widget.ProgressBar.Small"
- android:layout_width="20dp"
- android:layout_height="20dp"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- android:visibility="gone" />
-
</RelativeLayout>
View
17 res/values/strings.xml
@@ -7,6 +7,11 @@
<!-- Preference Activity -->
<string name="prefsCat_General">General</string>
+ <string name="prefsSync_title">Show last sync time</string>
+ <string name="prefsSync_summarNot">Not yet synced!</string>
+ <string name="prefs24_title">Use 24-hour format</string>
+ <string name="prefs24_summaryOne">01:00 pm</string>
+ <string name="prefs24_summaryTwo">13:00</string>
<string name="prefsOnClick_title">On touch action</string>
<string name="prefsOnClick_dialogTitle">On touch action</string>
<string name="prefsOnClick_summary">Choose what should be done when you touch the widget -</string>
@@ -23,16 +28,16 @@
<item>2</item>
<item>3</item>
</string-array>
-
+
<string name="prefsURL_title">URL</string>
<string name="prefsURL_summary">Set a Custom URL -</string>
<string name="prefsURL_default">091labs.com</string>
-
- <string name="prefsSync_title">Show last sync time</string>
- <string name="prefsSync_summarNot">Not yet synced!</string>
-
<string name="prefsCat_Info">Information</string>
- <string name="prefsInfo_Title">091 Labs lo-lo v1.3</string>
+ <string name="prefsInfo_Title">091 Labs lo-lo v1.4</string>
<string name="prefsInfo_summary">GNU GPL License v3 - 2012</string>
+ <!-- About -->
+ <string name="about_title">091 Labs lo-lo</string>
+ <string name="about_summary">About</string>
+
</resources>
View
8 res/values/styles.xml
@@ -2,4 +2,12 @@
<style name="AppTheme" parent="android:Theme.Light" />
+ <drawable name="transparent_background">#DD000000</drawable>
+
+ <style name="Theme.Translucent" parent="android:style/Theme.Translucent">
+ <item name="android:windowBackground">@drawable/transparent_background</item>
+ <item name="android:windowNoTitle">true</item>
+ <item name="android:colorForeground">#fff</item>
+ </style>
+
</resources>
View
2 res/xml/loloinfo.xml
@@ -3,6 +3,6 @@
android:initialLayout="@layout/widget"
android:minHeight="72dip"
android:minWidth="72dip"
- android:updatePeriodMillis="90000"
+ android:updatePeriodMillis="180000"
android:configure="com.codeskraps.lolo.PrefsActivity"
android:previewImage="@drawable/open" />
View
5 res/xml/preferences.xml
@@ -6,6 +6,10 @@
android:defaultValue="true"
android:key="chkSync"
android:title="@string/prefsSync_title" />
+ <CheckBoxPreference
+ android:defaultValue="true"
+ android:key="chk24"
+ android:title="@string/prefs24_title" />
<ListPreference
android:defaultValue="0"
@@ -22,6 +26,7 @@
</PreferenceCategory>
<PreferenceCategory android:title="@string/prefsCat_Info" >
<Preference
+ android:key="prefAbout"
android:summary="@string/prefsInfo_summary"
android:title="@string/prefsInfo_Title" />
</PreferenceCategory>
View
42 src/com/codeskraps/lolo/AboutActivity.java
@@ -0,0 +1,42 @@
+package com.codeskraps.lolo;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.text.Html;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.TextView;
+
+public class AboutActivity extends Activity implements OnClickListener {
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ setContentView(R.layout.about);
+
+ TextView txtOk = (TextView) findViewById(R.id.content_ok);
+ TextView txtContent = (TextView) findViewById(R.id.txtContent);
+
+ txtOk.setOnClickListener(this);
+
+ StringBuilder sb = new StringBuilder();
+ sb.append("091 Labs is a collaborative community space based in Galway City, Ireland. It is a shared physical space for any and all creative projects: art, woodwork, software, photography and electronics – to name but a few. Our aim is to provide Galway with a place for people to work and collaborate on creative projects, to learn and to share their knowledge. We welcome all skill levels and all creative ideas.<br /><br />");
+ sb.append("<h2><font color='red'>lo-lo</font></h2>");
+ sb.append("This is a way for members and the general public when the Labs on or labs off.<br /><br />");
+ sb.append("<font color='#AAAAAA'>Website:</font> 091labs.com<br />");
+ sb.append("<font color='#AAAAAA'>Email:</font> info@091labs.com<br />");
+ String version = getString(R.string.prefsInfo_Title);
+ sb.append(String.format("<font color='#AAAAAA'>Version:</font> %s<br />", version));
+ sb.append("<font color='#AAAAAA'>License:</font> GNU GPL v3<br />");
+ sb.append("<font color='#AAAAAA'>Code:</font> https://github.com/091labs/lo-lo<br />");
+
+ txtContent.setText(Html.fromHtml(sb.toString()));
+
+ }
+
+ @Override
+ public void onClick(View v) {
+ this.finish();
+ }
+}
View
17 src/com/codeskraps/lolo/LoloProvider.java
@@ -21,6 +21,9 @@
package com.codeskraps.lolo;
+import java.util.Calendar;
+
+import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
@@ -39,14 +42,16 @@
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
- Log.d(TAG, "onUpdate");
+ if (BuildConfig.DEBUG == true)
+ Log.d(TAG, "onUpdate");
updateWidget(context);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onReceive(Context context, Intent intent) {
- Log.d(TAG, "onReceive");
+ if (BuildConfig.DEBUG == true)
+ Log.d(TAG, "onReceive");
final String action = intent.getAction();
if (action.equals(FORCE_WIDGET_UPDATE))
@@ -56,15 +61,19 @@ public void onReceive(Context context, Intent intent) {
}
public void updateWidget(Context context) {
- Log.d(TAG, "updateWidget");
+ if (BuildConfig.DEBUG == true)
+ Log.d(TAG, "updateWidget");
+
ComponentName thisWidget = new ComponentName(context, LoloProvider.class);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
updateWidget(context, appWidgetManager, appWidgetIds);
}
private void updateWidget(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
- Log.d(TAG, "updateWidget2");
+ if (BuildConfig.DEBUG == true)
+ Log.d(TAG, "updateWidget2");
+
final int N = appWidgetIds.length;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
View
64 src/com/codeskraps/lolo/PrefsActivity.java
@@ -29,6 +29,8 @@
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
+import android.preference.Preference;
+import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;
@@ -38,26 +40,30 @@
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
-public class PrefsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
+public class PrefsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener, OnPreferenceClickListener {
private static final String TAG = PrefsActivity.class.getSimpleName();
private static String CONFIGURE_ACTION = "android.appwidget.action.APPWIDGET_CONFIGURE";
public static final String FORCE_WIDGET_UPDATE = "com.codeskraps.lolo.FORCE_WIDGET_UPDATE";
public static final String ONCLICK = "lstOnClick";
public static final String EURL = "eURL";
public static final String LAST_SYNC = "sync";
public static final String SHOW_SYNC = "chkSync";
+ public static final String HOUR24 = "chk24";
+ public static final String ABOUT = "prefAbout";
private SharedPreferences prefs = null;
private ListPreference lstOnClick = null;
private EditTextPreference eURL = null;
private CheckBoxPreference chkSync = null;
+ private CheckBoxPreference chk24 = null;
private String[] entries_OnClick = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
- Log.d(TAG, "onCreate");
+ if (BuildConfig.DEBUG == true)
+ Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
-
+
addPreferencesFromResource(R.xml.preferences);
setContentView(R.layout.prefs);
@@ -69,17 +75,27 @@ protected void onCreate(Bundle savedInstanceState) {
lstOnClick = (ListPreference) findPreference(ONCLICK);
eURL = (EditTextPreference) findPreference(EURL);
chkSync = (CheckBoxPreference) findPreference(SHOW_SYNC);
+ chk24 = (CheckBoxPreference) findPreference(HOUR24);
+ Preference prefAbout = (Preference) findPreference(ABOUT);
+ prefAbout.setOnPreferenceClickListener(this);
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+
String lstSync = prefs.getString(LAST_SYNC, null);
if (lstSync == null)
chkSync.setSummary(getString(R.string.prefsSync_summarNot));
else
chkSync.setSummary(lstSync);
- }
- @Override
- protected void onResume() {
- super.onResume();
+ boolean hour24 = prefs.getBoolean(HOUR24, true);
+ if (hour24)
+ chk24.setSummary(getString(R.string.prefs24_summaryTwo));
+ else
+ chk24.setSummary(getString(R.string.prefs24_summaryOne));
String onClick = prefs.getString(ONCLICK, entries_OnClick[0]);
int action = Integer.parseInt(onClick);
@@ -95,15 +111,25 @@ protected void onResume() {
@Override
protected void onPause() {
- Log.d(TAG, "onPause");
+ if (BuildConfig.DEBUG == true)
+ Log.d(TAG, "onPause");
super.onPause();
prefs.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
- Log.d(TAG, "onSharedPreferenceChanged");
- if (key.equals(ONCLICK)) {
+ if (BuildConfig.DEBUG == true)
+ Log.d(TAG, "onSharedPreferenceChanged");
+
+ if (key.equals(HOUR24)) {
+ boolean hour24 = prefs.getBoolean(HOUR24, true);
+ if (hour24)
+ chk24.setSummary(getString(R.string.prefs24_summaryTwo));
+ else
+ chk24.setSummary(getString(R.string.prefs24_summaryOne));
+
+ } else if (key.equals(ONCLICK)) {
String onClick = prefs.getString(ONCLICK, entries_OnClick[0]);
int action = Integer.parseInt(onClick);
lstOnClick.setSummary(entries_OnClick[action]);
@@ -121,7 +147,9 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
- Log.d(TAG, "onKeyDown");
+ if (BuildConfig.DEBUG == true)
+ Log.d(TAG, "onKeyDown");
+
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR
&& keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// Take care of calling this method on earlier versions of
@@ -138,14 +166,16 @@ public void onBackPressed() {
// This will be called either automatically for you on 2.0
// or later, or by the code above on earlier versions of the
// platform.
- Log.d(TAG, "onBackPressed");
+ if (BuildConfig.DEBUG == true)
+ Log.d(TAG, "onBackPressed");
resultIntent();
finish();
return;
}
private void resultIntent() {
- Log.d(TAG, "resultIntent");
+ if (BuildConfig.DEBUG == true)
+ Log.d(TAG, "resultIntent");
if (CONFIGURE_ACTION.equals(getIntent().getAction())) {
Intent intent = getIntent();
@@ -161,4 +191,12 @@ private void resultIntent() {
}
sendBroadcast(new Intent(FORCE_WIDGET_UPDATE));
}
+
+ @Override
+ public boolean onPreferenceClick(Preference pref) {
+ if (pref.getKey().equals(ABOUT)) {
+ startActivity(new Intent(this, AboutActivity.class));
+ }
+ return false;
+ }
}
View
149 src/com/codeskraps/lolo/RSSXmlParser.java
@@ -0,0 +1,149 @@
+package com.codeskraps.lolo;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
+
+import android.util.Xml;
+
+public class RSSXmlParser {
+
+ private static final String ns = null;
+
+ public List<Entry> parse(InputStream in) throws XmlPullParserException, IOException {
+ try {
+ XmlPullParser parser = Xml.newPullParser();
+ parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
+ parser.setInput(in, null);
+ parser.nextTag();
+ return readFeed(parser);
+ } finally {
+ in.close();
+ }
+ }
+
+ private List<Entry> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
+ List<Entry> entries = new ArrayList<Entry>();
+
+ parser.require(XmlPullParser.START_TAG, ns, "feed");
+ while (parser.next() != XmlPullParser.END_TAG) {
+ if (parser.getEventType() != XmlPullParser.START_TAG) {
+ continue;
+ }
+ String name = parser.getName();
+ // Starts by looking for the entry tag
+ if (name.equals("entry")) {
+ entries.add(readEntry(parser));
+ } else {
+ skip(parser);
+ }
+ }
+ return entries;
+ }
+
+ // This class represents a single entry (post) in the XML feed.
+ // It includes the data members "title," "link," and "summary."
+ public static class Entry {
+ public final String title;
+ public final String link;
+ public final String summary;
+
+ private Entry(String title, String summary, String link) {
+ this.title = title;
+ this.summary = summary;
+ this.link = link;
+ }
+ }
+
+ // Parses the contents of an entry. If it encounters a title, summary, or link tag, hands them
+ // off
+ // to their respective &quot;read&quot; methods for processing. Otherwise, skips the tag.
+ private Entry readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
+ parser.require(XmlPullParser.START_TAG, ns, "entry");
+ String title = null;
+ String summary = null;
+ String link = null;
+ while (parser.next() != XmlPullParser.END_TAG) {
+ if (parser.getEventType() != XmlPullParser.START_TAG) {
+ continue;
+ }
+ String name = parser.getName();
+ if (name.equals("title")) {
+ title = readTitle(parser);
+ } else if (name.equals("summary")) {
+ summary = readSummary(parser);
+ } else if (name.equals("link")) {
+ link = readLink(parser);
+ } else {
+ skip(parser);
+ }
+ }
+ return new Entry(title, summary, link);
+ }
+
+ // Processes title tags in the feed.
+ private String readTitle(XmlPullParser parser) throws IOException, XmlPullParserException {
+ parser.require(XmlPullParser.START_TAG, ns, "title");
+ String title = readText(parser);
+ parser.require(XmlPullParser.END_TAG, ns, "title");
+ return title;
+ }
+
+ // Processes link tags in the feed.
+ private String readLink(XmlPullParser parser) throws IOException, XmlPullParserException {
+ String link = "";
+ parser.require(XmlPullParser.START_TAG, ns, "link");
+ String tag = parser.getName();
+ String relType = parser.getAttributeValue(null, "rel");
+ if (tag.equals("link")) {
+ if (relType.equals("alternate")) {
+ link = parser.getAttributeValue(null, "href");
+ parser.nextTag();
+ }
+ }
+ parser.require(XmlPullParser.END_TAG, ns, "link");
+ return link;
+ }
+
+ // Processes summary tags in the feed.
+ private String readSummary(XmlPullParser parser) throws IOException, XmlPullParserException {
+ parser.require(XmlPullParser.START_TAG, ns, "summary");
+ String summary = readText(parser);
+ parser.require(XmlPullParser.END_TAG, ns, "summary");
+ return summary;
+ }
+
+ // For the tags title and summary, extracts their text values.
+ private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
+ String result = "";
+ if (parser.next() == XmlPullParser.TEXT) {
+ result = parser.getText();
+ parser.nextTag();
+ }
+ return result;
+ }
+
+ // Skips tags the parser isn't interested in. Uses depth to handle nested tags. i.e.,
+ // if the next tag after a START_TAG isn't a matching END_TAG, it keeps going until it
+ // finds the matching END_TAG (as indicated by the value of "depth" being 0).
+ private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
+ if (parser.getEventType() != XmlPullParser.START_TAG) {
+ throw new IllegalStateException();
+ }
+ int depth = 1;
+ while (depth != 0) {
+ switch (parser.next()) {
+ case XmlPullParser.END_TAG:
+ depth--;
+ break;
+ case XmlPullParser.START_TAG:
+ depth++;
+ break;
+ }
+ }
+ }
+}
View
108 src/com/codeskraps/lolo/UpdateWidgetService.java
@@ -22,14 +22,24 @@
package com.codeskraps.lolo;
import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
import java.text.DateFormat;
+import java.text.SimpleDateFormat;
import java.util.Calendar;
+import java.util.List;
+
+import org.xmlpull.v1.XmlPullParserException;
+
+import com.codeskraps.lolo.RSSXmlParser.Entry;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
+import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import android.preference.PreferenceManager;
@@ -53,10 +63,12 @@ public IBinder onBind(Intent intent) {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
- Log.d(TAG, "onStartCommand");
+ if (BuildConfig.DEBUG == true)
+ Log.d(TAG, "onStartCommand");
- UpdateWidgetService.context = getApplicationContext();
UpdateWidgetService.intent = intent;
+ UpdateWidgetService.context = getApplicationContext();
+
handler = new Handler();
if (Utils.isNetworkAvailable(getApplicationContext())) {
@@ -78,6 +90,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {
downloadThread = new MyThread();
downloadThread.start();
+
+// new DownloadXmlTask().execute();
} else {
Log.d(TAG, "No network connection");
}
@@ -87,7 +101,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
- static public class MyThread extends Thread {
+ static private class MyThread extends Thread {
@Override
public void run() {
try {
@@ -121,19 +135,29 @@ public void run() {
}
Calendar c = Calendar.getInstance();
-
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
+
+ SharedPreferences prefs = PreferenceManager
+ .getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(PrefsActivity.LAST_SYNC, DateFormat.getDateTimeInstance()
.format(c.getTime()));
editor.commit();
boolean showSync = prefs.getBoolean(PrefsActivity.SHOW_SYNC, true);
if (showSync) {
- String hours = new String("");
- if (c.get(Calendar.HOUR_OF_DAY) < 10)
- hours += "0";
- hours += c.get(Calendar.HOUR_OF_DAY);
+ boolean hour24 = prefs.getBoolean(PrefsActivity.HOUR24, true);
+ String hours = new String();
+ if (hour24) {
+ hours = new String("");
+ if (c.get(Calendar.HOUR_OF_DAY) < 10)
+ hours += "0";
+ hours += c.get(Calendar.HOUR_OF_DAY);
+ } else {
+ hours = new String("");
+ if (c.get(Calendar.HOUR) < 10)
+ hours += "0";
+ hours += c.get(Calendar.HOUR);
+ }
String minutes = new String("");
if (c.get(Calendar.MINUTE) < 10)
@@ -147,14 +171,76 @@ public void run() {
} else {
remoteViews.setViewVisibility(R.id.txtSync, View.GONE);
}
-
+
remoteViews.setViewVisibility(R.id.prgBar, View.GONE);
-
+
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
} else {
Log.d(TAG, "No widgets installed");
}
}
}
+
+ // Implementation of AsyncTask used to download XML feed from
+ // stackoverflow.com.
+ private class DownloadXmlTask extends AsyncTask<String, Void, String> {
+
+ @Override
+ protected String doInBackground(String... urls) {
+ try {
+ return loadXmlFromNetwork("http://stackoverflow.com/feeds/tag?tagnames=android&sort=newest");
+ } catch (IOException e) {
+ return e.getMessage();
+ } catch (XmlPullParserException e) {
+ return e.getMessage();
+ }
+ }
+
+ @Override
+ protected void onPostExecute(String result) {
+ Log.d(TAG, result);
+ }
+ }
+
+ // Uploads XML from stackoverflow.com, parses it, and combines it with
+ // HTML markup. Returns HTML string.
+ private String loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException {
+ InputStream stream = null;
+ RSSXmlParser rssXmlParser = new RSSXmlParser();
+ List<Entry> entries = null;
+
+ try {
+ stream = downloadUrl(urlString);
+ entries = rssXmlParser.parse(stream);
+ // Makes sure that the InputStream is closed after the app is
+ // finished using it.
+ } finally {
+ if (stream != null) {
+ stream.close();
+ }
+ }
+
+ // Each Entry object represents a single post in the XML feed.
+ // This section processes the entries list to combine each entry with
+ // HTML markup.
+ // Each entry is displayed in the UI as a link that optionally includes
+ // a text summary.
+ return entries.get(0).title;
+ }
+
+ // Given a string representation of a URL, sets up a connection and gets
+ // an input stream.
+ private InputStream downloadUrl(String urlString) throws IOException {
+ URL url = new URL(urlString);
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ conn.setReadTimeout(10000 /* milliseconds */);
+ conn.setConnectTimeout(15000 /* milliseconds */);
+ conn.setRequestMethod("GET");
+ conn.setDoInput(true);
+ // Starts the query
+ conn.connect();
+ InputStream stream = conn.getInputStream();
+ return stream;
+ }
}
View
27 src/com/codeskraps/lolo/Utils.java
@@ -23,7 +23,10 @@
import java.io.BufferedReader;
import java.io.IOException;
+import java.io.InputStream;
import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
@@ -32,11 +35,14 @@
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
+import android.util.Xml;
public class Utils {
private static final String TAG = Utils.class.getSimpleName();
@@ -51,7 +57,7 @@ public static boolean isNetworkAvailable(Context context) {
}
return false;
}
-
+
public static boolean getLolo() throws IOException {
long startTime = System.currentTimeMillis();
Log.d(TAG, "download begining");
@@ -64,24 +70,21 @@ public static boolean getLolo() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity()
.getContent(), "UTF-8"));
String json = reader.readLine();
-// Log.d(TAG, "json: " + json);
+ reader.close();
+
JSONTokener tokener = new JSONTokener(json);
- String lolo = null;
+ boolean lolo = false;
try {
JSONObject finalResult = new JSONObject(tokener);
- lolo = finalResult.getString("open");
- Log.d(TAG, "lolo: " + lolo);
+ lolo = Boolean.getBoolean(finalResult.getString("open"));
+ // Log.d(TAG, "lolo: " + lolo);
} catch (JSONException e) {
Log.e(TAG, e.getMessage());
}
- Log.d(TAG, "download ready in "
- + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
-
- if (lolo.equals("true")) {
- return true;
- } else
- return false;
+ Log.d(TAG, "download ready in " + ((System.currentTimeMillis() - startTime) / 1000)
+ + " sec");
+ return lolo;
}
}

0 comments on commit 347893a

Please sign in to comment.