OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.ui; | 5 package org.chromium.ui; |
6 | 6 |
7 import android.app.Activity; | |
8 import android.content.ActivityNotFoundException; | |
9 import android.content.ContentResolver; | 7 import android.content.ContentResolver; |
10 import android.content.Context; | 8 import android.content.Context; |
11 import android.content.Intent; | 9 import android.content.Intent; |
12 import android.graphics.Bitmap; | |
13 import android.graphics.Rect; | |
14 import android.os.Bundle; | 10 import android.os.Bundle; |
15 import android.util.Log; | 11 import android.util.Log; |
16 import android.util.SparseArray; | 12 import android.util.SparseArray; |
17 import android.view.View; | |
18 import android.widget.Toast; | 13 import android.widget.Toast; |
19 | 14 |
20 import java.io.ByteArrayOutputStream; | |
21 import java.util.HashMap; | 15 import java.util.HashMap; |
22 | 16 |
23 import org.chromium.base.CalledByNative; | 17 import org.chromium.base.CalledByNative; |
24 import org.chromium.base.JNINamespace; | 18 import org.chromium.base.JNINamespace; |
25 | 19 |
26 /** | 20 /** |
27 * The window base class that has the minimum functionality. | 21 * The window base class that has the minimum functionality. |
28 */ | 22 */ |
29 @JNINamespace("ui") | 23 @JNINamespace("ui") |
30 public class WindowAndroid { | 24 public class WindowAndroid { |
25 private static void logActivityRequiredWarning() { | |
26 Log.w(TAG, "A Context of type Activity is required."); | |
27 } | |
31 | 28 |
32 private static final String TAG = "WindowAndroid"; | 29 private static final String TAG = "WindowAndroid"; |
33 | 30 |
34 // Native pointer to the c++ WindowAndroid object. | 31 // Native pointer to the c++ WindowAndroid object. |
35 private int mNativeWindowAndroid = 0; | 32 private int mNativeWindowAndroid = 0; |
36 | 33 |
37 // Constants used for intent request code bounding. | |
38 private static final int REQUEST_CODE_PREFIX = 1000; | |
39 private static final int REQUEST_CODE_RANGE_SIZE = 100; | |
40 // A string used as a key to store intent errors in a bundle | 34 // A string used as a key to store intent errors in a bundle |
41 static final String WINDOW_CALLBACK_ERRORS = "window_callback_errors"; | 35 static final String WINDOW_CALLBACK_ERRORS = "window_callback_errors"; |
42 | 36 |
43 private int mNextRequestCode = 0; | |
44 protected Activity mActivity; | |
45 protected Context mApplicationContext; | 37 protected Context mApplicationContext; |
46 protected SparseArray<IntentCallback> mOutstandingIntents; | 38 protected SparseArray<IntentCallback> mOutstandingIntents; |
47 protected HashMap<Integer, String> mIntentErrors; | 39 protected HashMap<Integer, String> mIntentErrors; |
48 | 40 |
49 /** | 41 /** |
50 * @param activity | 42 * @param context, the application context.. |
51 */ | 43 */ |
52 public WindowAndroid(Activity activity) { | 44 public WindowAndroid(Context context) { |
53 mActivity = activity; | 45 assert context == context.getApplicationContext(); |
54 mApplicationContext = mActivity.getApplicationContext(); | 46 mApplicationContext = context; |
55 mOutstandingIntents = new SparseArray<IntentCallback>(); | 47 mOutstandingIntents = new SparseArray<IntentCallback>(); |
56 mIntentErrors = new HashMap<Integer, String>(); | 48 mIntentErrors = new HashMap<Integer, String>(); |
57 | |
58 } | 49 } |
59 | 50 |
60 /** | 51 /** |
61 * Shows an intent and returns the results to the callback object. | 52 * Shows an intent and returns the results to the callback object. |
62 * @param intent The intent that needs to be showed. | 53 * @param intent The intent that needs to be showed. |
63 * @param callback The object that will receive the results for the intent. | 54 * @param callback The object that will receive the results for the intent. |
64 * @param errorId The ID of error string to be show if activity is paused be fore intent | 55 * @param errorId The ID of error string to be show if activity is paused be fore intent |
65 * results. | 56 * results. |
66 * @return Whether the intent was shown. | 57 * @return Whether the intent was shown. |
67 */ | 58 */ |
68 public boolean showIntent(Intent intent, IntentCallback callback, int errorI d) { | 59 public boolean showIntent(Intent intent, IntentCallback callback, int errorI d) { |
69 int requestCode = REQUEST_CODE_PREFIX + mNextRequestCode; | 60 logActivityRequiredWarning(); |
joth
2013/11/01 23:12:18
I think you'd be better having a clearer log here:
michaelbai
2013/11/07 21:10:03
Done.
| |
70 mNextRequestCode = (mNextRequestCode + 1) % REQUEST_CODE_RANGE_SIZE; | 61 return false; |
71 | |
72 try { | |
73 mActivity.startActivityForResult(intent, requestCode); | |
74 } catch (ActivityNotFoundException e) { | |
75 return false; | |
76 } | |
77 | |
78 mOutstandingIntents.put(requestCode, callback); | |
79 mIntentErrors.put(requestCode, mActivity.getString(errorId)); | |
80 | |
81 return true; | |
82 } | 62 } |
83 | 63 |
84 /** | 64 /** |
85 * Displays an error message with a provided error message string. | 65 * Displays an error message with a provided error message string. |
86 * @param error The error message string to be displayed. | 66 * @param error The error message string to be displayed. |
87 */ | 67 */ |
88 public void showError(String error) { | 68 public void showError(String error) { |
89 if (error != null) { | 69 if (error != null) { |
90 Toast.makeText(mActivity, error, Toast.LENGTH_SHORT).show(); | 70 Toast.makeText(mApplicationContext, error, Toast.LENGTH_SHORT).show( ); |
91 } | 71 } |
92 } | 72 } |
93 | 73 |
94 /** | 74 /** |
95 * Displays an error message from the given resource id. | 75 * Displays an error message from the given resource id. |
96 * @param resId The error message string's resource id. | 76 * @param resId The error message string's resource id. |
97 */ | 77 */ |
98 public void showError(int resId) { | 78 public void showError(int resId) { |
99 showError(mActivity.getString(resId)); | 79 showError(mApplicationContext.getString(resId)); |
100 } | 80 } |
101 | 81 |
102 /** | 82 /** |
103 * Displays an error message for a nonexistent callback. | 83 * Displays an error message for a nonexistent callback. |
104 * @param error The error message string to be displayed. | 84 * @param error The error message string to be displayed. |
105 */ | 85 */ |
106 protected void showCallbackNonExistentError(String error) { | 86 protected void showCallbackNonExistentError(String error) { |
107 showError(error); | 87 showError(error); |
108 } | 88 } |
109 | 89 |
110 /** | 90 /** |
111 * Broadcasts the given intent to all interested BroadcastReceivers. | 91 * Broadcasts the given intent to all interested BroadcastReceivers. |
112 */ | 92 */ |
113 public void sendBroadcast(Intent intent) { | 93 public void sendBroadcast(Intent intent) { |
114 mActivity.sendBroadcast(intent); | 94 mApplicationContext.sendBroadcast(intent); |
115 } | 95 } |
116 | 96 |
117 /** | 97 /** |
118 * TODO(nileshagrawal): Stop returning Activity Context crbug.com/233440. | 98 * TODO(nileshagrawal): Stop returning Activity Context crbug.com/233440. |
119 * @return Activity context. | 99 * @return Activity context, it could be null. Note, in most cases, you prob ably |
100 * just need Application Context returned by getApplicationContext(). | |
120 * @see #getApplicationContext() | 101 * @see #getApplicationContext() |
121 */ | 102 */ |
122 @Deprecated | 103 @Deprecated |
123 public Context getContext() { | 104 public Context getContext() { |
124 return mActivity; | 105 logActivityRequiredWarning(); |
106 return null; | |
125 } | 107 } |
126 | 108 |
127 /** | 109 /** |
128 * @return The application context for this activity. | 110 * @return The application context for this activity. |
129 */ | 111 */ |
130 public Context getApplicationContext() { | 112 public Context getApplicationContext() { |
131 return mApplicationContext; | 113 return mApplicationContext; |
132 } | 114 } |
133 | 115 |
134 /** | 116 /** |
(...skipping 22 matching lines...) Expand all Loading... | |
157 } | 139 } |
158 | 140 |
159 /** | 141 /** |
160 * Responds to the intent result if the intent was created by the native win dow. | 142 * Responds to the intent result if the intent was created by the native win dow. |
161 * @param requestCode Request code of the requested intent. | 143 * @param requestCode Request code of the requested intent. |
162 * @param resultCode Result code of the requested intent. | 144 * @param resultCode Result code of the requested intent. |
163 * @param data The data returned by the intent. | 145 * @param data The data returned by the intent. |
164 * @return Boolean value of whether the intent was started by the native win dow. | 146 * @return Boolean value of whether the intent was started by the native win dow. |
165 */ | 147 */ |
166 public boolean onActivityResult(int requestCode, int resultCode, Intent data ) { | 148 public boolean onActivityResult(int requestCode, int resultCode, Intent data ) { |
167 IntentCallback callback = mOutstandingIntents.get(requestCode); | 149 logActivityRequiredWarning(); |
168 mOutstandingIntents.delete(requestCode); | |
169 String errorMessage = mIntentErrors.remove(requestCode); | |
170 | |
171 if (callback != null) { | |
172 callback.onIntentCompleted(this, resultCode, | |
173 mActivity.getContentResolver(), data); | |
174 return true; | |
175 } else { | |
176 if (errorMessage != null) { | |
177 showCallbackNonExistentError(errorMessage); | |
178 return true; | |
179 } | |
180 } | |
181 return false; | 150 return false; |
182 } | 151 } |
183 | 152 |
184 /** | 153 /** |
185 * An interface that intent callback objects have to implement. | 154 * An interface that intent callback objects have to implement. |
186 */ | 155 */ |
187 public interface IntentCallback { | 156 public interface IntentCallback { |
188 /** | 157 /** |
189 * Handles the data returned by the requested intent. | 158 * Handles the data returned by the requested intent. |
190 * @param window A window reference. | 159 * @param window A window reference. |
(...skipping 26 matching lines...) Expand all Loading... | |
217 } | 186 } |
218 return mNativeWindowAndroid; | 187 return mNativeWindowAndroid; |
219 } | 188 } |
220 | 189 |
221 /** | 190 /** |
222 * Returns a PNG-encoded screenshot of the the window region at (|windowX|, | 191 * Returns a PNG-encoded screenshot of the the window region at (|windowX|, |
223 * |windowY|) with the size |width| by |height| pixels. | 192 * |windowY|) with the size |width| by |height| pixels. |
224 */ | 193 */ |
225 @CalledByNative | 194 @CalledByNative |
226 public byte[] grabSnapshot(int windowX, int windowY, int width, int height) { | 195 public byte[] grabSnapshot(int windowX, int windowY, int width, int height) { |
227 try { | 196 logActivityRequiredWarning(); |
228 // Take a screenshot of the root activity view. This generally inclu des UI | 197 return null; |
229 // controls such as the URL bar and OS windows such as the status ba r. | |
230 View rootView = mActivity.findViewById(android.R.id.content).getRoot View(); | |
231 Bitmap bitmap = UiUtils.generateScaledScreenshot(rootView, 0, Bitmap .Config.ARGB_8888); | |
232 if (bitmap == null) return null; | |
233 | |
234 // Clip the result into the requested region. | |
235 if (windowX > 0 || windowY > 0 || width != bitmap.getWidth() || | |
236 height != bitmap.getHeight()) { | |
237 Rect clip = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight() ); | |
238 clip.intersect(windowX, windowY, windowX + width, windowY + heig ht); | |
239 bitmap = Bitmap.createBitmap( | |
240 bitmap, clip.left, clip.top, clip.width(), clip.height() ); | |
241 } | |
242 | |
243 // Compress the result into a PNG. | |
244 ByteArrayOutputStream result = new ByteArrayOutputStream(); | |
245 if (!bitmap.compress(Bitmap.CompressFormat.PNG, 100, result)) return null; | |
246 bitmap.recycle(); | |
247 return result.toByteArray(); | |
248 } catch (OutOfMemoryError e) { | |
249 Log.e(TAG, "Out of memory while grabbing window snapshot.", e); | |
250 return null; | |
251 } | |
252 } | 198 } |
253 | 199 |
254 private native int nativeInit(); | 200 private native int nativeInit(); |
255 private native void nativeDestroy(int nativeWindowAndroid); | 201 private native void nativeDestroy(int nativeWindowAndroid); |
256 | 202 |
257 } | 203 } |
OLD | NEW |