Merge pull request #4 from telerik/kkotorov/fix-for-taking-screenshots-in-crosswalk

Fix for taking screenshots on Android devices in Crosswalk
This commit is contained in:
Kaloyan Kotorov 2016-07-08 17:55:15 +03:00 коммит произвёл GitHub
Родитель e0070fd442 ec63276d0a
Коммит 70716a5a13
1 изменённых файлов: 62 добавлений и 2 удалений

Просмотреть файл

@ -12,12 +12,32 @@ import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.provider.Settings;
import android.graphics.Bitmap;
import android.view.View;
import android.app.AlertDialog;
import android.content.DialogInterface;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.CountDownLatch;
import com.telerik.widget.feedback.BitmapResolver;
import com.telerik.widget.feedback.RadFeedback;
public class AppFeedback extends CordovaPlugin implements RadFeedback.OnSendFeedbackFinishedListener {
private RadFeedback radFeedback;
private boolean shouldShowFeedback;
private static boolean isCrosswalk;
private CountDownLatch crosswalkLatch;
private Bitmap crosswalkScreenshot;
// Check if the app uses Crosswalk
static {
try {
Class.forName("org.crosswalk.engine.XWalkWebViewEngine");
isCrosswalk = true;
} catch (Exception ignore) {
}
}
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
@ -29,7 +49,7 @@ public class AppFeedback extends CordovaPlugin implements RadFeedback.OnSendFeed
int appResId = cordova.getActivity().getResources().getIdentifier(variableName, "string", cordova.getActivity().getPackageName());
String variableValue = cordova.getActivity().getString(appResId);
data.put(variableName, variableValue);
} catch(Exception ex) {
} catch (Exception ex) {
}
}
callbackContext.success(data);
@ -63,9 +83,49 @@ public class AppFeedback extends CordovaPlugin implements RadFeedback.OnSendFeed
this.radFeedback = RadFeedback.instance();
this.radFeedback.setOnFeedbackFinishedListener(this);
this.radFeedback.init(apiKey, apiBaseUrl, uid);
// Change the default BitmapResolver if the app uses Crosswalk
if (isCrosswalk) {
this.radFeedback.setBitmapResolver(new BitmapResolverCrosswalk());
}
}
}
private class BitmapResolverCrosswalk implements BitmapResolver {
@Override
public Bitmap getBitmapFromView(View view) {
AppFeedback.this.crosswalkLatch = new CountDownLatch(1);
webView.getPluginManager().postMessage("captureXWalkBitmap", this);
try {
AppFeedback.this.crosswalkLatch.await(5L, TimeUnit.SECONDS);
} catch (Exception e) {
AlertDialog.Builder latchErrorBuilder = new AlertDialog.Builder(AppFeedback.this.cordova.getActivity());
latchErrorBuilder.setTitle("AppFeedback Error");
latchErrorBuilder.setMessage("" + e);
latchErrorBuilder.setCancelable(false);
latchErrorBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog latchError = latchErrorBuilder.create();
latchError.show();
}
return AppFeedback.this.crosswalkScreenshot;
}
}
// Handle the screenshot from Crosswalk
@Override
public Object onMessage(String id, Object data) {
if (id.equals("onGotXWalkBitmap")) {
this.crosswalkScreenshot = (Bitmap) data;
this.crosswalkLatch.countDown();
}
return null;
}
private void showFeedback() {
if (this.radFeedback != null && this.shouldShowFeedback == true) {
this.shouldShowFeedback = false;