Fix for Kitkat devices by copy the custom wallpaper on local directory to bypass the external file security imposed by Android

This commit is contained in:
Michel Roux 2014-08-02 11:42:48 +02:00
parent 803fa8cb6c
commit 528ff2254d
3 changed files with 49 additions and 28 deletions

View File

@ -1,5 +1,7 @@
package com.derpfish.pinkielive;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
@ -12,7 +14,6 @@ import com.derpfish.pinkielive.download.PonyDownloader;
import android.app.WallpaperManager;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
@ -22,7 +23,6 @@ import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Handler;
import android.os.SystemClock;
import android.service.wallpaper.WallpaperService;
@ -185,22 +185,20 @@ public class PinkiePieLiveWallpaper extends WallpaperService
selectedBg.recycle();
}
final Uri bgImage = Uri.parse(imageUriStr);
try
{
final ContentResolver contentResolver = getContentResolver();
File bg = new File(getApplicationContext().getFilesDir(), imageUriStr);
final WallpaperManager wmMan = WallpaperManager.getInstance(getApplicationContext());
InputStream istr = contentResolver.openInputStream(bgImage);
FileInputStream istr = new FileInputStream(bg);
final int sampleSize = BitmapLoader.getSampleSizeFromInputStream(istr,
wmMan.getDesiredMinimumWidth(), wmMan.getDesiredMinimumHeight());
istr.close();
istr = contentResolver.openInputStream(bgImage);
istr = new FileInputStream(bg);
selectedBg = BitmapLoader.decodeSampledBitmapFromInputStream(istr, sampleSize);
istr.close();
}
catch (IOException e)
catch (Exception e)
{
selectedBg = null;
Log.w("PinkieLive", e);

View File

@ -146,9 +146,8 @@ public class PinkieAnimation implements PonyAnimation
lastAnim = (lastAnim + 1) % NUM_ANIMATIONS;
if (bmAnimation != null)
{
for (int i = 0; i < bmAnimation.length; i++)
{
bmAnimation[i].recycle();
for (Bitmap aBmAnimation : bmAnimation) {
aBmAnimation.recycle();
}
bmAnimation = null;
}
@ -157,7 +156,7 @@ public class PinkieAnimation implements PonyAnimation
final InputStream istr = assetManager.open("jump" + (lastAnim + 1) + ".zip");
final ZipInputStream zis = new ZipInputStream(istr);
final Map<String, Bitmap> bitmaps = new HashMap<String, Bitmap>();
ZipEntry zipEntry = null;
ZipEntry zipEntry;
while ((zipEntry = zis.getNextEntry()) != null)
{
bitmaps.put(zipEntry.getName(), BitmapFactory.decodeStream(zis));
@ -198,9 +197,8 @@ public class PinkieAnimation implements PonyAnimation
{
if (bmAnimation != null)
{
for (int i = 0; i < bmAnimation.length; i++)
{
bmAnimation[i].recycle();
for (Bitmap aBmAnimation : bmAnimation) {
aBmAnimation.recycle();
}
}
}

View File

@ -1,5 +1,6 @@
package com.derpfish.pinkielive.preference;
import android.content.Context;
import com.derpfish.pinkielive.PinkiePieLiveWallpaper;
import com.derpfish.pinkielive.R;
@ -12,6 +13,8 @@ import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import java.io.*;
public class PinkiePieLiveWallpaperSettings extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener, OnPreferenceClickListener
{
private static final int SELECT_PICTURE = 1;
@ -82,11 +85,33 @@ public class PinkiePieLiveWallpaperSettings extends PreferenceActivity implement
if (requestCode == SELECT_PICTURE)
{
Uri selectedImageUri = data.getData();
try {
// Delete old image
String oldLocation = getPreferenceManager().getSharedPreferences().getString("livewallpaper_image", null);
if (oldLocation != null) {
File oldFile = new File(getApplicationContext().getFilesDir(), oldLocation);
if (oldFile.exists()) oldFile.delete();
}
// Copy the new one
FileOutputStream fos = openFileOutput(selectedImageUri.getLastPathSegment(), Context.MODE_PRIVATE);
InputStream fis = getContentResolver().openInputStream(selectedImageUri);
byte[] buf = new byte[1024];
int len;
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
SharedPreferences.Editor editor = getPreferenceManager().getSharedPreferences().edit();
editor.putString("livewallpaper_image", selectedImageUri.toString());
editor.putString("livewallpaper_image", selectedImageUri.getLastPathSegment());
editor.putBoolean("livewallpaper_defaultbg", false);
editor.commit();
((CheckBoxPreference) findPreference("livewallpaper_defaultbg")).setChecked(false);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}