Android Open Source - hackspacewidget Update Widget Task






From Project

Back to project page hackspacewidget.

License

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.

Java Source Code

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>();
  }
}




Java Source Code List

de.ring0.hackspace.HackspaceInfoFragment.java
de.ring0.hackspace.HackspaceListFragment.java
de.ring0.hackspace.HackspaceStatusAPI.java
de.ring0.hackspace.HackspaceStatusProvider.java
de.ring0.hackspace.HackspaceViewActivity.java
de.ring0.hackspace.HackspaceWidgetConfig.java
de.ring0.hackspace.UpdateWidgetTask.java
de.ring0.hackspace.database.InternalHelper.java
de.ring0.hackspace.database.SpaceDataSource.java
de.ring0.hackspace.database.StatusDataSource.java
de.ring0.hackspace.datatypes.Space.java
de.ring0.hackspace.datatypes.field.SpaceCache.java
de.ring0.hackspace.datatypes.field.SpaceContact.java
de.ring0.hackspace.datatypes.field.SpaceEvent.java
de.ring0.hackspace.datatypes.field.SpaceFED.java
de.ring0.hackspace.datatypes.field.SpaceFeeds.java
de.ring0.hackspace.datatypes.field.SpaceIcon.java
de.ring0.hackspace.datatypes.field.SpaceKeymaster.java
de.ring0.hackspace.datatypes.field.SpaceLocation.java
de.ring0.hackspace.datatypes.field.SpaceRadioShow.java
de.ring0.hackspace.datatypes.field.SpaceSensors.java
de.ring0.hackspace.datatypes.field.SpaceState.java
de.ring0.hackspace.datatypes.sensors.AccountBalance.java
de.ring0.hackspace.datatypes.sensors.Barometer.java
de.ring0.hackspace.datatypes.sensors.BeverageSupply.java
de.ring0.hackspace.datatypes.sensors.DoorLocked.java
de.ring0.hackspace.datatypes.sensors.Humidity.java
de.ring0.hackspace.datatypes.sensors.NetworkConnections.java
de.ring0.hackspace.datatypes.sensors.PeopleNowPresent.java
de.ring0.hackspace.datatypes.sensors.PowerConsumption.java
de.ring0.hackspace.datatypes.sensors.Radiation.java
de.ring0.hackspace.datatypes.sensors.Sensor.java
de.ring0.hackspace.datatypes.sensors.Temperature.java
de.ring0.hackspace.datatypes.sensors.TotalMemberCount.java
de.ring0.hackspace.datatypes.sensors.Wind.java