public static Bitmap takeScreenShot(Activity activity, View viewFragment) { // View是你需要截图的View View view = activity.getWindow().getDecorView(); if (view == null) { System.out.println("view is null"); } else { System.out.println("view:" + view.toString()); } // View view = viewFragment; view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap b1 = view.getDrawingCache(); // 获取状态栏高度 Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; // 获取屏幕长和高 int width = activity.getWindowManager().getDefaultDisplay().getWidth(); int height = activity.getWindowManager().getDefaultDisplay().getHeight(); Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return b; }
public static Bitmap screenshot(View view, boolean hasstatubar) { if (view == null) { return null; } view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap cachebmp = view.getDrawingCache(); Rect rect = new Rect(); view.getWindowVisibleDisplayFrame(rect); int statusBarHeights = rect.top; DisplayMetrics m = view.getResources().getDisplayMetrics(); // 获取屏幕宽和高 int widths = m.widthPixels; int heights = m.heightPixels; Bitmap bmp = null; if (cachebmp != null) { if (hasstatubar) { bmp = Bitmap.createBitmap(cachebmp, 0, 0, widths, heights); } else { bmp = Bitmap.createBitmap(cachebmp, 0, statusBarHeights, widths, heights - statusBarHeights); } } view.destroyDrawingCache(); return bmp; }
/** * 截屏 * * @param activity * @return */ public static Bitmap getScreenViewBitmap(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); return bitmap; }
public static Bitmap convertViewToBitmap(View paramView) { paramView.measure( View.MeasureSpec.makeMeasureSpec(0, 0), View.MeasureSpec.makeMeasureSpec(0, 0)); paramView.layout(0, 0, paramView.getMeasuredWidth(), paramView.getMeasuredHeight()); paramView.buildDrawingCache(); return paramView.getDrawingCache(); }
/** Draw the view into a bitmap. */ private static Bitmap getViewBitmap(View v) { v.clearFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent // for the duration of this operation int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException()); return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; }
public static Bitmap convertViewToBitmap(View view) { view.measure( View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.buildDrawingCache(); return view.getDrawingCache(); }
private Bitmap getChildDrawingCache(final View child) { Bitmap bitmap = child.getDrawingCache(); if (bitmap == null) { child.setDrawingCacheEnabled(true); child.buildDrawingCache(); bitmap = child.getDrawingCache(); } return bitmap; }
@Override protected void setChildrenDrawingCacheEnabled(boolean enabled) { final int count = getChildCount(); for (int i = 0; i < count; i++) { final View view = getChildAt(i); view.setDrawingCacheEnabled(enabled); // Update the drawing caches view.buildDrawingCache(true); } }
/** * 获取当前屏幕截图,包含状态栏 * * @param activity activity * @return Bitmap */ public static Bitmap captureWithStatusBar(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); int width = getScreenWidth(activity); int height = getScreenHeight(activity); Bitmap bp = Bitmap.createBitmap(bmp, 0, 0, width, height); view.destroyDrawingCache(); return bp; }
public static void setActivity(Activity act) { host = act; ViewTools.setActivity(act); View rootView = host.getWindow().getDecorView().getRootView(); rootView.setDrawingCacheEnabled(true); rootView.buildDrawingCache(); // host.getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new // LayoutObserver()); }
/** * 获取当前屏幕截图,包含状态栏 * * @return */ public Bitmap snapShotWithStatusBar() { View view = getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); int width = getContainerWidth(); int height = getContainerHeight(); Bitmap bp = null; bp = Bitmap.createBitmap(bmp, 0, 0, width, height); view.destroyDrawingCache(); return bp; }
public void shareImage() { // View view = findViewById(R.id.layout);// your layout id // view = view.getRootView(); View view = getWindow().getDecorView(); String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { File picDir = new File(Environment.getExternalStorageDirectory() + "/myPic"); if (!picDir.exists()) { picDir.mkdir(); } view.setDrawingCacheEnabled(true); view.buildDrawingCache(true); Bitmap bitmap = view.getDrawingCache(); // Date date = new Date(); String fileName = "mylove" + ".png"; File picFile = new File(picDir + "/" + fileName); try { picFile.createNewFile(); FileOutputStream picOut = new FileOutputStream(picFile); // bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), // (int) (bitmap.getHeight() / 1.2)); boolean saved = bitmap.compress(CompressFormat.PNG, 100, picOut); if (saved) { Log.d("CORRECTO", "Image saved to your device Pictures " + "directory!"); } else { Log.e("ERROR", "no se pudo guardar archivo " + picFile.getAbsolutePath()); } picOut.close(); } catch (Exception e) { e.printStackTrace(); } view.destroyDrawingCache(); /* Intent sharingIntent = new Intent( android.content.Intent.ACTION_SEND); sharingIntent.setType("image/png"); sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(picFile.getAbsolutePath())); sharingIntent.setData(Uri.fromFile(picFile)); startActivity(Intent.createChooser(sharingIntent, "Share via")); */ Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); // shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + picFile.getAbsolutePath())); shareIntent.setType("image/png"); startActivity(Intent.createChooser(shareIntent, "Compartir vía")); } else { Log.e("ERROR", "pa q ptas"); } }
/** * 获取当前屏幕截图,不包含状态栏 * * @return */ public Bitmap snapShotWithoutStatusBar() { View view = getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); Rect frame = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; int width = getContainerWidth(); int height = getContainerHeight(); Bitmap bp = null; bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return bp; }
/** 把View对象转换成bitmap */ public static Bitmap convertViewToBitmap(View view) { view.measure( MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); if (bitmap != null) { System.out.println("这不是nullde1"); Log.d("nullde1", "nullde1"); } else { System.out.println("这nullnulllnulnlul"); } return bitmap; }
public static Bitmap takeScreenShot(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap drawingCache = view.getDrawingCache(); Rect rect = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); int statusBarHeight = rect.top; int width = activity.getWindowManager().getDefaultDisplay().getWidth(); int height = activity.getWindowManager().getDefaultDisplay().getHeight(); Bitmap bitmap = Bitmap.createBitmap(drawingCache, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return bitmap; }
@Override public boolean onLongClick(View view) { if (((BrickAdapter) getAdapter()).getActionMode() != BrickAdapter.ActionModeEnum.NO_ACTION) { return true; } ((BrickAdapter) getAdapter()).isDragging = true; ((BrickAdapter) getAdapter()).setSpinnersEnabled(false); int itemPosition = calculateItemPositionAndTouchPointY(view); boolean drawingCacheEnabled = view.isDrawingCacheEnabled(); view.setDrawingCacheEnabled(true); view.measure( MeasureSpec.makeMeasureSpec(ScreenValues.SCREEN_WIDTH, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec( Utils.getPhysicalPixels(WIDTH_OF_BRICK_PREVIEW_IMAGE, getContext()), MeasureSpec.AT_MOST)); view.layout(0, 0, ScreenValues.SCREEN_WIDTH, view.getMeasuredHeight()); view.setDrawingCacheBackgroundColor(Color.TRANSPARENT); view.buildDrawingCache(true); Bitmap bitmap; if (view.getDrawingCache() == null) { view.setDrawingCacheEnabled(drawingCacheEnabled); bitmap = getBitmapFromView(view, getMeasuredWidth(), view.getHeight()); } else { bitmap = Bitmap.createBitmap(view.getDrawingCache()); } view.setDrawingCacheEnabled(drawingCacheEnabled); startDragging(bitmap, touchPointY); if (!dragNewBrick) { setDragViewAnimation(0); dragNewBrick = false; } dragAndDropListener.drag(itemPosition, itemPosition); dimBackground = true; previousItemPosition = itemPosition; return true; }
public static Bitmap createDrawableFromView(Context context, View view) { DisplayMetrics displayMetrics = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); view.setLayoutParams( new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, AbsoluteLayout.LayoutParams.WRAP_CONTENT)); view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels); view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels); view.buildDrawingCache(); Bitmap bitmap = Bitmap.createBitmap( view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); return bitmap; }
/** * Returns a bitmap of a given View. * * @param view the view to save a bitmap from * @return a bitmap of the given view */ private Bitmap getBitmapOfView(final View view) { view.destroyDrawingCache(); view.buildDrawingCache(false); Bitmap orig = view.getDrawingCache(); Bitmap.Config config = null; if (orig == null) { return null; } config = orig.getConfig(); if (config == null) { config = Bitmap.Config.ARGB_8888; } Bitmap b = orig.copy(config, false); orig.recycle(); view.destroyDrawingCache(); return b; }
// Convert a view to bitmap private Bitmap createDrawableFromView(View view) { Log.d(TAG, "createDrawableFromView"); DisplayMetrics displayMetrics = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); view.setLayoutParams( new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT)); view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels); view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels); view.buildDrawingCache(); Bitmap bitmap = Bitmap.createBitmap( view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); return bitmap; }
/** * 截屏 * * @param activity * @return */ public static Bitmap getScreenShot(Activity activity) { // 获取windows中最顶层的view View view = activity.getWindow().getDecorView(); view.buildDrawingCache(); // 获取状态栏高度 Rect rect = new Rect(); view.getWindowVisibleDisplayFrame(rect); // 允许当前窗口保存缓存信息 view.setDrawingCacheEnabled(true); // 去掉状态栏 Bitmap bmp = Bitmap.createBitmap( view.getDrawingCache(), 0, 0, view.getDrawingCache().getWidth(), view.getDrawingCache().getHeight()); // 销毁缓存信息 view.destroyDrawingCache(); return bmp; }
@Nullable @Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mView = inflater.inflate(R.layout.menu, null); mGroup = (ViewGroup) getActivity().getWindow().getDecorView(); mGroup.addView(mView); Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.push_left_in); ButterKnife.inject(this, mView); menuLayout.startAnimation(animation); setListener(); blurView.setDrawingCacheEnabled(true); blurView.buildDrawingCache(); menuBlur.setImageBitmap(convertToBlur(blurView.getDrawingCache(), getActivity())); return super.onCreateView(inflater, container, savedInstanceState); }
public static Bitmap takeScreenShot(Activity activity) { // View是你需要截图的View View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap b1 = view.getDrawingCache(); // 获取状态栏高度 Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; // 获取屏幕长和高 int width = activity.getWindowManager().getDefaultDisplay().getWidth(); int height = activity.getWindowManager().getDefaultDisplay().getHeight(); // 去掉标题栏 // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455); Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return b; }
private void prepareContent() { if (mAnimating) { return; } // Something changed in the content, we need to honor the layout request // before creating the cached bitmap final View content = mContent; if (content.isLayoutRequested()) { if (mVertical) { final int childHeight = mHandleHeight; int height = mBottom - mTop - childHeight - mTopOffset; content.measure( MeasureSpec.makeMeasureSpec(mRight - mLeft, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); content.layout( 0, mTopOffset + childHeight, content.getMeasuredWidth(), mTopOffset + childHeight + content.getMeasuredHeight()); } else { final int childWidth = mHandle.getWidth(); int width = mRight - mLeft - childWidth - mTopOffset; content.measure( MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(mBottom - mTop, MeasureSpec.EXACTLY)); content.layout( childWidth + mTopOffset, 0, mTopOffset + childWidth + content.getMeasuredWidth(), content.getMeasuredHeight()); } } // Try only once... we should really loop but it's not a big deal // if the draw was cancelled, it will only be temporary anyway content.getViewTreeObserver().dispatchOnPreDraw(); content.buildDrawingCache(); content.setVisibility(View.GONE); }
/** Draw the view into a bitmap. */ private Bitmap getViewBitmap(View v) { v.clearFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent // for the duration of this operation int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); // float xScale = v.getScaleX(); // float yScale = v.getScaleY(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException()); return null; } // if(xScale != 1.0f || xScale != 1.0f ){ // cacheBitmap = getResizedBitmap(cacheBitmap, (int)(cacheBitmap.getHeight()*yScale), // (int)(cacheBitmap.getWidth()*xScale)); // } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; }
// get the screen shot of Activity,save it as png public static Bitmap takeScreenShot(Activity activity) { // The view you will take screenshot View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap wholeBitmap = view.getDrawingCache(); // Get the height of device's statusBar Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; Log.i("TAG", "" + statusBarHeight); // Get the height and width of device screen int width = activity.getWindowManager().getDefaultDisplay().getWidth(); int height = activity.getWindowManager().getDefaultDisplay().getHeight(); // remove the area of statusbar Bitmap lastBitmap = Bitmap.createBitmap(wholeBitmap, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return lastBitmap; }
public static Bitmap getViewBitmap(View v) { v.clearFocus(); v.setPressed(false); // 能画缓存就返回false boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap cacheBitmap = v.getDrawingCache(); if (cacheBitmap == null) { return null; } Bitmap bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; }
@Override public void onClick(final View view) { if (!viewSwitchLock.tryLock()) { return; } animatedBricks.clear(); final int itemPosition = calculateItemPositionAndTouchPointY(view); final List<CharSequence> items = new ArrayList<CharSequence>(); if (brickList.get(itemPosition) instanceof ScriptBrick) { int scriptIndex = getScriptIndexFromProject(itemPosition); ProjectManager.getInstance().setCurrentScript(sprite.getScript(scriptIndex)); } if (!(brickList.get(itemPosition) instanceof DeadEndBrick) && !(brickList.get(itemPosition) instanceof ScriptBrick)) { items.add(context.getText(R.string.brick_context_dialog_move_brick)); } if ((brickList.get(itemPosition) instanceof UserBrick)) { items.add(context.getText(R.string.brick_context_dialog_show_source)); } if (brickList.get(itemPosition) instanceof NestingBrick) { items.add(context.getText(R.string.brick_context_dialog_animate_bricks)); } if (!(brickList.get(itemPosition) instanceof ScriptBrick)) { items.add(context.getText(R.string.brick_context_dialog_copy_brick)); items.add(context.getText(R.string.brick_context_dialog_delete_brick)); } else { items.add(context.getText(R.string.brick_context_dialog_delete_script)); } if (brickHasAFormula(brickList.get(itemPosition))) { items.add(context.getText(R.string.brick_context_dialog_formula_edit_brick)); } AlertDialog.Builder builder = new AlertDialog.Builder(context); boolean drawingCacheEnabled = view.isDrawingCacheEnabled(); view.setDrawingCacheEnabled(true); view.setDrawingCacheBackgroundColor(Color.TRANSPARENT); view.buildDrawingCache(true); if (view.getDrawingCache() != null) { Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache()); view.setDrawingCacheEnabled(drawingCacheEnabled); ImageView imageView = dragAndDropListView.getGlowingBorder(bitmap); builder.setCustomTitle(imageView); } builder.setItems( items.toArray(new CharSequence[items.size()]), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int item) { CharSequence clickedItemText = items.get(item); if (clickedItemText.equals(context.getText(R.string.brick_context_dialog_move_brick))) { view.performLongClick(); } else if (clickedItemText.equals( context.getText(R.string.brick_context_dialog_show_source))) { launchAddBrickAndSelectBrickAt(context, itemPosition); } else if (clickedItemText.equals( context.getText(R.string.brick_context_dialog_copy_brick))) { copyBrickListAndProject(itemPosition); } else if (clickedItemText.equals( context.getText(R.string.brick_context_dialog_delete_brick)) || clickedItemText.equals( context.getText(R.string.brick_context_dialog_delete_script))) { showConfirmDeleteDialog(itemPosition); } else if (clickedItemText.equals( context.getText(R.string.brick_context_dialog_animate_bricks))) { int itemPosition = calculateItemPositionAndTouchPointY(view); Brick brick = brickList.get(itemPosition); if (brick instanceof NestingBrick) { List<NestingBrick> list = ((NestingBrick) brick).getAllNestingBrickParts(true); for (NestingBrick tempBrick : list) { animatedBricks.add((Brick) tempBrick); } } notifyDataSetChanged(); } else if (clickedItemText.equals( context.getText(R.string.brick_context_dialog_formula_edit_brick))) { clickedEditFormula(brickList.get(itemPosition), view); } } }); alertDialog = builder.create(); if ((selectMode == ListView.CHOICE_MODE_NONE)) { alertDialog.show(); } }
/** * Returns a bitmap of a given View. * * @param view the view to save a bitmap from * @return a bitmap of the given view */ private Bitmap getBitmapOfView(final View view) { view.destroyDrawingCache(); view.buildDrawingCache(false); Bitmap b = view.getDrawingCache(); return b; }
/** * Calls {@link View#buildDrawingCache(boolean)}. If you call this manually without calling {@link * View#setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you should clean up by * calling {@link View#destroyDrawingCache()} afterward. * * @param view what to build a drawing cache of */ public static void buildDrawingCache(View view, boolean autoScale) { view.buildDrawingCache(autoScale); }
@SuppressWarnings({"rawtypes", "unchecked"}) public Response serve( String uri, String method, Properties header, Properties params, Properties files) { System.out.println("URI: " + uri); System.out.println("params: " + params); if (uri.endsWith("/ping")) { return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, "pong"); } else if (uri.endsWith("/dump")) { FranklyResult errorResult = null; try { String json = params.getProperty("json"); if (json == null) { Map<?, ?> dumpTree = new ViewDump().dumpWithoutElements(); return new NanoHTTPD.Response( HTTP_OK, "application/json;charset=utf-8", JSONUtils.asJson(dumpTree)); } else { ObjectMapper mapper = new ObjectMapper(); Map dumpSpec = mapper.readValue(json, Map.class); List<Integer> path = (List<Integer>) dumpSpec.get("path"); if (path == null) { Map<?, ?> dumpTree = new ViewDump().dumpWithoutElements(); return new NanoHTTPD.Response( HTTP_OK, "application/json;charset=utf-8", JSONUtils.asJson(dumpTree)); } Map<?, ?> dumpTree = new ViewDump().dumpPathWithoutElements(path); if (dumpTree == null) { return new NanoHTTPD.Response(HTTP_NOTFOUND, "application/json;charset=utf-8", "{}"); } else { return new NanoHTTPD.Response( HTTP_OK, "application/json;charset=utf-8", JSONUtils.asJson(dumpTree)); } } } catch (Exception e) { e.printStackTrace(); errorResult = FranklyResult.fromThrowable(e); } return new NanoHTTPD.Response( HTTP_INTERNALERROR, "application/json;charset=utf-8", errorResult.asJson()); } else if (uri.endsWith("/map")) { FranklyResult errorResult = null; try { String commandString = params.getProperty("json"); ObjectMapper mapper = new ObjectMapper(); Map command = mapper.readValue(commandString, Map.class); String uiQuery = (String) command.get("query"); Map op = (Map) command.get("operation"); @SuppressWarnings("unused") // TODO: support other methods, e.g., flash String methodName = (String) op.get("method_name"); List arguments = (List) op.get("arguments"); // For now we only support query List queryResult = new Query(uiQuery, arguments).executeQuery(); return new NanoHTTPD.Response( HTTP_OK, "application/json;charset=utf-8", FranklyResult.successResult(queryResult).asJson()); } catch (Exception e) { e.printStackTrace(); errorResult = FranklyResult.fromThrowable(e); } return new NanoHTTPD.Response( HTTP_OK, "application/json;charset=utf-8", errorResult.asJson()); } else if (uri.endsWith("/query")) { return new Response( HTTP_BADREQUEST, MIME_PLAINTEXT, "/query endpoint is discontinued - use /map with operation query"); } else if (uri.endsWith("/kill")) { lock.lock(); try { running = false; System.out.println("Stopping test server"); stop(); shutdownCondition.signal(); return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, "Affirmative!"); } finally { lock.unlock(); } } else if (uri.endsWith("/ready")) { return new Response(HTTP_OK, MIME_HTML, Boolean.toString(ready)); } else if (uri.endsWith("/screenshot")) { try { Bitmap bitmap; View rootView = getRootView(); rootView.setDrawingCacheEnabled(true); rootView.buildDrawingCache(true); bitmap = Bitmap.createBitmap(rootView.getDrawingCache()); rootView.setDrawingCacheEnabled(false); ByteArrayOutputStream out = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 90, out); return new NanoHTTPD.Response( HTTP_OK, "image/png", new ByteArrayInputStream(out.toByteArray())); } catch (Exception e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); return new NanoHTTPD.Response(HTTP_INTERNALERROR, null, sw.toString()); } } System.out.println("header: " + header); System.out.println("params: " + params); Enumeration<String> propertyNames = (Enumeration<String>) params.propertyNames(); while (propertyNames.hasMoreElements()) { String s = propertyNames.nextElement(); System.out.println("ProP " + s + " = " + params.getProperty(s)); } System.out.println("files: " + files); String commandString = params.getProperty("json"); System.out.println("command: " + commandString); String result = toJson(runCommand(commandString)); System.out.println("result:" + result); return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, result); }