catch exceptions in widget update service

This commit is contained in:
Andrew Dolgov 2013-05-21 16:31:05 +04:00
parent 390eee760c
commit aa64bbcb13

View File

@ -45,87 +45,95 @@ public class WidgetUpdateService extends Service {
@Override @Override
public void onStart(Intent intent, int startId) { public void onStart(Intent intent, int startId) {
final RemoteViews view = new RemoteViews(getPackageName(), R.layout.widget_small); final RemoteViews view = new RemoteViews(getPackageName(), R.layout.widget_small);
view.setTextViewText(R.id.counter, String.valueOf(""));
view.setViewVisibility(R.id.progress, View.VISIBLE);
final ComponentName thisWidget = new ComponentName(this, SmallWidgetProvider.class); final ComponentName thisWidget = new ComponentName(this, SmallWidgetProvider.class);
final AppWidgetManager manager = AppWidgetManager.getInstance(this); final AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, view);
final SharedPreferences m_prefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
if (m_prefs.getString("ttrss_url", "").trim().length() == 0) {
// Toast: need configure
} else {
ApiRequest ar = new ApiRequest(getApplicationContext()) { try {
@SuppressWarnings({ "unchecked", "serial" }) view.setTextViewText(R.id.counter, String.valueOf(""));
@Override view.setViewVisibility(R.id.progress, View.VISIBLE);
protected void onPostExecute(JsonElement result) {
if (result != null) {
JsonObject content = result.getAsJsonObject();
if (content != null) {
final String sessionId = content.get("session_id").getAsString();
ApiRequest aru = new ApiRequest(getApplicationContext()) {
@Override
protected void onPostExecute(JsonElement result) {
if (result != null) {
JsonObject content = result.getAsJsonObject();
if (content != null) {
int unread = content.get("unread").getAsInt();
view.setViewVisibility(R.id.progress, View.GONE);
view.setTextViewText(R.id.counter, String.valueOf(unread));
manager.updateAppWidget(thisWidget, view);
return;
}
}
view.setViewVisibility(R.id.progress, View.GONE);
view.setTextViewText(R.id.counter, getString(R.string.app_name));
manager.updateAppWidget(thisWidget, view);
}
};
HashMap<String, String> umap = new HashMap<String, String>() { manager.updateAppWidget(thisWidget, view);
{
put("op", "getUnread"); final SharedPreferences m_prefs = PreferenceManager
put("sid", sessionId); .getDefaultSharedPreferences(getApplicationContext());
}
}; if (m_prefs.getString("ttrss_url", "").trim().length() == 0) {
aru.execute(umap); // Toast: need configure
return;
} } else {
}
ApiRequest ar = new ApiRequest(getApplicationContext()) {
// Toast: login failed @SuppressWarnings({ "unchecked", "serial" })
@Override
view.setViewVisibility(R.id.progress, View.GONE); protected void onPostExecute(JsonElement result) {
view.setTextViewText(R.id.counter, getString(R.string.app_name)); if (result != null) {
manager.updateAppWidget(thisWidget, view); JsonObject content = result.getAsJsonObject();
};
}; if (content != null) {
final String sessionId = content.get("session_id").getAsString();
HashMap<String, String> map = new HashMap<String, String>() {
{ ApiRequest aru = new ApiRequest(getApplicationContext()) {
put("op", "login"); @Override
put("user", m_prefs.getString("login", "").trim()); protected void onPostExecute(JsonElement result) {
put("password", m_prefs.getString("password", "").trim()); if (result != null) {
} JsonObject content = result.getAsJsonObject();
};
if (content != null) {
ar.execute(map); int unread = content.get("unread").getAsInt();
}
view.setViewVisibility(R.id.progress, View.GONE);
view.setTextViewText(R.id.counter, String.valueOf(unread));
manager.updateAppWidget(thisWidget, view);
return;
}
}
view.setViewVisibility(R.id.progress, View.GONE);
view.setTextViewText(R.id.counter, getString(R.string.app_name));
manager.updateAppWidget(thisWidget, view);
}
};
HashMap<String, String> umap = new HashMap<String, String>() {
{
put("op", "getUnread");
put("sid", sessionId);
}
};
aru.execute(umap);
return;
}
}
// Toast: login failed
view.setViewVisibility(R.id.progress, View.GONE);
view.setTextViewText(R.id.counter, getString(R.string.app_name));
manager.updateAppWidget(thisWidget, view);
};
};
HashMap<String, String> map = new HashMap<String, String>() {
{
put("op", "login");
put("user", m_prefs.getString("login", "").trim());
put("password", m_prefs.getString("password", "").trim());
}
};
ar.execute(map);
}
} catch (Exception e) {
e.printStackTrace();
view.setViewVisibility(R.id.progress, View.GONE);
view.setTextViewText(R.id.counter, getString(R.string.app_name));
manager.updateAppWidget(thisWidget, view);
}
} }
} }