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.gfx; | 5 package org.chromium.ui; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
8 import android.content.ActivityNotFoundException; | 8 import android.content.ActivityNotFoundException; |
9 import android.content.ContentResolver; | |
10 import android.content.Context; | |
9 import android.content.Intent; | 11 import android.content.Intent; |
10 import android.os.Bundle; | 12 import android.os.Bundle; |
11 import android.util.SparseArray; | 13 import android.util.SparseArray; |
12 import android.widget.Toast; | 14 import android.widget.Toast; |
13 | 15 |
14 import java.util.HashMap; | 16 import java.util.HashMap; |
15 | 17 |
18 import org.chromium.base.JNINamespace; | |
19 | |
16 /** | 20 /** |
17 * The window that has access to the main activity and is able to create and rec eive intents, | 21 * The window base class that has the minimum functionality. |
18 * and show error messages. | |
19 */ | 22 */ |
20 public class ActivityNativeWindow extends NativeWindow { | 23 @JNINamespace("ui") |
24 public class WindowAndroid { | |
25 | |
26 // Native pointer to the c++ WindowAndroid object. | |
27 private int mNativeWindowAndroid = 0; | |
21 | 28 |
22 // Constants used for intent request code bounding. | 29 // Constants used for intent request code bounding. |
23 private static final int REQUEST_CODE_PREFIX = 1000; | 30 private static final int REQUEST_CODE_PREFIX = 1000; |
24 private static final int REQUEST_CODE_RANGE_SIZE = 100; | 31 private static final int REQUEST_CODE_RANGE_SIZE = 100; |
25 // A string used as a key to store intent errors in a bundle | 32 // A string used as a key to store intent errors in a bundle |
26 static final String WINDOW_CALLBACK_ERRORS = "window_callback_errors"; | 33 static final String WINDOW_CALLBACK_ERRORS = "window_callback_errors"; |
27 | 34 |
28 private int mNextRequestCode = 0; | 35 private int mNextRequestCode = 0; |
29 protected Activity mActivity; | 36 protected Activity mActivity; |
30 protected SparseArray<IntentCallback> mOutstandingIntents; | 37 protected SparseArray<IntentCallback> mOutstandingIntents; |
31 protected HashMap<Integer, String> mIntentErrors; | 38 protected HashMap<Integer, String> mIntentErrors; |
32 | 39 |
33 /** | 40 /** |
34 * @param activity | 41 * @param activity |
35 */ | 42 */ |
36 public ActivityNativeWindow(Activity activity) { | 43 public WindowAndroid(Activity activity) { |
37 super(activity); | |
38 mActivity = activity; | 44 mActivity = activity; |
39 mOutstandingIntents = new SparseArray<IntentCallback>(); | 45 mOutstandingIntents = new SparseArray<IntentCallback>(); |
40 mIntentErrors = new HashMap<Integer, String>(); | 46 mIntentErrors = new HashMap<Integer, String>(); |
41 | 47 |
42 } | 48 } |
43 | 49 |
44 /** | 50 /** |
45 * Shows an intent and returns the results to the callback object. | 51 * Shows an intent and returns the results to the callback object. |
46 * @param intent The intent that needs to be showed. | 52 * @param intent The intent that needs to be showed. |
47 * @param callback The object that will receive the results for the intent. | 53 * @param callback The object that will receive the results for the intent. |
48 * @param error The error string to be show if activity is paused before int ent results. | 54 * @param error The error string to be show if activity is paused before int ent results. |
49 * @return Whether the intent was shown. | 55 * @return Whether the intent was shown. |
50 */ | 56 */ |
51 @Override | |
52 public boolean showIntent(Intent intent, IntentCallback callback, String err or) { | 57 public boolean showIntent(Intent intent, IntentCallback callback, String err or) { |
53 int requestCode = REQUEST_CODE_PREFIX + mNextRequestCode; | 58 int requestCode = REQUEST_CODE_PREFIX + mNextRequestCode; |
54 mNextRequestCode = (mNextRequestCode + 1) % REQUEST_CODE_RANGE_SIZE; | 59 mNextRequestCode = (mNextRequestCode + 1) % REQUEST_CODE_RANGE_SIZE; |
55 | 60 |
56 try { | 61 try { |
57 mActivity.startActivityForResult(intent, requestCode); | 62 mActivity.startActivityForResult(intent, requestCode); |
58 } catch (ActivityNotFoundException e) { | 63 } catch (ActivityNotFoundException e) { |
59 return false; | 64 return false; |
60 } | 65 } |
61 | 66 |
62 mOutstandingIntents.put(requestCode, callback); | 67 mOutstandingIntents.put(requestCode, callback); |
63 if (error != null) mIntentErrors.put(requestCode, error); | 68 if (error != null) mIntentErrors.put(requestCode, error); |
64 | 69 |
65 return true; | 70 return true; |
66 } | 71 } |
67 | 72 |
68 /** | 73 /** |
69 * Displays an error message with a provided error message string. | 74 * Displays an error message with a provided error message string. |
70 * @param error The error message string to be displayed. | 75 * @param error The error message string to be displayed. |
71 */ | 76 */ |
72 @Override | |
73 public void showError(String error) { | 77 public void showError(String error) { |
74 if (error != null) { | 78 if (error != null) { |
75 Toast.makeText(mActivity, error, Toast.LENGTH_SHORT).show(); | 79 Toast.makeText(mActivity, error, Toast.LENGTH_SHORT).show(); |
76 } | 80 } |
77 } | 81 } |
78 | 82 |
79 /** | 83 /** |
80 * Displays an error message for a nonexistent callback. | 84 * Displays an error message for a nonexistent callback. |
81 * @param error The error message string to be displayed. | 85 * @param error The error message string to be displayed. |
82 */ | 86 */ |
83 protected void showCallbackNonExistentError(String error) { | 87 protected void showCallbackNonExistentError(String error) { |
84 showError(error); | 88 showError(error); |
85 } | 89 } |
86 | 90 |
87 /** | 91 /** |
88 * Broadcasts the given intent to all interested BroadcastReceivers. | 92 * Broadcasts the given intent to all interested BroadcastReceivers. |
89 */ | 93 */ |
90 @Override | |
91 public void sendBroadcast(Intent intent) { | 94 public void sendBroadcast(Intent intent) { |
92 mActivity.sendBroadcast(intent); | 95 mActivity.sendBroadcast(intent); |
93 } | 96 } |
94 | 97 |
95 /** | 98 /** |
96 * @return Application activity. | 99 * @return Application activity. |
97 */ | 100 */ |
98 public Activity getActivity() { | 101 public Activity getActivity() { |
nilesh
2013/04/18 21:02:14
Is anyone calling this? Please remove if not.
aurimas (slooooooooow)
2013/04/18 21:22:58
Good point, I was used in SelectFileDialogAndroid
| |
99 return mActivity; | 102 return mActivity; |
100 } | 103 } |
101 | 104 |
102 /** | 105 /** |
106 * @return Application context. | |
107 */ | |
108 public Context getContext() { | |
109 return mActivity.getApplicationContext(); | |
110 } | |
111 | |
112 /** | |
103 * Saves the error messages that should be shown if any pending intents woul d return | 113 * Saves the error messages that should be shown if any pending intents woul d return |
104 * after the application has been put onPause. | 114 * after the application has been put onPause. |
105 * @param bundle The bundle to save the information in onPause | 115 * @param bundle The bundle to save the information in onPause |
106 */ | 116 */ |
107 public void saveInstanceState(Bundle bundle) { | 117 public void saveInstanceState(Bundle bundle) { |
108 bundle.putSerializable(WINDOW_CALLBACK_ERRORS, mIntentErrors); | 118 bundle.putSerializable(WINDOW_CALLBACK_ERRORS, mIntentErrors); |
109 } | 119 } |
110 | 120 |
111 /** | 121 /** |
112 * Restores the error messages that should be shown if any pending intents w ould return | 122 * Restores the error messages that should be shown if any pending intents w ould return |
(...skipping 29 matching lines...) Expand all Loading... | |
142 return true; | 152 return true; |
143 } else { | 153 } else { |
144 if (errorMessage != null) { | 154 if (errorMessage != null) { |
145 showCallbackNonExistentError(errorMessage); | 155 showCallbackNonExistentError(errorMessage); |
146 return true; | 156 return true; |
147 } | 157 } |
148 } | 158 } |
149 return false; | 159 return false; |
150 } | 160 } |
151 | 161 |
162 /** | |
163 * An interface that intent callback objects have to implement. | |
164 */ | |
165 public interface IntentCallback { | |
166 /** | |
167 * Handles the data returned by the requested intent. | |
168 * @param window A window reference. | |
169 * @param resultCode Result code of the requested intent. | |
170 * @param contentResolver An instance of ContentResolver class for acces sing returned data. | |
171 * @param data The data returned by the intent. | |
172 */ | |
173 public void onIntentCompleted(WindowAndroid window, int resultCode, | |
174 ContentResolver contentResolver, Intent data); | |
175 } | |
176 | |
177 /** | |
178 * Destroys the c++ WindowAndroid object if one has been created. | |
179 */ | |
180 public void destroy() { | |
181 if (mNativeWindowAndroid != 0) { | |
182 nativeDestroy(mNativeWindowAndroid); | |
183 mNativeWindowAndroid = 0; | |
184 } | |
185 } | |
186 | |
187 /** | |
188 * Returns a pointer to the c++ AndroidWindow object and calls the initializ er if | |
189 * the object has not been previously initialized. | |
190 * @return A pointer to the c++ AndroidWindow. | |
191 */ | |
192 public int getNativePointer() { | |
193 if (mNativeWindowAndroid == 0) { | |
194 mNativeWindowAndroid = nativeInit(); | |
195 } | |
196 return mNativeWindowAndroid; | |
197 } | |
198 | |
199 private native int nativeInit(); | |
200 private native void nativeDestroy(int nativeWindowAndroid); | |
201 | |
152 } | 202 } |
OLD | NEW |