public static void suggestWallpaperDimension(
      Resources res,
      final SharedPreferences sharedPrefs,
      WindowManager windowManager,
      final WallpaperManager wallpaperManager,
      boolean fallBackToDefaults) {
    final Point defaultWallpaperSize = getDefaultWallpaperSize(res, windowManager);
    // If we have saved a wallpaper width/height, use that instead

    int savedWidth = sharedPrefs.getInt(WALLPAPER_WIDTH_KEY, -1);
    int savedHeight = sharedPrefs.getInt(WALLPAPER_HEIGHT_KEY, -1);

    if (savedWidth == -1 || savedHeight == -1) {
      if (!fallBackToDefaults) {
        return;
      } else {
        savedWidth = defaultWallpaperSize.x;
        savedHeight = defaultWallpaperSize.y;
      }
    }

    if (savedWidth != wallpaperManager.getDesiredMinimumWidth()
        || savedHeight != wallpaperManager.getDesiredMinimumHeight()) {
      wallpaperManager.suggestDesiredDimensions(savedWidth, savedHeight);
    }
  }
 public WallpaperBackupHelper(Context context, String as[], String as1[]) {
   super(context);
   mContext = context;
   mFiles = as;
   mKeys = as1;
   WallpaperManager wallpapermanager = (WallpaperManager) context.getSystemService("wallpaper");
   mDesiredMinWidth = wallpapermanager.getDesiredMinimumWidth();
   mDesiredMinHeight = wallpapermanager.getDesiredMinimumHeight();
   if (mDesiredMinWidth <= 0.0D || mDesiredMinHeight <= 0.0D) {
     Display display = ((WindowManager) context.getSystemService("window")).getDefaultDisplay();
     Point point = new Point();
     display.getSize(point);
     mDesiredMinWidth = point.x;
     mDesiredMinHeight = point.y;
   }
 }
  @Override
  public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    CONTEXT = this.getApplicationContext();
    AM = this.getAssets();

    try {
      PLACEHOLDERBMP = BitmapFactory.decodeStream(AM.open("transparent.png"));
    } catch (IOException e) {
      e.printStackTrace();
    }

    WPM = WallpaperManager.getInstance(this);
    PREFS = PreferenceManager.getDefaultSharedPreferences(OMWPP.CONTEXT);

    BMPQUERYOPTIONS = new BitmapFactory.Options();
    BMPQUERYOPTIONS.inJustDecodeBounds = true;

    BMPVALIDOPTIONS = new BitmapFactory.Options();
    BMPVALIDOPTIONS.inSampleSize = 4;

    BMPAPPLYOPTIONS = new BitmapFactory.Options();
    BMPAPPLYOPTIONS.inSampleSize = 1;
    BMPAPPLYOPTIONS.inScaled = false;
    BMPAPPLYOPTIONS.inDither = false;
    BMPAPPLYOPTIONS.inPreferredConfig = Config.ARGB_8888;

    // Initialize the four queues.
    THUMBNAILQUEUE = new ConcurrentLinkedQueue<File>();
    DOWNLOADQUEUE = new ArrayBlockingQueue<URL>(20, false);
    UNZIPQUEUE = new ArrayBlockingQueue<File>(20, false);

    SCREENWIDTH = getResources().getDisplayMetrics().widthPixels;
    SCREENHEIGHT = getResources().getDisplayMetrics().heightPixels;

    WPWIDTH = WPM.getDesiredMinimumWidth();
    WPHEIGHT = WPM.getDesiredMinimumHeight();
    if (WPWIDTH < SCREENWIDTH * 2) WPWIDTH = SCREENWIDTH * 2;
    if (WPHEIGHT < SCREENHEIGHT) WPHEIGHT = SCREENHEIGHT;
    if (OMWPP.DEBUG) Log.i("OMWPPApp", "Target Width is" + WPWIDTH);
    if (OMWPP.DEBUG) Log.i("OMWPPApp", "Target Height is " + WPHEIGHT);

    try {
      ENDMARKER_URL = new URL("http://localhost");
    } catch (Exception e) {
      e.printStackTrace();
    }

    if (isSDPresent()) {
      // Check and/or create the wallpapers directory.
      SDROOT = new File(Environment.getExternalStorageDirectory().getPath() + "/ubuntuwps/");
      if (OMWPP.DEBUG) Log.i("OMWPPApp", "Checking/Creating " + SDROOT.getAbsoluteFile());
      SDROOT.mkdirs();
      THUMBNAILROOT = getExternalFilesDir(null);
      if (OMWPP.DEBUG) Log.i("OMWPPApp", "Checking/Creating " + THUMBNAILROOT.getAbsoluteFile());
      THUMBNAILROOT.mkdirs();
      File nomedia = new File(THUMBNAILROOT.getAbsolutePath() + "/.nomedia");
      try {
        if (!nomedia.exists()) nomedia.createNewFile();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    // First of all, let's load up the latest configuration JSON file.
    try {
      if (isSDPresent()) {
        if (OMWPP.DEBUG) Log.i("OMWPPApp", "Loading config file from TN folder");
        CONFIGJSON =
            streamToJSONObject(
                new FileInputStream(new File(THUMBNAILROOT.getPath() + "/omwpp_config.json")));
      } else {
        if (OMWPP.DEBUG) Log.i("OMWPPApp", "No config file in TN folder");
        throw new Exception();
      }
    } catch (Exception e) {
      try {
        if (OMWPP.DEBUG) Log.i("OMWPPApp", "Copying default files to Wallpaper folder");
        copyAssetToFile("omwpp_config.json", THUMBNAILROOT.getPath() + "/omwpp_config.json");
        CONFIGJSON =
            streamToJSONObject(
                new FileInputStream(new File(THUMBNAILROOT.getPath() + "/omwpp_config.json")));
        CONFIGJSON.putOpt("localpaths", new JSONArray("[\"" + SDROOT.toString() + "\"]"));
        commitJSONChanges();

        try {
          for (String sFile : OMWPP.AM.list("")) {
            copyAssetToFile(sFile, SDROOT.getPath() + "/" + sFile);
          }
        } catch (Exception ee) {
          ee.printStackTrace();
        }
      } catch (Exception ee) {
        e.printStackTrace();
      }
    }

    // Figure out when we last downloaded a new config file.
    LASTCONFIGREFRESH = OMWPP.PREFS.getLong("LASTCONFIGREFRESH", 0l);
  }
  @Override
  public void run() {
    WallpaperManager wm = WallpaperManager.getInstance(mContext);
    Drawable oldWallpaper = wm.getDrawable();
    InputStream inputstream = null;
    try {
      // TODO: This will cause the resource to be downloaded again, when
      // we should in most cases be able to grab it from the cache. To fix
      // this we should query WebCore to see if we can access a cached
      // version and instead open an input stream on that. This pattern
      // could also be used in the download manager where the same problem
      // exists.
      inputstream = openStream();
      if (inputstream != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int seg = 0;
        byte buf[] = new byte[8192];
        while ((seg = inputstream.read(buf)) != -1) {
          baos.write(buf, 0, seg);
        }
        final byte[] imageData = baos.toByteArray();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        // We give decodeStream a wrapped input stream so it doesn't
        // mess with our mark (currently it sets a mark of 1024)
        Bitmap firstImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
        int maxWidth = wm.getDesiredMinimumWidth();
        int maxHeight = wm.getDesiredMinimumHeight();
        // Give maxWidth and maxHeight some leeway
        maxWidth *= 1.25;
        maxHeight *= 1.25;
        int bmWidth = options.outWidth;
        int bmHeight = options.outHeight;

        int scale = 1;
        while (bmWidth > maxWidth || bmHeight > maxHeight) {
          scale <<= 1;
          bmWidth >>= 1;
          bmHeight >>= 1;
        }
        options.inJustDecodeBounds = false;
        options.inSampleSize = scale;
        Bitmap scaledWallpaper =
            BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
        if (scaledWallpaper != null) {
          wm.setBitmap(scaledWallpaper);
        }
      }
    } catch (IOException e) {
      Log.e(LOGTAG, "Unable to set new wallpaper");
      // Act as though the user canceled the operation so we try to
      // restore the old wallpaper.
      mCanceled = true;
    } finally {
      if (inputstream != null) {
        try {
          inputstream.close();
        } catch (IOException e) {
          // Ignore
        }
      }
    }

    if (mCanceled) {
      // Restore the old wallpaper if the user cancelled whilst we were
      // setting
      // the new wallpaper.
      int width = oldWallpaper.getIntrinsicWidth();
      int height = oldWallpaper.getIntrinsicHeight();
      Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
      Canvas canvas = new Canvas(bm);
      oldWallpaper.setBounds(0, 0, width, height);
      oldWallpaper.draw(canvas);
      canvas.setBitmap(null);
      try {
        wm.setBitmap(bm);
      } catch (IOException e) {
        Log.e(LOGTAG, "Unable to restore old wallpaper.");
      }
      mCanceled = false;
    }

    if (mWallpaperProgress.isShowing()) {
      mWallpaperProgress.dismiss();
    }
  }