@Override public void run() { boolean needPrepare = false; if (mTsbSupport) { mWallpaperDrawable = null; // 动态壁纸 mSysDrawSuport = true; } else { synchronized (mSupperWallpaperLock) { try { mWallpaperDrawable = null; mWallpaperDrawable = mWallPaperManager.getDrawable(); Resources resources = GOLauncherApp.getContext().getResources(); int screenWidth = resources.getDisplayMetrics().widthPixels; int screenHeight = resources.getDisplayMetrics().heightPixels; boolean needReturen = preventAmplifyWallpaper(resources, screenWidth); if (needReturen) { mSettingSupperWallpaper = false; return; } if (mWallpaperDrawable != null) { int bmpWidth = mWallpaperDrawable.getIntrinsicWidth(); int bmpHeight = mWallpaperDrawable.getIntrinsicHeight(); if (bmpHeight < screenHeight || bmpWidth < screenWidth) { mWallpaperDrawable = preventBlackScreen(mWallpaperDrawable); } } needPrepare = true; } catch (OutOfMemoryError e) { Log.e(TAG, "outOfMemory in updateWallpaper(boolean)" + e.getMessage()); OutOfMemoryHandler.handle(); resetData(); // 交由系统去处理 } catch (Exception e) { mSettingSupperWallpaper = false; Log.e(TAG, "updateWallPaper()" + e.getMessage()); } mSysDrawSuport = false; if (null == mWallpaperDrawable) { // 当获取壁纸失败时,交由系统自己处理 mSysDrawSuport = true; } mHandler.sendEmptyMessage(MSG_UPDATE_WALLPAPER_TYPE); if (needPrepare && null != mWallpaperDrawable) { // 逻辑预处理 mWallpaperDrawable = prepareOrientationChange(mWallpaperDrawable); } mHandler.sendEmptyMessage(MSG_UPDATE_WALLPAPER); } // end synchronized } // end else } // end run
/** * 设置壁纸 * * @param context context * @param resources 如果是主题包,必须用主题包的resource * @param resId 图片资源id */ private void setWallpaper(Context context, Resources resources, int resId) { OutOfMemoryHandler.handle(); if (context == null || resources == null || resId < 0) { return; } boolean bSetOk = false; WallpaperManager wpm = null; Drawable drb = null; BitmapDrawable bdrb = null; // InputStream in = null; ByteArrayOutputStream out = null; try { // 获取宽高 // 竖屏状态 int screenW = 0; int screenH = 0; if (resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { screenW = resources.getDisplayMetrics().widthPixels; screenH = resources.getDisplayMetrics().heightPixels; } else { screenW = resources.getDisplayMetrics().heightPixels; screenH = resources.getDisplayMetrics().widthPixels; } wpm = (WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE); drb = resources.getDrawable(resId); { // 图片处理 // 对Lephone的特殊处理 if (Machine.isLephone()) { bdrb = WindowControl.prepareWallpaper((BitmapDrawable) drb, screenH, screenH, resources); } else { bdrb = WindowControl.prepareWallpaper( (BitmapDrawable) drb, screenW * WindowControl.WALLPAPER_SCREENS_SPAN, screenH, resources); } out = new ByteArrayOutputStream(); boolean b = bdrb.getBitmap().compress(CompressFormat.JPEG, 100, out); if (b) { mInputStream = new BufferedInputStream(new ByteArrayInputStream(out.toByteArray())); out.close(); out = null; } else { mInputStream = resources.openRawResource(resId); } bSetOk = true; mHandler.sendEmptyMessage(MSG_SET_WALLPAPER_INPUTSTREAM); } } catch (OutOfMemoryError e) { // 内存爆掉,不进行图片处理 OutOfMemoryHandler.handle(); try { if (wpm != null) { mInputStream = resources.openRawResource(resId); mHandler.sendEmptyMessage(MSG_SET_WALLPAPER_INPUTSTREAM); } bSetOk = true; } catch (Throwable e2) { Log.i(LogConstants.HEART_TAG, "fail to re-change wallpaper " + e2); } } catch (IOException e) { Log.i(LogConstants.HEART_TAG, "fail to change wallpaper " + e); } catch (Exception e) { Log.i(LogConstants.HEART_TAG, "fail to change wallpaper " + e); } finally { if (null != out) { try { out.close(); } catch (Exception e2) { e2.printStackTrace(); } } } }
public void updateWallpaper(boolean fromIntentReceiver) { boolean needPrepare = false; boolean hasException = false; Object wallpaperInfo = null; try { Method getWallpaperInfo = mWallPaperManager.getClass().getMethod("getWallpaperInfo"); wallpaperInfo = getWallpaperInfo.invoke(mWallPaperManager); } catch (Throwable e) { hasException = true; } final boolean isLiveWallpaper = !hasException && wallpaperInfo != null ? true : false; boolean livecChange = false; if (isLiveWallpaper && wallpaperInfo instanceof WallpaperInfo) { String newPkgName = ((WallpaperInfo) wallpaperInfo).getPackageName(); if (!newPkgName.equals(mLwpPkgName)) { mLwpPkgName = newPkgName; livecChange = true; } } if (isLiveWallpaper != mLwpSupport || livecChange) { // 壁纸动/静切换或者是动态壁纸切换动态壁纸,均需要通知更新 mLwpSupport = isLiveWallpaper; if (Workspace.getLayoutScale() < 1.0f) { // 如果壁纸发生(动/静)变更并且桌面是添加状态 // 通知添加模块添加壁纸设置选项发生变更 GoLauncher.sendMessage( this, IDiyFrameIds.SCREEN_EDIT_BOX_FRAME, IDiyMsgIds.SCREEN_EDIT_UPDATE_WALLPAPER_ITEMS, 0, null, null); } } synchronized (mSupperWallpaperLock) { if (mTsbSupport || isLiveWallpaper) { mWallpaperDrawable = null; // 动态壁纸 mSysDrawSuport = true; } else { if (fromIntentReceiver || mWallpaperDrawable == null) { try { mWallpaperDrawable = null; mWallpaperDrawable = mWallPaperManager.getDrawable(); Resources resources = GOLauncherApp.getContext().getResources(); int screenWidth = resources.getDisplayMetrics().widthPixels; int screenHeight = resources.getDisplayMetrics().heightPixels; boolean needReturen = preventAmplifyWallpaper(resources, screenWidth); if (needReturen) { return; } if (mWallpaperDrawable != null) { int bmpWidth = mWallpaperDrawable.getIntrinsicWidth(); int bmpHeight = mWallpaperDrawable.getIntrinsicHeight(); if (bmpHeight < screenHeight || bmpWidth < screenWidth) { mWallpaperDrawable = preventBlackScreen(mWallpaperDrawable); } } needPrepare = true; } catch (OutOfMemoryError e) { Log.e(TAG, "outOfMemory in updateWallpaper(boolean)" + e.getMessage()); OutOfMemoryHandler.handle(); resetData(); // 交由系统去处理 } catch (Exception e) { Log.e(TAG, "updateWallPaper()" + e.getMessage()); } } // mLwpSupport = false; mSysDrawSuport = false; if (null == mWallpaperDrawable) { // 当获取壁纸失败时,交由系统自己处理 // mLwpSupport = true; mSysDrawSuport = true; } } setWindowBackground(mSysDrawSuport); // 更新壁纸 if (needPrepare && null != mWallpaperDrawable) { // 逻辑预处理 mWallpaperDrawable = prepareOrientationChange(mWallpaperDrawable); } invokeWallPaperChange(); // 通知 if (needPrepare && mWallpaperDrawable != null && sIsWallpaperSetting) { // 调整壁纸宽度 sIsWallpaperSetting = false; adjustWallpaperDimension(mWallPaperManager); } } // end synchronized }