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

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

Issue 10916160: Upstreaming SelectFileDialog for Android (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Elliot's nits Created 8 years, 3 months 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 package org.chromium.ui.gfx;
6
7 import android.app.Activity;
8 import android.content.ActivityNotFoundException;
9 import android.content.ContentResolver;
10 import android.content.Intent;
11 import android.content.res.Resources;
12 import android.os.Bundle;
13 import android.util.SparseArray;
14 import android.widget.Toast;
15
16 import java.io.Serializable;
17 import java.util.HashMap;
18
19 /**
20 * The window that has access to the main activity and is able to create and rec eive intents,
21 * and show error messages.
22 */
Yaron 2012/09/12 19:14:45 @JNINamespace("ui")
aurimas (slooooooooow) 2012/09/13 15:37:15 Done.
23 public class NativeWindow {
24
25 // Constants used for intent request code bounding.
26 private static final int REQUEST_CODE_PREFIX = 1000;
27 private static final int REQUEST_CODE_RANGE_SIZE = 100;
28 // A string used as a key to store intent errors in a bundle
29 static final String WINDOW_CALLBACK_ERRORS = "window_callback_errors";
30
31 // Native pointer to the c++ WindowAndroid object.
32 private int mNativeWindowAndroid = 0;
33 private int mNextRequestCode = 0;
34 protected Activity mActivity;
35 private SparseArray<IntentCallback> mOutstandingIntents;
36 private HashMap<Integer, String> mIntentErrors;
37
38 /**
39 * An interface that intent callback objects have to implement.
40 */
41 public interface IntentCallback {
42 /**
43 * Handles the data returned by the requested intent.
44 * @param window A window reference.
45 * @param resultCode Result code of the requested intent.
46 * @param contentResolver An instance of ContentResolver class for acces sing returned data.
47 * @param data The data returned by the intent.
48 */
49 public void onIntentCompleted(NativeWindow window, int resultCode,
50 ContentResolver contentResolver, Intent data);
51 }
52
53 /**
54 * Constructs a Window object, saves a reference to the main activity, and i nitializes the
55 * outstanding intent map. NativeWindowAndroid gets lazily loaded on getNati veWindow().
56 * @param activity The main application activity.
57 */
58 public NativeWindow(Activity activity) {
59 mActivity = activity;
60 mOutstandingIntents = new SparseArray<IntentCallback>();
61 mIntentErrors = new HashMap<Integer, String>();
62 mNativeWindowAndroid = 0;
63 }
64
65 /**
66 * Destroys the c++ WindowAndroid object if one has been created.
67 */
68 public void destroy() {
69 if (mNativeWindowAndroid != 0) {
70 nativeDestroy(mNativeWindowAndroid);
71 mNativeWindowAndroid = 0;
72 }
73 }
74
75 /**
76 * Shows an intent and returns the results to the callback object.
77 * @param intent The intent that needs to be showed.
78 * @param callback The object that will receive the results for the intent.
79 * @return Whether the intent was shown.
80 */
81 public boolean showIntent(Intent intent, IntentCallback callback) {
82 return showIntent(intent, callback, null);
83 }
84
85 /**
86 * Shows an intent and returns the results to the callback object.
87 * @param intent The intent that needs to be showed.
88 * @param callback The object that will receive the results for the intent.
89 * @param errorId The id of the error string to be show if activity is pause d before intent
90 * results.
91 * @return Whether the intent was shown.
92 */
93 public boolean showIntent(Intent intent, IntentCallback callback, int errorI d) {
94 String error = null;
95 try {
96 error = mActivity.getString(errorId);
97 } catch (Resources.NotFoundException e) { }
98 return showIntent(intent, callback, error);
99 }
100
101 /**
102 * Shows an intent and returns the results to the callback object.
103 * @param intent The intent that needs to be showed.
104 * @param callback The object that will receive the results for the intent.
105 * @param error The error string to be show if activity is paused before int ent results.
106 * @return Whether the intent was shown.
107 */
108 public boolean showIntent(Intent intent, IntentCallback callback, String err or) {
109 int requestCode = REQUEST_CODE_PREFIX + mNextRequestCode;
110 mNextRequestCode = (mNextRequestCode + 1) % REQUEST_CODE_RANGE_SIZE;
111
112 try {
113 mActivity.startActivityForResult(intent, requestCode);
114 } catch (ActivityNotFoundException e) {
115 return false;
116 }
117
118 mOutstandingIntents.put(requestCode, callback);
119 if (error != null) mIntentErrors.put(requestCode, error);
120
121 return true;
122 }
123
124 /**
125 * Saves the error messages that should be shown if any pending intents woul d return
126 * after the application has been put onPause.
127 * @param bundle The bundle to save the information in onPause
128 */
129 public void saveInstanceState(Bundle bundle) {
130 bundle.putSerializable(WINDOW_CALLBACK_ERRORS, mIntentErrors);
131 }
132
133 /**
134 * Restores the error messages that should be shown if any pending intents w ould return
135 * after the application has been put onPause.
136 * @param bundle The bundle to restore the information from onResume
137 */
138 public void restoreInstanceState(Bundle bundle) {
139 if (bundle == null) return;
140
141 Object errors = bundle.getSerializable(WINDOW_CALLBACK_ERRORS);
142 if (errors instanceof HashMap) {
143 @SuppressWarnings("unchecked")
144 HashMap<Integer, String> intentErrors = (HashMap<Integer, String>) e rrors;
145 mIntentErrors = intentErrors;
146 }
147 }
148
149 /**
150 * Displays an error message with a provided error message string.
151 * @param error The error message string to be displayed.
152 */
153 public void showError(String error) {
154 if (error != null) Toast.makeText(mActivity, error, Toast.LENGTH_SHORT). show();
155 }
156
157 /**
158 * Displays an error message with a provided error message string id.
159 * @param errorId The string id of the error message string to be displayed.
160 */
161 public void showError(int errorId) {
162 String error = null;
163 try {
164 error = mActivity.getString(errorId);
165 } catch (Resources.NotFoundException e) { }
166 showError(error);
167 }
168
169 /**
170 * Displays an error message for a nonexistent callback.
171 * @param error The error message string to be displayed.
172 */
173 protected void showCallbackNonExistentError(String error) {
174 showError(error);
175 }
176
177 /**
178 * @return The main application activity.
179 */
180 public Activity getActivity() {
181 return mActivity;
182 }
183
184 /**
185 * Responds to the intent result if the intent was created by the native win dow.
186 * @param requestCode Request code of the requested intent.
187 * @param resultCode Result code of the requested intent.
188 * @param data The data returned by the intent.
189 * @return Boolean value of whether the intent was started by the native win dow.
190 */
191 public boolean onActivityResult(int requestCode, int resultCode, Intent data ) {
192 IntentCallback callback = mOutstandingIntents.get(requestCode);
193 mOutstandingIntents.delete(requestCode);
194 String errorMessage = mIntentErrors.remove(requestCode);
195
196 if (callback != null) {
197 callback.onIntentCompleted(this, resultCode,
198 mActivity.getContentResolver(), data);
199 return true;
200 } else {
201 if (errorMessage != null) {
202 showCallbackNonExistentError(errorMessage);
203 return true;
204 }
205 }
206 return false;
207 }
208
209 /**
210 * Returns a pointer to the c++ AndroidWindow object and calls the initializ er if
211 * the object has not been previously initialized.
212 * @return A pointer to the c++ AndroidWindow.
213 */
214 public int getNativePointer() {
215 if (mNativeWindowAndroid == 0) {
216 mNativeWindowAndroid = nativeInit();
217 }
218 return mNativeWindowAndroid;
219 }
220
221 private native int nativeInit();
222 private native void nativeDestroy(int nativeWindowAndroid);
223
224 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698