Android Open Source - BingScreenAndroid Chg Bkgd Activity






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;
//from w  w  w  . j a va  2 s  . c o m
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.Activity;
import android.app.WallpaperManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class ChgBkgdActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chg_bkgd);
  }

  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(getApplicationContext())
              .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);
      Toast.makeText(getApplicationContext(),
          getResources().getString(R.string.main_wallpaper_updated),
          Toast.LENGTH_SHORT).show();
      finish();
    }
  }
}




Java Source Code List

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