Back to project page hackspacewidget.
The source code is released under:
Apache License
If you think the Android project hackspacewidget listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package de.ring0.hackspace; // w ww .j ava 2 s. c om import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.HashMap; import org.apache.http.client.ClientProtocolException; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.preference.PreferenceManager; import android.util.Log; import android.widget.RemoteViews; import com.google.gson.Gson; import de.ring0.hackspace.UpdateWidgetTask.TaskParameters; import de.ring0.hackspace.datatypes.Space; public class UpdateWidgetTask extends AsyncTask<TaskParameters, Void, Space> { private static final String TAG = UpdateWidgetTask.class.getSimpleName(); private Bitmap openIcon, closedIcon; private TaskParameters tp; @Override protected Space doInBackground(TaskParameters... tps) { tp = tps[0]; SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(tp.context); String url; Gson g = new Gson(); RefreshStatus rs = null; /* stop the widget from rendering before the configuration is completed */ if(sp.contains("refreshStatus")) { rs = g.fromJson(sp.getString("refreshStatus", ""), RefreshStatus.class); if(!rs.refreshWidget.containsKey(tp.appWidgetId)) { rs.refreshWidget.put(tp.appWidgetId, false); sp.edit().putString("refreshStatus", g.toJson(rs)).commit(); return null; } } else { rs = new RefreshStatus(); sp.edit().putString("refreshStatus", g.toJson(rs)).commit(); return null; } if(sp.getBoolean("custom_hackspace", false)) url = sp.getString("custom_url", "http://localhost"); else url = sp.getString("predefined_hackspace", "http://localhost"); try { HackspaceStatusAPI hss = new HackspaceStatusAPI(url); Space ss = hss.run(); URL open = new URL(ss.state.icon.open); URL closed = new URL(ss.state.icon.closed); URLConnection urlOpen = open.openConnection(); URLConnection urlClosed = closed.openConnection(); urlOpen.setUseCaches(true); urlClosed.setUseCaches(true); openIcon = BitmapFactory.decodeStream((InputStream) urlOpen.getContent()); closedIcon = BitmapFactory.decodeStream((InputStream) urlClosed.getContent()); return ss; } catch (ClientProtocolException e) { Log.e(TAG, e.getMessage()); } catch (IOException e) { Log.e(TAG, e.getMessage()); } return null; } @Override protected void onPostExecute (Space result) { if(result != null) { RemoteViews views = new RemoteViews(tp.context.getPackageName(), R.layout.widget_layout); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(tp.context); Gson g = new Gson(); String serialized = g.toJson(result); if(result.state.open) views.setImageViewBitmap(R.id.imageView1, openIcon); else views.setImageViewBitmap(R.id.imageView1, closedIcon); Intent intent = new Intent(tp.context, HackspaceViewActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(tp.context, 0, intent, 0); views.setOnClickPendingIntent(R.id.imageView1, pendingIntent); tp.appWidgetManager.updateAppWidget(tp.appWidgetId, views); sp.edit().putString("status", serialized).commit(); } } public static class TaskParameters { public Context context; public AppWidgetManager appWidgetManager; public int appWidgetId; public TaskParameters(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { this.context = context; this.appWidgetManager = appWidgetManager; this.appWidgetId = appWidgetId; } } public static class RefreshStatus { public HashMap<Integer,Boolean> refreshWidget = new HashMap<Integer,Boolean>(); } }