This source file includes following definitions.
- showKeyboard
- hideKeyboard
- isKeyboardShowing
- insertBefore
- insertAfter
- insertView
- generateScaledScreenshot
- prepareViewHierarchyForScreenshot
package org.chromium.ui;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
public class UiUtils {
private static final String TAG = "UiUtils";
private UiUtils() {
}
private static final float KEYBOARD_DETECT_BOTTOM_THRESHOLD_DP = 100;
public static void showKeyboard(View view) {
InputMethodManager imm =
(InputMethodManager) view.getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
public static boolean hideKeyboard(View view) {
InputMethodManager imm =
(InputMethodManager) view.getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
return imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
public static boolean isKeyboardShowing(Context context, View view) {
View rootView = view.getRootView();
if (rootView == null) return false;
Rect appRect = new Rect();
rootView.getWindowVisibleDisplayFrame(appRect);
final float screenHeight = context.getResources().getDisplayMetrics().heightPixels;
final float bottomMargin = Math.abs(appRect.bottom - screenHeight);
final float density = context.getResources().getDisplayMetrics().density;
return bottomMargin > KEYBOARD_DETECT_BOTTOM_THRESHOLD_DP * density;
}
public static int insertBefore(ViewGroup container, View newView, View existingView) {
return insertView(container, newView, existingView, false);
}
public static int insertAfter(ViewGroup container, View newView, View existingView) {
return insertView(container, newView, existingView, true);
}
private static int insertView(
ViewGroup container, View newView, View existingView, boolean after) {
int index = container.indexOfChild(newView);
if (index >= 0) return index;
index = container.indexOfChild(existingView);
if (index < 0) return -1;
if (after) index++;
container.addView(newView, index);
return index;
}
public static Bitmap generateScaledScreenshot(
View currentView, int maximumDimension, Bitmap.Config bitmapConfig) {
Bitmap screenshot = null;
boolean drawingCacheEnabled = currentView.isDrawingCacheEnabled();
try {
prepareViewHierarchyForScreenshot(currentView, true);
if (!drawingCacheEnabled) currentView.setDrawingCacheEnabled(true);
Bitmap originalBitmap = currentView.getDrawingCache();
if (originalBitmap != null) {
double originalHeight = originalBitmap.getHeight();
double originalWidth = originalBitmap.getWidth();
int newWidth = (int) originalWidth;
int newHeight = (int) originalHeight;
if (maximumDimension > 0) {
double scale = maximumDimension / Math.max(originalWidth, originalHeight);
newWidth = (int) Math.round(originalWidth * scale);
newHeight = (int) Math.round(originalHeight * scale);
}
Bitmap scaledScreenshot =
Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, true);
if (scaledScreenshot.getConfig() != bitmapConfig) {
screenshot = scaledScreenshot.copy(bitmapConfig, false);
scaledScreenshot.recycle();
scaledScreenshot = null;
} else {
screenshot = scaledScreenshot;
}
} else if (currentView.getMeasuredHeight() > 0 && currentView.getMeasuredWidth() > 0) {
double originalHeight = currentView.getMeasuredHeight();
double originalWidth = currentView.getMeasuredWidth();
int newWidth = (int) originalWidth;
int newHeight = (int) originalHeight;
if (maximumDimension > 0) {
double scale = maximumDimension / Math.max(originalWidth, originalHeight);
newWidth = (int) Math.round(originalWidth * scale);
newHeight = (int) Math.round(originalHeight * scale);
}
Bitmap bitmap = Bitmap.createBitmap(newWidth, newHeight, bitmapConfig);
Canvas canvas = new Canvas(bitmap);
canvas.scale((float) (newWidth / originalWidth),
(float) (newHeight / originalHeight));
currentView.draw(canvas);
screenshot = bitmap;
}
} catch (OutOfMemoryError e) {
Log.d(TAG, "Unable to capture screenshot and scale it down." + e.getMessage());
} finally {
if (!drawingCacheEnabled) currentView.setDrawingCacheEnabled(false);
prepareViewHierarchyForScreenshot(currentView, false);
}
return screenshot;
}
private static void prepareViewHierarchyForScreenshot(View view, boolean takingScreenshot) {
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
prepareViewHierarchyForScreenshot(viewGroup.getChildAt(i), takingScreenshot);
}
} else if (view instanceof SurfaceView) {
view.setWillNotDraw(!takingScreenshot);
}
}
}