package fc.fcstudio;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import org.apache.cordova.PluginResult;
import java.io.IOException;
import java.io.InputStream;

/**
 * This class called from JavaScript.
 */
public class wallpaper extends CordovaPlugin {

	public Context context = null;
	private static final boolean IS_AT_LEAST_LOLLIPOP = Build.VERSION.SDK_INT >= 21;
	
    @Override
    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
		
		context = IS_AT_LEAST_LOLLIPOP ? cordova.getActivity().getWindow().getContext() : cordova.getActivity().getApplicationContext();
		String imgSrc = "";
		Boolean base64 = false;
		
        if (action.equals("start")) {
			for (int i = 0; i < args.length(); i++) {
				JSONObject jsonobject = args.getJSONObject(i);
				imgSrc = jsonobject.getString("image");
				base64 = jsonobject.getBoolean("base64");
			}
            this.echo(imgSrc, base64, context);
			PluginResult pr = new PluginResult(PluginResult.Status.OK);
          	pr.setKeepCallback(true);
          	callbackContext.sendPluginResult(pr);
           	return true;
        }
		
		callbackContext.error("Set wallpaper is not a supported.");
        return false;
    }

    public void echo(String image, Boolean base64, Context context) {
		try {
			AssetManager assetManager = context.getAssets();
			Bitmap bitmap;
			if(base64) { //Base64 encoded
				byte[] decoded = Base64.decode(image, Base64.DEFAULT);
				bitmap = BitmapFactory.decodeByteArray(decoded, 0, decoded.length);
			}
			else { //normal path
				InputStream instr = assetManager.open("www/" + image);
				bitmap = BitmapFactory.decodeStream(instr);
			}
			WallpaperManager myWallpaperManager	= WallpaperManager.getInstance(context);
			myWallpaperManager.setBitmap(bitmap);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}

@@ -578,19 +578,18 @@
 vaPlugin
- %7B
 %0A
+%7B
 %0A%09public
@@ -697,20 +697,17 @@
 = 21;%0A%09%0A
-    
+%09
 @Overrid
@@ -708,20 +708,17 @@
 verride%0A
-    
+%09
 public b
@@ -822,21 +822,19 @@
 xception
- %7B
 %0A%09
-%09
+%7B
 %0A%09%09conte
@@ -1008,24 +1008,18 @@
 lse;%0A%09%09%0A
-        
+%09%09
 if (acti
@@ -1041,108 +1041,13 @@
 t%22))
- %7B
 %0A%09%09
-%09for (int i = 0; i %3C args.length(); i++) %7B%0A%09%09%09%09JSONObject jsonobject = args.getJSONObject(i);%0A%09
+%7B%0A
 %09%09%09i
@@ -1088,17 +1088,16 @@
 e%22);%0A%09%09%09
-%09
 base64 =
@@ -1137,22 +1137,8 @@
 %0A%09%09%09
-%7D%0A            
 this
@@ -1232,26 +1232,18 @@
 us.OK);%0A
-          
+%09%09
 %09pr.setK
@@ -1261,26 +1261,18 @@
 (true);%0A
-          
+%09%09
 %09callbac
@@ -1306,19 +1306,10 @@
 r);%0A
-           
+%09%09
 %09ret
@@ -1322,20 +1322,11 @@
 ue;%0A
-        %7D%0A
 %09%09
+%7D
 %0A%09%09c
@@ -1392,16 +1392,17 @@
         
+%09
 return f
@@ -1411,19 +1411,13 @@
 se;%0A
-    %7D%0A%0A    
+%09%7D%0A%0A%09
 publ
@@ -1479,17 +1479,20 @@
 ext)
- 
+%0A%09
 %7B%0A%09%09try
- 
+%0A%09%09
 %7B%0A%09%09
@@ -1572,18 +1572,16 @@
 (base64)
- %7B
  //Base6
@@ -1590,16 +1590,21 @@
 encoded%0A
+%09%09%09%7B%0A
 %09%09%09%09byte
@@ -1738,18 +1738,16 @@
 %0A%09%09%09else
- %7B
  //norma
@@ -1753,16 +1753,21 @@
 al path%0A
+%09%09%09%7B%0A
 %09%09%09%09Inpu
@@ -1908,17 +1908,17 @@
 rManager
-%09
+ 
 = Wallpa
@@ -1994,17 +1994,19 @@
 ap);%0A%09%09%7D
- 
+%0A%09%09
 catch (I
@@ -2018,17 +2018,19 @@
 ption e)
- 
+%0A%09%09
 %7B%0A%09%09%09// 
@@ -2089,16 +2089,13 @@
 ();%0A%09%09%7D%0A
-    
+%09
 %7D%0A%7D%0A

Update wallpaper.java