Sets an HTC lockscreen wallpaper. - Android User Interface

Android examples for User Interface:Wallpaper

Description

Sets an HTC lockscreen wallpaper.

Demo Code

/*/*  ww w .  j  ava 2 s  .c  o m*/
 * Copyright (C) 2010, T-Mobile USA, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.content.Context;
import android.net.Uri;
import android.util.Log;

public class Main {
  private static final String TAG = "WallpaperUtilities";
  /**
   * Lock held while the temporary lock wallpaper is being written to disk. This
   * is used to prevent a possible race condition if multiple events occur in
   * quick succession to apply a specific theme with a lock paper.
   * <p>
   * TODO: This should be a file lock to avoid breaking down when carried over
   * multiple processes.
   */
  private static Object sLockWallpaperLock = new Object();
  private static boolean sLockscreenTested = false;
  private static Method sLockscreenManager_setStream;

  /**
   * Sets an HTC lockscreen wallpaper. It is necessary that you only invoke this
   * method if {@link #supportsLockWallpaper} yields true.
   */
  public static void setLockWallpaper(Context context, Uri uri) {
    Method lockscreenManager_setStream;
    synchronized (Main.class) {
      if (!sLockscreenTested) {
        throw new IllegalStateException("Invoke supportsLockWallpaper first!");
      }
      lockscreenManager_setStream = sLockscreenManager_setStream;
    }
    synchronized (sLockWallpaperLock) {
      InputStream in = null;
      try {
        in = context.getContentResolver().openInputStream(uri);
        lockscreenManager_setStream.invoke(null, in, context);
        in.close();
      } catch (Exception e) {
        if ((e instanceof InvocationTargetException)
            || (e instanceof IOException)) {
          Log.w(TAG, "Unable to set lock screen wallpaper (uri=" + uri + "): "
              + e);
        } else {
          /* Explode on unexpected reflection errors... */
          throw new RuntimeException(e);
        }
      }
    }
  }
}

Related Tutorials