Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: ui/android/java/src/org/chromium/ui/WindowAndroid.java

Issue 29303004: Make WindowAndroid constructor takes context as param. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // This simple class is used to prevent activity from being directly accesse d
26 // and output the alert if the activity is null.
27 private static void showActivityRequiredWarning() {
28 Log.w(TAG, "Wrong type of Context, Activity's Context is required");
29 }
31 30
32 private static final String TAG = "WindowAndroid"; 31 private static final String TAG = "WindowAndroid";
33 32
34 // Native pointer to the c++ WindowAndroid object. 33 // Native pointer to the c++ WindowAndroid object.
35 private int mNativeWindowAndroid = 0; 34 private int mNativeWindowAndroid = 0;
36 35
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 36 // A string used as a key to store intent errors in a bundle
41 static final String WINDOW_CALLBACK_ERRORS = "window_callback_errors"; 37 static final String WINDOW_CALLBACK_ERRORS = "window_callback_errors";
42 38
43 private int mNextRequestCode = 0;
44 protected Activity mActivity;
45 protected Context mApplicationContext; 39 protected Context mApplicationContext;
46 protected SparseArray<IntentCallback> mOutstandingIntents; 40 protected SparseArray<IntentCallback> mOutstandingIntents;
47 protected HashMap<Integer, String> mIntentErrors; 41 protected HashMap<Integer, String> mIntentErrors;
48 42
49 /** 43 /**
50 * @param activity 44 * @param context, the application context..
newt (away) 2013/10/27 19:57:51 "@param context The application context."
michaelbai 2013/10/28 17:40:38 Done.
51 */ 45 */
52 public WindowAndroid(Activity activity) { 46 public WindowAndroid(Context context) {
newt (away) 2013/10/27 19:57:51 might be good to add: assert context == conte
michaelbai 2013/10/28 17:40:38 Done.
53 mActivity = activity; 47 mApplicationContext = context;
54 mApplicationContext = mActivity.getApplicationContext();
55 mOutstandingIntents = new SparseArray<IntentCallback>(); 48 mOutstandingIntents = new SparseArray<IntentCallback>();
56 mIntentErrors = new HashMap<Integer, String>(); 49 mIntentErrors = new HashMap<Integer, String>();
57
58 } 50 }
59 51
60 /** 52 /**
61 * Shows an intent and returns the results to the callback object. 53 * Shows an intent and returns the results to the callback object.
62 * @param intent The intent that needs to be showed. 54 * @param intent The intent that needs to be showed.
63 * @param callback The object that will receive the results for the intent. 55 * @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 56 * @param errorId The ID of error string to be show if activity is paused be fore intent
65 * results. 57 * results.
66 * @return Whether the intent was shown. 58 * @return Whether the intent was shown.
67 */ 59 */
68 public boolean showIntent(Intent intent, IntentCallback callback, int errorI d) { 60 public boolean showIntent(Intent intent, IntentCallback callback, int errorI d) {
69 int requestCode = REQUEST_CODE_PREFIX + mNextRequestCode; 61 showActivityRequiredWarning();
70 mNextRequestCode = (mNextRequestCode + 1) % REQUEST_CODE_RANGE_SIZE; 62 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 } 63 }
83 64
84 /** 65 /**
85 * Displays an error message with a provided error message string. 66 * Displays an error message with a provided error message string.
86 * @param error The error message string to be displayed. 67 * @param error The error message string to be displayed.
87 */ 68 */
88 public void showError(String error) { 69 public void showError(String error) {
89 if (error != null) { 70 if (error != null) {
90 Toast.makeText(mActivity, error, Toast.LENGTH_SHORT).show(); 71 Toast.makeText(mApplicationContext, error, Toast.LENGTH_SHORT).show( );
91 } 72 }
92 } 73 }
93 74
94 /** 75 /**
95 * Displays an error message from the given resource id. 76 * Displays an error message from the given resource id.
96 * @param resId The error message string's resource id. 77 * @param resId The error message string's resource id.
97 */ 78 */
98 public void showError(int resId) { 79 public void showError(int resId) {
99 showError(mActivity.getString(resId)); 80 showError(mApplicationContext.getString(resId));
100 } 81 }
101 82
102 /** 83 /**
103 * Displays an error message for a nonexistent callback. 84 * Displays an error message for a nonexistent callback.
104 * @param error The error message string to be displayed. 85 * @param error The error message string to be displayed.
105 */ 86 */
106 protected void showCallbackNonExistentError(String error) { 87 protected void showCallbackNonExistentError(String error) {
107 showError(error); 88 showError(error);
108 } 89 }
109 90
110 /** 91 /**
111 * Broadcasts the given intent to all interested BroadcastReceivers. 92 * Broadcasts the given intent to all interested BroadcastReceivers.
112 */ 93 */
113 public void sendBroadcast(Intent intent) { 94 public void sendBroadcast(Intent intent) {
114 mActivity.sendBroadcast(intent); 95 mApplicationContext.sendBroadcast(intent);
115 } 96 }
116 97
117 /** 98 /**
118 * TODO(nileshagrawal): Stop returning Activity Context crbug.com/233440. 99 * TODO(nileshagrawal): Stop returning Activity Context crbug.com/233440.
newt (away) 2013/10/27 19:57:51 remove TODO now
michaelbai 2013/10/28 17:40:38 Done.
119 * @return Activity context. 100 * @return Activity context, it could be null. Note, in most cases, you prob ably
101 * just need Application Context returned by getApplicationContext().
120 * @see #getApplicationContext() 102 * @see #getApplicationContext()
121 */ 103 */
122 @Deprecated 104 @Deprecated
123 public Context getContext() { 105 public Context getContext() {
newt (away) 2013/10/27 19:57:51 we should call this getActivityContext() to be cle
michaelbai 2013/10/28 17:40:38 As I plan to remove this method, not worthy of cha
124 return mActivity; 106 showActivityRequiredWarning();
107 return null;
125 } 108 }
126 109
127 /** 110 /**
128 * @return The application context for this activity. 111 * @return The application context for this activity.
129 */ 112 */
130 public Context getApplicationContext() { 113 public Context getApplicationContext() {
131 return mApplicationContext; 114 return mApplicationContext;
132 } 115 }
133 116
134 /** 117 /**
(...skipping 22 matching lines...) Expand all
157 } 140 }
158 141
159 /** 142 /**
160 * Responds to the intent result if the intent was created by the native win dow. 143 * Responds to the intent result if the intent was created by the native win dow.
161 * @param requestCode Request code of the requested intent. 144 * @param requestCode Request code of the requested intent.
162 * @param resultCode Result code of the requested intent. 145 * @param resultCode Result code of the requested intent.
163 * @param data The data returned by the intent. 146 * @param data The data returned by the intent.
164 * @return Boolean value of whether the intent was started by the native win dow. 147 * @return Boolean value of whether the intent was started by the native win dow.
165 */ 148 */
166 public boolean onActivityResult(int requestCode, int resultCode, Intent data ) { 149 public boolean onActivityResult(int requestCode, int resultCode, Intent data ) {
167 IntentCallback callback = mOutstandingIntents.get(requestCode); 150 showActivityRequiredWarning();
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; 151 return false;
182 } 152 }
183 153
184 /** 154 /**
185 * An interface that intent callback objects have to implement. 155 * An interface that intent callback objects have to implement.
186 */ 156 */
187 public interface IntentCallback { 157 public interface IntentCallback {
188 /** 158 /**
189 * Handles the data returned by the requested intent. 159 * Handles the data returned by the requested intent.
190 * @param window A window reference. 160 * @param window A window reference.
(...skipping 26 matching lines...) Expand all
217 } 187 }
218 return mNativeWindowAndroid; 188 return mNativeWindowAndroid;
219 } 189 }
220 190
221 /** 191 /**
222 * Returns a PNG-encoded screenshot of the the window region at (|windowX|, 192 * Returns a PNG-encoded screenshot of the the window region at (|windowX|,
223 * |windowY|) with the size |width| by |height| pixels. 193 * |windowY|) with the size |width| by |height| pixels.
224 */ 194 */
225 @CalledByNative 195 @CalledByNative
226 public byte[] grabSnapshot(int windowX, int windowY, int width, int height) { 196 public byte[] grabSnapshot(int windowX, int windowY, int width, int height) {
227 try { 197 showActivityRequiredWarning();
228 // Take a screenshot of the root activity view. This generally inclu des UI 198 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 } 199 }
253 200
254 private native int nativeInit(); 201 private native int nativeInit();
255 private native void nativeDestroy(int nativeWindowAndroid); 202 private native void nativeDestroy(int nativeWindowAndroid);
256 203
257 } 204 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698