Methods Summary |
---|
private java.io.InputStream | downloadUrl(java.lang.String urlString)
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;
|
private void | loadPage()
if (((sPref.equals(ANY)) && (wifiConnected || mobileConnected))
|| ((sPref.equals(WIFI)) && (wifiConnected))) {
// AsyncTask subclass
new DownloadXmlTask().execute(URL);
} else {
showErrorPage();
}
|
private java.lang.String | loadXmlFromNetwork(java.lang.String urlString)
InputStream stream = null;
StackOverflowXmlParser stackOverflowXmlParser = new StackOverflowXmlParser();
List<Entry> entries = null;
String title = null;
String url = null;
String summary = null;
Calendar rightNow = Calendar.getInstance();
DateFormat formatter = new SimpleDateFormat("MMM dd h:mmaa");
// Checks whether the user set the preference to include summary text
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean pref = sharedPrefs.getBoolean("summaryPref", false);
StringBuilder htmlString = new StringBuilder();
htmlString.append("<h3>" + getResources().getString(R.string.page_title) + "</h3>");
htmlString.append("<em>" + getResources().getString(R.string.updated) + " " +
formatter.format(rightNow.getTime()) + "</em>");
try {
stream = downloadUrl(urlString);
entries = stackOverflowXmlParser.parse(stream);
// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (stream != null) {
stream.close();
}
}
// StackOverflowXmlParser returns a List (called "entries") of Entry objects.
// 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.
for (Entry entry : entries) {
htmlString.append("<p><a href='");
htmlString.append(entry.link);
htmlString.append("'>" + entry.title + "</a></p>");
// If the user set the preference to include summary text,
// adds it to the display.
if (pref) {
htmlString.append(entry.summary);
}
}
return htmlString.toString();
|
public void | onCreate(android.os.Bundle savedInstanceState)
super.onCreate(savedInstanceState);
// Register BroadcastReceiver to track connection changes.
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
receiver = new NetworkReceiver();
this.registerReceiver(receiver, filter);
|
public boolean | onCreateOptionsMenu(android.view.Menu menu)
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
|
public void | onDestroy()
super.onDestroy();
if (receiver != null) {
this.unregisterReceiver(receiver);
}
|
public boolean | onOptionsItemSelected(android.view.MenuItem item)
switch (item.getItemId()) {
case R.id.settings:
Intent settingsActivity = new Intent(getBaseContext(), SettingsActivity.class);
startActivity(settingsActivity);
return true;
case R.id.refresh:
loadPage();
return true;
default:
return super.onOptionsItemSelected(item);
}
|
public void | onStart()
super.onStart();
// Gets the user's network preference settings
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
// Retrieves a string value for the preferences. The second parameter
// is the default value to use if a preference value is not found.
sPref = sharedPrefs.getString("listPref", "Wi-Fi");
updateConnectedFlags();
// Only loads the page if refreshDisplay is true. Otherwise, keeps previous
// display. For example, if the user has set "Wi-Fi only" in prefs and the
// device loses its Wi-Fi connection midway through the user using the app,
// you don't want to refresh the display--this would force the display of
// an error page instead of stackoverflow.com content.
if (refreshDisplay) {
loadPage();
}
|
private void | showErrorPage()
setContentView(R.layout.main);
// The specified network connection is not available. Displays error message.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadData(getResources().getString(R.string.connection_error),
"text/html", null);
|
private void | updateConnectedFlags()
ConnectivityManager connMgr =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
if (activeInfo != null && activeInfo.isConnected()) {
wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
} else {
wifiConnected = false;
mobileConnected = false;
}
|