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