framework 服务
frameworks\base\core\java\android\app\IWallpaperManager.aidl
与设置获取墙纸相关的服务端接口
frameworks\base\core\java\android\app\IWallpaperManagerCallback.aidl
oneway interface IWallpaperManagerCallback { /** * Called when the wallpaper has changed */ void onWallpaperChanged(); }
当服务端墙纸内容准备好,准备显示时,会回调onWallpaperChanged通知客户端。
frameworks\base\core\java\android\app\WallpaperManager.java
墙纸服务管理类,其内部类
static class Globals extends IWallpaperManagerCallback.Stub { private IWallpaperManager mService; private Bitmap mCachedWallpaper; private int mCachedWallpaperUserId; private Bitmap mDefaultWallpaper; Globals(Looper looper) { IBinder b = ServiceManager.getService(Context.WALLPAPER_SERVICE); mService = IWallpaperManager.Stub.asInterface(b); forgetLoadedWallpaper(); }
// Private completion callback for setWallpaper() synchronization private class WallpaperSetCompletion extends IWallpaperManagerCallback.Stub { final CountDownLatch mLatch; public WallpaperSetCompletion() { mLatch = new CountDownLatch(1); }
内部类Globals 获取墙纸服务,并实现客户端回调接口
内部类WallpaperSetCompletion 用于服务内部使用,设置墙纸完成回调。
frameworks\base\services\core\java\com\android\server\wallpaper\WallpaperManagerService.java
public class WallpaperManagerService extends IWallpaperManager.Stub { static final String TAG = "WallpaperManagerService"; static final boolean DEBUG = false; public static class Lifecycle extends SystemService { private WallpaperManagerService mService; public Lifecycle(Context context) { super(context); } @Override public void onStart() { mService = new WallpaperManagerService(getContext()); publishBinderService(Context.WALLPAPER_SERVICE, mService); }
实现墙纸服务客户端,并发布服务。
应用端
frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\phone\LockscreenWallpaper.java
public class LockscreenWallpaper extends IWallpaperManagerCallback.Stub implements Runnable { private static final String TAG = "LockscreenWallpaper"; private final PhoneStatusBar mBar; private final WallpaperManager mWallpaperManager; private final Handler mH; private boolean mCached; private Bitmap mCache; private int mCurrentUserId; // The user selected in the UI, or null if no user is selected or UI doesn't support selecting // users. private UserHandle mSelectedUser; private AsyncTask<Void, Void, LoaderResult> mLoader; public LockscreenWallpaper(Context ctx, PhoneStatusBar bar, Handler h) { mBar = bar; mH = h; mWallpaperManager = (WallpaperManager) ctx.getSystemService(Context.WALLPAPER_SERVICE); mCurrentUserId = ActivityManager.getCurrentUser(); IWallpaperManager service = IWallpaperManager.Stub.asInterface( ServiceManager.getService(Context.WALLPAPER_SERVICE)); try { service.setLockWallpaperCallback(this); } catch (RemoteException e) { Log.e(TAG, "System dead?" + e); } }
锁屏墙纸实现了 onWallpaperChanged接口并继承runnable ,这样当壁纸准备好可以显示时就会调用该接口,而该接口将该类post 到主线程,从而执行该类中run 函数。当加载完壁纸,这更新状态栏,mBar.updateMediaMetaData。
frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\phone\PhoneStatusBar.java
if (ENABLE_LOCKSCREEN_WALLPAPER) { mLockscreenWallpaper = new LockscreenWallpaper(mContext, this, mHandler); }
桌面背景壁纸服务类
frameworks\base\core\java\android\service\wallpaper\WallpaperService.java