您的位置:首页 > 其它

解决内部存储空间紧张,不加载桌面壁纸,桌面壁纸显示

2014-10-21 18:05 447 查看

说明:

当内部存储空间不足的情况下不加载壁纸,以节省资源。

修改方式:在WallpaperManagerService.java中进行修改;

思路:首先在加载壁纸之前我们需要判断当前存储空间是否紧张。代码如下:(源码地址

public boolean isStorageLow(){
try{
if(mIpackageManager!=null){
return mIPackageManager.isStorageLow();
}

}catch(RemoteException e){

}
return false;
}

boolean bindWallpaperComponentLocked(ComponentName componentName, boolean force,

0813 boolean fromUser, WallpaperData wallpaper, IRemoteCallback reply) {

0814 if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName);


if(isStorageLow()){
return false;
}


0815 // Has the component changed?

0816 if (!force) {

0817 if (wallpaper.connection != null) {

0818 if (wallpaper.wallpaperComponent == null) {

0819 if (componentName == null) {

0820 if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: still using default");

0821 // Still using default wallpaper.

0822 return true;

0823 }

0824 } else if (wallpaper.wallpaperComponent.equals(componentName)) {

0825 // Changing to same wallpaper.

0826 if (DEBUG) Slog.v(TAG, "same wallpaper");

0827 return true;

0828 }

0829 }

0830 }

0831

0832 try {

0833 if (componentName == null) {

0834 String defaultComponent =

0835 mContext.getString(com.android.internal.R.string.default_wallpaper_component);

0836 if (defaultComponent != null) {

0837 // See if there is a default wallpaper component specified

0838 componentName = ComponentName.unflattenFromString(defaultComponent);

0839 if (DEBUG) Slog.v(TAG, "Use default component wallpaper:" + componentName);

0840 }

0841 if (componentName == null) {

0842 // Fall back to static image wallpaper

0843 componentName = IMAGE_WALLPAPER;

0844 //clearWallpaperComponentLocked();

0845 //return;

0846 if (DEBUG) Slog.v(TAG, "Using image wallpaper");

0847 }

0848 }

0849 int serviceUserId = wallpaper.userId;

0850 ServiceInfo si = mIPackageManager.getServiceInfo(componentName,

0851 PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS, serviceUserId);

0852 if (si == null) {

0853 // The wallpaper component we're trying to use doesn't exist

0854 Slog.w(TAG, "Attempted wallpaper " + componentName + " is unavailable");

0855 return false;

0856 }

0857 if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) {

0858 String msg = "Selected service does not require "

0859 + android.Manifest.permission.BIND_WALLPAPER

0860 + ": " + componentName;

0861 if (fromUser) {

0862 throw new SecurityException(msg);

0863 }

0864 Slog.w(TAG, msg);

0865 return false;

0866 }

0867

0868 WallpaperInfo wi = null;

0869

0870 Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);

0871 if (componentName != null && !componentName.equals(IMAGE_WALLPAPER)) {

0872 // Make sure the selected service is actually a wallpaper service.

0873 List<ResolveInfo> ris =

0874 mIPackageManager.queryIntentServices(intent,

0875 intent.resolveTypeIfNeeded(mContext.getContentResolver()),

0876 PackageManager.GET_META_DATA, serviceUserId);

0877 for (int i=0; i<ris.size(); i++) {

0878 ServiceInfo rsi = ris.get(i).serviceInfo;

0879 if (rsi.name.equals(si.name) &&

0880 rsi.packageName.equals(si.packageName)) {

0881 try {

0882 wi = new WallpaperInfo(mContext, ris.get(i));

0883 } catch (XmlPullParserException e) {

0884 if (fromUser) {

0885 throw new IllegalArgumentException(e);

0886 }

0887 Slog.w(TAG, e);

0888 return false;

0889 } catch (IOException e) {

0890 if (fromUser) {

0891 throw new IllegalArgumentException(e);

0892 }

0893 Slog.w(TAG, e);

0894 return false;

0895 }

0896 break;

0897 }

0898 }

0899 if (wi == null) {

0900 String msg = "Selected service is not a wallpaper: "

0901 + componentName;

0902 if (fromUser) {

0903 throw new SecurityException(msg);

0904 }

0905 Slog.w(TAG, msg);

0906 return false;

0907 }

0908 }

0909

0910 // Bind the service!

0911 if (DEBUG) Slog.v(TAG, "Binding to:" + componentName);

0912 WallpaperConnection newConn = new WallpaperConnection(wi, wallpaper);

0913 intent.setComponent(componentName);

0914 intent.putExtra(Intent.EXTRA_CLIENT_LABEL,

0915 com.android.internal.R.string.wallpaper_binding_label);

0916 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser(

0917 mContext, 0,

0918 Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER),

0919 mContext.getText(com.android.internal.R.string.chooser_wallpaper)),

0920 0, null, new UserHandle(serviceUserId)));

0921 if (!mContext.bindServiceAsUser(intent, newConn,

0922 Context.BIND_AUTO_CREATE | Context.BIND_SHOWING_UI,

0923 new UserHandle(serviceUserId))) {

0924 String msg = "Unable to bind service: "

0925 + componentName;

0926 if (fromUser) {

0927 throw new IllegalArgumentException(msg);

0928 }

0929 Slog.w(TAG, msg);

0930 return false;

0931 }

0932 if (wallpaper.userId == mCurrentUserId && mLastWallpaper != null) {

0933 detachWallpaperLocked(mLastWallpaper);

0934 }

0935 wallpaper.wallpaperComponent = componentName;

0936 wallpaper.connection = newConn;

0937 wallpaper.lastDiedTime = SystemClock.uptimeMillis();

0938 newConn.mReply = reply;

0939 try {

0940 if (wallpaper.userId == mCurrentUserId) {

0941 if (DEBUG)

0942 Slog.v(TAG, "Adding window token: " + newConn.mToken);

0943 mIWindowManager.addWindowToken(newConn.mToken,

0944 WindowManager.LayoutParams.TYPE_WALLPAPER);

0945 mLastWallpaper = wallpaper;

0946 }

0947 } catch (RemoteException e) {

0948 }

0949 } catch (RemoteException e) {

0950 String msg = "Remote exception for " + componentName + "\n" + e;

0951 if (fromUser) {

0952 throw new IllegalArgumentException(msg);

0953 }

0954 Slog.w(TAG, msg);

0955 return false;

0956 }

0957 return true;

0958 }

注!

红色部分为增加代码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: