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