Android Open Source - BingScreenAndroid Boot Receiver






From Project

Back to project page BingScreenAndroid.

License

The source code is released under:

GNU General Public License

If you think the Android project BingScreenAndroid 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 net.pm.bingscreen.receiver;
/* w  ww.  j av  a2  s  .c om*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.WallpaperManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;

public class BootReceiver extends BroadcastReceiver {
  private Context mContext = null;

  @Override
  public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
      mContext = context;
      new BingImgPathRetriever().execute((Void) null);
    }
  }

  public void changeBackground(View v) {
    new BingImgPathRetriever().execute((Void) null);
  }

  /**
   * Set a new wallpaper on the phone
   */
  private void setNewWallpaper(final InputStream img) {
    Thread t = new Thread(new Runnable() {

      @Override
      public void run() {
        try {
          WallpaperManager.getInstance(mContext).setStream(img);
        } catch (IOException e) {
          Log.e("Bing wallpaper",
              "Wallpaper not changed:" + e.getLocalizedMessage());
        }
      }
    });
    t.start();
  }

  /**
   * Open an InputStream for a given IMG URL
   * 
   * @author Paul Marret
   * 
   */
  private class ImgRetrievalTask extends AsyncTask<String, Void, InputStream> {

    @Override
    protected InputStream doInBackground(String... params) {
      URL url = null;
      HttpURLConnection conn = null;

      if (params[0].length() == 0)
        return null;

      try {
        url = new URL(params[0]);
      } catch (MalformedURLException e) {
        Log.e("IMG retrieval",
            "Img malformed: " + e.getLocalizedMessage());
      }
      try {
        if (url != null)
          conn = (HttpURLConnection) url.openConnection();
        else
          return null;
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        // Starts the query
        conn.connect();
      } catch (IOException e) {
        Log.e("IMG retrieval", "IO except: " + e.getLocalizedMessage());
      }
      if (conn != null)
        try {
          return conn.getInputStream();
        } catch (IOException e) {
          Log.e("IMG retrieval",
              "IO except: " + e.getLocalizedMessage());
        }
      return null;
    }

    @Override
    protected void onPostExecute(InputStream result) {
      if (result != null) {
        setNewWallpaper(result);
        Log.d("IMG retrieval", "IMG OKAY");
      } else
        Log.d("IMG retrieval", "IMG null");
    }
  }

  private class BingImgPathRetriever extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
      String imgSrc = "";
      boolean passedLogo = false;
      URL url = null;
      HttpURLConnection conn = null;

      try {
        url = new URL("http://bingwallpaper.com");
      } catch (MalformedURLException e) {
        Log.e("HomeIMG name",
            "URL malformed: " + e.getLocalizedMessage());
      }

      try {
        if (url != null) {
          conn = (HttpURLConnection) url.openConnection();
        } else
          return null;
        conn.connect();

        if (conn.getResponseCode() != 200) {
          System.out.println("Cannot open connection.");
        } else {
          BufferedReader reader = new BufferedReader(
              new InputStreamReader(conn.getInputStream()));
          String str = "";
          while ((str = reader.readLine()) != null) {
            if (str.indexOf("<img") > 0 && passedLogo) {
              // Log.d("str", str);
            }
            if (str.indexOf("<img") > 0) {
              if (passedLogo) {
                imgSrc = str.substring(
                    (str.indexOf("<img") + 10),
                    (str.indexOf("alt") - 2));
                break;
              } else
                passedLogo = true;
            }

            // Log.d("Sortie", "Hello " + imgSrc);
          }
          reader.close();
          conn.disconnect();
        }
      } catch (IOException e) {
        Log.e("IMG path retrieval",
            "Can't contact bing.com: " + e.getLocalizedMessage());
      }

      Log.d("IMG retrieval", "path: " + imgSrc);
      return imgSrc;
    }

    @Override
    protected void onPostExecute(String result) {
      new ImgRetrievalTask().execute(result);
    }
  }

}




Java Source Code List

net.pm.bingscreen.ChgBkgdActivity.java
net.pm.bingscreen.receiver.BootReceiver.java