Chromium Code Reviews| Index: ui/android/java/src/org/chromium/ui/ActivityWindowAndroid.java |
| diff --git a/ui/android/java/src/org/chromium/ui/ActivityWindowAndroid.java b/ui/android/java/src/org/chromium/ui/ActivityWindowAndroid.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d02111b0ebaf2b6ede07db6b5e34ba812c249624 |
| --- /dev/null |
| +++ b/ui/android/java/src/org/chromium/ui/ActivityWindowAndroid.java |
| @@ -0,0 +1,111 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
|
newt (away)
2013/10/27 19:57:51
don't need "(c)"
michaelbai
2013/10/28 17:40:38
Done.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.ui; |
| + |
| +import android.app.Activity; |
| +import android.content.ActivityNotFoundException; |
| +import android.content.Context; |
| +import android.content.Intent; |
| +import android.graphics.Bitmap; |
| +import android.graphics.Rect; |
| +import android.util.Log; |
| +import android.view.View; |
| + |
| +import java.io.ByteArrayOutputStream; |
| + |
| +/** |
| + * The class provides the WindowAndroid's implementation which requires |
| + * Activity Instance. |
| + * Only instance this class when you need the implemented features. |
| + */ |
| +public class ActivityWindowAndroid extends WindowAndroid{ |
|
newt (away)
2013/10/27 19:57:51
space before {
michaelbai
2013/10/28 17:40:38
Done.
|
| + // Constants used for intent request code bounding. |
| + private static final int REQUEST_CODE_PREFIX = 1000; |
| + private static final int REQUEST_CODE_RANGE_SIZE = 100; |
| + private static final String TAG = "ActivityWindowAndroid"; |
| + |
| + private Activity mActivity; |
| + private int mNextRequestCode = 0; |
| + |
| + public ActivityWindowAndroid(Activity activity) { |
| + super(activity.getApplicationContext()); |
| + mActivity = activity; |
| + } |
| + |
| + @Override |
| + public boolean showIntent(Intent intent, IntentCallback callback, int errorId) { |
| + int requestCode = REQUEST_CODE_PREFIX + mNextRequestCode; |
| + mNextRequestCode = (mNextRequestCode + 1) % REQUEST_CODE_RANGE_SIZE; |
| + |
| + try { |
| + mActivity.startActivityForResult(intent, requestCode); |
| + } catch (ActivityNotFoundException e) { |
| + return false; |
| + } |
| + |
| + mOutstandingIntents.put(requestCode, callback); |
| + mIntentErrors.put(requestCode, mApplicationContext.getString(errorId)); |
| + |
| + return true; |
| + } |
| + |
| + @Override |
| + public boolean onActivityResult(int requestCode, int resultCode, Intent data) { |
| + IntentCallback callback = mOutstandingIntents.get(requestCode); |
| + mOutstandingIntents.delete(requestCode); |
| + String errorMessage = mIntentErrors.remove(requestCode); |
| + |
| + if (callback != null) { |
| + callback.onIntentCompleted(this, resultCode, |
| + mApplicationContext.getContentResolver(), data); |
| + return true; |
| + } else { |
| + if (errorMessage != null) { |
| + showCallbackNonExistentError(errorMessage); |
| + return true; |
| + } |
| + } |
| + return false; |
| + } |
| + |
| + @Override |
| + @Deprecated |
|
newt (away)
2013/10/27 19:57:51
this isn't deprecated at all, is it? we are guaran
michaelbai
2013/10/28 17:40:38
We shouldn't pass the Activity Context outside of
|
| + public Context getContext() { |
| + return mActivity; |
| + } |
| + |
| + /** |
| + * Returns a PNG-encoded screenshot of the the window region at (|windowX|, |
| + * |windowY|) with the size |width| by |height| pixels. |
| + */ |
| + @Override |
| + public byte[] grabSnapshot(int windowX, int windowY, int width, int height) { |
| + try { |
| + // Take a screenshot of the root activity view. This generally includes UI |
| + // controls such as the URL bar and OS windows such as the status bar. |
| + View rootView = mActivity.findViewById(android.R.id.content).getRootView(); |
| + Bitmap bitmap = UiUtils.generateScaledScreenshot(rootView, 0, Bitmap.Config.ARGB_8888); |
| + if (bitmap == null) return null; |
| + |
| + // Clip the result into the requested region. |
| + if (windowX > 0 || windowY > 0 || width != bitmap.getWidth() || |
| + height != bitmap.getHeight()) { |
| + Rect clip = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); |
| + clip.intersect(windowX, windowY, windowX + width, windowY + height); |
| + bitmap = Bitmap.createBitmap( |
| + bitmap, clip.left, clip.top, clip.width(), clip.height()); |
| + } |
| + |
| + // Compress the result into a PNG. |
| + ByteArrayOutputStream result = new ByteArrayOutputStream(); |
| + if (!bitmap.compress(Bitmap.CompressFormat.PNG, 100, result)) return null; |
| + bitmap.recycle(); |
| + return result.toByteArray(); |
| + } catch (OutOfMemoryError e) { |
| + Log.e(TAG, "Out of memory while grabbing window snapshot.", e); |
| + return null; |
| + } |
| + } |
| +} |