Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.ui; | |
| 6 | |
| 7 import android.app.Activity; | |
| 8 import android.content.ActivityNotFoundException; | |
| 9 import android.content.Context; | |
| 10 import android.content.Intent; | |
| 11 import android.graphics.Bitmap; | |
| 12 import android.graphics.Rect; | |
| 13 import android.util.Log; | |
| 14 import android.view.View; | |
| 15 | |
| 16 import java.io.ByteArrayOutputStream; | |
| 17 | |
| 18 /** | |
| 19 * The class provides the WindowAndroid's implementation which requires | |
| 20 * Activity Instance. | |
| 21 * Only instance this class when you need the implemented features. | |
|
joth
2013/11/01 18:45:01
instance => instantiate
Although this statement i
michaelbai
2013/11/01 21:27:05
Done
| |
| 22 */ | |
| 23 public class ActivityWindowAndroid extends WindowAndroid { | |
| 24 // Constants used for intent request code bounding. | |
| 25 private static final int REQUEST_CODE_PREFIX = 1000; | |
| 26 private static final int REQUEST_CODE_RANGE_SIZE = 100; | |
| 27 private static final String TAG = "ActivityWindowAndroid"; | |
| 28 | |
| 29 private Activity mActivity; | |
| 30 private int mNextRequestCode = 0; | |
| 31 | |
| 32 public ActivityWindowAndroid(Activity activity) { | |
| 33 super(activity.getApplicationContext()); | |
| 34 mActivity = activity; | |
| 35 } | |
| 36 | |
| 37 @Override | |
| 38 public boolean showIntent(Intent intent, IntentCallback callback, int errorI d) { | |
| 39 int requestCode = REQUEST_CODE_PREFIX + mNextRequestCode; | |
| 40 mNextRequestCode = (mNextRequestCode + 1) % REQUEST_CODE_RANGE_SIZE; | |
| 41 | |
| 42 try { | |
| 43 mActivity.startActivityForResult(intent, requestCode); | |
| 44 } catch (ActivityNotFoundException e) { | |
| 45 return false; | |
| 46 } | |
| 47 | |
| 48 mOutstandingIntents.put(requestCode, callback); | |
| 49 mIntentErrors.put(requestCode, mApplicationContext.getString(errorId)); | |
| 50 | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 @Override | |
| 55 public boolean onActivityResult(int requestCode, int resultCode, Intent data ) { | |
| 56 IntentCallback callback = mOutstandingIntents.get(requestCode); | |
| 57 mOutstandingIntents.delete(requestCode); | |
| 58 String errorMessage = mIntentErrors.remove(requestCode); | |
| 59 | |
| 60 if (callback != null) { | |
| 61 callback.onIntentCompleted(this, resultCode, | |
| 62 mApplicationContext.getContentResolver(), data); | |
| 63 return true; | |
| 64 } else { | |
| 65 if (errorMessage != null) { | |
| 66 showCallbackNonExistentError(errorMessage); | |
| 67 return true; | |
| 68 } | |
| 69 } | |
| 70 return false; | |
| 71 } | |
| 72 | |
| 73 @Override | |
| 74 @Deprecated | |
| 75 public Context getContext() { | |
| 76 return mActivity; | |
| 77 } | |
| 78 | |
| 79 /** | |
| 80 * Returns a PNG-encoded screenshot of the the window region at (|windowX|, | |
| 81 * |windowY|) with the size |width| by |height| pixels. | |
| 82 */ | |
| 83 @Override | |
| 84 public byte[] grabSnapshot(int windowX, int windowY, int width, int height) { | |
| 85 try { | |
| 86 // Take a screenshot of the root activity view. This generally inclu des UI | |
| 87 // controls such as the URL bar and OS windows such as the status ba r. | |
| 88 View rootView = mActivity.findViewById(android.R.id.content).getRoot View(); | |
| 89 Bitmap bitmap = UiUtils.generateScaledScreenshot(rootView, 0, Bitmap .Config.ARGB_8888); | |
| 90 if (bitmap == null) return null; | |
| 91 | |
| 92 // Clip the result into the requested region. | |
| 93 if (windowX > 0 || windowY > 0 || width != bitmap.getWidth() || | |
| 94 height != bitmap.getHeight()) { | |
| 95 Rect clip = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight() ); | |
| 96 clip.intersect(windowX, windowY, windowX + width, windowY + heig ht); | |
| 97 bitmap = Bitmap.createBitmap( | |
| 98 bitmap, clip.left, clip.top, clip.width(), clip.height() ); | |
| 99 } | |
| 100 | |
| 101 // Compress the result into a PNG. | |
| 102 ByteArrayOutputStream result = new ByteArrayOutputStream(); | |
| 103 if (!bitmap.compress(Bitmap.CompressFormat.PNG, 100, result)) return null; | |
| 104 bitmap.recycle(); | |
| 105 return result.toByteArray(); | |
| 106 } catch (OutOfMemoryError e) { | |
| 107 Log.e(TAG, "Out of memory while grabbing window snapshot.", e); | |
| 108 return null; | |
| 109 } | |
| 110 } | |
| 111 } | |
| OLD | NEW |