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

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
« no previous file with comments | « ui/android/java/src/org/chromium/ui/ActivityWindowAndroid.java ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 {
31
32 private static final String TAG = "WindowAndroid"; 25 private static final String TAG = "WindowAndroid";
33 26
34 // Native pointer to the c++ WindowAndroid object. 27 // Native pointer to the c++ WindowAndroid object.
35 private int mNativeWindowAndroid = 0; 28 private int mNativeWindowAndroid = 0;
36 29
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 30 // A string used as a key to store intent errors in a bundle
41 static final String WINDOW_CALLBACK_ERRORS = "window_callback_errors"; 31 static final String WINDOW_CALLBACK_ERRORS = "window_callback_errors";
42 32
43 private int mNextRequestCode = 0;
44 protected Activity mActivity;
45 protected Context mApplicationContext; 33 protected Context mApplicationContext;
46 protected SparseArray<IntentCallback> mOutstandingIntents; 34 protected SparseArray<IntentCallback> mOutstandingIntents;
47 protected HashMap<Integer, String> mIntentErrors; 35 protected HashMap<Integer, String> mIntentErrors;
48 36
49 /** 37 /**
50 * @param activity 38 * @param context, the application context..
51 */ 39 */
52 public WindowAndroid(Activity activity) { 40 public WindowAndroid(Context context) {
53 mActivity = activity; 41 assert context == context.getApplicationContext();
54 mApplicationContext = mActivity.getApplicationContext(); 42 mApplicationContext = context;
55 mOutstandingIntents = new SparseArray<IntentCallback>(); 43 mOutstandingIntents = new SparseArray<IntentCallback>();
56 mIntentErrors = new HashMap<Integer, String>(); 44 mIntentErrors = new HashMap<Integer, String>();
57
58 } 45 }
59 46
60 /** 47 /**
61 * Shows an intent and returns the results to the callback object. 48 * Shows an intent and returns the results to the callback object.
62 * @param intent The intent that needs to be showed. 49 * @param intent The intent that needs to be showed.
63 * @param callback The object that will receive the results for the intent. 50 * @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 51 * @param errorId The ID of error string to be show if activity is paused be fore intent
65 * results. 52 * results.
66 * @return Whether the intent was shown. 53 * @return Whether the intent was shown.
67 */ 54 */
68 public boolean showIntent(Intent intent, IntentCallback callback, int errorI d) { 55 public boolean showIntent(Intent intent, IntentCallback callback, int errorI d) {
69 int requestCode = REQUEST_CODE_PREFIX + mNextRequestCode; 56 Log.d(TAG, "Can't show intent as context is not an Activity: " + intent) ;;
newt (away) 2013/11/07 21:55:13 double ;;
michaelbai 2013/11/07 22:18:24 Done.
70 mNextRequestCode = (mNextRequestCode + 1) % REQUEST_CODE_RANGE_SIZE; 57 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 } 58 }
83 59
84 /** 60 /**
85 * Displays an error message with a provided error message string. 61 * Displays an error message with a provided error message string.
86 * @param error The error message string to be displayed. 62 * @param error The error message string to be displayed.
87 */ 63 */
88 public void showError(String error) { 64 public void showError(String error) {
89 if (error != null) { 65 if (error != null) {
90 Toast.makeText(mActivity, error, Toast.LENGTH_SHORT).show(); 66 Toast.makeText(mApplicationContext, error, Toast.LENGTH_SHORT).show( );
91 } 67 }
92 } 68 }
93 69
94 /** 70 /**
95 * Displays an error message from the given resource id. 71 * Displays an error message from the given resource id.
96 * @param resId The error message string's resource id. 72 * @param resId The error message string's resource id.
97 */ 73 */
98 public void showError(int resId) { 74 public void showError(int resId) {
99 showError(mActivity.getString(resId)); 75 showError(mApplicationContext.getString(resId));
100 } 76 }
101 77
102 /** 78 /**
103 * Displays an error message for a nonexistent callback. 79 * Displays an error message for a nonexistent callback.
104 * @param error The error message string to be displayed. 80 * @param error The error message string to be displayed.
105 */ 81 */
106 protected void showCallbackNonExistentError(String error) { 82 protected void showCallbackNonExistentError(String error) {
107 showError(error); 83 showError(error);
108 } 84 }
109 85
110 /** 86 /**
111 * Broadcasts the given intent to all interested BroadcastReceivers. 87 * Broadcasts the given intent to all interested BroadcastReceivers.
112 */ 88 */
113 public void sendBroadcast(Intent intent) { 89 public void sendBroadcast(Intent intent) {
114 mActivity.sendBroadcast(intent); 90 mApplicationContext.sendBroadcast(intent);
115 } 91 }
116 92
117 /** 93 /**
118 * TODO(nileshagrawal): Stop returning Activity Context crbug.com/233440. 94 * TODO(nileshagrawal): Stop returning Activity Context crbug.com/233440.
119 * @return Activity context. 95 * @return Activity context, it could be null. Note, in most cases, you prob ably
96 * just need Application Context returned by getApplicationContext().
120 * @see #getApplicationContext() 97 * @see #getApplicationContext()
121 */ 98 */
122 @Deprecated 99 @Deprecated
123 public Context getContext() { 100 public Context getContext() {
124 return mActivity; 101 return null;
125 } 102 }
126 103
127 /** 104 /**
128 * @return The application context for this activity. 105 * @return The application context for this activity.
129 */ 106 */
130 public Context getApplicationContext() { 107 public Context getApplicationContext() {
131 return mApplicationContext; 108 return mApplicationContext;
132 } 109 }
133 110
134 /** 111 /**
(...skipping 22 matching lines...) Expand all
157 } 134 }
158 135
159 /** 136 /**
160 * Responds to the intent result if the intent was created by the native win dow. 137 * Responds to the intent result if the intent was created by the native win dow.
161 * @param requestCode Request code of the requested intent. 138 * @param requestCode Request code of the requested intent.
162 * @param resultCode Result code of the requested intent. 139 * @param resultCode Result code of the requested intent.
163 * @param data The data returned by the intent. 140 * @param data The data returned by the intent.
164 * @return Boolean value of whether the intent was started by the native win dow. 141 * @return Boolean value of whether the intent was started by the native win dow.
165 */ 142 */
166 public boolean onActivityResult(int requestCode, int resultCode, Intent data ) { 143 public boolean onActivityResult(int requestCode, int resultCode, Intent data ) {
167 IntentCallback callback = mOutstandingIntents.get(requestCode);
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; 144 return false;
182 } 145 }
183 146
184 /** 147 /**
185 * An interface that intent callback objects have to implement. 148 * An interface that intent callback objects have to implement.
186 */ 149 */
187 public interface IntentCallback { 150 public interface IntentCallback {
188 /** 151 /**
189 * Handles the data returned by the requested intent. 152 * Handles the data returned by the requested intent.
190 * @param window A window reference. 153 * @param window A window reference.
(...skipping 26 matching lines...) Expand all
217 } 180 }
218 return mNativeWindowAndroid; 181 return mNativeWindowAndroid;
219 } 182 }
220 183
221 /** 184 /**
222 * Returns a PNG-encoded screenshot of the the window region at (|windowX|, 185 * Returns a PNG-encoded screenshot of the the window region at (|windowX|,
223 * |windowY|) with the size |width| by |height| pixels. 186 * |windowY|) with the size |width| by |height| pixels.
224 */ 187 */
225 @CalledByNative 188 @CalledByNative
226 public byte[] grabSnapshot(int windowX, int windowY, int width, int height) { 189 public byte[] grabSnapshot(int windowX, int windowY, int width, int height) {
227 try { 190 return null;
228 // Take a screenshot of the root activity view. This generally inclu des UI
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 } 191 }
253 192
254 private native int nativeInit(); 193 private native int nativeInit();
255 private native void nativeDestroy(int nativeWindowAndroid); 194 private native void nativeDestroy(int nativeWindowAndroid);
256 195
257 } 196 }
OLDNEW
« no previous file with comments | « ui/android/java/src/org/chromium/ui/ActivityWindowAndroid.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698