Android Open Source - cicada Web Image Player






From Project

Back to project page cicada.

License

The source code is released under:

Apache License

If you think the Android project cicada 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 org.cicadasong.samples.webimageplayer;
/*ww  w.  j  a va  2 s . c o m*/
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.cicadasong.cicadalib.CicadaApp;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;

public class WebImagePlayer extends CicadaApp implements OnSharedPreferenceChangeListener {
  public static final String TAG = WebImagePlayer.class.getSimpleName();

  private Bitmap downloadedImage = null;
  
  private Runnable updateStatusTask;
  private Handler handler;
  private Paint paint;
  private String imageUrl;
  private int updateIntervalMsec;
  private boolean dither;
  
  @Override
  public void onCreate() {
    paint = new Paint();
  
    super.onCreate();
  }

  @Override
  protected void onResume() {
    Log.v(TAG, "WebImagePlayer activated in mode: " + getCurrentMode());

    Preferences.getSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
    readPreferences();
    if (updateStatusTask == null) {
      updateStatusTask = new Runnable() {

        @Override
        public void run() {
          if (!isActive()) return;

          (new GetTimesTask()).execute(imageUrl);
        }
      };
    }
    if (handler == null) {
      handler = new Handler();
    }
    handler.removeCallbacks(updateStatusTask);
    handler.post(updateStatusTask);
  }

  private void readPreferences() {
    imageUrl = Preferences.getImageUrl(this);
    updateIntervalMsec = Preferences.getUpdateFrequency(this) * 1000;
    dither = Preferences.getDithering(this);
  }

  @Override
  protected void onPause() {
    Preferences.getSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
    handler.removeCallbacks(updateStatusTask);
  }

  @Override
  protected void onButtonPress(WatchButton button) {
  }

  @Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    readPreferences();
    handler.removeCallbacks(updateStatusTask);
    handler.post(updateStatusTask);
  }

  protected void onDraw(Canvas canvas) {
    if (downloadedImage != null) {
      Rect src = new Rect(0, 0, downloadedImage.getWidth(), downloadedImage.getHeight());
      Rect canvasBounds = canvas.getClipBounds();
      Rect dst = new Rect(canvasBounds);
      
      // Scale & center image to fit the display
      boolean tall = downloadedImage.getWidth() < downloadedImage.getHeight();
      if (tall) {
        int width = (int) (canvasBounds.width() *
            (float)downloadedImage.getWidth() / (float)downloadedImage.getHeight());
        dst.left = (canvasBounds.width() - width) / 2;
        dst.right = dst.left + width;
      } else {
        int height = (int) (canvasBounds.height() *
            (float)downloadedImage.getHeight() / (float)downloadedImage.getWidth());
        dst.top = (canvasBounds.height() - height) / 2;
        dst.bottom = dst.top + height;
      }
      
      if (!dither) {
        canvas.drawBitmap(downloadedImage, src, dst, paint);
      } else {
        Bitmap scaledBitmap = Bitmap.createBitmap(
            canvasBounds.width(), canvasBounds.height(), Bitmap.Config.ARGB_8888);
        Canvas scaledCanvas = new Canvas(scaledBitmap);
        
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
        paint.setColorFilter(f);
        scaledCanvas.drawBitmap(downloadedImage, src, dst, paint);
        
        // TODO: Implement dithering
        
        canvas.drawBitmap(scaledBitmap, 0, 0, paint);
      }
    }
    return;
  }

  private void processStatusUpdate(Bitmap bitmap) {
    if (!isActive()) {
      return;
    }
    
    if (bitmap != null) {
      downloadedImage = bitmap;
      // TODO: Only send to the watch if the image differs, either here or upstream.
      invalidate();
    }

    handler.postDelayed(updateStatusTask, updateIntervalMsec);
  }

  private class GetTimesTask extends AsyncTask<String, Void, Bitmap> {
    @Override
    protected void onPostExecute(Bitmap result) {
      processStatusUpdate(result);
    }
    
    @Override
    protected Bitmap doInBackground(String... params) {
      HttpURLConnection connection = null;
      Bitmap bitmap = null;
      String imageUrlString = params[0];
      try {
        URL url = new URL(imageUrlString);
        connection = (HttpURLConnection) url.openConnection();
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
          bitmap = BitmapFactory.decodeStream(connection.getInputStream());
        }
      } catch (MalformedURLException e) {
        Log.e(TAG, "Malformed request URL: " + e);
      } catch (IOException e) {
        Log.e(TAG, "Connection error");
      } finally {
        if (connection != null) {
          connection.disconnect();
        }
      }

      Log.v(TAG, "Image fetch for URL " + imageUrlString + " yielded " + bitmap);
      return bitmap;
    }
  }
}




Java Source Code List

com.hydraproductions.cicada.StopWatch.java
org.cicadasong.apollo.ApolloConfig.java
org.cicadasong.apollo.ApolloIntents.java
org.cicadasong.apollo.BitmapUtil.java
org.cicadasong.apollo.SimulatedDisplayView.java
org.cicadasong.cicada.AppDatabase.java
org.cicadasong.cicada.AppDescription.java
org.cicadasong.cicada.AppList.java
org.cicadasong.cicada.AppScanner.java
org.cicadasong.cicada.CicadaService.java
org.cicadasong.cicada.CicadaSettingsActivity.java
org.cicadasong.cicada.Cicada.java
org.cicadasong.cicada.DeviceService.java
org.cicadasong.cicada.HotkeySetupActivity.java
org.cicadasong.cicada.HotkeySetupEntry.java
org.cicadasong.cicada.MetaWatchConnection.java
org.cicadasong.cicada.NotificationRenderer.java
org.cicadasong.cicada.PackageMonitor.java
org.cicadasong.cicada.PackageUtil.java
org.cicadasong.cicada.PrefUtil.java
org.cicadasong.cicada.WidgetScreen.java
org.cicadasong.cicada.WidgetSetup.java
org.cicadasong.cicadalib.CicadaApp.java
org.cicadasong.cicadalib.CicadaIntents.java
org.cicadasong.cicadalib.CicadaNotificationManager.java
org.cicadasong.cicadalib.CicadaNotification.java
org.cicadasong.cicadalib.TextBlock.java
org.cicadasong.samples.bitly.Bitly.java
org.cicadasong.samples.digitalclock.DigitalClock.java
org.cicadasong.samples.hellocicada.HelloCicada.java
org.cicadasong.samples.nextbuses.NextBuses.java
org.cicadasong.samples.notificationdemo.NotificationDemo.java
org.cicadasong.samples.notifications.NotificationAccessibilityService.java
org.cicadasong.samples.notifications.Notifications.java
org.cicadasong.samples.quicktext.QuickTextSetup.java
org.cicadasong.samples.quicktext.QuickText.java
org.cicadasong.samples.smsnotifier.SMSBroadcastReceiver.java
org.cicadasong.samples.tubestatus.Preferences.java
org.cicadasong.samples.tubestatus.TubeStatusSettingsActivity.java
org.cicadasong.samples.tubestatus.TubeStatus.java
org.cicadasong.samples.webimageplayer.Preferences.java
org.cicadasong.samples.webimageplayer.WebImagePlayerSettingsActivity.java
org.cicadasong.samples.webimageplayer.WebImagePlayer.java