The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.app; |
| 18 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame^] | 19 | import java.lang.reflect.Constructor; |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.HashMap; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 22 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.ComponentCallbacks; |
| 24 | import android.content.ComponentName; |
| 25 | import android.content.ContentResolver; |
| 26 | import android.content.Context; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 27 | import android.content.IIntentSender; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame^] | 28 | import android.content.Intent; |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 29 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.content.SharedPreferences; |
| 31 | import android.content.pm.ActivityInfo; |
| 32 | import android.content.res.Configuration; |
| 33 | import android.content.res.Resources; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 34 | import android.content.res.TypedArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.database.Cursor; |
| 36 | import android.graphics.Bitmap; |
| 37 | import android.graphics.Canvas; |
| 38 | import android.graphics.drawable.Drawable; |
| 39 | import android.media.AudioManager; |
| 40 | import android.net.Uri; |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 41 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.os.Handler; |
| 44 | import android.os.IBinder; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 45 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.text.Selection; |
| 47 | import android.text.SpannableStringBuilder; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 48 | import android.text.TextUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.text.method.TextKeyListener; |
| 50 | import android.util.AttributeSet; |
| 51 | import android.util.Config; |
| 52 | import android.util.EventLog; |
| 53 | import android.util.Log; |
| 54 | import android.util.SparseArray; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame^] | 55 | import android.view.ActionBarView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import android.view.ContextMenu; |
| 57 | import android.view.ContextThemeWrapper; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 58 | import android.view.InflateException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import android.view.KeyEvent; |
| 60 | import android.view.LayoutInflater; |
| 61 | import android.view.Menu; |
| 62 | import android.view.MenuInflater; |
| 63 | import android.view.MenuItem; |
| 64 | import android.view.MotionEvent; |
| 65 | import android.view.View; |
| 66 | import android.view.ViewGroup; |
| 67 | import android.view.ViewManager; |
| 68 | import android.view.Window; |
| 69 | import android.view.WindowManager; |
| 70 | import android.view.ContextMenu.ContextMenuInfo; |
| 71 | import android.view.View.OnCreateContextMenuListener; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 72 | import android.view.ViewGroup.LayoutParams; |
| 73 | import android.view.accessibility.AccessibilityEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | import android.widget.AdapterView; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame^] | 75 | import android.widget.LinearLayout; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame^] | 77 | import com.android.internal.app.SplitActionBar; |
| 78 | import com.android.internal.policy.PolicyManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * An activity is a single, focused thing that the user can do. Almost all |
| 82 | * activities interact with the user, so the Activity class takes care of |
| 83 | * creating a window for you in which you can place your UI with |
| 84 | * {@link #setContentView}. While activities are often presented to the user |
| 85 | * as full-screen windows, they can also be used in other ways: as floating |
| 86 | * windows (via a theme with {@link android.R.attr#windowIsFloating} set) |
| 87 | * or embedded inside of another activity (using {@link ActivityGroup}). |
| 88 | * |
| 89 | * There are two methods almost all subclasses of Activity will implement: |
| 90 | * |
| 91 | * <ul> |
| 92 | * <li> {@link #onCreate} is where you initialize your activity. Most |
| 93 | * importantly, here you will usually call {@link #setContentView(int)} |
| 94 | * with a layout resource defining your UI, and using {@link #findViewById} |
| 95 | * to retrieve the widgets in that UI that you need to interact with |
| 96 | * programmatically. |
| 97 | * |
| 98 | * <li> {@link #onPause} is where you deal with the user leaving your |
| 99 | * activity. Most importantly, any changes made by the user should at this |
| 100 | * point be committed (usually to the |
| 101 | * {@link android.content.ContentProvider} holding the data). |
| 102 | * </ul> |
| 103 | * |
| 104 | * <p>To be of use with {@link android.content.Context#startActivity Context.startActivity()}, all |
| 105 | * activity classes must have a corresponding |
| 106 | * {@link android.R.styleable#AndroidManifestActivity <activity>} |
| 107 | * declaration in their package's <code>AndroidManifest.xml</code>.</p> |
| 108 | * |
| 109 | * <p>The Activity class is an important part of an application's overall lifecycle, |
| 110 | * and the way activities are launched and put together is a fundamental |
| 111 | * part of the platform's application model. For a detailed perspective on the structure of |
| 112 | * Android applications and lifecycles, please read the <em>Dev Guide</em> document on |
| 113 | * <a href="{@docRoot}guide/topics/fundamentals.html">Application Fundamentals</a>.</p> |
| 114 | * |
| 115 | * <p>Topics covered here: |
| 116 | * <ol> |
| 117 | * <li><a href="#ActivityLifecycle">Activity Lifecycle</a> |
| 118 | * <li><a href="#ConfigurationChanges">Configuration Changes</a> |
| 119 | * <li><a href="#StartingActivities">Starting Activities and Getting Results</a> |
| 120 | * <li><a href="#SavingPersistentState">Saving Persistent State</a> |
| 121 | * <li><a href="#Permissions">Permissions</a> |
| 122 | * <li><a href="#ProcessLifecycle">Process Lifecycle</a> |
| 123 | * </ol> |
| 124 | * |
| 125 | * <a name="ActivityLifecycle"></a> |
| 126 | * <h3>Activity Lifecycle</h3> |
| 127 | * |
| 128 | * <p>Activities in the system are managed as an <em>activity stack</em>. |
| 129 | * When a new activity is started, it is placed on the top of the stack |
| 130 | * and becomes the running activity -- the previous activity always remains |
| 131 | * below it in the stack, and will not come to the foreground again until |
| 132 | * the new activity exits.</p> |
| 133 | * |
| 134 | * <p>An activity has essentially four states:</p> |
| 135 | * <ul> |
| 136 | * <li> If an activity in the foreground of the screen (at the top of |
| 137 | * the stack), |
| 138 | * it is <em>active</em> or <em>running</em>. </li> |
| 139 | * <li>If an activity has lost focus but is still visible (that is, a new non-full-sized |
| 140 | * or transparent activity has focus on top of your activity), it |
| 141 | * is <em>paused</em>. A paused activity is completely alive (it |
| 142 | * maintains all state and member information and remains attached to |
| 143 | * the window manager), but can be killed by the system in extreme |
| 144 | * low memory situations. |
| 145 | * <li>If an activity is completely obscured by another activity, |
| 146 | * it is <em>stopped</em>. It still retains all state and member information, |
| 147 | * however, it is no longer visible to the user so its window is hidden |
| 148 | * and it will often be killed by the system when memory is needed |
| 149 | * elsewhere.</li> |
| 150 | * <li>If an activity is paused or stopped, the system can drop the activity |
| 151 | * from memory by either asking it to finish, or simply killing its |
| 152 | * process. When it is displayed again to the user, it must be |
| 153 | * completely restarted and restored to its previous state.</li> |
| 154 | * </ul> |
| 155 | * |
| 156 | * <p>The following diagram shows the important state paths of an Activity. |
| 157 | * The square rectangles represent callback methods you can implement to |
| 158 | * perform operations when the Activity moves between states. The colored |
| 159 | * ovals are major states the Activity can be in.</p> |
| 160 | * |
| 161 | * <p><img src="../../../images/activity_lifecycle.png" |
| 162 | * alt="State diagram for an Android Activity Lifecycle." border="0" /></p> |
| 163 | * |
| 164 | * <p>There are three key loops you may be interested in monitoring within your |
| 165 | * activity: |
| 166 | * |
| 167 | * <ul> |
| 168 | * <li>The <b>entire lifetime</b> of an activity happens between the first call |
| 169 | * to {@link android.app.Activity#onCreate} through to a single final call |
| 170 | * to {@link android.app.Activity#onDestroy}. An activity will do all setup |
| 171 | * of "global" state in onCreate(), and release all remaining resources in |
| 172 | * onDestroy(). For example, if it has a thread running in the background |
| 173 | * to download data from the network, it may create that thread in onCreate() |
| 174 | * and then stop the thread in onDestroy(). |
| 175 | * |
| 176 | * <li>The <b>visible lifetime</b> of an activity happens between a call to |
| 177 | * {@link android.app.Activity#onStart} until a corresponding call to |
| 178 | * {@link android.app.Activity#onStop}. During this time the user can see the |
| 179 | * activity on-screen, though it may not be in the foreground and interacting |
| 180 | * with the user. Between these two methods you can maintain resources that |
| 181 | * are needed to show the activity to the user. For example, you can register |
| 182 | * a {@link android.content.BroadcastReceiver} in onStart() to monitor for changes |
| 183 | * that impact your UI, and unregister it in onStop() when the user an no |
| 184 | * longer see what you are displaying. The onStart() and onStop() methods |
| 185 | * can be called multiple times, as the activity becomes visible and hidden |
| 186 | * to the user. |
| 187 | * |
| 188 | * <li>The <b>foreground lifetime</b> of an activity happens between a call to |
| 189 | * {@link android.app.Activity#onResume} until a corresponding call to |
| 190 | * {@link android.app.Activity#onPause}. During this time the activity is |
| 191 | * in front of all other activities and interacting with the user. An activity |
| 192 | * can frequently go between the resumed and paused states -- for example when |
| 193 | * the device goes to sleep, when an activity result is delivered, when a new |
| 194 | * intent is delivered -- so the code in these methods should be fairly |
| 195 | * lightweight. |
| 196 | * </ul> |
| 197 | * |
| 198 | * <p>The entire lifecycle of an activity is defined by the following |
| 199 | * Activity methods. All of these are hooks that you can override |
| 200 | * to do appropriate work when the activity changes state. All |
| 201 | * activities will implement {@link android.app.Activity#onCreate} |
| 202 | * to do their initial setup; many will also implement |
| 203 | * {@link android.app.Activity#onPause} to commit changes to data and |
| 204 | * otherwise prepare to stop interacting with the user. You should always |
| 205 | * call up to your superclass when implementing these methods.</p> |
| 206 | * |
| 207 | * </p> |
| 208 | * <pre class="prettyprint"> |
| 209 | * public class Activity extends ApplicationContext { |
| 210 | * protected void onCreate(Bundle savedInstanceState); |
| 211 | * |
| 212 | * protected void onStart(); |
| 213 | * |
| 214 | * protected void onRestart(); |
| 215 | * |
| 216 | * protected void onResume(); |
| 217 | * |
| 218 | * protected void onPause(); |
| 219 | * |
| 220 | * protected void onStop(); |
| 221 | * |
| 222 | * protected void onDestroy(); |
| 223 | * } |
| 224 | * </pre> |
| 225 | * |
| 226 | * <p>In general the movement through an activity's lifecycle looks like |
| 227 | * this:</p> |
| 228 | * |
| 229 | * <table border="2" width="85%" align="center" frame="hsides" rules="rows"> |
| 230 | * <colgroup align="left" span="3" /> |
| 231 | * <colgroup align="left" /> |
| 232 | * <colgroup align="center" /> |
| 233 | * <colgroup align="center" /> |
| 234 | * |
| 235 | * <thead> |
| 236 | * <tr><th colspan="3">Method</th> <th>Description</th> <th>Killable?</th> <th>Next</th></tr> |
| 237 | * </thead> |
| 238 | * |
| 239 | * <tbody> |
| 240 | * <tr><th colspan="3" align="left" border="0">{@link android.app.Activity#onCreate onCreate()}</th> |
| 241 | * <td>Called when the activity is first created. |
| 242 | * This is where you should do all of your normal static set up: |
| 243 | * create views, bind data to lists, etc. This method also |
| 244 | * provides you with a Bundle containing the activity's previously |
| 245 | * frozen state, if there was one. |
| 246 | * <p>Always followed by <code>onStart()</code>.</td> |
| 247 | * <td align="center">No</td> |
| 248 | * <td align="center"><code>onStart()</code></td> |
| 249 | * </tr> |
| 250 | * |
| 251 | * <tr><td rowspan="5" style="border-left: none; border-right: none;"> </td> |
| 252 | * <th colspan="2" align="left" border="0">{@link android.app.Activity#onRestart onRestart()}</th> |
| 253 | * <td>Called after your activity has been stopped, prior to it being |
| 254 | * started again. |
| 255 | * <p>Always followed by <code>onStart()</code></td> |
| 256 | * <td align="center">No</td> |
| 257 | * <td align="center"><code>onStart()</code></td> |
| 258 | * </tr> |
| 259 | * |
| 260 | * <tr><th colspan="2" align="left" border="0">{@link android.app.Activity#onStart onStart()}</th> |
| 261 | * <td>Called when the activity is becoming visible to the user. |
| 262 | * <p>Followed by <code>onResume()</code> if the activity comes |
| 263 | * to the foreground, or <code>onStop()</code> if it becomes hidden.</td> |
| 264 | * <td align="center">No</td> |
| 265 | * <td align="center"><code>onResume()</code> or <code>onStop()</code></td> |
| 266 | * </tr> |
| 267 | * |
| 268 | * <tr><td rowspan="2" style="border-left: none;"> </td> |
| 269 | * <th align="left" border="0">{@link android.app.Activity#onResume onResume()}</th> |
| 270 | * <td>Called when the activity will start |
| 271 | * interacting with the user. At this point your activity is at |
| 272 | * the top of the activity stack, with user input going to it. |
| 273 | * <p>Always followed by <code>onPause()</code>.</td> |
| 274 | * <td align="center">No</td> |
| 275 | * <td align="center"><code>onPause()</code></td> |
| 276 | * </tr> |
| 277 | * |
| 278 | * <tr><th align="left" border="0">{@link android.app.Activity#onPause onPause()}</th> |
| 279 | * <td>Called when the system is about to start resuming a previous |
| 280 | * activity. This is typically used to commit unsaved changes to |
| 281 | * persistent data, stop animations and other things that may be consuming |
| 282 | * CPU, etc. Implementations of this method must be very quick because |
| 283 | * the next activity will not be resumed until this method returns. |
| 284 | * <p>Followed by either <code>onResume()</code> if the activity |
| 285 | * returns back to the front, or <code>onStop()</code> if it becomes |
| 286 | * invisible to the user.</td> |
| 287 | * <td align="center"><font color="#800000"><strong>Yes</strong></font></td> |
| 288 | * <td align="center"><code>onResume()</code> or<br> |
| 289 | * <code>onStop()</code></td> |
| 290 | * </tr> |
| 291 | * |
| 292 | * <tr><th colspan="2" align="left" border="0">{@link android.app.Activity#onStop onStop()}</th> |
| 293 | * <td>Called when the activity is no longer visible to the user, because |
| 294 | * another activity has been resumed and is covering this one. This |
| 295 | * may happen either because a new activity is being started, an existing |
| 296 | * one is being brought in front of this one, or this one is being |
| 297 | * destroyed. |
| 298 | * <p>Followed by either <code>onRestart()</code> if |
| 299 | * this activity is coming back to interact with the user, or |
| 300 | * <code>onDestroy()</code> if this activity is going away.</td> |
| 301 | * <td align="center"><font color="#800000"><strong>Yes</strong></font></td> |
| 302 | * <td align="center"><code>onRestart()</code> or<br> |
| 303 | * <code>onDestroy()</code></td> |
| 304 | * </tr> |
| 305 | * |
| 306 | * <tr><th colspan="3" align="left" border="0">{@link android.app.Activity#onDestroy onDestroy()}</th> |
| 307 | * <td>The final call you receive before your |
| 308 | * activity is destroyed. This can happen either because the |
| 309 | * activity is finishing (someone called {@link Activity#finish} on |
| 310 | * it, or because the system is temporarily destroying this |
| 311 | * instance of the activity to save space. You can distinguish |
| 312 | * between these two scenarios with the {@link |
| 313 | * Activity#isFinishing} method.</td> |
| 314 | * <td align="center"><font color="#800000"><strong>Yes</strong></font></td> |
| 315 | * <td align="center"><em>nothing</em></td> |
| 316 | * </tr> |
| 317 | * </tbody> |
| 318 | * </table> |
| 319 | * |
| 320 | * <p>Note the "Killable" column in the above table -- for those methods that |
| 321 | * are marked as being killable, after that method returns the process hosting the |
| 322 | * activity may killed by the system <em>at any time</em> without another line |
| 323 | * of its code being executed. Because of this, you should use the |
| 324 | * {@link #onPause} method to write any persistent data (such as user edits) |
| 325 | * to storage. In addition, the method |
| 326 | * {@link #onSaveInstanceState(Bundle)} is called before placing the activity |
| 327 | * in such a background state, allowing you to save away any dynamic instance |
| 328 | * state in your activity into the given Bundle, to be later received in |
| 329 | * {@link #onCreate} if the activity needs to be re-created. |
| 330 | * See the <a href="#ProcessLifecycle">Process Lifecycle</a> |
| 331 | * section for more information on how the lifecycle of a process is tied |
| 332 | * to the activities it is hosting. Note that it is important to save |
| 333 | * persistent data in {@link #onPause} instead of {@link #onSaveInstanceState} |
| 334 | * because the later is not part of the lifecycle callbacks, so will not |
| 335 | * be called in every situation as described in its documentation.</p> |
| 336 | * |
| 337 | * <p>For those methods that are not marked as being killable, the activity's |
| 338 | * process will not be killed by the system starting from the time the method |
| 339 | * is called and continuing after it returns. Thus an activity is in the killable |
| 340 | * state, for example, between after <code>onPause()</code> to the start of |
| 341 | * <code>onResume()</code>.</p> |
| 342 | * |
| 343 | * <a name="ConfigurationChanges"></a> |
| 344 | * <h3>Configuration Changes</h3> |
| 345 | * |
| 346 | * <p>If the configuration of the device (as defined by the |
| 347 | * {@link Configuration Resources.Configuration} class) changes, |
| 348 | * then anything displaying a user interface will need to update to match that |
| 349 | * configuration. Because Activity is the primary mechanism for interacting |
| 350 | * with the user, it includes special support for handling configuration |
| 351 | * changes.</p> |
| 352 | * |
| 353 | * <p>Unless you specify otherwise, a configuration change (such as a change |
| 354 | * in screen orientation, language, input devices, etc) will cause your |
| 355 | * current activity to be <em>destroyed</em>, going through the normal activity |
| 356 | * lifecycle process of {@link #onPause}, |
| 357 | * {@link #onStop}, and {@link #onDestroy} as appropriate. If the activity |
| 358 | * had been in the foreground or visible to the user, once {@link #onDestroy} is |
| 359 | * called in that instance then a new instance of the activity will be |
| 360 | * created, with whatever savedInstanceState the previous instance had generated |
| 361 | * from {@link #onSaveInstanceState}.</p> |
| 362 | * |
| 363 | * <p>This is done because any application resource, |
| 364 | * including layout files, can change based on any configuration value. Thus |
| 365 | * the only safe way to handle a configuration change is to re-retrieve all |
| 366 | * resources, including layouts, drawables, and strings. Because activities |
| 367 | * must already know how to save their state and re-create themselves from |
| 368 | * that state, this is a convenient way to have an activity restart itself |
| 369 | * with a new configuration.</p> |
| 370 | * |
| 371 | * <p>In some special cases, you may want to bypass restarting of your |
| 372 | * activity based on one or more types of configuration changes. This is |
| 373 | * done with the {@link android.R.attr#configChanges android:configChanges} |
| 374 | * attribute in its manifest. For any types of configuration changes you say |
| 375 | * that you handle there, you will receive a call to your current activity's |
| 376 | * {@link #onConfigurationChanged} method instead of being restarted. If |
| 377 | * a configuration change involves any that you do not handle, however, the |
| 378 | * activity will still be restarted and {@link #onConfigurationChanged} |
| 379 | * will not be called.</p> |
| 380 | * |
| 381 | * <a name="StartingActivities"></a> |
| 382 | * <h3>Starting Activities and Getting Results</h3> |
| 383 | * |
| 384 | * <p>The {@link android.app.Activity#startActivity} |
| 385 | * method is used to start a |
| 386 | * new activity, which will be placed at the top of the activity stack. It |
| 387 | * takes a single argument, an {@link android.content.Intent Intent}, |
| 388 | * which describes the activity |
| 389 | * to be executed.</p> |
| 390 | * |
| 391 | * <p>Sometimes you want to get a result back from an activity when it |
| 392 | * ends. For example, you may start an activity that lets the user pick |
| 393 | * a person in a list of contacts; when it ends, it returns the person |
| 394 | * that was selected. To do this, you call the |
| 395 | * {@link android.app.Activity#startActivityForResult(Intent, int)} |
| 396 | * version with a second integer parameter identifying the call. The result |
| 397 | * will come back through your {@link android.app.Activity#onActivityResult} |
| 398 | * method.</p> |
| 399 | * |
| 400 | * <p>When an activity exits, it can call |
| 401 | * {@link android.app.Activity#setResult(int)} |
| 402 | * to return data back to its parent. It must always supply a result code, |
| 403 | * which can be the standard results RESULT_CANCELED, RESULT_OK, or any |
| 404 | * custom values starting at RESULT_FIRST_USER. In addition, it can optionally |
| 405 | * return back an Intent containing any additional data it wants. All of this |
| 406 | * information appears back on the |
| 407 | * parent's <code>Activity.onActivityResult()</code>, along with the integer |
| 408 | * identifier it originally supplied.</p> |
| 409 | * |
| 410 | * <p>If a child activity fails for any reason (such as crashing), the parent |
| 411 | * activity will receive a result with the code RESULT_CANCELED.</p> |
| 412 | * |
| 413 | * <pre class="prettyprint"> |
| 414 | * public class MyActivity extends Activity { |
| 415 | * ... |
| 416 | * |
| 417 | * static final int PICK_CONTACT_REQUEST = 0; |
| 418 | * |
| 419 | * protected boolean onKeyDown(int keyCode, KeyEvent event) { |
| 420 | * if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { |
| 421 | * // When the user center presses, let them pick a contact. |
| 422 | * startActivityForResult( |
| 423 | * new Intent(Intent.ACTION_PICK, |
| 424 | * new Uri("content://contacts")), |
| 425 | * PICK_CONTACT_REQUEST); |
| 426 | * return true; |
| 427 | * } |
| 428 | * return false; |
| 429 | * } |
| 430 | * |
| 431 | * protected void onActivityResult(int requestCode, int resultCode, |
| 432 | * Intent data) { |
| 433 | * if (requestCode == PICK_CONTACT_REQUEST) { |
| 434 | * if (resultCode == RESULT_OK) { |
| 435 | * // A contact was picked. Here we will just display it |
| 436 | * // to the user. |
| 437 | * startActivity(new Intent(Intent.ACTION_VIEW, data)); |
| 438 | * } |
| 439 | * } |
| 440 | * } |
| 441 | * } |
| 442 | * </pre> |
| 443 | * |
| 444 | * <a name="SavingPersistentState"></a> |
| 445 | * <h3>Saving Persistent State</h3> |
| 446 | * |
| 447 | * <p>There are generally two kinds of persistent state than an activity |
| 448 | * will deal with: shared document-like data (typically stored in a SQLite |
| 449 | * database using a {@linkplain android.content.ContentProvider content provider}) |
| 450 | * and internal state such as user preferences.</p> |
| 451 | * |
| 452 | * <p>For content provider data, we suggest that activities use a |
| 453 | * "edit in place" user model. That is, any edits a user makes are effectively |
| 454 | * made immediately without requiring an additional confirmation step. |
| 455 | * Supporting this model is generally a simple matter of following two rules:</p> |
| 456 | * |
| 457 | * <ul> |
| 458 | * <li> <p>When creating a new document, the backing database entry or file for |
| 459 | * it is created immediately. For example, if the user chooses to write |
| 460 | * a new e-mail, a new entry for that e-mail is created as soon as they |
| 461 | * start entering data, so that if they go to any other activity after |
| 462 | * that point this e-mail will now appear in the list of drafts.</p> |
| 463 | * <li> <p>When an activity's <code>onPause()</code> method is called, it should |
| 464 | * commit to the backing content provider or file any changes the user |
| 465 | * has made. This ensures that those changes will be seen by any other |
| 466 | * activity that is about to run. You will probably want to commit |
| 467 | * your data even more aggressively at key times during your |
| 468 | * activity's lifecycle: for example before starting a new |
| 469 | * activity, before finishing your own activity, when the user |
| 470 | * switches between input fields, etc.</p> |
| 471 | * </ul> |
| 472 | * |
| 473 | * <p>This model is designed to prevent data loss when a user is navigating |
| 474 | * between activities, and allows the system to safely kill an activity (because |
| 475 | * system resources are needed somewhere else) at any time after it has been |
| 476 | * paused. Note this implies |
| 477 | * that the user pressing BACK from your activity does <em>not</em> |
| 478 | * mean "cancel" -- it means to leave the activity with its current contents |
| 479 | * saved away. Cancelling edits in an activity must be provided through |
| 480 | * some other mechanism, such as an explicit "revert" or "undo" option.</p> |
| 481 | * |
| 482 | * <p>See the {@linkplain android.content.ContentProvider content package} for |
| 483 | * more information about content providers. These are a key aspect of how |
| 484 | * different activities invoke and propagate data between themselves.</p> |
| 485 | * |
| 486 | * <p>The Activity class also provides an API for managing internal persistent state |
| 487 | * associated with an activity. This can be used, for example, to remember |
| 488 | * the user's preferred initial display in a calendar (day view or week view) |
| 489 | * or the user's default home page in a web browser.</p> |
| 490 | * |
| 491 | * <p>Activity persistent state is managed |
| 492 | * with the method {@link #getPreferences}, |
| 493 | * allowing you to retrieve and |
| 494 | * modify a set of name/value pairs associated with the activity. To use |
| 495 | * preferences that are shared across multiple application components |
| 496 | * (activities, receivers, services, providers), you can use the underlying |
| 497 | * {@link Context#getSharedPreferences Context.getSharedPreferences()} method |
| 498 | * to retrieve a preferences |
| 499 | * object stored under a specific name. |
| 500 | * (Note that it is not possible to share settings data across application |
| 501 | * packages -- for that you will need a content provider.)</p> |
| 502 | * |
| 503 | * <p>Here is an excerpt from a calendar activity that stores the user's |
| 504 | * preferred view mode in its persistent settings:</p> |
| 505 | * |
| 506 | * <pre class="prettyprint"> |
| 507 | * public class CalendarActivity extends Activity { |
| 508 | * ... |
| 509 | * |
| 510 | * static final int DAY_VIEW_MODE = 0; |
| 511 | * static final int WEEK_VIEW_MODE = 1; |
| 512 | * |
| 513 | * private SharedPreferences mPrefs; |
| 514 | * private int mCurViewMode; |
| 515 | * |
| 516 | * protected void onCreate(Bundle savedInstanceState) { |
| 517 | * super.onCreate(savedInstanceState); |
| 518 | * |
| 519 | * SharedPreferences mPrefs = getSharedPreferences(); |
| 520 | * mCurViewMode = mPrefs.getInt("view_mode" DAY_VIEW_MODE); |
| 521 | * } |
| 522 | * |
| 523 | * protected void onPause() { |
| 524 | * super.onPause(); |
| 525 | * |
| 526 | * SharedPreferences.Editor ed = mPrefs.edit(); |
| 527 | * ed.putInt("view_mode", mCurViewMode); |
| 528 | * ed.commit(); |
| 529 | * } |
| 530 | * } |
| 531 | * </pre> |
| 532 | * |
| 533 | * <a name="Permissions"></a> |
| 534 | * <h3>Permissions</h3> |
| 535 | * |
| 536 | * <p>The ability to start a particular Activity can be enforced when it is |
| 537 | * declared in its |
| 538 | * manifest's {@link android.R.styleable#AndroidManifestActivity <activity>} |
| 539 | * tag. By doing so, other applications will need to declare a corresponding |
| 540 | * {@link android.R.styleable#AndroidManifestUsesPermission <uses-permission>} |
| 541 | * element in their own manifest to be able to start that activity. |
| 542 | * |
| 543 | * <p>See the <a href="{@docRoot}guide/topics/security/security.html">Security and Permissions</a> |
| 544 | * document for more information on permissions and security in general. |
| 545 | * |
| 546 | * <a name="ProcessLifecycle"></a> |
| 547 | * <h3>Process Lifecycle</h3> |
| 548 | * |
| 549 | * <p>The Android system attempts to keep application process around for as |
| 550 | * long as possible, but eventually will need to remove old processes when |
| 551 | * memory runs low. As described in <a href="#ActivityLifecycle">Activity |
| 552 | * Lifecycle</a>, the decision about which process to remove is intimately |
| 553 | * tied to the state of the user's interaction with it. In general, there |
| 554 | * are four states a process can be in based on the activities running in it, |
| 555 | * listed here in order of importance. The system will kill less important |
| 556 | * processes (the last ones) before it resorts to killing more important |
| 557 | * processes (the first ones). |
| 558 | * |
| 559 | * <ol> |
| 560 | * <li> <p>The <b>foreground activity</b> (the activity at the top of the screen |
| 561 | * that the user is currently interacting with) is considered the most important. |
| 562 | * Its process will only be killed as a last resort, if it uses more memory |
| 563 | * than is available on the device. Generally at this point the device has |
| 564 | * reached a memory paging state, so this is required in order to keep the user |
| 565 | * interface responsive. |
| 566 | * <li> <p>A <b>visible activity</b> (an activity that is visible to the user |
| 567 | * but not in the foreground, such as one sitting behind a foreground dialog) |
| 568 | * is considered extremely important and will not be killed unless that is |
| 569 | * required to keep the foreground activity running. |
| 570 | * <li> <p>A <b>background activity</b> (an activity that is not visible to |
| 571 | * the user and has been paused) is no longer critical, so the system may |
| 572 | * safely kill its process to reclaim memory for other foreground or |
| 573 | * visible processes. If its process needs to be killed, when the user navigates |
| 574 | * back to the activity (making it visible on the screen again), its |
| 575 | * {@link #onCreate} method will be called with the savedInstanceState it had previously |
| 576 | * supplied in {@link #onSaveInstanceState} so that it can restart itself in the same |
| 577 | * state as the user last left it. |
| 578 | * <li> <p>An <b>empty process</b> is one hosting no activities or other |
| 579 | * application components (such as {@link Service} or |
| 580 | * {@link android.content.BroadcastReceiver} classes). These are killed very |
| 581 | * quickly by the system as memory becomes low. For this reason, any |
| 582 | * background operation you do outside of an activity must be executed in the |
| 583 | * context of an activity BroadcastReceiver or Service to ensure that the system |
| 584 | * knows it needs to keep your process around. |
| 585 | * </ol> |
| 586 | * |
| 587 | * <p>Sometimes an Activity may need to do a long-running operation that exists |
| 588 | * independently of the activity lifecycle itself. An example may be a camera |
| 589 | * application that allows you to upload a picture to a web site. The upload |
| 590 | * may take a long time, and the application should allow the user to leave |
| 591 | * the application will it is executing. To accomplish this, your Activity |
| 592 | * should start a {@link Service} in which the upload takes place. This allows |
| 593 | * the system to properly prioritize your process (considering it to be more |
| 594 | * important than other non-visible applications) for the duration of the |
| 595 | * upload, independent of whether the original activity is paused, stopped, |
| 596 | * or finished. |
| 597 | */ |
| 598 | public class Activity extends ContextThemeWrapper |
| 599 | implements LayoutInflater.Factory, |
| 600 | Window.Callback, KeyEvent.Callback, |
| 601 | OnCreateContextMenuListener, ComponentCallbacks { |
| 602 | private static final String TAG = "Activity"; |
| 603 | |
| 604 | /** Standard activity result: operation canceled. */ |
| 605 | public static final int RESULT_CANCELED = 0; |
| 606 | /** Standard activity result: operation succeeded. */ |
| 607 | public static final int RESULT_OK = -1; |
| 608 | /** Start of user-defined activity results. */ |
| 609 | public static final int RESULT_FIRST_USER = 1; |
| 610 | |
| 611 | private static long sInstanceCount = 0; |
| 612 | |
| 613 | private static final String WINDOW_HIERARCHY_TAG = "android:viewHierarchyState"; |
| 614 | private static final String SAVED_DIALOG_IDS_KEY = "android:savedDialogIds"; |
| 615 | private static final String SAVED_DIALOGS_TAG = "android:savedDialogs"; |
| 616 | private static final String SAVED_DIALOG_KEY_PREFIX = "android:dialog_"; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 617 | private static final String SAVED_DIALOG_ARGS_KEY_PREFIX = "android:dialog_args_"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 619 | private static class ManagedDialog { |
| 620 | Dialog mDialog; |
| 621 | Bundle mArgs; |
| 622 | } |
| 623 | private SparseArray<ManagedDialog> mManagedDialogs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | |
| 625 | // set by the thread after the constructor and before onCreate(Bundle savedInstanceState) is called. |
| 626 | private Instrumentation mInstrumentation; |
| 627 | private IBinder mToken; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 628 | private int mIdent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | /*package*/ String mEmbeddedID; |
| 630 | private Application mApplication; |
Christopher Tate | b70f3df | 2009-04-07 16:07:59 -0700 | [diff] [blame] | 631 | /*package*/ Intent mIntent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | private ComponentName mComponent; |
| 633 | /*package*/ ActivityInfo mActivityInfo; |
| 634 | /*package*/ ActivityThread mMainThread; |
| 635 | /*package*/ Object mLastNonConfigurationInstance; |
| 636 | /*package*/ HashMap<String,Object> mLastNonConfigurationChildInstances; |
| 637 | Activity mParent; |
| 638 | boolean mCalled; |
| 639 | private boolean mResumed; |
| 640 | private boolean mStopped; |
| 641 | boolean mFinished; |
| 642 | boolean mStartedActivity; |
Jeff Hamilton | 3d32f6e | 2010-04-01 00:04:16 -0500 | [diff] [blame] | 643 | /** true if the activity is being destroyed in order to recreate it with a new configuration */ |
| 644 | /*package*/ boolean mChangingConfigurations = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | /*package*/ int mConfigChangeFlags; |
| 646 | /*package*/ Configuration mCurrentConfig; |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 647 | private SearchManager mSearchManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | |
| 649 | private Window mWindow; |
| 650 | |
| 651 | private WindowManager mWindowManager; |
| 652 | /*package*/ View mDecor = null; |
| 653 | /*package*/ boolean mWindowAdded = false; |
| 654 | /*package*/ boolean mVisibleFromServer = false; |
| 655 | /*package*/ boolean mVisibleFromClient = true; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame^] | 656 | /*package*/ ActionBar mActionBar = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | |
| 658 | private CharSequence mTitle; |
| 659 | private int mTitleColor = 0; |
| 660 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 661 | final FragmentManager mFragments = new FragmentManager(); |
| 662 | |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 663 | private final Object[] sConstructorArgs = new Object[0]; |
| 664 | |
| 665 | private static final Class[] sConstructorSignature = new Class[] { }; |
| 666 | |
| 667 | private static final HashMap<String, Constructor> sConstructorMap = |
| 668 | new HashMap<String, Constructor>(); |
| 669 | |
| 670 | private final class FragmentTransactionImpl implements FragmentTransaction, |
| 671 | Runnable, BackStackState { |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 672 | ArrayList<Fragment> mAdded; |
| 673 | ArrayList<Fragment> mRemoved; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 674 | int mTransition; |
| 675 | int mTransitionStyle; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 676 | boolean mAddToBackStack; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 677 | String mName; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 678 | boolean mCommitted; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 679 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 680 | public FragmentTransaction add(Fragment fragment) { |
| 681 | return add(fragment, 0); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 684 | public FragmentTransaction add(Fragment fragment, int containerViewId) { |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 685 | if (fragment.mActivity != null) { |
| 686 | throw new IllegalStateException("Fragment already added: " + fragment); |
| 687 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 688 | if (mRemoved != null) { |
| 689 | mRemoved.remove(fragment); |
| 690 | } |
| 691 | if (mAdded == null) { |
| 692 | mAdded = new ArrayList<Fragment>(); |
| 693 | } |
| 694 | fragment.mContainerId = containerViewId; |
| 695 | mAdded.add(fragment); |
| 696 | return this; |
| 697 | } |
| 698 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 699 | public FragmentTransaction replace(Fragment fragment, int containerViewId) { |
| 700 | if (containerViewId == 0) { |
| 701 | throw new IllegalArgumentException("Must use non-zero containerViewId"); |
| 702 | } |
| 703 | if (mFragments.mFragments != null) { |
| 704 | for (int i=0; i<mFragments.mFragments.size(); i++) { |
| 705 | Fragment old = mFragments.mFragments.get(i); |
| 706 | if (old.mContainerId == containerViewId) { |
| 707 | remove(old); |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | return add(fragment, containerViewId); |
| 712 | } |
| 713 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 714 | public FragmentTransaction remove(Fragment fragment) { |
| 715 | if (fragment.mActivity == null) { |
| 716 | throw new IllegalStateException("Fragment not added: " + fragment); |
| 717 | } |
| 718 | if (mAdded != null) { |
| 719 | mAdded.remove(fragment); |
| 720 | } |
| 721 | if (mRemoved == null) { |
| 722 | mRemoved = new ArrayList<Fragment>(); |
| 723 | } |
| 724 | mRemoved.add(fragment); |
| 725 | return this; |
| 726 | } |
| 727 | |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 728 | public FragmentTransaction setTransition(int transition) { |
| 729 | mTransition = transition; |
| 730 | return this; |
| 731 | } |
| 732 | |
| 733 | public FragmentTransaction setTransitionStyle(int styleRes) { |
| 734 | mTransitionStyle = styleRes; |
| 735 | return this; |
| 736 | } |
| 737 | |
| 738 | public FragmentTransaction addToBackStack(String name) { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 739 | mAddToBackStack = true; |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 740 | mName = name; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 741 | return this; |
| 742 | } |
| 743 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 744 | public void commit() { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 745 | if (mCommitted) throw new IllegalStateException("commit already called"); |
| 746 | mCommitted = true; |
| 747 | mHandler.post(this); |
| 748 | } |
| 749 | |
| 750 | public void run() { |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 751 | if (mRemoved != null) { |
| 752 | for (int i=mRemoved.size()-1; i>=0; i--) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 753 | mFragments.removeFragment(mRemoved.get(i), mTransition, |
| 754 | mTransitionStyle); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | if (mAdded != null) { |
| 758 | for (int i=mAdded.size()-1; i>=0; i--) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 759 | Fragment f = mAdded.get(i); |
| 760 | mFragments.addFragment(f, false); |
| 761 | if (mAddToBackStack) { |
| 762 | f.mBackStackNesting++; |
| 763 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 764 | } |
| 765 | } |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 766 | mFragments.moveToState(mFragments.mCurState, mTransition, |
| 767 | mTransitionStyle, true); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 768 | if (mAddToBackStack) { |
| 769 | mFragments.addBackStackState(this); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 770 | } |
| 771 | } |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 772 | |
| 773 | public void popFromBackStack() { |
| 774 | if (mAdded != null) { |
| 775 | for (int i=mAdded.size()-1; i>=0; i--) { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 776 | Fragment f = mAdded.get(i); |
| 777 | if (mAddToBackStack) { |
| 778 | f.mBackStackNesting--; |
| 779 | } |
| 780 | mFragments.removeFragment(f, |
| 781 | FragmentManager.reverseTransit(mTransition), |
| 782 | mTransitionStyle); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | if (mRemoved != null) { |
| 786 | for (int i=mRemoved.size()-1; i>=0; i--) { |
| 787 | mFragments.addFragment(mRemoved.get(i), false); |
| 788 | } |
| 789 | } |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | public String getName() { |
| 793 | return mName; |
| 794 | } |
| 795 | |
| 796 | public int getTransition() { |
| 797 | return mTransition; |
| 798 | } |
| 799 | |
| 800 | public int getTransitionStyle() { |
| 801 | return mTransitionStyle; |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 802 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 803 | } |
| 804 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 805 | private static final class ManagedCursor { |
| 806 | ManagedCursor(Cursor cursor) { |
| 807 | mCursor = cursor; |
| 808 | mReleased = false; |
| 809 | mUpdated = false; |
| 810 | } |
| 811 | |
| 812 | private final Cursor mCursor; |
| 813 | private boolean mReleased; |
| 814 | private boolean mUpdated; |
| 815 | } |
| 816 | private final ArrayList<ManagedCursor> mManagedCursors = |
| 817 | new ArrayList<ManagedCursor>(); |
| 818 | |
| 819 | // protected by synchronized (this) |
| 820 | int mResultCode = RESULT_CANCELED; |
| 821 | Intent mResultData = null; |
| 822 | |
| 823 | private boolean mTitleReady = false; |
| 824 | |
| 825 | private int mDefaultKeyMode = DEFAULT_KEYS_DISABLE; |
| 826 | private SpannableStringBuilder mDefaultKeySsb = null; |
| 827 | |
| 828 | protected static final int[] FOCUSED_STATE_SET = {com.android.internal.R.attr.state_focused}; |
| 829 | |
| 830 | private Thread mUiThread; |
| 831 | private final Handler mHandler = new Handler(); |
| 832 | |
Carl Shapiro | 82fe564 | 2010-02-24 00:14:23 -0800 | [diff] [blame] | 833 | // Used for debug only |
| 834 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 835 | public Activity() { |
| 836 | ++sInstanceCount; |
| 837 | } |
| 838 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 839 | @Override |
| 840 | protected void finalize() throws Throwable { |
| 841 | super.finalize(); |
| 842 | --sInstanceCount; |
| 843 | } |
Carl Shapiro | 82fe564 | 2010-02-24 00:14:23 -0800 | [diff] [blame] | 844 | */ |
| 845 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 846 | public static long getInstanceCount() { |
| 847 | return sInstanceCount; |
| 848 | } |
| 849 | |
| 850 | /** Return the intent that started this activity. */ |
| 851 | public Intent getIntent() { |
| 852 | return mIntent; |
| 853 | } |
| 854 | |
| 855 | /** |
| 856 | * Change the intent returned by {@link #getIntent}. This holds a |
| 857 | * reference to the given intent; it does not copy it. Often used in |
| 858 | * conjunction with {@link #onNewIntent}. |
| 859 | * |
| 860 | * @param newIntent The new Intent object to return from getIntent |
| 861 | * |
| 862 | * @see #getIntent |
| 863 | * @see #onNewIntent |
| 864 | */ |
| 865 | public void setIntent(Intent newIntent) { |
| 866 | mIntent = newIntent; |
| 867 | } |
| 868 | |
| 869 | /** Return the application that owns this activity. */ |
| 870 | public final Application getApplication() { |
| 871 | return mApplication; |
| 872 | } |
| 873 | |
| 874 | /** Is this activity embedded inside of another activity? */ |
| 875 | public final boolean isChild() { |
| 876 | return mParent != null; |
| 877 | } |
| 878 | |
| 879 | /** Return the parent activity if this view is an embedded child. */ |
| 880 | public final Activity getParent() { |
| 881 | return mParent; |
| 882 | } |
| 883 | |
| 884 | /** Retrieve the window manager for showing custom windows. */ |
| 885 | public WindowManager getWindowManager() { |
| 886 | return mWindowManager; |
| 887 | } |
| 888 | |
| 889 | /** |
| 890 | * Retrieve the current {@link android.view.Window} for the activity. |
| 891 | * This can be used to directly access parts of the Window API that |
| 892 | * are not available through Activity/Screen. |
| 893 | * |
| 894 | * @return Window The current window, or null if the activity is not |
| 895 | * visual. |
| 896 | */ |
| 897 | public Window getWindow() { |
| 898 | return mWindow; |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * Calls {@link android.view.Window#getCurrentFocus} on the |
| 903 | * Window of this Activity to return the currently focused view. |
| 904 | * |
| 905 | * @return View The current View with focus or null. |
| 906 | * |
| 907 | * @see #getWindow |
| 908 | * @see android.view.Window#getCurrentFocus |
| 909 | */ |
| 910 | public View getCurrentFocus() { |
| 911 | return mWindow != null ? mWindow.getCurrentFocus() : null; |
| 912 | } |
| 913 | |
| 914 | @Override |
| 915 | public int getWallpaperDesiredMinimumWidth() { |
| 916 | int width = super.getWallpaperDesiredMinimumWidth(); |
| 917 | return width <= 0 ? getWindowManager().getDefaultDisplay().getWidth() : width; |
| 918 | } |
| 919 | |
| 920 | @Override |
| 921 | public int getWallpaperDesiredMinimumHeight() { |
| 922 | int height = super.getWallpaperDesiredMinimumHeight(); |
| 923 | return height <= 0 ? getWindowManager().getDefaultDisplay().getHeight() : height; |
| 924 | } |
| 925 | |
| 926 | /** |
| 927 | * Called when the activity is starting. This is where most initialization |
| 928 | * should go: calling {@link #setContentView(int)} to inflate the |
| 929 | * activity's UI, using {@link #findViewById} to programmatically interact |
| 930 | * with widgets in the UI, calling |
| 931 | * {@link #managedQuery(android.net.Uri , String[], String, String[], String)} to retrieve |
| 932 | * cursors for data being displayed, etc. |
| 933 | * |
| 934 | * <p>You can call {@link #finish} from within this function, in |
| 935 | * which case onDestroy() will be immediately called without any of the rest |
| 936 | * of the activity lifecycle ({@link #onStart}, {@link #onResume}, |
| 937 | * {@link #onPause}, etc) executing. |
| 938 | * |
| 939 | * <p><em>Derived classes must call through to the super class's |
| 940 | * implementation of this method. If they do not, an exception will be |
| 941 | * thrown.</em></p> |
| 942 | * |
| 943 | * @param savedInstanceState If the activity is being re-initialized after |
| 944 | * previously being shut down then this Bundle contains the data it most |
| 945 | * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b> |
| 946 | * |
| 947 | * @see #onStart |
| 948 | * @see #onSaveInstanceState |
| 949 | * @see #onRestoreInstanceState |
| 950 | * @see #onPostCreate |
| 951 | */ |
| 952 | protected void onCreate(Bundle savedInstanceState) { |
Dianne Hackborn | bfe319e | 2009-09-21 00:34:05 -0700 | [diff] [blame] | 953 | mVisibleFromClient = !mWindow.getWindowStyle().getBoolean( |
| 954 | com.android.internal.R.styleable.Window_windowNoDisplay, false); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 955 | mFragments.dispatchCreate(savedInstanceState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 956 | mCalled = true; |
| 957 | } |
| 958 | |
| 959 | /** |
| 960 | * The hook for {@link ActivityThread} to restore the state of this activity. |
| 961 | * |
| 962 | * Calls {@link #onSaveInstanceState(android.os.Bundle)} and |
| 963 | * {@link #restoreManagedDialogs(android.os.Bundle)}. |
| 964 | * |
| 965 | * @param savedInstanceState contains the saved state |
| 966 | */ |
| 967 | final void performRestoreInstanceState(Bundle savedInstanceState) { |
| 968 | onRestoreInstanceState(savedInstanceState); |
| 969 | restoreManagedDialogs(savedInstanceState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | /** |
| 973 | * This method is called after {@link #onStart} when the activity is |
| 974 | * being re-initialized from a previously saved state, given here in |
Mike LeBeau | 305de9d | 2010-03-11 09:21:08 -0800 | [diff] [blame] | 975 | * <var>savedInstanceState</var>. Most implementations will simply use {@link #onCreate} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 976 | * to restore their state, but it is sometimes convenient to do it here |
| 977 | * after all of the initialization has been done or to allow subclasses to |
| 978 | * decide whether to use your default implementation. The default |
| 979 | * implementation of this method performs a restore of any view state that |
| 980 | * had previously been frozen by {@link #onSaveInstanceState}. |
| 981 | * |
| 982 | * <p>This method is called between {@link #onStart} and |
| 983 | * {@link #onPostCreate}. |
| 984 | * |
| 985 | * @param savedInstanceState the data most recently supplied in {@link #onSaveInstanceState}. |
| 986 | * |
| 987 | * @see #onCreate |
| 988 | * @see #onPostCreate |
| 989 | * @see #onResume |
| 990 | * @see #onSaveInstanceState |
| 991 | */ |
| 992 | protected void onRestoreInstanceState(Bundle savedInstanceState) { |
| 993 | if (mWindow != null) { |
| 994 | Bundle windowState = savedInstanceState.getBundle(WINDOW_HIERARCHY_TAG); |
| 995 | if (windowState != null) { |
| 996 | mWindow.restoreHierarchyState(windowState); |
| 997 | } |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | /** |
| 1002 | * Restore the state of any saved managed dialogs. |
| 1003 | * |
| 1004 | * @param savedInstanceState The bundle to restore from. |
| 1005 | */ |
| 1006 | private void restoreManagedDialogs(Bundle savedInstanceState) { |
| 1007 | final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG); |
| 1008 | if (b == null) { |
| 1009 | return; |
| 1010 | } |
| 1011 | |
| 1012 | final int[] ids = b.getIntArray(SAVED_DIALOG_IDS_KEY); |
| 1013 | final int numDialogs = ids.length; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1014 | mManagedDialogs = new SparseArray<ManagedDialog>(numDialogs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1015 | for (int i = 0; i < numDialogs; i++) { |
| 1016 | final Integer dialogId = ids[i]; |
| 1017 | Bundle dialogState = b.getBundle(savedDialogKeyFor(dialogId)); |
| 1018 | if (dialogState != null) { |
Romain Guy | e35c235 | 2009-06-19 13:18:12 -0700 | [diff] [blame] | 1019 | // Calling onRestoreInstanceState() below will invoke dispatchOnCreate |
| 1020 | // so tell createDialog() not to do it, otherwise we get an exception |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1021 | final ManagedDialog md = new ManagedDialog(); |
| 1022 | md.mArgs = b.getBundle(savedDialogArgsKeyFor(dialogId)); |
| 1023 | md.mDialog = createDialog(dialogId, dialogState, md.mArgs); |
| 1024 | if (md.mDialog != null) { |
| 1025 | mManagedDialogs.put(dialogId, md); |
| 1026 | onPrepareDialog(dialogId, md.mDialog, md.mArgs); |
| 1027 | md.mDialog.onRestoreInstanceState(dialogState); |
| 1028 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1033 | private Dialog createDialog(Integer dialogId, Bundle state, Bundle args) { |
| 1034 | final Dialog dialog = onCreateDialog(dialogId, args); |
Romain Guy | 764d533 | 2009-06-17 16:52:22 -0700 | [diff] [blame] | 1035 | if (dialog == null) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1036 | return null; |
Romain Guy | 764d533 | 2009-06-17 16:52:22 -0700 | [diff] [blame] | 1037 | } |
Romain Guy | 6de4aed | 2009-07-08 10:54:45 -0700 | [diff] [blame] | 1038 | dialog.dispatchOnCreate(state); |
Romain Guy | 764d533 | 2009-06-17 16:52:22 -0700 | [diff] [blame] | 1039 | return dialog; |
| 1040 | } |
| 1041 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1042 | private static String savedDialogKeyFor(int key) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1043 | return SAVED_DIALOG_KEY_PREFIX + key; |
| 1044 | } |
| 1045 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1046 | private static String savedDialogArgsKeyFor(int key) { |
| 1047 | return SAVED_DIALOG_ARGS_KEY_PREFIX + key; |
| 1048 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1049 | |
| 1050 | /** |
| 1051 | * Called when activity start-up is complete (after {@link #onStart} |
| 1052 | * and {@link #onRestoreInstanceState} have been called). Applications will |
| 1053 | * generally not implement this method; it is intended for system |
| 1054 | * classes to do final initialization after application code has run. |
| 1055 | * |
| 1056 | * <p><em>Derived classes must call through to the super class's |
| 1057 | * implementation of this method. If they do not, an exception will be |
| 1058 | * thrown.</em></p> |
| 1059 | * |
| 1060 | * @param savedInstanceState If the activity is being re-initialized after |
| 1061 | * previously being shut down then this Bundle contains the data it most |
| 1062 | * recently supplied in {@link #onSaveInstanceState}. <b><i>Note: Otherwise it is null.</i></b> |
| 1063 | * @see #onCreate |
| 1064 | */ |
| 1065 | protected void onPostCreate(Bundle savedInstanceState) { |
| 1066 | if (!isChild()) { |
| 1067 | mTitleReady = true; |
| 1068 | onTitleChanged(getTitle(), getTitleColor()); |
| 1069 | } |
| 1070 | mCalled = true; |
| 1071 | } |
| 1072 | |
| 1073 | /** |
| 1074 | * Called after {@link #onCreate} — or after {@link #onRestart} when |
| 1075 | * the activity had been stopped, but is now again being displayed to the |
| 1076 | * user. It will be followed by {@link #onResume}. |
| 1077 | * |
| 1078 | * <p><em>Derived classes must call through to the super class's |
| 1079 | * implementation of this method. If they do not, an exception will be |
| 1080 | * thrown.</em></p> |
| 1081 | * |
| 1082 | * @see #onCreate |
| 1083 | * @see #onStop |
| 1084 | * @see #onResume |
| 1085 | */ |
| 1086 | protected void onStart() { |
| 1087 | mCalled = true; |
| 1088 | } |
| 1089 | |
| 1090 | /** |
| 1091 | * Called after {@link #onStop} when the current activity is being |
| 1092 | * re-displayed to the user (the user has navigated back to it). It will |
| 1093 | * be followed by {@link #onStart} and then {@link #onResume}. |
| 1094 | * |
| 1095 | * <p>For activities that are using raw {@link Cursor} objects (instead of |
| 1096 | * creating them through |
| 1097 | * {@link #managedQuery(android.net.Uri , String[], String, String[], String)}, |
| 1098 | * this is usually the place |
| 1099 | * where the cursor should be requeried (because you had deactivated it in |
| 1100 | * {@link #onStop}. |
| 1101 | * |
| 1102 | * <p><em>Derived classes must call through to the super class's |
| 1103 | * implementation of this method. If they do not, an exception will be |
| 1104 | * thrown.</em></p> |
| 1105 | * |
| 1106 | * @see #onStop |
| 1107 | * @see #onStart |
| 1108 | * @see #onResume |
| 1109 | */ |
| 1110 | protected void onRestart() { |
| 1111 | mCalled = true; |
| 1112 | } |
| 1113 | |
| 1114 | /** |
| 1115 | * Called after {@link #onRestoreInstanceState}, {@link #onRestart}, or |
| 1116 | * {@link #onPause}, for your activity to start interacting with the user. |
| 1117 | * This is a good place to begin animations, open exclusive-access devices |
| 1118 | * (such as the camera), etc. |
| 1119 | * |
| 1120 | * <p>Keep in mind that onResume is not the best indicator that your activity |
| 1121 | * is visible to the user; a system window such as the keyguard may be in |
| 1122 | * front. Use {@link #onWindowFocusChanged} to know for certain that your |
| 1123 | * activity is visible to the user (for example, to resume a game). |
| 1124 | * |
| 1125 | * <p><em>Derived classes must call through to the super class's |
| 1126 | * implementation of this method. If they do not, an exception will be |
| 1127 | * thrown.</em></p> |
| 1128 | * |
| 1129 | * @see #onRestoreInstanceState |
| 1130 | * @see #onRestart |
| 1131 | * @see #onPostResume |
| 1132 | * @see #onPause |
| 1133 | */ |
| 1134 | protected void onResume() { |
| 1135 | mCalled = true; |
| 1136 | } |
| 1137 | |
| 1138 | /** |
| 1139 | * Called when activity resume is complete (after {@link #onResume} has |
| 1140 | * been called). Applications will generally not implement this method; |
| 1141 | * it is intended for system classes to do final setup after application |
| 1142 | * resume code has run. |
| 1143 | * |
| 1144 | * <p><em>Derived classes must call through to the super class's |
| 1145 | * implementation of this method. If they do not, an exception will be |
| 1146 | * thrown.</em></p> |
| 1147 | * |
| 1148 | * @see #onResume |
| 1149 | */ |
| 1150 | protected void onPostResume() { |
| 1151 | final Window win = getWindow(); |
| 1152 | if (win != null) win.makeActive(); |
| 1153 | mCalled = true; |
| 1154 | } |
| 1155 | |
| 1156 | /** |
| 1157 | * This is called for activities that set launchMode to "singleTop" in |
| 1158 | * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} |
| 1159 | * flag when calling {@link #startActivity}. In either case, when the |
| 1160 | * activity is re-launched while at the top of the activity stack instead |
| 1161 | * of a new instance of the activity being started, onNewIntent() will be |
| 1162 | * called on the existing instance with the Intent that was used to |
| 1163 | * re-launch it. |
| 1164 | * |
| 1165 | * <p>An activity will always be paused before receiving a new intent, so |
| 1166 | * you can count on {@link #onResume} being called after this method. |
| 1167 | * |
| 1168 | * <p>Note that {@link #getIntent} still returns the original Intent. You |
| 1169 | * can use {@link #setIntent} to update it to this new Intent. |
| 1170 | * |
| 1171 | * @param intent The new intent that was started for the activity. |
| 1172 | * |
| 1173 | * @see #getIntent |
| 1174 | * @see #setIntent |
| 1175 | * @see #onResume |
| 1176 | */ |
| 1177 | protected void onNewIntent(Intent intent) { |
| 1178 | } |
| 1179 | |
| 1180 | /** |
| 1181 | * The hook for {@link ActivityThread} to save the state of this activity. |
| 1182 | * |
| 1183 | * Calls {@link #onSaveInstanceState(android.os.Bundle)} |
| 1184 | * and {@link #saveManagedDialogs(android.os.Bundle)}. |
| 1185 | * |
| 1186 | * @param outState The bundle to save the state to. |
| 1187 | */ |
| 1188 | final void performSaveInstanceState(Bundle outState) { |
| 1189 | onSaveInstanceState(outState); |
| 1190 | saveManagedDialogs(outState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | /** |
| 1194 | * Called to retrieve per-instance state from an activity before being killed |
| 1195 | * so that the state can be restored in {@link #onCreate} or |
| 1196 | * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this method |
| 1197 | * will be passed to both). |
| 1198 | * |
| 1199 | * <p>This method is called before an activity may be killed so that when it |
| 1200 | * comes back some time in the future it can restore its state. For example, |
| 1201 | * if activity B is launched in front of activity A, and at some point activity |
| 1202 | * A is killed to reclaim resources, activity A will have a chance to save the |
| 1203 | * current state of its user interface via this method so that when the user |
| 1204 | * returns to activity A, the state of the user interface can be restored |
| 1205 | * via {@link #onCreate} or {@link #onRestoreInstanceState}. |
| 1206 | * |
| 1207 | * <p>Do not confuse this method with activity lifecycle callbacks such as |
| 1208 | * {@link #onPause}, which is always called when an activity is being placed |
| 1209 | * in the background or on its way to destruction, or {@link #onStop} which |
| 1210 | * is called before destruction. One example of when {@link #onPause} and |
| 1211 | * {@link #onStop} is called and not this method is when a user navigates back |
| 1212 | * from activity B to activity A: there is no need to call {@link #onSaveInstanceState} |
| 1213 | * on B because that particular instance will never be restored, so the |
| 1214 | * system avoids calling it. An example when {@link #onPause} is called and |
| 1215 | * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A: |
| 1216 | * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't |
| 1217 | * killed during the lifetime of B since the state of the user interface of |
| 1218 | * A will stay intact. |
| 1219 | * |
| 1220 | * <p>The default implementation takes care of most of the UI per-instance |
| 1221 | * state for you by calling {@link android.view.View#onSaveInstanceState()} on each |
| 1222 | * view in the hierarchy that has an id, and by saving the id of the currently |
| 1223 | * focused view (all of which is restored by the default implementation of |
| 1224 | * {@link #onRestoreInstanceState}). If you override this method to save additional |
| 1225 | * information not captured by each individual view, you will likely want to |
| 1226 | * call through to the default implementation, otherwise be prepared to save |
| 1227 | * all of the state of each view yourself. |
| 1228 | * |
| 1229 | * <p>If called, this method will occur before {@link #onStop}. There are |
| 1230 | * no guarantees about whether it will occur before or after {@link #onPause}. |
| 1231 | * |
| 1232 | * @param outState Bundle in which to place your saved state. |
| 1233 | * |
| 1234 | * @see #onCreate |
| 1235 | * @see #onRestoreInstanceState |
| 1236 | * @see #onPause |
| 1237 | */ |
| 1238 | protected void onSaveInstanceState(Bundle outState) { |
| 1239 | outState.putBundle(WINDOW_HIERARCHY_TAG, mWindow.saveHierarchyState()); |
| 1240 | } |
| 1241 | |
| 1242 | /** |
| 1243 | * Save the state of any managed dialogs. |
| 1244 | * |
| 1245 | * @param outState place to store the saved state. |
| 1246 | */ |
| 1247 | private void saveManagedDialogs(Bundle outState) { |
| 1248 | if (mManagedDialogs == null) { |
| 1249 | return; |
| 1250 | } |
| 1251 | |
| 1252 | final int numDialogs = mManagedDialogs.size(); |
| 1253 | if (numDialogs == 0) { |
| 1254 | return; |
| 1255 | } |
| 1256 | |
| 1257 | Bundle dialogState = new Bundle(); |
| 1258 | |
| 1259 | int[] ids = new int[mManagedDialogs.size()]; |
| 1260 | |
| 1261 | // save each dialog's bundle, gather the ids |
| 1262 | for (int i = 0; i < numDialogs; i++) { |
| 1263 | final int key = mManagedDialogs.keyAt(i); |
| 1264 | ids[i] = key; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1265 | final ManagedDialog md = mManagedDialogs.valueAt(i); |
| 1266 | dialogState.putBundle(savedDialogKeyFor(key), md.mDialog.onSaveInstanceState()); |
| 1267 | if (md.mArgs != null) { |
| 1268 | dialogState.putBundle(savedDialogArgsKeyFor(key), md.mArgs); |
| 1269 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | dialogState.putIntArray(SAVED_DIALOG_IDS_KEY, ids); |
| 1273 | outState.putBundle(SAVED_DIALOGS_TAG, dialogState); |
| 1274 | } |
| 1275 | |
| 1276 | |
| 1277 | /** |
| 1278 | * Called as part of the activity lifecycle when an activity is going into |
| 1279 | * the background, but has not (yet) been killed. The counterpart to |
| 1280 | * {@link #onResume}. |
| 1281 | * |
| 1282 | * <p>When activity B is launched in front of activity A, this callback will |
| 1283 | * be invoked on A. B will not be created until A's {@link #onPause} returns, |
| 1284 | * so be sure to not do anything lengthy here. |
| 1285 | * |
| 1286 | * <p>This callback is mostly used for saving any persistent state the |
| 1287 | * activity is editing, to present a "edit in place" model to the user and |
| 1288 | * making sure nothing is lost if there are not enough resources to start |
| 1289 | * the new activity without first killing this one. This is also a good |
| 1290 | * place to do things like stop animations and other things that consume a |
| 1291 | * noticeable mount of CPU in order to make the switch to the next activity |
| 1292 | * as fast as possible, or to close resources that are exclusive access |
| 1293 | * such as the camera. |
| 1294 | * |
| 1295 | * <p>In situations where the system needs more memory it may kill paused |
| 1296 | * processes to reclaim resources. Because of this, you should be sure |
| 1297 | * that all of your state is saved by the time you return from |
| 1298 | * this function. In general {@link #onSaveInstanceState} is used to save |
| 1299 | * per-instance state in the activity and this method is used to store |
| 1300 | * global persistent data (in content providers, files, etc.) |
| 1301 | * |
| 1302 | * <p>After receiving this call you will usually receive a following call |
| 1303 | * to {@link #onStop} (after the next activity has been resumed and |
| 1304 | * displayed), however in some cases there will be a direct call back to |
| 1305 | * {@link #onResume} without going through the stopped state. |
| 1306 | * |
| 1307 | * <p><em>Derived classes must call through to the super class's |
| 1308 | * implementation of this method. If they do not, an exception will be |
| 1309 | * thrown.</em></p> |
| 1310 | * |
| 1311 | * @see #onResume |
| 1312 | * @see #onSaveInstanceState |
| 1313 | * @see #onStop |
| 1314 | */ |
| 1315 | protected void onPause() { |
| 1316 | mCalled = true; |
| 1317 | } |
| 1318 | |
| 1319 | /** |
| 1320 | * Called as part of the activity lifecycle when an activity is about to go |
| 1321 | * into the background as the result of user choice. For example, when the |
| 1322 | * user presses the Home key, {@link #onUserLeaveHint} will be called, but |
| 1323 | * when an incoming phone call causes the in-call Activity to be automatically |
| 1324 | * brought to the foreground, {@link #onUserLeaveHint} will not be called on |
| 1325 | * the activity being interrupted. In cases when it is invoked, this method |
| 1326 | * is called right before the activity's {@link #onPause} callback. |
| 1327 | * |
| 1328 | * <p>This callback and {@link #onUserInteraction} are intended to help |
| 1329 | * activities manage status bar notifications intelligently; specifically, |
| 1330 | * for helping activities determine the proper time to cancel a notfication. |
| 1331 | * |
| 1332 | * @see #onUserInteraction() |
| 1333 | */ |
| 1334 | protected void onUserLeaveHint() { |
| 1335 | } |
| 1336 | |
| 1337 | /** |
| 1338 | * Generate a new thumbnail for this activity. This method is called before |
| 1339 | * pausing the activity, and should draw into <var>outBitmap</var> the |
| 1340 | * imagery for the desired thumbnail in the dimensions of that bitmap. It |
| 1341 | * can use the given <var>canvas</var>, which is configured to draw into the |
| 1342 | * bitmap, for rendering if desired. |
| 1343 | * |
| 1344 | * <p>The default implementation renders the Screen's current view |
| 1345 | * hierarchy into the canvas to generate a thumbnail. |
| 1346 | * |
| 1347 | * <p>If you return false, the bitmap will be filled with a default |
| 1348 | * thumbnail. |
| 1349 | * |
| 1350 | * @param outBitmap The bitmap to contain the thumbnail. |
| 1351 | * @param canvas Can be used to render into the bitmap. |
| 1352 | * |
| 1353 | * @return Return true if you have drawn into the bitmap; otherwise after |
| 1354 | * you return it will be filled with a default thumbnail. |
| 1355 | * |
| 1356 | * @see #onCreateDescription |
| 1357 | * @see #onSaveInstanceState |
| 1358 | * @see #onPause |
| 1359 | */ |
| 1360 | public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) { |
| 1361 | final View view = mDecor; |
| 1362 | if (view == null) { |
| 1363 | return false; |
| 1364 | } |
| 1365 | |
| 1366 | final int vw = view.getWidth(); |
| 1367 | final int vh = view.getHeight(); |
| 1368 | final int dw = outBitmap.getWidth(); |
| 1369 | final int dh = outBitmap.getHeight(); |
| 1370 | |
| 1371 | canvas.save(); |
| 1372 | canvas.scale(((float)dw)/vw, ((float)dh)/vh); |
| 1373 | view.draw(canvas); |
| 1374 | canvas.restore(); |
| 1375 | |
| 1376 | return true; |
| 1377 | } |
| 1378 | |
| 1379 | /** |
| 1380 | * Generate a new description for this activity. This method is called |
| 1381 | * before pausing the activity and can, if desired, return some textual |
| 1382 | * description of its current state to be displayed to the user. |
| 1383 | * |
| 1384 | * <p>The default implementation returns null, which will cause you to |
| 1385 | * inherit the description from the previous activity. If all activities |
| 1386 | * return null, generally the label of the top activity will be used as the |
| 1387 | * description. |
| 1388 | * |
| 1389 | * @return A description of what the user is doing. It should be short and |
| 1390 | * sweet (only a few words). |
| 1391 | * |
| 1392 | * @see #onCreateThumbnail |
| 1393 | * @see #onSaveInstanceState |
| 1394 | * @see #onPause |
| 1395 | */ |
| 1396 | public CharSequence onCreateDescription() { |
| 1397 | return null; |
| 1398 | } |
| 1399 | |
| 1400 | /** |
| 1401 | * Called when you are no longer visible to the user. You will next |
| 1402 | * receive either {@link #onRestart}, {@link #onDestroy}, or nothing, |
| 1403 | * depending on later user activity. |
| 1404 | * |
| 1405 | * <p>Note that this method may never be called, in low memory situations |
| 1406 | * where the system does not have enough memory to keep your activity's |
| 1407 | * process running after its {@link #onPause} method is called. |
| 1408 | * |
| 1409 | * <p><em>Derived classes must call through to the super class's |
| 1410 | * implementation of this method. If they do not, an exception will be |
| 1411 | * thrown.</em></p> |
| 1412 | * |
| 1413 | * @see #onRestart |
| 1414 | * @see #onResume |
| 1415 | * @see #onSaveInstanceState |
| 1416 | * @see #onDestroy |
| 1417 | */ |
| 1418 | protected void onStop() { |
| 1419 | mCalled = true; |
| 1420 | } |
| 1421 | |
| 1422 | /** |
| 1423 | * Perform any final cleanup before an activity is destroyed. This can |
| 1424 | * happen either because the activity is finishing (someone called |
| 1425 | * {@link #finish} on it, or because the system is temporarily destroying |
| 1426 | * this instance of the activity to save space. You can distinguish |
| 1427 | * between these two scenarios with the {@link #isFinishing} method. |
| 1428 | * |
| 1429 | * <p><em>Note: do not count on this method being called as a place for |
| 1430 | * saving data! For example, if an activity is editing data in a content |
| 1431 | * provider, those edits should be committed in either {@link #onPause} or |
| 1432 | * {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to |
| 1433 | * free resources like threads that are associated with an activity, so |
| 1434 | * that a destroyed activity does not leave such things around while the |
| 1435 | * rest of its application is still running. There are situations where |
| 1436 | * the system will simply kill the activity's hosting process without |
| 1437 | * calling this method (or any others) in it, so it should not be used to |
| 1438 | * do things that are intended to remain around after the process goes |
| 1439 | * away. |
| 1440 | * |
| 1441 | * <p><em>Derived classes must call through to the super class's |
| 1442 | * implementation of this method. If they do not, an exception will be |
| 1443 | * thrown.</em></p> |
| 1444 | * |
| 1445 | * @see #onPause |
| 1446 | * @see #onStop |
| 1447 | * @see #finish |
| 1448 | * @see #isFinishing |
| 1449 | */ |
| 1450 | protected void onDestroy() { |
| 1451 | mCalled = true; |
| 1452 | |
| 1453 | // dismiss any dialogs we are managing. |
| 1454 | if (mManagedDialogs != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1455 | final int numDialogs = mManagedDialogs.size(); |
| 1456 | for (int i = 0; i < numDialogs; i++) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1457 | final ManagedDialog md = mManagedDialogs.valueAt(i); |
| 1458 | if (md.mDialog.isShowing()) { |
| 1459 | md.mDialog.dismiss(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1460 | } |
| 1461 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1462 | mManagedDialogs = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1463 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1464 | |
| 1465 | // close any cursors we are managing. |
Makoto Onuki | 2f6a018 | 2010-02-22 13:26:59 -0800 | [diff] [blame] | 1466 | synchronized (mManagedCursors) { |
| 1467 | int numCursors = mManagedCursors.size(); |
| 1468 | for (int i = 0; i < numCursors; i++) { |
| 1469 | ManagedCursor c = mManagedCursors.get(i); |
| 1470 | if (c != null) { |
| 1471 | c.mCursor.close(); |
| 1472 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1473 | } |
Makoto Onuki | 2f6a018 | 2010-02-22 13:26:59 -0800 | [diff] [blame] | 1474 | mManagedCursors.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1475 | } |
Amith Yamasani | 4986044 | 2010-03-17 20:54:10 -0700 | [diff] [blame] | 1476 | |
| 1477 | // Close any open search dialog |
| 1478 | if (mSearchManager != null) { |
| 1479 | mSearchManager.stopSearch(); |
| 1480 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | /** |
| 1484 | * Called by the system when the device configuration changes while your |
| 1485 | * activity is running. Note that this will <em>only</em> be called if |
| 1486 | * you have selected configurations you would like to handle with the |
| 1487 | * {@link android.R.attr#configChanges} attribute in your manifest. If |
| 1488 | * any configuration change occurs that is not selected to be reported |
| 1489 | * by that attribute, then instead of reporting it the system will stop |
| 1490 | * and restart the activity (to have it launched with the new |
| 1491 | * configuration). |
| 1492 | * |
| 1493 | * <p>At the time that this function has been called, your Resources |
| 1494 | * object will have been updated to return resource values matching the |
| 1495 | * new configuration. |
| 1496 | * |
| 1497 | * @param newConfig The new device configuration. |
| 1498 | */ |
| 1499 | public void onConfigurationChanged(Configuration newConfig) { |
| 1500 | mCalled = true; |
Bjorn Bringert | 444c727 | 2009-07-06 21:32:50 +0100 | [diff] [blame] | 1501 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1502 | if (mWindow != null) { |
| 1503 | // Pass the configuration changed event to the window |
| 1504 | mWindow.onConfigurationChanged(newConfig); |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | /** |
| 1509 | * If this activity is being destroyed because it can not handle a |
| 1510 | * configuration parameter being changed (and thus its |
| 1511 | * {@link #onConfigurationChanged(Configuration)} method is |
| 1512 | * <em>not</em> being called), then you can use this method to discover |
| 1513 | * the set of changes that have occurred while in the process of being |
| 1514 | * destroyed. Note that there is no guarantee that these will be |
| 1515 | * accurate (other changes could have happened at any time), so you should |
| 1516 | * only use this as an optimization hint. |
| 1517 | * |
| 1518 | * @return Returns a bit field of the configuration parameters that are |
| 1519 | * changing, as defined by the {@link android.content.res.Configuration} |
| 1520 | * class. |
| 1521 | */ |
| 1522 | public int getChangingConfigurations() { |
| 1523 | return mConfigChangeFlags; |
| 1524 | } |
| 1525 | |
| 1526 | /** |
| 1527 | * Retrieve the non-configuration instance data that was previously |
| 1528 | * returned by {@link #onRetainNonConfigurationInstance()}. This will |
| 1529 | * be available from the initial {@link #onCreate} and |
| 1530 | * {@link #onStart} calls to the new instance, allowing you to extract |
| 1531 | * any useful dynamic state from the previous instance. |
| 1532 | * |
| 1533 | * <p>Note that the data you retrieve here should <em>only</em> be used |
| 1534 | * as an optimization for handling configuration changes. You should always |
| 1535 | * be able to handle getting a null pointer back, and an activity must |
| 1536 | * still be able to restore itself to its previous state (through the |
| 1537 | * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this |
| 1538 | * function returns null. |
| 1539 | * |
| 1540 | * @return Returns the object previously returned by |
| 1541 | * {@link #onRetainNonConfigurationInstance()}. |
| 1542 | */ |
| 1543 | public Object getLastNonConfigurationInstance() { |
| 1544 | return mLastNonConfigurationInstance; |
| 1545 | } |
| 1546 | |
| 1547 | /** |
| 1548 | * Called by the system, as part of destroying an |
| 1549 | * activity due to a configuration change, when it is known that a new |
| 1550 | * instance will immediately be created for the new configuration. You |
| 1551 | * can return any object you like here, including the activity instance |
| 1552 | * itself, which can later be retrieved by calling |
| 1553 | * {@link #getLastNonConfigurationInstance()} in the new activity |
| 1554 | * instance. |
| 1555 | * |
| 1556 | * <p>This function is called purely as an optimization, and you must |
| 1557 | * not rely on it being called. When it is called, a number of guarantees |
| 1558 | * will be made to help optimize configuration switching: |
| 1559 | * <ul> |
| 1560 | * <li> The function will be called between {@link #onStop} and |
| 1561 | * {@link #onDestroy}. |
| 1562 | * <li> A new instance of the activity will <em>always</em> be immediately |
| 1563 | * created after this one's {@link #onDestroy()} is called. |
| 1564 | * <li> The object you return here will <em>always</em> be available from |
| 1565 | * the {@link #getLastNonConfigurationInstance()} method of the following |
| 1566 | * activity instance as described there. |
| 1567 | * </ul> |
| 1568 | * |
| 1569 | * <p>These guarantees are designed so that an activity can use this API |
| 1570 | * to propagate extensive state from the old to new activity instance, from |
| 1571 | * loaded bitmaps, to network connections, to evenly actively running |
| 1572 | * threads. Note that you should <em>not</em> propagate any data that |
| 1573 | * may change based on the configuration, including any data loaded from |
| 1574 | * resources such as strings, layouts, or drawables. |
| 1575 | * |
| 1576 | * @return Return any Object holding the desired state to propagate to the |
| 1577 | * next activity instance. |
| 1578 | */ |
| 1579 | public Object onRetainNonConfigurationInstance() { |
| 1580 | return null; |
| 1581 | } |
| 1582 | |
| 1583 | /** |
| 1584 | * Retrieve the non-configuration instance data that was previously |
| 1585 | * returned by {@link #onRetainNonConfigurationChildInstances()}. This will |
| 1586 | * be available from the initial {@link #onCreate} and |
| 1587 | * {@link #onStart} calls to the new instance, allowing you to extract |
| 1588 | * any useful dynamic state from the previous instance. |
| 1589 | * |
| 1590 | * <p>Note that the data you retrieve here should <em>only</em> be used |
| 1591 | * as an optimization for handling configuration changes. You should always |
| 1592 | * be able to handle getting a null pointer back, and an activity must |
| 1593 | * still be able to restore itself to its previous state (through the |
| 1594 | * normal {@link #onSaveInstanceState(Bundle)} mechanism) even if this |
| 1595 | * function returns null. |
| 1596 | * |
| 1597 | * @return Returns the object previously returned by |
| 1598 | * {@link #onRetainNonConfigurationChildInstances()} |
| 1599 | */ |
| 1600 | HashMap<String,Object> getLastNonConfigurationChildInstances() { |
| 1601 | return mLastNonConfigurationChildInstances; |
| 1602 | } |
| 1603 | |
| 1604 | /** |
| 1605 | * This method is similar to {@link #onRetainNonConfigurationInstance()} except that |
| 1606 | * it should return either a mapping from child activity id strings to arbitrary objects, |
| 1607 | * or null. This method is intended to be used by Activity framework subclasses that control a |
| 1608 | * set of child activities, such as ActivityGroup. The same guarantees and restrictions apply |
| 1609 | * as for {@link #onRetainNonConfigurationInstance()}. The default implementation returns null. |
| 1610 | */ |
| 1611 | HashMap<String,Object> onRetainNonConfigurationChildInstances() { |
| 1612 | return null; |
| 1613 | } |
| 1614 | |
| 1615 | public void onLowMemory() { |
| 1616 | mCalled = true; |
| 1617 | } |
| 1618 | |
| 1619 | /** |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 1620 | * Start a series of edit operations on the Fragments associated with |
| 1621 | * this activity. |
| 1622 | */ |
| 1623 | public FragmentTransaction openFragmentTransaction() { |
| 1624 | return new FragmentTransactionImpl(); |
| 1625 | } |
| 1626 | |
| 1627 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1628 | * Wrapper around |
| 1629 | * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)} |
| 1630 | * that gives the resulting {@link Cursor} to call |
| 1631 | * {@link #startManagingCursor} so that the activity will manage its |
| 1632 | * lifecycle for you. |
| 1633 | * |
| 1634 | * @param uri The URI of the content provider to query. |
| 1635 | * @param projection List of columns to return. |
| 1636 | * @param selection SQL WHERE clause. |
| 1637 | * @param sortOrder SQL ORDER BY clause. |
| 1638 | * |
| 1639 | * @return The Cursor that was returned by query(). |
| 1640 | * |
| 1641 | * @see ContentResolver#query(android.net.Uri , String[], String, String[], String) |
| 1642 | * @see #startManagingCursor |
| 1643 | * @hide |
| 1644 | */ |
| 1645 | public final Cursor managedQuery(Uri uri, |
| 1646 | String[] projection, |
| 1647 | String selection, |
| 1648 | String sortOrder) |
| 1649 | { |
| 1650 | Cursor c = getContentResolver().query(uri, projection, selection, null, sortOrder); |
| 1651 | if (c != null) { |
| 1652 | startManagingCursor(c); |
| 1653 | } |
| 1654 | return c; |
| 1655 | } |
| 1656 | |
| 1657 | /** |
| 1658 | * Wrapper around |
| 1659 | * {@link ContentResolver#query(android.net.Uri , String[], String, String[], String)} |
| 1660 | * that gives the resulting {@link Cursor} to call |
| 1661 | * {@link #startManagingCursor} so that the activity will manage its |
| 1662 | * lifecycle for you. |
| 1663 | * |
| 1664 | * @param uri The URI of the content provider to query. |
| 1665 | * @param projection List of columns to return. |
| 1666 | * @param selection SQL WHERE clause. |
| 1667 | * @param selectionArgs The arguments to selection, if any ?s are pesent |
| 1668 | * @param sortOrder SQL ORDER BY clause. |
| 1669 | * |
| 1670 | * @return The Cursor that was returned by query(). |
| 1671 | * |
| 1672 | * @see ContentResolver#query(android.net.Uri , String[], String, String[], String) |
| 1673 | * @see #startManagingCursor |
| 1674 | */ |
| 1675 | public final Cursor managedQuery(Uri uri, |
| 1676 | String[] projection, |
| 1677 | String selection, |
| 1678 | String[] selectionArgs, |
| 1679 | String sortOrder) |
| 1680 | { |
| 1681 | Cursor c = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder); |
| 1682 | if (c != null) { |
| 1683 | startManagingCursor(c); |
| 1684 | } |
| 1685 | return c; |
| 1686 | } |
| 1687 | |
| 1688 | /** |
| 1689 | * Wrapper around {@link Cursor#commitUpdates()} that takes care of noting |
| 1690 | * that the Cursor needs to be requeried. You can call this method in |
| 1691 | * {@link #onPause} or {@link #onStop} to have the system call |
| 1692 | * {@link Cursor#requery} for you if the activity is later resumed. This |
| 1693 | * allows you to avoid determing when to do the requery yourself (which is |
| 1694 | * required for the Cursor to see any data changes that were committed with |
| 1695 | * it). |
| 1696 | * |
| 1697 | * @param c The Cursor whose changes are to be committed. |
| 1698 | * |
| 1699 | * @see #managedQuery(android.net.Uri , String[], String, String[], String) |
| 1700 | * @see #startManagingCursor |
| 1701 | * @see Cursor#commitUpdates() |
| 1702 | * @see Cursor#requery |
| 1703 | * @hide |
| 1704 | */ |
| 1705 | @Deprecated |
| 1706 | public void managedCommitUpdates(Cursor c) { |
| 1707 | synchronized (mManagedCursors) { |
| 1708 | final int N = mManagedCursors.size(); |
| 1709 | for (int i=0; i<N; i++) { |
| 1710 | ManagedCursor mc = mManagedCursors.get(i); |
| 1711 | if (mc.mCursor == c) { |
| 1712 | c.commitUpdates(); |
| 1713 | mc.mUpdated = true; |
| 1714 | return; |
| 1715 | } |
| 1716 | } |
| 1717 | throw new RuntimeException( |
| 1718 | "Cursor " + c + " is not currently managed"); |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | /** |
| 1723 | * This method allows the activity to take care of managing the given |
| 1724 | * {@link Cursor}'s lifecycle for you based on the activity's lifecycle. |
| 1725 | * That is, when the activity is stopped it will automatically call |
| 1726 | * {@link Cursor#deactivate} on the given Cursor, and when it is later restarted |
| 1727 | * it will call {@link Cursor#requery} for you. When the activity is |
| 1728 | * destroyed, all managed Cursors will be closed automatically. |
| 1729 | * |
| 1730 | * @param c The Cursor to be managed. |
| 1731 | * |
| 1732 | * @see #managedQuery(android.net.Uri , String[], String, String[], String) |
| 1733 | * @see #stopManagingCursor |
| 1734 | */ |
| 1735 | public void startManagingCursor(Cursor c) { |
| 1736 | synchronized (mManagedCursors) { |
| 1737 | mManagedCursors.add(new ManagedCursor(c)); |
| 1738 | } |
| 1739 | } |
| 1740 | |
| 1741 | /** |
| 1742 | * Given a Cursor that was previously given to |
| 1743 | * {@link #startManagingCursor}, stop the activity's management of that |
| 1744 | * cursor. |
| 1745 | * |
| 1746 | * @param c The Cursor that was being managed. |
| 1747 | * |
| 1748 | * @see #startManagingCursor |
| 1749 | */ |
| 1750 | public void stopManagingCursor(Cursor c) { |
| 1751 | synchronized (mManagedCursors) { |
| 1752 | final int N = mManagedCursors.size(); |
| 1753 | for (int i=0; i<N; i++) { |
| 1754 | ManagedCursor mc = mManagedCursors.get(i); |
| 1755 | if (mc.mCursor == c) { |
| 1756 | mManagedCursors.remove(i); |
| 1757 | break; |
| 1758 | } |
| 1759 | } |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | /** |
| 1764 | * Control whether this activity is required to be persistent. By default |
| 1765 | * activities are not persistent; setting this to true will prevent the |
| 1766 | * system from stopping this activity or its process when running low on |
| 1767 | * resources. |
| 1768 | * |
| 1769 | * <p><em>You should avoid using this method</em>, it has severe negative |
| 1770 | * consequences on how well the system can manage its resources. A better |
| 1771 | * approach is to implement an application service that you control with |
| 1772 | * {@link Context#startService} and {@link Context#stopService}. |
| 1773 | * |
| 1774 | * @param isPersistent Control whether the current activity must be |
| 1775 | * persistent, true if so, false for the normal |
| 1776 | * behavior. |
| 1777 | */ |
| 1778 | public void setPersistent(boolean isPersistent) { |
| 1779 | if (mParent == null) { |
| 1780 | try { |
| 1781 | ActivityManagerNative.getDefault() |
| 1782 | .setPersistent(mToken, isPersistent); |
| 1783 | } catch (RemoteException e) { |
| 1784 | // Empty |
| 1785 | } |
| 1786 | } else { |
| 1787 | throw new RuntimeException("setPersistent() not yet supported for embedded activities"); |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | /** |
| 1792 | * Finds a view that was identified by the id attribute from the XML that |
| 1793 | * was processed in {@link #onCreate}. |
| 1794 | * |
| 1795 | * @return The view if found or null otherwise. |
| 1796 | */ |
| 1797 | public View findViewById(int id) { |
| 1798 | return getWindow().findViewById(id); |
| 1799 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame^] | 1800 | |
| 1801 | /** |
| 1802 | * Retrieve a reference to this activity's ActionBar. |
| 1803 | * |
| 1804 | * <p><em>Note:</em> The ActionBar is initialized when a content view |
| 1805 | * is set. This function will return null if called before {@link #setContentView} |
| 1806 | * or {@link #addContentView}. |
| 1807 | * @return The Activity's ActionBar, or null if it does not have one. |
| 1808 | */ |
| 1809 | public ActionBar getActionBar() { |
| 1810 | return mActionBar; |
| 1811 | } |
| 1812 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1813 | /** |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 1814 | * Finds a fragment that was identified by the given id either when inflated |
| 1815 | * from XML or as the container ID when added in a transaction. This only |
| 1816 | * returns fragments that are currently added to the activity's content. |
| 1817 | * @return The fragment if found or null otherwise. |
| 1818 | */ |
| 1819 | public Fragment findFragmentById(int id) { |
| 1820 | return mFragments.findFragmentById(id); |
| 1821 | } |
| 1822 | |
| 1823 | /** |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame^] | 1824 | * Creates a new ActionBar, locates the inflated ActionBarView, |
| 1825 | * initializes the ActionBar with the view, and sets mActionBar. |
| 1826 | */ |
| 1827 | private void initActionBar() { |
| 1828 | if (!getWindow().hasFeature(Window.FEATURE_ACTION_BAR)) { |
| 1829 | return; |
| 1830 | } |
| 1831 | |
| 1832 | ActionBarView view = (ActionBarView) findViewById(com.android.internal.R.id.action_bar); |
| 1833 | if (view != null) { |
| 1834 | LinearLayout splitView = |
| 1835 | (LinearLayout) findViewById(com.android.internal.R.id.context_action_bar); |
| 1836 | if (splitView != null) { |
| 1837 | mActionBar = new SplitActionBar(view, splitView); |
| 1838 | } |
| 1839 | } else { |
| 1840 | Log.e(TAG, "Could not create action bar; view not found in window decor."); |
| 1841 | } |
| 1842 | } |
| 1843 | |
| 1844 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1845 | * Set the activity content from a layout resource. The resource will be |
| 1846 | * inflated, adding all top-level views to the activity. |
| 1847 | * |
| 1848 | * @param layoutResID Resource ID to be inflated. |
| 1849 | */ |
| 1850 | public void setContentView(int layoutResID) { |
| 1851 | getWindow().setContentView(layoutResID); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame^] | 1852 | initActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | /** |
| 1856 | * Set the activity content to an explicit view. This view is placed |
| 1857 | * directly into the activity's view hierarchy. It can itself be a complex |
| 1858 | * view hierarhcy. |
| 1859 | * |
| 1860 | * @param view The desired content to display. |
| 1861 | */ |
| 1862 | public void setContentView(View view) { |
| 1863 | getWindow().setContentView(view); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame^] | 1864 | initActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | /** |
| 1868 | * Set the activity content to an explicit view. This view is placed |
| 1869 | * directly into the activity's view hierarchy. It can itself be a complex |
| 1870 | * view hierarhcy. |
| 1871 | * |
| 1872 | * @param view The desired content to display. |
| 1873 | * @param params Layout parameters for the view. |
| 1874 | */ |
| 1875 | public void setContentView(View view, ViewGroup.LayoutParams params) { |
| 1876 | getWindow().setContentView(view, params); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame^] | 1877 | initActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | /** |
| 1881 | * Add an additional content view to the activity. Added after any existing |
| 1882 | * ones in the activity -- existing views are NOT removed. |
| 1883 | * |
| 1884 | * @param view The desired content to display. |
| 1885 | * @param params Layout parameters for the view. |
| 1886 | */ |
| 1887 | public void addContentView(View view, ViewGroup.LayoutParams params) { |
| 1888 | getWindow().addContentView(view, params); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame^] | 1889 | initActionBar(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | /** |
| 1893 | * Use with {@link #setDefaultKeyMode} to turn off default handling of |
| 1894 | * keys. |
| 1895 | * |
| 1896 | * @see #setDefaultKeyMode |
| 1897 | */ |
| 1898 | static public final int DEFAULT_KEYS_DISABLE = 0; |
| 1899 | /** |
| 1900 | * Use with {@link #setDefaultKeyMode} to launch the dialer during default |
| 1901 | * key handling. |
| 1902 | * |
| 1903 | * @see #setDefaultKeyMode |
| 1904 | */ |
| 1905 | static public final int DEFAULT_KEYS_DIALER = 1; |
| 1906 | /** |
| 1907 | * Use with {@link #setDefaultKeyMode} to execute a menu shortcut in |
| 1908 | * default key handling. |
| 1909 | * |
| 1910 | * <p>That is, the user does not need to hold down the menu key to execute menu shortcuts. |
| 1911 | * |
| 1912 | * @see #setDefaultKeyMode |
| 1913 | */ |
| 1914 | static public final int DEFAULT_KEYS_SHORTCUT = 2; |
| 1915 | /** |
| 1916 | * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes |
| 1917 | * will start an application-defined search. (If the application or activity does not |
| 1918 | * actually define a search, the the keys will be ignored.) |
| 1919 | * |
| 1920 | * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details. |
| 1921 | * |
| 1922 | * @see #setDefaultKeyMode |
| 1923 | */ |
| 1924 | static public final int DEFAULT_KEYS_SEARCH_LOCAL = 3; |
| 1925 | |
| 1926 | /** |
| 1927 | * Use with {@link #setDefaultKeyMode} to specify that unhandled keystrokes |
| 1928 | * will start a global search (typically web search, but some platforms may define alternate |
| 1929 | * methods for global search) |
| 1930 | * |
| 1931 | * <p>See {@link android.app.SearchManager android.app.SearchManager} for more details. |
| 1932 | * |
| 1933 | * @see #setDefaultKeyMode |
| 1934 | */ |
| 1935 | static public final int DEFAULT_KEYS_SEARCH_GLOBAL = 4; |
| 1936 | |
| 1937 | /** |
| 1938 | * Select the default key handling for this activity. This controls what |
| 1939 | * will happen to key events that are not otherwise handled. The default |
| 1940 | * mode ({@link #DEFAULT_KEYS_DISABLE}) will simply drop them on the |
| 1941 | * floor. Other modes allow you to launch the dialer |
| 1942 | * ({@link #DEFAULT_KEYS_DIALER}), execute a shortcut in your options |
| 1943 | * menu without requiring the menu key be held down |
| 1944 | * ({@link #DEFAULT_KEYS_SHORTCUT}), or launch a search ({@link #DEFAULT_KEYS_SEARCH_LOCAL} |
| 1945 | * and {@link #DEFAULT_KEYS_SEARCH_GLOBAL}). |
| 1946 | * |
| 1947 | * <p>Note that the mode selected here does not impact the default |
| 1948 | * handling of system keys, such as the "back" and "menu" keys, and your |
| 1949 | * activity and its views always get a first chance to receive and handle |
| 1950 | * all application keys. |
| 1951 | * |
| 1952 | * @param mode The desired default key mode constant. |
| 1953 | * |
| 1954 | * @see #DEFAULT_KEYS_DISABLE |
| 1955 | * @see #DEFAULT_KEYS_DIALER |
| 1956 | * @see #DEFAULT_KEYS_SHORTCUT |
| 1957 | * @see #DEFAULT_KEYS_SEARCH_LOCAL |
| 1958 | * @see #DEFAULT_KEYS_SEARCH_GLOBAL |
| 1959 | * @see #onKeyDown |
| 1960 | */ |
| 1961 | public final void setDefaultKeyMode(int mode) { |
| 1962 | mDefaultKeyMode = mode; |
| 1963 | |
| 1964 | // Some modes use a SpannableStringBuilder to track & dispatch input events |
| 1965 | // This list must remain in sync with the switch in onKeyDown() |
| 1966 | switch (mode) { |
| 1967 | case DEFAULT_KEYS_DISABLE: |
| 1968 | case DEFAULT_KEYS_SHORTCUT: |
| 1969 | mDefaultKeySsb = null; // not used in these modes |
| 1970 | break; |
| 1971 | case DEFAULT_KEYS_DIALER: |
| 1972 | case DEFAULT_KEYS_SEARCH_LOCAL: |
| 1973 | case DEFAULT_KEYS_SEARCH_GLOBAL: |
| 1974 | mDefaultKeySsb = new SpannableStringBuilder(); |
| 1975 | Selection.setSelection(mDefaultKeySsb,0); |
| 1976 | break; |
| 1977 | default: |
| 1978 | throw new IllegalArgumentException(); |
| 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | /** |
| 1983 | * Called when a key was pressed down and not handled by any of the views |
| 1984 | * inside of the activity. So, for example, key presses while the cursor |
| 1985 | * is inside a TextView will not trigger the event (unless it is a navigation |
| 1986 | * to another object) because TextView handles its own key presses. |
| 1987 | * |
| 1988 | * <p>If the focused view didn't want this event, this method is called. |
| 1989 | * |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 1990 | * <p>The default implementation takes care of {@link KeyEvent#KEYCODE_BACK} |
| 1991 | * by calling {@link #onBackPressed()}, though the behavior varies based |
| 1992 | * on the application compatibility mode: for |
| 1993 | * {@link android.os.Build.VERSION_CODES#ECLAIR} or later applications, |
| 1994 | * it will set up the dispatch to call {@link #onKeyUp} where the action |
| 1995 | * will be performed; for earlier applications, it will perform the |
| 1996 | * action immediately in on-down, as those versions of the platform |
| 1997 | * behaved. |
| 1998 | * |
| 1999 | * <p>Other additional default key handling may be performed |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2000 | * if configured with {@link #setDefaultKeyMode}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2001 | * |
| 2002 | * @return Return <code>true</code> to prevent this event from being propagated |
| 2003 | * further, or <code>false</code> to indicate that you have not handled |
| 2004 | * this event and it should continue to be propagated. |
| 2005 | * @see #onKeyUp |
| 2006 | * @see android.view.KeyEvent |
| 2007 | */ |
| 2008 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2009 | if (keyCode == KeyEvent.KEYCODE_BACK) { |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2010 | if (getApplicationInfo().targetSdkVersion |
| 2011 | >= Build.VERSION_CODES.ECLAIR) { |
| 2012 | event.startTracking(); |
| 2013 | } else { |
| 2014 | onBackPressed(); |
| 2015 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2016 | return true; |
| 2017 | } |
| 2018 | |
| 2019 | if (mDefaultKeyMode == DEFAULT_KEYS_DISABLE) { |
| 2020 | return false; |
| 2021 | } else if (mDefaultKeyMode == DEFAULT_KEYS_SHORTCUT) { |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2022 | if (getWindow().performPanelShortcut(Window.FEATURE_OPTIONS_PANEL, |
| 2023 | keyCode, event, Menu.FLAG_ALWAYS_PERFORM_CLOSE)) { |
| 2024 | return true; |
| 2025 | } |
| 2026 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2027 | } else { |
| 2028 | // Common code for DEFAULT_KEYS_DIALER & DEFAULT_KEYS_SEARCH_* |
| 2029 | boolean clearSpannable = false; |
| 2030 | boolean handled; |
| 2031 | if ((event.getRepeatCount() != 0) || event.isSystem()) { |
| 2032 | clearSpannable = true; |
| 2033 | handled = false; |
| 2034 | } else { |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2035 | handled = TextKeyListener.getInstance().onKeyDown( |
| 2036 | null, mDefaultKeySsb, keyCode, event); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2037 | if (handled && mDefaultKeySsb.length() > 0) { |
| 2038 | // something useable has been typed - dispatch it now. |
| 2039 | |
| 2040 | final String str = mDefaultKeySsb.toString(); |
| 2041 | clearSpannable = true; |
| 2042 | |
| 2043 | switch (mDefaultKeyMode) { |
| 2044 | case DEFAULT_KEYS_DIALER: |
| 2045 | Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + str)); |
| 2046 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2047 | startActivity(intent); |
| 2048 | break; |
| 2049 | case DEFAULT_KEYS_SEARCH_LOCAL: |
| 2050 | startSearch(str, false, null, false); |
| 2051 | break; |
| 2052 | case DEFAULT_KEYS_SEARCH_GLOBAL: |
| 2053 | startSearch(str, false, null, true); |
| 2054 | break; |
| 2055 | } |
| 2056 | } |
| 2057 | } |
| 2058 | if (clearSpannable) { |
| 2059 | mDefaultKeySsb.clear(); |
| 2060 | mDefaultKeySsb.clearSpans(); |
| 2061 | Selection.setSelection(mDefaultKeySsb,0); |
| 2062 | } |
| 2063 | return handled; |
| 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2068 | * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent) |
| 2069 | * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle |
| 2070 | * the event). |
| 2071 | */ |
| 2072 | public boolean onKeyLongPress(int keyCode, KeyEvent event) { |
| 2073 | return false; |
| 2074 | } |
| 2075 | |
| 2076 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2077 | * Called when a key was released and not handled by any of the views |
| 2078 | * inside of the activity. So, for example, key presses while the cursor |
| 2079 | * is inside a TextView will not trigger the event (unless it is a navigation |
| 2080 | * to another object) because TextView handles its own key presses. |
| 2081 | * |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2082 | * <p>The default implementation handles KEYCODE_BACK to stop the activity |
| 2083 | * and go back. |
| 2084 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2085 | * @return Return <code>true</code> to prevent this event from being propagated |
| 2086 | * further, or <code>false</code> to indicate that you have not handled |
| 2087 | * this event and it should continue to be propagated. |
| 2088 | * @see #onKeyDown |
| 2089 | * @see KeyEvent |
| 2090 | */ |
| 2091 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2092 | if (getApplicationInfo().targetSdkVersion |
| 2093 | >= Build.VERSION_CODES.ECLAIR) { |
| 2094 | if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking() |
| 2095 | && !event.isCanceled()) { |
| 2096 | onBackPressed(); |
| 2097 | return true; |
| 2098 | } |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2099 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2100 | return false; |
| 2101 | } |
| 2102 | |
| 2103 | /** |
| 2104 | * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent) |
| 2105 | * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle |
| 2106 | * the event). |
| 2107 | */ |
| 2108 | public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { |
| 2109 | return false; |
| 2110 | } |
| 2111 | |
| 2112 | /** |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 2113 | * Pop the last fragment transition from the local activity's fragment |
| 2114 | * back stack. If there is nothing to pop, false is returned. |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2115 | * @param name If non-null, this is the name of a previous back state |
| 2116 | * to look for; if found, all states up to (but not including) that |
| 2117 | * state will be popped. If null, only the top state is popped. |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 2118 | */ |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2119 | public boolean popBackStack(String name) { |
| 2120 | return mFragments.popBackStackState(mHandler, name); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | /** |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2124 | * Called when the activity has detected the user's press of the back |
| 2125 | * key. The default implementation simply finishes the current activity, |
| 2126 | * but you can override this to do whatever you want. |
| 2127 | */ |
| 2128 | public void onBackPressed() { |
Dianne Hackborn | f121be7 | 2010-05-06 14:10:32 -0700 | [diff] [blame] | 2129 | if (!popBackStack(null)) { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 2130 | finish(); |
| 2131 | } |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2135 | * Called when a touch screen event was not handled by any of the views |
| 2136 | * under it. This is most useful to process touch events that happen |
| 2137 | * outside of your window bounds, where there is no view to receive it. |
| 2138 | * |
| 2139 | * @param event The touch screen event being processed. |
| 2140 | * |
| 2141 | * @return Return true if you have consumed the event, false if you haven't. |
| 2142 | * The default implementation always returns false. |
| 2143 | */ |
| 2144 | public boolean onTouchEvent(MotionEvent event) { |
| 2145 | return false; |
| 2146 | } |
| 2147 | |
| 2148 | /** |
| 2149 | * Called when the trackball was moved and not handled by any of the |
| 2150 | * views inside of the activity. So, for example, if the trackball moves |
| 2151 | * while focus is on a button, you will receive a call here because |
| 2152 | * buttons do not normally do anything with trackball events. The call |
| 2153 | * here happens <em>before</em> trackball movements are converted to |
| 2154 | * DPAD key events, which then get sent back to the view hierarchy, and |
| 2155 | * will be processed at the point for things like focus navigation. |
| 2156 | * |
| 2157 | * @param event The trackball event being processed. |
| 2158 | * |
| 2159 | * @return Return true if you have consumed the event, false if you haven't. |
| 2160 | * The default implementation always returns false. |
| 2161 | */ |
| 2162 | public boolean onTrackballEvent(MotionEvent event) { |
| 2163 | return false; |
| 2164 | } |
| 2165 | |
| 2166 | /** |
| 2167 | * Called whenever a key, touch, or trackball event is dispatched to the |
| 2168 | * activity. Implement this method if you wish to know that the user has |
| 2169 | * interacted with the device in some way while your activity is running. |
| 2170 | * This callback and {@link #onUserLeaveHint} are intended to help |
| 2171 | * activities manage status bar notifications intelligently; specifically, |
| 2172 | * for helping activities determine the proper time to cancel a notfication. |
| 2173 | * |
| 2174 | * <p>All calls to your activity's {@link #onUserLeaveHint} callback will |
| 2175 | * be accompanied by calls to {@link #onUserInteraction}. This |
| 2176 | * ensures that your activity will be told of relevant user activity such |
| 2177 | * as pulling down the notification pane and touching an item there. |
| 2178 | * |
| 2179 | * <p>Note that this callback will be invoked for the touch down action |
| 2180 | * that begins a touch gesture, but may not be invoked for the touch-moved |
| 2181 | * and touch-up actions that follow. |
| 2182 | * |
| 2183 | * @see #onUserLeaveHint() |
| 2184 | */ |
| 2185 | public void onUserInteraction() { |
| 2186 | } |
| 2187 | |
| 2188 | public void onWindowAttributesChanged(WindowManager.LayoutParams params) { |
| 2189 | // Update window manager if: we have a view, that view is |
| 2190 | // attached to its parent (which will be a RootView), and |
| 2191 | // this activity is not embedded. |
| 2192 | if (mParent == null) { |
| 2193 | View decor = mDecor; |
| 2194 | if (decor != null && decor.getParent() != null) { |
| 2195 | getWindowManager().updateViewLayout(decor, params); |
| 2196 | } |
| 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | public void onContentChanged() { |
| 2201 | } |
| 2202 | |
| 2203 | /** |
| 2204 | * Called when the current {@link Window} of the activity gains or loses |
| 2205 | * focus. This is the best indicator of whether this activity is visible |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2206 | * to the user. The default implementation clears the key tracking |
| 2207 | * state, so should always be called. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2208 | * |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 2209 | * <p>Note that this provides information about global focus state, which |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2210 | * is managed independently of activity lifecycles. As such, while focus |
| 2211 | * changes will generally have some relation to lifecycle changes (an |
| 2212 | * activity that is stopped will not generally get window focus), you |
| 2213 | * should not rely on any particular order between the callbacks here and |
| 2214 | * those in the other lifecycle methods such as {@link #onResume}. |
| 2215 | * |
| 2216 | * <p>As a general rule, however, a resumed activity will have window |
| 2217 | * focus... unless it has displayed other dialogs or popups that take |
| 2218 | * input focus, in which case the activity itself will not have focus |
| 2219 | * when the other windows have it. Likewise, the system may display |
| 2220 | * system-level windows (such as the status bar notification panel or |
| 2221 | * a system alert) which will temporarily take window input focus without |
| 2222 | * pausing the foreground activity. |
| 2223 | * |
| 2224 | * @param hasFocus Whether the window of this activity has focus. |
| 2225 | * |
| 2226 | * @see #hasWindowFocus() |
| 2227 | * @see #onResume |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 2228 | * @see View#onWindowFocusChanged(boolean) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2229 | */ |
| 2230 | public void onWindowFocusChanged(boolean hasFocus) { |
| 2231 | } |
| 2232 | |
| 2233 | /** |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 2234 | * Called when the main window associated with the activity has been |
| 2235 | * attached to the window manager. |
| 2236 | * See {@link View#onAttachedToWindow() View.onAttachedToWindow()} |
| 2237 | * for more information. |
| 2238 | * @see View#onAttachedToWindow |
| 2239 | */ |
| 2240 | public void onAttachedToWindow() { |
| 2241 | } |
| 2242 | |
| 2243 | /** |
| 2244 | * Called when the main window associated with the activity has been |
| 2245 | * detached from the window manager. |
| 2246 | * See {@link View#onDetachedFromWindow() View.onDetachedFromWindow()} |
| 2247 | * for more information. |
| 2248 | * @see View#onDetachedFromWindow |
| 2249 | */ |
| 2250 | public void onDetachedFromWindow() { |
| 2251 | } |
| 2252 | |
| 2253 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2254 | * Returns true if this activity's <em>main</em> window currently has window focus. |
| 2255 | * Note that this is not the same as the view itself having focus. |
| 2256 | * |
| 2257 | * @return True if this activity's main window currently has window focus. |
| 2258 | * |
| 2259 | * @see #onWindowAttributesChanged(android.view.WindowManager.LayoutParams) |
| 2260 | */ |
| 2261 | public boolean hasWindowFocus() { |
| 2262 | Window w = getWindow(); |
| 2263 | if (w != null) { |
| 2264 | View d = w.getDecorView(); |
| 2265 | if (d != null) { |
| 2266 | return d.hasWindowFocus(); |
| 2267 | } |
| 2268 | } |
| 2269 | return false; |
| 2270 | } |
| 2271 | |
| 2272 | /** |
| 2273 | * Called to process key events. You can override this to intercept all |
| 2274 | * key events before they are dispatched to the window. Be sure to call |
| 2275 | * this implementation for key events that should be handled normally. |
| 2276 | * |
| 2277 | * @param event The key event. |
| 2278 | * |
| 2279 | * @return boolean Return true if this event was consumed. |
| 2280 | */ |
| 2281 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 2282 | onUserInteraction(); |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2283 | Window win = getWindow(); |
| 2284 | if (win.superDispatchKeyEvent(event)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2285 | return true; |
| 2286 | } |
Dianne Hackborn | 8d37426 | 2009-09-14 21:21:52 -0700 | [diff] [blame] | 2287 | View decor = mDecor; |
| 2288 | if (decor == null) decor = win.getDecorView(); |
| 2289 | return event.dispatch(this, decor != null |
| 2290 | ? decor.getKeyDispatcherState() : null, this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2291 | } |
| 2292 | |
| 2293 | /** |
| 2294 | * Called to process touch screen events. You can override this to |
| 2295 | * intercept all touch screen events before they are dispatched to the |
| 2296 | * window. Be sure to call this implementation for touch screen events |
| 2297 | * that should be handled normally. |
| 2298 | * |
| 2299 | * @param ev The touch screen event. |
| 2300 | * |
| 2301 | * @return boolean Return true if this event was consumed. |
| 2302 | */ |
| 2303 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 2304 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
| 2305 | onUserInteraction(); |
| 2306 | } |
| 2307 | if (getWindow().superDispatchTouchEvent(ev)) { |
| 2308 | return true; |
| 2309 | } |
| 2310 | return onTouchEvent(ev); |
| 2311 | } |
| 2312 | |
| 2313 | /** |
| 2314 | * Called to process trackball events. You can override this to |
| 2315 | * intercept all trackball events before they are dispatched to the |
| 2316 | * window. Be sure to call this implementation for trackball events |
| 2317 | * that should be handled normally. |
| 2318 | * |
| 2319 | * @param ev The trackball event. |
| 2320 | * |
| 2321 | * @return boolean Return true if this event was consumed. |
| 2322 | */ |
| 2323 | public boolean dispatchTrackballEvent(MotionEvent ev) { |
| 2324 | onUserInteraction(); |
| 2325 | if (getWindow().superDispatchTrackballEvent(ev)) { |
| 2326 | return true; |
| 2327 | } |
| 2328 | return onTrackballEvent(ev); |
| 2329 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2330 | |
| 2331 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 2332 | event.setClassName(getClass().getName()); |
| 2333 | event.setPackageName(getPackageName()); |
| 2334 | |
| 2335 | LayoutParams params = getWindow().getAttributes(); |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 2336 | boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT) && |
| 2337 | (params.height == LayoutParams.MATCH_PARENT); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2338 | event.setFullScreen(isFullScreen); |
| 2339 | |
| 2340 | CharSequence title = getTitle(); |
| 2341 | if (!TextUtils.isEmpty(title)) { |
| 2342 | event.getText().add(title); |
| 2343 | } |
| 2344 | |
| 2345 | return true; |
| 2346 | } |
| 2347 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2348 | /** |
| 2349 | * Default implementation of |
| 2350 | * {@link android.view.Window.Callback#onCreatePanelView} |
| 2351 | * for activities. This |
| 2352 | * simply returns null so that all panel sub-windows will have the default |
| 2353 | * menu behavior. |
| 2354 | */ |
| 2355 | public View onCreatePanelView(int featureId) { |
| 2356 | return null; |
| 2357 | } |
| 2358 | |
| 2359 | /** |
| 2360 | * Default implementation of |
| 2361 | * {@link android.view.Window.Callback#onCreatePanelMenu} |
| 2362 | * for activities. This calls through to the new |
| 2363 | * {@link #onCreateOptionsMenu} method for the |
| 2364 | * {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel, |
| 2365 | * so that subclasses of Activity don't need to deal with feature codes. |
| 2366 | */ |
| 2367 | public boolean onCreatePanelMenu(int featureId, Menu menu) { |
| 2368 | if (featureId == Window.FEATURE_OPTIONS_PANEL) { |
| 2369 | return onCreateOptionsMenu(menu); |
| 2370 | } |
| 2371 | return false; |
| 2372 | } |
| 2373 | |
| 2374 | /** |
| 2375 | * Default implementation of |
| 2376 | * {@link android.view.Window.Callback#onPreparePanel} |
| 2377 | * for activities. This |
| 2378 | * calls through to the new {@link #onPrepareOptionsMenu} method for the |
| 2379 | * {@link android.view.Window#FEATURE_OPTIONS_PANEL} |
| 2380 | * panel, so that subclasses of |
| 2381 | * Activity don't need to deal with feature codes. |
| 2382 | */ |
| 2383 | public boolean onPreparePanel(int featureId, View view, Menu menu) { |
| 2384 | if (featureId == Window.FEATURE_OPTIONS_PANEL && menu != null) { |
| 2385 | boolean goforit = onPrepareOptionsMenu(menu); |
| 2386 | return goforit && menu.hasVisibleItems(); |
| 2387 | } |
| 2388 | return true; |
| 2389 | } |
| 2390 | |
| 2391 | /** |
| 2392 | * {@inheritDoc} |
| 2393 | * |
| 2394 | * @return The default implementation returns true. |
| 2395 | */ |
| 2396 | public boolean onMenuOpened(int featureId, Menu menu) { |
| 2397 | return true; |
| 2398 | } |
| 2399 | |
| 2400 | /** |
| 2401 | * Default implementation of |
| 2402 | * {@link android.view.Window.Callback#onMenuItemSelected} |
| 2403 | * for activities. This calls through to the new |
| 2404 | * {@link #onOptionsItemSelected} method for the |
| 2405 | * {@link android.view.Window#FEATURE_OPTIONS_PANEL} |
| 2406 | * panel, so that subclasses of |
| 2407 | * Activity don't need to deal with feature codes. |
| 2408 | */ |
| 2409 | public boolean onMenuItemSelected(int featureId, MenuItem item) { |
| 2410 | switch (featureId) { |
| 2411 | case Window.FEATURE_OPTIONS_PANEL: |
| 2412 | // Put event logging here so it gets called even if subclass |
| 2413 | // doesn't call through to superclass's implmeentation of each |
| 2414 | // of these methods below |
| 2415 | EventLog.writeEvent(50000, 0, item.getTitleCondensed()); |
| 2416 | return onOptionsItemSelected(item); |
| 2417 | |
| 2418 | case Window.FEATURE_CONTEXT_MENU: |
| 2419 | EventLog.writeEvent(50000, 1, item.getTitleCondensed()); |
| 2420 | return onContextItemSelected(item); |
| 2421 | |
| 2422 | default: |
| 2423 | return false; |
| 2424 | } |
| 2425 | } |
| 2426 | |
| 2427 | /** |
| 2428 | * Default implementation of |
| 2429 | * {@link android.view.Window.Callback#onPanelClosed(int, Menu)} for |
| 2430 | * activities. This calls through to {@link #onOptionsMenuClosed(Menu)} |
| 2431 | * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL} panel, |
| 2432 | * so that subclasses of Activity don't need to deal with feature codes. |
| 2433 | * For context menus ({@link Window#FEATURE_CONTEXT_MENU}), the |
| 2434 | * {@link #onContextMenuClosed(Menu)} will be called. |
| 2435 | */ |
| 2436 | public void onPanelClosed(int featureId, Menu menu) { |
| 2437 | switch (featureId) { |
| 2438 | case Window.FEATURE_OPTIONS_PANEL: |
| 2439 | onOptionsMenuClosed(menu); |
| 2440 | break; |
| 2441 | |
| 2442 | case Window.FEATURE_CONTEXT_MENU: |
| 2443 | onContextMenuClosed(menu); |
| 2444 | break; |
| 2445 | } |
| 2446 | } |
| 2447 | |
| 2448 | /** |
| 2449 | * Initialize the contents of the Activity's standard options menu. You |
| 2450 | * should place your menu items in to <var>menu</var>. |
| 2451 | * |
| 2452 | * <p>This is only called once, the first time the options menu is |
| 2453 | * displayed. To update the menu every time it is displayed, see |
| 2454 | * {@link #onPrepareOptionsMenu}. |
| 2455 | * |
| 2456 | * <p>The default implementation populates the menu with standard system |
| 2457 | * menu items. These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that |
| 2458 | * they will be correctly ordered with application-defined menu items. |
| 2459 | * Deriving classes should always call through to the base implementation. |
| 2460 | * |
| 2461 | * <p>You can safely hold on to <var>menu</var> (and any items created |
| 2462 | * from it), making modifications to it as desired, until the next |
| 2463 | * time onCreateOptionsMenu() is called. |
| 2464 | * |
| 2465 | * <p>When you add items to the menu, you can implement the Activity's |
| 2466 | * {@link #onOptionsItemSelected} method to handle them there. |
| 2467 | * |
| 2468 | * @param menu The options menu in which you place your items. |
| 2469 | * |
| 2470 | * @return You must return true for the menu to be displayed; |
| 2471 | * if you return false it will not be shown. |
| 2472 | * |
| 2473 | * @see #onPrepareOptionsMenu |
| 2474 | * @see #onOptionsItemSelected |
| 2475 | */ |
| 2476 | public boolean onCreateOptionsMenu(Menu menu) { |
| 2477 | if (mParent != null) { |
| 2478 | return mParent.onCreateOptionsMenu(menu); |
| 2479 | } |
| 2480 | return true; |
| 2481 | } |
| 2482 | |
| 2483 | /** |
| 2484 | * Prepare the Screen's standard options menu to be displayed. This is |
| 2485 | * called right before the menu is shown, every time it is shown. You can |
| 2486 | * use this method to efficiently enable/disable items or otherwise |
| 2487 | * dynamically modify the contents. |
| 2488 | * |
| 2489 | * <p>The default implementation updates the system menu items based on the |
| 2490 | * activity's state. Deriving classes should always call through to the |
| 2491 | * base class implementation. |
| 2492 | * |
| 2493 | * @param menu The options menu as last shown or first initialized by |
| 2494 | * onCreateOptionsMenu(). |
| 2495 | * |
| 2496 | * @return You must return true for the menu to be displayed; |
| 2497 | * if you return false it will not be shown. |
| 2498 | * |
| 2499 | * @see #onCreateOptionsMenu |
| 2500 | */ |
| 2501 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 2502 | if (mParent != null) { |
| 2503 | return mParent.onPrepareOptionsMenu(menu); |
| 2504 | } |
| 2505 | return true; |
| 2506 | } |
| 2507 | |
| 2508 | /** |
| 2509 | * This hook is called whenever an item in your options menu is selected. |
| 2510 | * The default implementation simply returns false to have the normal |
| 2511 | * processing happen (calling the item's Runnable or sending a message to |
| 2512 | * its Handler as appropriate). You can use this method for any items |
| 2513 | * for which you would like to do processing without those other |
| 2514 | * facilities. |
| 2515 | * |
| 2516 | * <p>Derived classes should call through to the base class for it to |
| 2517 | * perform the default menu handling. |
| 2518 | * |
| 2519 | * @param item The menu item that was selected. |
| 2520 | * |
| 2521 | * @return boolean Return false to allow normal menu processing to |
| 2522 | * proceed, true to consume it here. |
| 2523 | * |
| 2524 | * @see #onCreateOptionsMenu |
| 2525 | */ |
| 2526 | public boolean onOptionsItemSelected(MenuItem item) { |
| 2527 | if (mParent != null) { |
| 2528 | return mParent.onOptionsItemSelected(item); |
| 2529 | } |
| 2530 | return false; |
| 2531 | } |
| 2532 | |
| 2533 | /** |
| 2534 | * This hook is called whenever the options menu is being closed (either by the user canceling |
| 2535 | * the menu with the back/menu button, or when an item is selected). |
| 2536 | * |
| 2537 | * @param menu The options menu as last shown or first initialized by |
| 2538 | * onCreateOptionsMenu(). |
| 2539 | */ |
| 2540 | public void onOptionsMenuClosed(Menu menu) { |
| 2541 | if (mParent != null) { |
| 2542 | mParent.onOptionsMenuClosed(menu); |
| 2543 | } |
| 2544 | } |
| 2545 | |
| 2546 | /** |
| 2547 | * Programmatically opens the options menu. If the options menu is already |
| 2548 | * open, this method does nothing. |
| 2549 | */ |
| 2550 | public void openOptionsMenu() { |
| 2551 | mWindow.openPanel(Window.FEATURE_OPTIONS_PANEL, null); |
| 2552 | } |
| 2553 | |
| 2554 | /** |
| 2555 | * Progammatically closes the options menu. If the options menu is already |
| 2556 | * closed, this method does nothing. |
| 2557 | */ |
| 2558 | public void closeOptionsMenu() { |
| 2559 | mWindow.closePanel(Window.FEATURE_OPTIONS_PANEL); |
| 2560 | } |
| 2561 | |
| 2562 | /** |
| 2563 | * Called when a context menu for the {@code view} is about to be shown. |
| 2564 | * Unlike {@link #onCreateOptionsMenu(Menu)}, this will be called every |
| 2565 | * time the context menu is about to be shown and should be populated for |
| 2566 | * the view (or item inside the view for {@link AdapterView} subclasses, |
| 2567 | * this can be found in the {@code menuInfo})). |
| 2568 | * <p> |
| 2569 | * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an |
| 2570 | * item has been selected. |
| 2571 | * <p> |
| 2572 | * It is not safe to hold onto the context menu after this method returns. |
| 2573 | * {@inheritDoc} |
| 2574 | */ |
| 2575 | public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
| 2576 | } |
| 2577 | |
| 2578 | /** |
| 2579 | * Registers a context menu to be shown for the given view (multiple views |
| 2580 | * can show the context menu). This method will set the |
| 2581 | * {@link OnCreateContextMenuListener} on the view to this activity, so |
| 2582 | * {@link #onCreateContextMenu(ContextMenu, View, ContextMenuInfo)} will be |
| 2583 | * called when it is time to show the context menu. |
| 2584 | * |
| 2585 | * @see #unregisterForContextMenu(View) |
| 2586 | * @param view The view that should show a context menu. |
| 2587 | */ |
| 2588 | public void registerForContextMenu(View view) { |
| 2589 | view.setOnCreateContextMenuListener(this); |
| 2590 | } |
| 2591 | |
| 2592 | /** |
| 2593 | * Prevents a context menu to be shown for the given view. This method will remove the |
| 2594 | * {@link OnCreateContextMenuListener} on the view. |
| 2595 | * |
| 2596 | * @see #registerForContextMenu(View) |
| 2597 | * @param view The view that should stop showing a context menu. |
| 2598 | */ |
| 2599 | public void unregisterForContextMenu(View view) { |
| 2600 | view.setOnCreateContextMenuListener(null); |
| 2601 | } |
| 2602 | |
| 2603 | /** |
| 2604 | * Programmatically opens the context menu for a particular {@code view}. |
| 2605 | * The {@code view} should have been added via |
| 2606 | * {@link #registerForContextMenu(View)}. |
| 2607 | * |
| 2608 | * @param view The view to show the context menu for. |
| 2609 | */ |
| 2610 | public void openContextMenu(View view) { |
| 2611 | view.showContextMenu(); |
| 2612 | } |
| 2613 | |
| 2614 | /** |
| 2615 | * Programmatically closes the most recently opened context menu, if showing. |
| 2616 | */ |
| 2617 | public void closeContextMenu() { |
| 2618 | mWindow.closePanel(Window.FEATURE_CONTEXT_MENU); |
| 2619 | } |
| 2620 | |
| 2621 | /** |
| 2622 | * This hook is called whenever an item in a context menu is selected. The |
| 2623 | * default implementation simply returns false to have the normal processing |
| 2624 | * happen (calling the item's Runnable or sending a message to its Handler |
| 2625 | * as appropriate). You can use this method for any items for which you |
| 2626 | * would like to do processing without those other facilities. |
| 2627 | * <p> |
| 2628 | * Use {@link MenuItem#getMenuInfo()} to get extra information set by the |
| 2629 | * View that added this menu item. |
| 2630 | * <p> |
| 2631 | * Derived classes should call through to the base class for it to perform |
| 2632 | * the default menu handling. |
| 2633 | * |
| 2634 | * @param item The context menu item that was selected. |
| 2635 | * @return boolean Return false to allow normal context menu processing to |
| 2636 | * proceed, true to consume it here. |
| 2637 | */ |
| 2638 | public boolean onContextItemSelected(MenuItem item) { |
| 2639 | if (mParent != null) { |
| 2640 | return mParent.onContextItemSelected(item); |
| 2641 | } |
| 2642 | return false; |
| 2643 | } |
| 2644 | |
| 2645 | /** |
| 2646 | * This hook is called whenever the context menu is being closed (either by |
| 2647 | * the user canceling the menu with the back/menu button, or when an item is |
| 2648 | * selected). |
| 2649 | * |
| 2650 | * @param menu The context menu that is being closed. |
| 2651 | */ |
| 2652 | public void onContextMenuClosed(Menu menu) { |
| 2653 | if (mParent != null) { |
| 2654 | mParent.onContextMenuClosed(menu); |
| 2655 | } |
| 2656 | } |
| 2657 | |
| 2658 | /** |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2659 | * @deprecated Old no-arguments version of {@link #onCreateDialog(int, Bundle)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2660 | */ |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2661 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2662 | protected Dialog onCreateDialog(int id) { |
| 2663 | return null; |
| 2664 | } |
| 2665 | |
| 2666 | /** |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2667 | * Callback for creating dialogs that are managed (saved and restored) for you |
| 2668 | * by the activity. The default implementation calls through to |
| 2669 | * {@link #onCreateDialog(int)} for compatibility. |
| 2670 | * |
| 2671 | * <p>If you use {@link #showDialog(int)}, the activity will call through to |
| 2672 | * this method the first time, and hang onto it thereafter. Any dialog |
| 2673 | * that is created by this method will automatically be saved and restored |
| 2674 | * for you, including whether it is showing. |
| 2675 | * |
| 2676 | * <p>If you would like the activity to manage saving and restoring dialogs |
| 2677 | * for you, you should override this method and handle any ids that are |
| 2678 | * passed to {@link #showDialog}. |
| 2679 | * |
| 2680 | * <p>If you would like an opportunity to prepare your dialog before it is shown, |
| 2681 | * override {@link #onPrepareDialog(int, Dialog, Bundle)}. |
| 2682 | * |
| 2683 | * @param id The id of the dialog. |
| 2684 | * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}. |
| 2685 | * @return The dialog. If you return null, the dialog will not be created. |
| 2686 | * |
| 2687 | * @see #onPrepareDialog(int, Dialog, Bundle) |
| 2688 | * @see #showDialog(int, Bundle) |
| 2689 | * @see #dismissDialog(int) |
| 2690 | * @see #removeDialog(int) |
| 2691 | */ |
| 2692 | protected Dialog onCreateDialog(int id, Bundle args) { |
| 2693 | return onCreateDialog(id); |
| 2694 | } |
| 2695 | |
| 2696 | /** |
| 2697 | * @deprecated Old no-arguments version of |
| 2698 | * {@link #onPrepareDialog(int, Dialog, Bundle)}. |
| 2699 | */ |
| 2700 | @Deprecated |
| 2701 | protected void onPrepareDialog(int id, Dialog dialog) { |
| 2702 | dialog.setOwnerActivity(this); |
| 2703 | } |
| 2704 | |
| 2705 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2706 | * Provides an opportunity to prepare a managed dialog before it is being |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2707 | * shown. The default implementation calls through to |
| 2708 | * {@link #onPrepareDialog(int, Dialog)} for compatibility. |
| 2709 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2710 | * <p> |
| 2711 | * Override this if you need to update a managed dialog based on the state |
| 2712 | * of the application each time it is shown. For example, a time picker |
| 2713 | * dialog might want to be updated with the current time. You should call |
| 2714 | * through to the superclass's implementation. The default implementation |
| 2715 | * will set this Activity as the owner activity on the Dialog. |
| 2716 | * |
| 2717 | * @param id The id of the managed dialog. |
| 2718 | * @param dialog The dialog. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2719 | * @param args The dialog arguments provided to {@link #showDialog(int, Bundle)}. |
| 2720 | * @see #onCreateDialog(int, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2721 | * @see #showDialog(int) |
| 2722 | * @see #dismissDialog(int) |
| 2723 | * @see #removeDialog(int) |
| 2724 | */ |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2725 | protected void onPrepareDialog(int id, Dialog dialog, Bundle args) { |
| 2726 | onPrepareDialog(id, dialog); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | /** |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2730 | * Simple version of {@link #showDialog(int, Bundle)} that does not |
| 2731 | * take any arguments. Simply calls {@link #showDialog(int, Bundle)} |
| 2732 | * with null arguments. |
| 2733 | */ |
| 2734 | public final void showDialog(int id) { |
| 2735 | showDialog(id, null); |
| 2736 | } |
| 2737 | |
| 2738 | /** |
| 2739 | * Show a dialog managed by this activity. A call to {@link #onCreateDialog(int, Bundle)} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2740 | * will be made with the same id the first time this is called for a given |
| 2741 | * id. From thereafter, the dialog will be automatically saved and restored. |
| 2742 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2743 | * <p>Each time a dialog is shown, {@link #onPrepareDialog(int, Dialog, Bundle)} will |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2744 | * be made to provide an opportunity to do any timely preparation. |
| 2745 | * |
| 2746 | * @param id The id of the managed dialog. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2747 | * @param args Arguments to pass through to the dialog. These will be saved |
| 2748 | * and restored for you. Note that if the dialog is already created, |
| 2749 | * {@link #onCreateDialog(int, Bundle)} will not be called with the new |
| 2750 | * arguments but {@link #onPrepareDialog(int, Dialog, Bundle)} will be. |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 2751 | * If you need to rebuild the dialog, call {@link #removeDialog(int)} first. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2752 | * @return Returns true if the Dialog was created; false is returned if |
| 2753 | * it is not created because {@link #onCreateDialog(int, Bundle)} returns false. |
| 2754 | * |
Joe Onorato | 37296dc | 2009-07-31 17:58:55 -0700 | [diff] [blame] | 2755 | * @see Dialog |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2756 | * @see #onCreateDialog(int, Bundle) |
| 2757 | * @see #onPrepareDialog(int, Dialog, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2758 | * @see #dismissDialog(int) |
| 2759 | * @see #removeDialog(int) |
| 2760 | */ |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2761 | public final boolean showDialog(int id, Bundle args) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2762 | if (mManagedDialogs == null) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2763 | mManagedDialogs = new SparseArray<ManagedDialog>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2764 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2765 | ManagedDialog md = mManagedDialogs.get(id); |
| 2766 | if (md == null) { |
| 2767 | md = new ManagedDialog(); |
| 2768 | md.mDialog = createDialog(id, null, args); |
| 2769 | if (md.mDialog == null) { |
| 2770 | return false; |
| 2771 | } |
| 2772 | mManagedDialogs.put(id, md); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2773 | } |
| 2774 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2775 | md.mArgs = args; |
| 2776 | onPrepareDialog(id, md.mDialog, args); |
| 2777 | md.mDialog.show(); |
| 2778 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2779 | } |
| 2780 | |
| 2781 | /** |
| 2782 | * Dismiss a dialog that was previously shown via {@link #showDialog(int)}. |
| 2783 | * |
| 2784 | * @param id The id of the managed dialog. |
| 2785 | * |
| 2786 | * @throws IllegalArgumentException if the id was not previously shown via |
| 2787 | * {@link #showDialog(int)}. |
| 2788 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2789 | * @see #onCreateDialog(int, Bundle) |
| 2790 | * @see #onPrepareDialog(int, Dialog, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2791 | * @see #showDialog(int) |
| 2792 | * @see #removeDialog(int) |
| 2793 | */ |
| 2794 | public final void dismissDialog(int id) { |
| 2795 | if (mManagedDialogs == null) { |
| 2796 | throw missingDialog(id); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2797 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2798 | |
| 2799 | final ManagedDialog md = mManagedDialogs.get(id); |
| 2800 | if (md == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2801 | throw missingDialog(id); |
| 2802 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2803 | md.mDialog.dismiss(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2804 | } |
| 2805 | |
| 2806 | /** |
| 2807 | * Creates an exception to throw if a user passed in a dialog id that is |
| 2808 | * unexpected. |
| 2809 | */ |
| 2810 | private IllegalArgumentException missingDialog(int id) { |
| 2811 | return new IllegalArgumentException("no dialog with id " + id + " was ever " |
| 2812 | + "shown via Activity#showDialog"); |
| 2813 | } |
| 2814 | |
| 2815 | /** |
| 2816 | * Removes any internal references to a dialog managed by this Activity. |
| 2817 | * If the dialog is showing, it will dismiss it as part of the clean up. |
| 2818 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2819 | * <p>This can be useful if you know that you will never show a dialog again and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2820 | * want to avoid the overhead of saving and restoring it in the future. |
| 2821 | * |
| 2822 | * @param id The id of the managed dialog. |
| 2823 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2824 | * @see #onCreateDialog(int, Bundle) |
| 2825 | * @see #onPrepareDialog(int, Dialog, Bundle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2826 | * @see #showDialog(int) |
| 2827 | * @see #dismissDialog(int) |
| 2828 | */ |
| 2829 | public final void removeDialog(int id) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2830 | if (mManagedDialogs == null) { |
| 2831 | return; |
| 2832 | } |
| 2833 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2834 | final ManagedDialog md = mManagedDialogs.get(id); |
| 2835 | if (md == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2836 | return; |
| 2837 | } |
| 2838 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2839 | md.mDialog.dismiss(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2840 | mManagedDialogs.remove(id); |
| 2841 | } |
| 2842 | |
| 2843 | /** |
| 2844 | * This hook is called when the user signals the desire to start a search. |
| 2845 | * |
Bjorn Bringert | 6266e40 | 2009-09-25 14:25:41 +0100 | [diff] [blame] | 2846 | * <p>You can use this function as a simple way to launch the search UI, in response to a |
| 2847 | * menu item, search button, or other widgets within your activity. Unless overidden, |
| 2848 | * calling this function is the same as calling |
| 2849 | * {@link #startSearch startSearch(null, false, null, false)}, which launches |
| 2850 | * search for the current activity as specified in its manifest, see {@link SearchManager}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2851 | * |
| 2852 | * <p>You can override this function to force global search, e.g. in response to a dedicated |
| 2853 | * search key, or to block search entirely (by simply returning false). |
| 2854 | * |
Bjorn Bringert | 6266e40 | 2009-09-25 14:25:41 +0100 | [diff] [blame] | 2855 | * @return Returns {@code true} if search launched, and {@code false} if activity blocks it. |
| 2856 | * The default implementation always returns {@code true}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2857 | * |
| 2858 | * @see android.app.SearchManager |
| 2859 | */ |
| 2860 | public boolean onSearchRequested() { |
| 2861 | startSearch(null, false, null, false); |
| 2862 | return true; |
| 2863 | } |
| 2864 | |
| 2865 | /** |
| 2866 | * This hook is called to launch the search UI. |
| 2867 | * |
| 2868 | * <p>It is typically called from onSearchRequested(), either directly from |
| 2869 | * Activity.onSearchRequested() or from an overridden version in any given |
| 2870 | * Activity. If your goal is simply to activate search, it is preferred to call |
| 2871 | * onSearchRequested(), which may have been overriden elsewhere in your Activity. If your goal |
| 2872 | * is to inject specific data such as context data, it is preferred to <i>override</i> |
| 2873 | * onSearchRequested(), so that any callers to it will benefit from the override. |
| 2874 | * |
| 2875 | * @param initialQuery Any non-null non-empty string will be inserted as |
| 2876 | * pre-entered text in the search query box. |
| 2877 | * @param selectInitialQuery If true, the intial query will be preselected, which means that |
| 2878 | * any further typing will replace it. This is useful for cases where an entire pre-formed |
| 2879 | * query is being inserted. If false, the selection point will be placed at the end of the |
| 2880 | * inserted query. This is useful when the inserted query is text that the user entered, |
| 2881 | * and the user would expect to be able to keep typing. <i>This parameter is only meaningful |
| 2882 | * if initialQuery is a non-empty string.</i> |
| 2883 | * @param appSearchData An application can insert application-specific |
| 2884 | * context here, in order to improve quality or specificity of its own |
| 2885 | * searches. This data will be returned with SEARCH intent(s). Null if |
| 2886 | * no extra data is required. |
| 2887 | * @param globalSearch If false, this will only launch the search that has been specifically |
| 2888 | * defined by the application (which is usually defined as a local search). If no default |
Mike LeBeau | cfa419b | 2009-08-17 10:56:02 -0700 | [diff] [blame] | 2889 | * search is defined in the current application or activity, global search will be launched. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2890 | * If true, this will always launch a platform-global (e.g. web-based) search instead. |
| 2891 | * |
| 2892 | * @see android.app.SearchManager |
| 2893 | * @see #onSearchRequested |
| 2894 | */ |
| 2895 | public void startSearch(String initialQuery, boolean selectInitialQuery, |
| 2896 | Bundle appSearchData, boolean globalSearch) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 2897 | ensureSearchManager(); |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 2898 | mSearchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2899 | appSearchData, globalSearch); |
| 2900 | } |
| 2901 | |
| 2902 | /** |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 2903 | * Similar to {@link #startSearch}, but actually fires off the search query after invoking |
| 2904 | * the search dialog. Made available for testing purposes. |
| 2905 | * |
| 2906 | * @param query The query to trigger. If empty, the request will be ignored. |
| 2907 | * @param appSearchData An application can insert application-specific |
| 2908 | * context here, in order to improve quality or specificity of its own |
| 2909 | * searches. This data will be returned with SEARCH intent(s). Null if |
| 2910 | * no extra data is required. |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 2911 | */ |
Bjorn Bringert | b782a2f | 2009-10-01 09:57:33 +0100 | [diff] [blame] | 2912 | public void triggerSearch(String query, Bundle appSearchData) { |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 2913 | ensureSearchManager(); |
Bjorn Bringert | b782a2f | 2009-10-01 09:57:33 +0100 | [diff] [blame] | 2914 | mSearchManager.triggerSearch(query, getComponentName(), appSearchData); |
krosaen | d2d6014 | 2009-08-17 08:56:48 -0700 | [diff] [blame] | 2915 | } |
| 2916 | |
| 2917 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2918 | * Request that key events come to this activity. Use this if your |
| 2919 | * activity has no views with focus, but the activity still wants |
| 2920 | * a chance to process key events. |
| 2921 | * |
| 2922 | * @see android.view.Window#takeKeyEvents |
| 2923 | */ |
| 2924 | public void takeKeyEvents(boolean get) { |
| 2925 | getWindow().takeKeyEvents(get); |
| 2926 | } |
| 2927 | |
| 2928 | /** |
| 2929 | * Enable extended window features. This is a convenience for calling |
| 2930 | * {@link android.view.Window#requestFeature getWindow().requestFeature()}. |
| 2931 | * |
| 2932 | * @param featureId The desired feature as defined in |
| 2933 | * {@link android.view.Window}. |
| 2934 | * @return Returns true if the requested feature is supported and now |
| 2935 | * enabled. |
| 2936 | * |
| 2937 | * @see android.view.Window#requestFeature |
| 2938 | */ |
| 2939 | public final boolean requestWindowFeature(int featureId) { |
| 2940 | return getWindow().requestFeature(featureId); |
| 2941 | } |
| 2942 | |
| 2943 | /** |
| 2944 | * Convenience for calling |
| 2945 | * {@link android.view.Window#setFeatureDrawableResource}. |
| 2946 | */ |
| 2947 | public final void setFeatureDrawableResource(int featureId, int resId) { |
| 2948 | getWindow().setFeatureDrawableResource(featureId, resId); |
| 2949 | } |
| 2950 | |
| 2951 | /** |
| 2952 | * Convenience for calling |
| 2953 | * {@link android.view.Window#setFeatureDrawableUri}. |
| 2954 | */ |
| 2955 | public final void setFeatureDrawableUri(int featureId, Uri uri) { |
| 2956 | getWindow().setFeatureDrawableUri(featureId, uri); |
| 2957 | } |
| 2958 | |
| 2959 | /** |
| 2960 | * Convenience for calling |
| 2961 | * {@link android.view.Window#setFeatureDrawable(int, Drawable)}. |
| 2962 | */ |
| 2963 | public final void setFeatureDrawable(int featureId, Drawable drawable) { |
| 2964 | getWindow().setFeatureDrawable(featureId, drawable); |
| 2965 | } |
| 2966 | |
| 2967 | /** |
| 2968 | * Convenience for calling |
| 2969 | * {@link android.view.Window#setFeatureDrawableAlpha}. |
| 2970 | */ |
| 2971 | public final void setFeatureDrawableAlpha(int featureId, int alpha) { |
| 2972 | getWindow().setFeatureDrawableAlpha(featureId, alpha); |
| 2973 | } |
| 2974 | |
| 2975 | /** |
| 2976 | * Convenience for calling |
| 2977 | * {@link android.view.Window#getLayoutInflater}. |
| 2978 | */ |
| 2979 | public LayoutInflater getLayoutInflater() { |
| 2980 | return getWindow().getLayoutInflater(); |
| 2981 | } |
| 2982 | |
| 2983 | /** |
| 2984 | * Returns a {@link MenuInflater} with this context. |
| 2985 | */ |
| 2986 | public MenuInflater getMenuInflater() { |
| 2987 | return new MenuInflater(this); |
| 2988 | } |
| 2989 | |
| 2990 | @Override |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 2991 | protected void onApplyThemeResource(Resources.Theme theme, int resid, |
| 2992 | boolean first) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2993 | if (mParent == null) { |
| 2994 | super.onApplyThemeResource(theme, resid, first); |
| 2995 | } else { |
| 2996 | try { |
| 2997 | theme.setTo(mParent.getTheme()); |
| 2998 | } catch (Exception e) { |
| 2999 | // Empty |
| 3000 | } |
| 3001 | theme.applyStyle(resid, false); |
| 3002 | } |
| 3003 | } |
| 3004 | |
| 3005 | /** |
| 3006 | * Launch an activity for which you would like a result when it finished. |
| 3007 | * When this activity exits, your |
| 3008 | * onActivityResult() method will be called with the given requestCode. |
| 3009 | * Using a negative requestCode is the same as calling |
| 3010 | * {@link #startActivity} (the activity is not launched as a sub-activity). |
| 3011 | * |
| 3012 | * <p>Note that this method should only be used with Intent protocols |
| 3013 | * that are defined to return a result. In other protocols (such as |
| 3014 | * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may |
| 3015 | * not get the result when you expect. For example, if the activity you |
| 3016 | * are launching uses the singleTask launch mode, it will not run in your |
| 3017 | * task and thus you will immediately receive a cancel result. |
| 3018 | * |
| 3019 | * <p>As a special case, if you call startActivityForResult() with a requestCode |
| 3020 | * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your |
| 3021 | * activity, then your window will not be displayed until a result is |
| 3022 | * returned back from the started activity. This is to avoid visible |
| 3023 | * flickering when redirecting to another activity. |
| 3024 | * |
| 3025 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 3026 | * if there was no Activity found to run the given Intent. |
| 3027 | * |
| 3028 | * @param intent The intent to start. |
| 3029 | * @param requestCode If >= 0, this code will be returned in |
| 3030 | * onActivityResult() when the activity exits. |
| 3031 | * |
| 3032 | * @throws android.content.ActivityNotFoundException |
| 3033 | * |
| 3034 | * @see #startActivity |
| 3035 | */ |
| 3036 | public void startActivityForResult(Intent intent, int requestCode) { |
| 3037 | if (mParent == null) { |
| 3038 | Instrumentation.ActivityResult ar = |
| 3039 | mInstrumentation.execStartActivity( |
| 3040 | this, mMainThread.getApplicationThread(), mToken, this, |
| 3041 | intent, requestCode); |
| 3042 | if (ar != null) { |
| 3043 | mMainThread.sendActivityResult( |
| 3044 | mToken, mEmbeddedID, requestCode, ar.getResultCode(), |
| 3045 | ar.getResultData()); |
| 3046 | } |
| 3047 | if (requestCode >= 0) { |
| 3048 | // If this start is requesting a result, we can avoid making |
| 3049 | // the activity visible until the result is received. Setting |
| 3050 | // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the |
| 3051 | // activity hidden during this time, to avoid flickering. |
| 3052 | // This can only be done when a result is requested because |
| 3053 | // that guarantees we will get information back when the |
| 3054 | // activity is finished, no matter what happens to it. |
| 3055 | mStartedActivity = true; |
| 3056 | } |
| 3057 | } else { |
| 3058 | mParent.startActivityFromChild(this, intent, requestCode); |
| 3059 | } |
| 3060 | } |
| 3061 | |
| 3062 | /** |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3063 | * Like {@link #startActivityForResult(Intent, int)}, but allowing you |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3064 | * to use a IntentSender to describe the activity to be started. If |
| 3065 | * the IntentSender is for an activity, that activity will be started |
| 3066 | * as if you had called the regular {@link #startActivityForResult(Intent, int)} |
| 3067 | * here; otherwise, its associated action will be executed (such as |
| 3068 | * sending a broadcast) as if you had called |
| 3069 | * {@link IntentSender#sendIntent IntentSender.sendIntent} on it. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3070 | * |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3071 | * @param intent The IntentSender to launch. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3072 | * @param requestCode If >= 0, this code will be returned in |
| 3073 | * onActivityResult() when the activity exits. |
| 3074 | * @param fillInIntent If non-null, this will be provided as the |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3075 | * intent parameter to {@link IntentSender#sendIntent}. |
| 3076 | * @param flagsMask Intent flags in the original IntentSender that you |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3077 | * would like to change. |
| 3078 | * @param flagsValues Desired values for any bits set in |
| 3079 | * <var>flagsMask</var> |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3080 | * @param extraFlags Always set to 0. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3081 | */ |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3082 | public void startIntentSenderForResult(IntentSender intent, int requestCode, |
| 3083 | Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) |
| 3084 | throws IntentSender.SendIntentException { |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3085 | if (mParent == null) { |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3086 | startIntentSenderForResultInner(intent, requestCode, fillInIntent, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3087 | flagsMask, flagsValues, this); |
| 3088 | } else { |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3089 | mParent.startIntentSenderFromChild(this, intent, requestCode, |
| 3090 | fillInIntent, flagsMask, flagsValues, extraFlags); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3091 | } |
| 3092 | } |
| 3093 | |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3094 | private void startIntentSenderForResultInner(IntentSender intent, int requestCode, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3095 | Intent fillInIntent, int flagsMask, int flagsValues, Activity activity) |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3096 | throws IntentSender.SendIntentException { |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3097 | try { |
| 3098 | String resolvedType = null; |
| 3099 | if (fillInIntent != null) { |
| 3100 | resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver()); |
| 3101 | } |
| 3102 | int result = ActivityManagerNative.getDefault() |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3103 | .startActivityIntentSender(mMainThread.getApplicationThread(), intent, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3104 | fillInIntent, resolvedType, mToken, activity.mEmbeddedID, |
| 3105 | requestCode, flagsMask, flagsValues); |
| 3106 | if (result == IActivityManager.START_CANCELED) { |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3107 | throw new IntentSender.SendIntentException(); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3108 | } |
| 3109 | Instrumentation.checkStartActivityResult(result, null); |
| 3110 | } catch (RemoteException e) { |
| 3111 | } |
| 3112 | if (requestCode >= 0) { |
| 3113 | // If this start is requesting a result, we can avoid making |
| 3114 | // the activity visible until the result is received. Setting |
| 3115 | // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the |
| 3116 | // activity hidden during this time, to avoid flickering. |
| 3117 | // This can only be done when a result is requested because |
| 3118 | // that guarantees we will get information back when the |
| 3119 | // activity is finished, no matter what happens to it. |
| 3120 | mStartedActivity = true; |
| 3121 | } |
| 3122 | } |
| 3123 | |
| 3124 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3125 | * Launch a new activity. You will not receive any information about when |
| 3126 | * the activity exits. This implementation overrides the base version, |
| 3127 | * providing information about |
| 3128 | * the activity performing the launch. Because of this additional |
| 3129 | * information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not |
| 3130 | * required; if not specified, the new activity will be added to the |
| 3131 | * task of the caller. |
| 3132 | * |
| 3133 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 3134 | * if there was no Activity found to run the given Intent. |
| 3135 | * |
| 3136 | * @param intent The intent to start. |
| 3137 | * |
| 3138 | * @throws android.content.ActivityNotFoundException |
| 3139 | * |
| 3140 | * @see #startActivityForResult |
| 3141 | */ |
| 3142 | @Override |
| 3143 | public void startActivity(Intent intent) { |
| 3144 | startActivityForResult(intent, -1); |
| 3145 | } |
| 3146 | |
| 3147 | /** |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3148 | * Like {@link #startActivity(Intent)}, but taking a IntentSender |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3149 | * to start; see |
Dianne Hackborn | ae22c05 | 2009-09-17 18:46:22 -0700 | [diff] [blame] | 3150 | * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)} |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3151 | * for more information. |
| 3152 | * |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3153 | * @param intent The IntentSender to launch. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3154 | * @param fillInIntent If non-null, this will be provided as the |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3155 | * intent parameter to {@link IntentSender#sendIntent}. |
| 3156 | * @param flagsMask Intent flags in the original IntentSender that you |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3157 | * would like to change. |
| 3158 | * @param flagsValues Desired values for any bits set in |
| 3159 | * <var>flagsMask</var> |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3160 | * @param extraFlags Always set to 0. |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3161 | */ |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3162 | public void startIntentSender(IntentSender intent, |
| 3163 | Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) |
| 3164 | throws IntentSender.SendIntentException { |
| 3165 | startIntentSenderForResult(intent, -1, fillInIntent, flagsMask, |
| 3166 | flagsValues, extraFlags); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3167 | } |
| 3168 | |
| 3169 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3170 | * A special variation to launch an activity only if a new activity |
| 3171 | * instance is needed to handle the given Intent. In other words, this is |
| 3172 | * just like {@link #startActivityForResult(Intent, int)} except: if you are |
| 3173 | * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or |
| 3174 | * singleTask or singleTop |
| 3175 | * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode}, |
| 3176 | * and the activity |
| 3177 | * that handles <var>intent</var> is the same as your currently running |
| 3178 | * activity, then a new instance is not needed. In this case, instead of |
| 3179 | * the normal behavior of calling {@link #onNewIntent} this function will |
| 3180 | * return and you can handle the Intent yourself. |
| 3181 | * |
| 3182 | * <p>This function can only be called from a top-level activity; if it is |
| 3183 | * called from a child activity, a runtime exception will be thrown. |
| 3184 | * |
| 3185 | * @param intent The intent to start. |
| 3186 | * @param requestCode If >= 0, this code will be returned in |
| 3187 | * onActivityResult() when the activity exits, as described in |
| 3188 | * {@link #startActivityForResult}. |
| 3189 | * |
| 3190 | * @return If a new activity was launched then true is returned; otherwise |
| 3191 | * false is returned and you must handle the Intent yourself. |
| 3192 | * |
| 3193 | * @see #startActivity |
| 3194 | * @see #startActivityForResult |
| 3195 | */ |
| 3196 | public boolean startActivityIfNeeded(Intent intent, int requestCode) { |
| 3197 | if (mParent == null) { |
| 3198 | int result = IActivityManager.START_RETURN_INTENT_TO_CALLER; |
| 3199 | try { |
| 3200 | result = ActivityManagerNative.getDefault() |
| 3201 | .startActivity(mMainThread.getApplicationThread(), |
| 3202 | intent, intent.resolveTypeIfNeeded( |
| 3203 | getContentResolver()), |
| 3204 | null, 0, |
| 3205 | mToken, mEmbeddedID, requestCode, true, false); |
| 3206 | } catch (RemoteException e) { |
| 3207 | // Empty |
| 3208 | } |
| 3209 | |
| 3210 | Instrumentation.checkStartActivityResult(result, intent); |
| 3211 | |
| 3212 | if (requestCode >= 0) { |
| 3213 | // If this start is requesting a result, we can avoid making |
| 3214 | // the activity visible until the result is received. Setting |
| 3215 | // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the |
| 3216 | // activity hidden during this time, to avoid flickering. |
| 3217 | // This can only be done when a result is requested because |
| 3218 | // that guarantees we will get information back when the |
| 3219 | // activity is finished, no matter what happens to it. |
| 3220 | mStartedActivity = true; |
| 3221 | } |
| 3222 | return result != IActivityManager.START_RETURN_INTENT_TO_CALLER; |
| 3223 | } |
| 3224 | |
| 3225 | throw new UnsupportedOperationException( |
| 3226 | "startActivityIfNeeded can only be called from a top-level activity"); |
| 3227 | } |
| 3228 | |
| 3229 | /** |
| 3230 | * Special version of starting an activity, for use when you are replacing |
| 3231 | * other activity components. You can use this to hand the Intent off |
| 3232 | * to the next Activity that can handle it. You typically call this in |
| 3233 | * {@link #onCreate} with the Intent returned by {@link #getIntent}. |
| 3234 | * |
| 3235 | * @param intent The intent to dispatch to the next activity. For |
| 3236 | * correct behavior, this must be the same as the Intent that started |
| 3237 | * your own activity; the only changes you can make are to the extras |
| 3238 | * inside of it. |
| 3239 | * |
| 3240 | * @return Returns a boolean indicating whether there was another Activity |
| 3241 | * to start: true if there was a next activity to start, false if there |
| 3242 | * wasn't. In general, if true is returned you will then want to call |
| 3243 | * finish() on yourself. |
| 3244 | */ |
| 3245 | public boolean startNextMatchingActivity(Intent intent) { |
| 3246 | if (mParent == null) { |
| 3247 | try { |
| 3248 | return ActivityManagerNative.getDefault() |
| 3249 | .startNextMatchingActivity(mToken, intent); |
| 3250 | } catch (RemoteException e) { |
| 3251 | // Empty |
| 3252 | } |
| 3253 | return false; |
| 3254 | } |
| 3255 | |
| 3256 | throw new UnsupportedOperationException( |
| 3257 | "startNextMatchingActivity can only be called from a top-level activity"); |
| 3258 | } |
| 3259 | |
| 3260 | /** |
| 3261 | * This is called when a child activity of this one calls its |
| 3262 | * {@link #startActivity} or {@link #startActivityForResult} method. |
| 3263 | * |
| 3264 | * <p>This method throws {@link android.content.ActivityNotFoundException} |
| 3265 | * if there was no Activity found to run the given Intent. |
| 3266 | * |
| 3267 | * @param child The activity making the call. |
| 3268 | * @param intent The intent to start. |
| 3269 | * @param requestCode Reply request code. < 0 if reply is not requested. |
| 3270 | * |
| 3271 | * @throws android.content.ActivityNotFoundException |
| 3272 | * |
| 3273 | * @see #startActivity |
| 3274 | * @see #startActivityForResult |
| 3275 | */ |
| 3276 | public void startActivityFromChild(Activity child, Intent intent, |
| 3277 | int requestCode) { |
| 3278 | Instrumentation.ActivityResult ar = |
| 3279 | mInstrumentation.execStartActivity( |
| 3280 | this, mMainThread.getApplicationThread(), mToken, child, |
| 3281 | intent, requestCode); |
| 3282 | if (ar != null) { |
| 3283 | mMainThread.sendActivityResult( |
| 3284 | mToken, child.mEmbeddedID, requestCode, |
| 3285 | ar.getResultCode(), ar.getResultData()); |
| 3286 | } |
| 3287 | } |
| 3288 | |
| 3289 | /** |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3290 | * Like {@link #startActivityFromChild(Activity, Intent, int)}, but |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3291 | * taking a IntentSender; see |
Dianne Hackborn | ae22c05 | 2009-09-17 18:46:22 -0700 | [diff] [blame] | 3292 | * {@link #startIntentSenderForResult(IntentSender, int, Intent, int, int, int)} |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3293 | * for more information. |
| 3294 | */ |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 3295 | public void startIntentSenderFromChild(Activity child, IntentSender intent, |
| 3296 | int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, |
| 3297 | int extraFlags) |
| 3298 | throws IntentSender.SendIntentException { |
| 3299 | startIntentSenderForResultInner(intent, requestCode, fillInIntent, |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 3300 | flagsMask, flagsValues, child); |
| 3301 | } |
| 3302 | |
| 3303 | /** |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 3304 | * Call immediately after one of the flavors of {@link #startActivity(Intent)} |
| 3305 | * or {@link #finish} to specify an explicit transition animation to |
| 3306 | * perform next. |
| 3307 | * @param enterAnim A resource ID of the animation resource to use for |
Dianne Hackborn | 8b571a8 | 2009-09-25 16:09:43 -0700 | [diff] [blame] | 3308 | * the incoming activity. Use 0 for no animation. |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 3309 | * @param exitAnim A resource ID of the animation resource to use for |
Dianne Hackborn | 8b571a8 | 2009-09-25 16:09:43 -0700 | [diff] [blame] | 3310 | * the outgoing activity. Use 0 for no animation. |
Dianne Hackborn | 3b3e145 | 2009-09-24 19:22:12 -0700 | [diff] [blame] | 3311 | */ |
| 3312 | public void overridePendingTransition(int enterAnim, int exitAnim) { |
| 3313 | try { |
| 3314 | ActivityManagerNative.getDefault().overridePendingTransition( |
| 3315 | mToken, getPackageName(), enterAnim, exitAnim); |
| 3316 | } catch (RemoteException e) { |
| 3317 | } |
| 3318 | } |
| 3319 | |
| 3320 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3321 | * Call this to set the result that your activity will return to its |
| 3322 | * caller. |
| 3323 | * |
| 3324 | * @param resultCode The result code to propagate back to the originating |
| 3325 | * activity, often RESULT_CANCELED or RESULT_OK |
| 3326 | * |
| 3327 | * @see #RESULT_CANCELED |
| 3328 | * @see #RESULT_OK |
| 3329 | * @see #RESULT_FIRST_USER |
| 3330 | * @see #setResult(int, Intent) |
| 3331 | */ |
| 3332 | public final void setResult(int resultCode) { |
| 3333 | synchronized (this) { |
| 3334 | mResultCode = resultCode; |
| 3335 | mResultData = null; |
| 3336 | } |
| 3337 | } |
| 3338 | |
| 3339 | /** |
| 3340 | * Call this to set the result that your activity will return to its |
| 3341 | * caller. |
| 3342 | * |
| 3343 | * @param resultCode The result code to propagate back to the originating |
| 3344 | * activity, often RESULT_CANCELED or RESULT_OK |
| 3345 | * @param data The data to propagate back to the originating activity. |
| 3346 | * |
| 3347 | * @see #RESULT_CANCELED |
| 3348 | * @see #RESULT_OK |
| 3349 | * @see #RESULT_FIRST_USER |
| 3350 | * @see #setResult(int) |
| 3351 | */ |
| 3352 | public final void setResult(int resultCode, Intent data) { |
| 3353 | synchronized (this) { |
| 3354 | mResultCode = resultCode; |
| 3355 | mResultData = data; |
| 3356 | } |
| 3357 | } |
| 3358 | |
| 3359 | /** |
| 3360 | * Return the name of the package that invoked this activity. This is who |
| 3361 | * the data in {@link #setResult setResult()} will be sent to. You can |
| 3362 | * use this information to validate that the recipient is allowed to |
| 3363 | * receive the data. |
| 3364 | * |
| 3365 | * <p>Note: if the calling activity is not expecting a result (that is it |
| 3366 | * did not use the {@link #startActivityForResult} |
| 3367 | * form that includes a request code), then the calling package will be |
| 3368 | * null. |
| 3369 | * |
| 3370 | * @return The package of the activity that will receive your |
| 3371 | * reply, or null if none. |
| 3372 | */ |
| 3373 | public String getCallingPackage() { |
| 3374 | try { |
| 3375 | return ActivityManagerNative.getDefault().getCallingPackage(mToken); |
| 3376 | } catch (RemoteException e) { |
| 3377 | return null; |
| 3378 | } |
| 3379 | } |
| 3380 | |
| 3381 | /** |
| 3382 | * Return the name of the activity that invoked this activity. This is |
| 3383 | * who the data in {@link #setResult setResult()} will be sent to. You |
| 3384 | * can use this information to validate that the recipient is allowed to |
| 3385 | * receive the data. |
| 3386 | * |
| 3387 | * <p>Note: if the calling activity is not expecting a result (that is it |
| 3388 | * did not use the {@link #startActivityForResult} |
| 3389 | * form that includes a request code), then the calling package will be |
| 3390 | * null. |
| 3391 | * |
| 3392 | * @return String The full name of the activity that will receive your |
| 3393 | * reply, or null if none. |
| 3394 | */ |
| 3395 | public ComponentName getCallingActivity() { |
| 3396 | try { |
| 3397 | return ActivityManagerNative.getDefault().getCallingActivity(mToken); |
| 3398 | } catch (RemoteException e) { |
| 3399 | return null; |
| 3400 | } |
| 3401 | } |
| 3402 | |
| 3403 | /** |
| 3404 | * Control whether this activity's main window is visible. This is intended |
| 3405 | * only for the special case of an activity that is not going to show a |
| 3406 | * UI itself, but can't just finish prior to onResume() because it needs |
| 3407 | * to wait for a service binding or such. Setting this to false allows |
| 3408 | * you to prevent your UI from being shown during that time. |
| 3409 | * |
| 3410 | * <p>The default value for this is taken from the |
| 3411 | * {@link android.R.attr#windowNoDisplay} attribute of the activity's theme. |
| 3412 | */ |
| 3413 | public void setVisible(boolean visible) { |
| 3414 | if (mVisibleFromClient != visible) { |
| 3415 | mVisibleFromClient = visible; |
| 3416 | if (mVisibleFromServer) { |
| 3417 | if (visible) makeVisible(); |
| 3418 | else mDecor.setVisibility(View.INVISIBLE); |
| 3419 | } |
| 3420 | } |
| 3421 | } |
| 3422 | |
| 3423 | void makeVisible() { |
| 3424 | if (!mWindowAdded) { |
| 3425 | ViewManager wm = getWindowManager(); |
| 3426 | wm.addView(mDecor, getWindow().getAttributes()); |
| 3427 | mWindowAdded = true; |
| 3428 | } |
| 3429 | mDecor.setVisibility(View.VISIBLE); |
| 3430 | } |
| 3431 | |
| 3432 | /** |
| 3433 | * Check to see whether this activity is in the process of finishing, |
| 3434 | * either because you called {@link #finish} on it or someone else |
| 3435 | * has requested that it finished. This is often used in |
| 3436 | * {@link #onPause} to determine whether the activity is simply pausing or |
| 3437 | * completely finishing. |
| 3438 | * |
| 3439 | * @return If the activity is finishing, returns true; else returns false. |
| 3440 | * |
| 3441 | * @see #finish |
| 3442 | */ |
| 3443 | public boolean isFinishing() { |
| 3444 | return mFinished; |
| 3445 | } |
| 3446 | |
| 3447 | /** |
Jeff Hamilton | 3d32f6e | 2010-04-01 00:04:16 -0500 | [diff] [blame] | 3448 | * Check to see whether this activity is in the process of being destroyed in order to be |
| 3449 | * recreated with a new configuration. This is often used in |
| 3450 | * {@link #onStop} to determine whether the state needs to be cleaned up or will be passed |
| 3451 | * on to the next instance of the activity via {@link #onRetainNonConfigurationInstance()}. |
| 3452 | * |
| 3453 | * @return If the activity is being torn down in order to be recreated with a new configuration, |
| 3454 | * returns true; else returns false. |
| 3455 | */ |
| 3456 | public boolean isChangingConfigurations() { |
| 3457 | return mChangingConfigurations; |
| 3458 | } |
| 3459 | |
| 3460 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3461 | * Call this when your activity is done and should be closed. The |
| 3462 | * ActivityResult is propagated back to whoever launched you via |
| 3463 | * onActivityResult(). |
| 3464 | */ |
| 3465 | public void finish() { |
| 3466 | if (mParent == null) { |
| 3467 | int resultCode; |
| 3468 | Intent resultData; |
| 3469 | synchronized (this) { |
| 3470 | resultCode = mResultCode; |
| 3471 | resultData = mResultData; |
| 3472 | } |
| 3473 | if (Config.LOGV) Log.v(TAG, "Finishing self: token=" + mToken); |
| 3474 | try { |
| 3475 | if (ActivityManagerNative.getDefault() |
| 3476 | .finishActivity(mToken, resultCode, resultData)) { |
| 3477 | mFinished = true; |
| 3478 | } |
| 3479 | } catch (RemoteException e) { |
| 3480 | // Empty |
| 3481 | } |
| 3482 | } else { |
| 3483 | mParent.finishFromChild(this); |
| 3484 | } |
| 3485 | } |
| 3486 | |
| 3487 | /** |
| 3488 | * This is called when a child activity of this one calls its |
| 3489 | * {@link #finish} method. The default implementation simply calls |
| 3490 | * finish() on this activity (the parent), finishing the entire group. |
| 3491 | * |
| 3492 | * @param child The activity making the call. |
| 3493 | * |
| 3494 | * @see #finish |
| 3495 | */ |
| 3496 | public void finishFromChild(Activity child) { |
| 3497 | finish(); |
| 3498 | } |
| 3499 | |
| 3500 | /** |
| 3501 | * Force finish another activity that you had previously started with |
| 3502 | * {@link #startActivityForResult}. |
| 3503 | * |
| 3504 | * @param requestCode The request code of the activity that you had |
| 3505 | * given to startActivityForResult(). If there are multiple |
| 3506 | * activities started with this request code, they |
| 3507 | * will all be finished. |
| 3508 | */ |
| 3509 | public void finishActivity(int requestCode) { |
| 3510 | if (mParent == null) { |
| 3511 | try { |
| 3512 | ActivityManagerNative.getDefault() |
| 3513 | .finishSubActivity(mToken, mEmbeddedID, requestCode); |
| 3514 | } catch (RemoteException e) { |
| 3515 | // Empty |
| 3516 | } |
| 3517 | } else { |
| 3518 | mParent.finishActivityFromChild(this, requestCode); |
| 3519 | } |
| 3520 | } |
| 3521 | |
| 3522 | /** |
| 3523 | * This is called when a child activity of this one calls its |
| 3524 | * finishActivity(). |
| 3525 | * |
| 3526 | * @param child The activity making the call. |
| 3527 | * @param requestCode Request code that had been used to start the |
| 3528 | * activity. |
| 3529 | */ |
| 3530 | public void finishActivityFromChild(Activity child, int requestCode) { |
| 3531 | try { |
| 3532 | ActivityManagerNative.getDefault() |
| 3533 | .finishSubActivity(mToken, child.mEmbeddedID, requestCode); |
| 3534 | } catch (RemoteException e) { |
| 3535 | // Empty |
| 3536 | } |
| 3537 | } |
| 3538 | |
| 3539 | /** |
| 3540 | * Called when an activity you launched exits, giving you the requestCode |
| 3541 | * you started it with, the resultCode it returned, and any additional |
| 3542 | * data from it. The <var>resultCode</var> will be |
| 3543 | * {@link #RESULT_CANCELED} if the activity explicitly returned that, |
| 3544 | * didn't return any result, or crashed during its operation. |
| 3545 | * |
| 3546 | * <p>You will receive this call immediately before onResume() when your |
| 3547 | * activity is re-starting. |
| 3548 | * |
| 3549 | * @param requestCode The integer request code originally supplied to |
| 3550 | * startActivityForResult(), allowing you to identify who this |
| 3551 | * result came from. |
| 3552 | * @param resultCode The integer result code returned by the child activity |
| 3553 | * through its setResult(). |
| 3554 | * @param data An Intent, which can return result data to the caller |
| 3555 | * (various data can be attached to Intent "extras"). |
| 3556 | * |
| 3557 | * @see #startActivityForResult |
| 3558 | * @see #createPendingResult |
| 3559 | * @see #setResult(int) |
| 3560 | */ |
| 3561 | protected void onActivityResult(int requestCode, int resultCode, |
| 3562 | Intent data) { |
| 3563 | } |
| 3564 | |
| 3565 | /** |
| 3566 | * Create a new PendingIntent object which you can hand to others |
| 3567 | * for them to use to send result data back to your |
| 3568 | * {@link #onActivityResult} callback. The created object will be either |
| 3569 | * one-shot (becoming invalid after a result is sent back) or multiple |
| 3570 | * (allowing any number of results to be sent through it). |
| 3571 | * |
| 3572 | * @param requestCode Private request code for the sender that will be |
| 3573 | * associated with the result data when it is returned. The sender can not |
| 3574 | * modify this value, allowing you to identify incoming results. |
| 3575 | * @param data Default data to supply in the result, which may be modified |
| 3576 | * by the sender. |
| 3577 | * @param flags May be {@link PendingIntent#FLAG_ONE_SHOT PendingIntent.FLAG_ONE_SHOT}, |
| 3578 | * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE}, |
| 3579 | * {@link PendingIntent#FLAG_CANCEL_CURRENT PendingIntent.FLAG_CANCEL_CURRENT}, |
| 3580 | * {@link PendingIntent#FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT}, |
| 3581 | * or any of the flags as supported by |
| 3582 | * {@link Intent#fillIn Intent.fillIn()} to control which unspecified parts |
| 3583 | * of the intent that can be supplied when the actual send happens. |
| 3584 | * |
| 3585 | * @return Returns an existing or new PendingIntent matching the given |
| 3586 | * parameters. May return null only if |
| 3587 | * {@link PendingIntent#FLAG_NO_CREATE PendingIntent.FLAG_NO_CREATE} has been |
| 3588 | * supplied. |
| 3589 | * |
| 3590 | * @see PendingIntent |
| 3591 | */ |
| 3592 | public PendingIntent createPendingResult(int requestCode, Intent data, |
| 3593 | int flags) { |
| 3594 | String packageName = getPackageName(); |
| 3595 | try { |
| 3596 | IIntentSender target = |
| 3597 | ActivityManagerNative.getDefault().getIntentSender( |
| 3598 | IActivityManager.INTENT_SENDER_ACTIVITY_RESULT, packageName, |
| 3599 | mParent == null ? mToken : mParent.mToken, |
| 3600 | mEmbeddedID, requestCode, data, null, flags); |
| 3601 | return target != null ? new PendingIntent(target) : null; |
| 3602 | } catch (RemoteException e) { |
| 3603 | // Empty |
| 3604 | } |
| 3605 | return null; |
| 3606 | } |
| 3607 | |
| 3608 | /** |
| 3609 | * Change the desired orientation of this activity. If the activity |
| 3610 | * is currently in the foreground or otherwise impacting the screen |
| 3611 | * orientation, the screen will immediately be changed (possibly causing |
| 3612 | * the activity to be restarted). Otherwise, this will be used the next |
| 3613 | * time the activity is visible. |
| 3614 | * |
| 3615 | * @param requestedOrientation An orientation constant as used in |
| 3616 | * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}. |
| 3617 | */ |
| 3618 | public void setRequestedOrientation(int requestedOrientation) { |
| 3619 | if (mParent == null) { |
| 3620 | try { |
| 3621 | ActivityManagerNative.getDefault().setRequestedOrientation( |
| 3622 | mToken, requestedOrientation); |
| 3623 | } catch (RemoteException e) { |
| 3624 | // Empty |
| 3625 | } |
| 3626 | } else { |
| 3627 | mParent.setRequestedOrientation(requestedOrientation); |
| 3628 | } |
| 3629 | } |
| 3630 | |
| 3631 | /** |
| 3632 | * Return the current requested orientation of the activity. This will |
| 3633 | * either be the orientation requested in its component's manifest, or |
| 3634 | * the last requested orientation given to |
| 3635 | * {@link #setRequestedOrientation(int)}. |
| 3636 | * |
| 3637 | * @return Returns an orientation constant as used in |
| 3638 | * {@link ActivityInfo#screenOrientation ActivityInfo.screenOrientation}. |
| 3639 | */ |
| 3640 | public int getRequestedOrientation() { |
| 3641 | if (mParent == null) { |
| 3642 | try { |
| 3643 | return ActivityManagerNative.getDefault() |
| 3644 | .getRequestedOrientation(mToken); |
| 3645 | } catch (RemoteException e) { |
| 3646 | // Empty |
| 3647 | } |
| 3648 | } else { |
| 3649 | return mParent.getRequestedOrientation(); |
| 3650 | } |
| 3651 | return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 3652 | } |
| 3653 | |
| 3654 | /** |
| 3655 | * Return the identifier of the task this activity is in. This identifier |
| 3656 | * will remain the same for the lifetime of the activity. |
| 3657 | * |
| 3658 | * @return Task identifier, an opaque integer. |
| 3659 | */ |
| 3660 | public int getTaskId() { |
| 3661 | try { |
| 3662 | return ActivityManagerNative.getDefault() |
| 3663 | .getTaskForActivity(mToken, false); |
| 3664 | } catch (RemoteException e) { |
| 3665 | return -1; |
| 3666 | } |
| 3667 | } |
| 3668 | |
| 3669 | /** |
| 3670 | * Return whether this activity is the root of a task. The root is the |
| 3671 | * first activity in a task. |
| 3672 | * |
| 3673 | * @return True if this is the root activity, else false. |
| 3674 | */ |
| 3675 | public boolean isTaskRoot() { |
| 3676 | try { |
| 3677 | return ActivityManagerNative.getDefault() |
| 3678 | .getTaskForActivity(mToken, true) >= 0; |
| 3679 | } catch (RemoteException e) { |
| 3680 | return false; |
| 3681 | } |
| 3682 | } |
| 3683 | |
| 3684 | /** |
| 3685 | * Move the task containing this activity to the back of the activity |
| 3686 | * stack. The activity's order within the task is unchanged. |
| 3687 | * |
| 3688 | * @param nonRoot If false then this only works if the activity is the root |
| 3689 | * of a task; if true it will work for any activity in |
| 3690 | * a task. |
| 3691 | * |
| 3692 | * @return If the task was moved (or it was already at the |
| 3693 | * back) true is returned, else false. |
| 3694 | */ |
| 3695 | public boolean moveTaskToBack(boolean nonRoot) { |
| 3696 | try { |
| 3697 | return ActivityManagerNative.getDefault().moveActivityTaskToBack( |
| 3698 | mToken, nonRoot); |
| 3699 | } catch (RemoteException e) { |
| 3700 | // Empty |
| 3701 | } |
| 3702 | return false; |
| 3703 | } |
| 3704 | |
| 3705 | /** |
| 3706 | * Returns class name for this activity with the package prefix removed. |
| 3707 | * This is the default name used to read and write settings. |
| 3708 | * |
| 3709 | * @return The local class name. |
| 3710 | */ |
| 3711 | public String getLocalClassName() { |
| 3712 | final String pkg = getPackageName(); |
| 3713 | final String cls = mComponent.getClassName(); |
| 3714 | int packageLen = pkg.length(); |
| 3715 | if (!cls.startsWith(pkg) || cls.length() <= packageLen |
| 3716 | || cls.charAt(packageLen) != '.') { |
| 3717 | return cls; |
| 3718 | } |
| 3719 | return cls.substring(packageLen+1); |
| 3720 | } |
| 3721 | |
| 3722 | /** |
| 3723 | * Returns complete component name of this activity. |
| 3724 | * |
| 3725 | * @return Returns the complete component name for this activity |
| 3726 | */ |
| 3727 | public ComponentName getComponentName() |
| 3728 | { |
| 3729 | return mComponent; |
| 3730 | } |
| 3731 | |
| 3732 | /** |
| 3733 | * Retrieve a {@link SharedPreferences} object for accessing preferences |
| 3734 | * that are private to this activity. This simply calls the underlying |
| 3735 | * {@link #getSharedPreferences(String, int)} method by passing in this activity's |
| 3736 | * class name as the preferences name. |
| 3737 | * |
| 3738 | * @param mode Operating mode. Use {@link #MODE_PRIVATE} for the default |
| 3739 | * operation, {@link #MODE_WORLD_READABLE} and |
| 3740 | * {@link #MODE_WORLD_WRITEABLE} to control permissions. |
| 3741 | * |
| 3742 | * @return Returns the single SharedPreferences instance that can be used |
| 3743 | * to retrieve and modify the preference values. |
| 3744 | */ |
| 3745 | public SharedPreferences getPreferences(int mode) { |
| 3746 | return getSharedPreferences(getLocalClassName(), mode); |
| 3747 | } |
| 3748 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3749 | private void ensureSearchManager() { |
| 3750 | if (mSearchManager != null) { |
| 3751 | return; |
| 3752 | } |
| 3753 | |
Amith Yamasani | e9ce3f0 | 2010-01-25 09:15:50 -0800 | [diff] [blame] | 3754 | mSearchManager = new SearchManager(this, null); |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3755 | } |
| 3756 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3757 | @Override |
| 3758 | public Object getSystemService(String name) { |
| 3759 | if (getBaseContext() == null) { |
| 3760 | throw new IllegalStateException( |
| 3761 | "System services not available to Activities before onCreate()"); |
| 3762 | } |
| 3763 | |
| 3764 | if (WINDOW_SERVICE.equals(name)) { |
| 3765 | return mWindowManager; |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 3766 | } else if (SEARCH_SERVICE.equals(name)) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 3767 | ensureSearchManager(); |
Bjorn Bringert | 8d17f3f | 2009-06-05 13:22:28 +0100 | [diff] [blame] | 3768 | return mSearchManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3769 | } |
| 3770 | return super.getSystemService(name); |
| 3771 | } |
| 3772 | |
| 3773 | /** |
| 3774 | * Change the title associated with this activity. If this is a |
| 3775 | * top-level activity, the title for its window will change. If it |
| 3776 | * is an embedded activity, the parent can do whatever it wants |
| 3777 | * with it. |
| 3778 | */ |
| 3779 | public void setTitle(CharSequence title) { |
| 3780 | mTitle = title; |
| 3781 | onTitleChanged(title, mTitleColor); |
| 3782 | |
| 3783 | if (mParent != null) { |
| 3784 | mParent.onChildTitleChanged(this, title); |
| 3785 | } |
| 3786 | } |
| 3787 | |
| 3788 | /** |
| 3789 | * Change the title associated with this activity. If this is a |
| 3790 | * top-level activity, the title for its window will change. If it |
| 3791 | * is an embedded activity, the parent can do whatever it wants |
| 3792 | * with it. |
| 3793 | */ |
| 3794 | public void setTitle(int titleId) { |
| 3795 | setTitle(getText(titleId)); |
| 3796 | } |
| 3797 | |
| 3798 | public void setTitleColor(int textColor) { |
| 3799 | mTitleColor = textColor; |
| 3800 | onTitleChanged(mTitle, textColor); |
| 3801 | } |
| 3802 | |
| 3803 | public final CharSequence getTitle() { |
| 3804 | return mTitle; |
| 3805 | } |
| 3806 | |
| 3807 | public final int getTitleColor() { |
| 3808 | return mTitleColor; |
| 3809 | } |
| 3810 | |
| 3811 | protected void onTitleChanged(CharSequence title, int color) { |
| 3812 | if (mTitleReady) { |
| 3813 | final Window win = getWindow(); |
| 3814 | if (win != null) { |
| 3815 | win.setTitle(title); |
| 3816 | if (color != 0) { |
| 3817 | win.setTitleColor(color); |
| 3818 | } |
| 3819 | } |
| 3820 | } |
| 3821 | } |
| 3822 | |
| 3823 | protected void onChildTitleChanged(Activity childActivity, CharSequence title) { |
| 3824 | } |
| 3825 | |
| 3826 | /** |
| 3827 | * Sets the visibility of the progress bar in the title. |
| 3828 | * <p> |
| 3829 | * In order for the progress bar to be shown, the feature must be requested |
| 3830 | * via {@link #requestWindowFeature(int)}. |
| 3831 | * |
| 3832 | * @param visible Whether to show the progress bars in the title. |
| 3833 | */ |
| 3834 | public final void setProgressBarVisibility(boolean visible) { |
| 3835 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, visible ? Window.PROGRESS_VISIBILITY_ON : |
| 3836 | Window.PROGRESS_VISIBILITY_OFF); |
| 3837 | } |
| 3838 | |
| 3839 | /** |
| 3840 | * Sets the visibility of the indeterminate progress bar in the title. |
| 3841 | * <p> |
| 3842 | * In order for the progress bar to be shown, the feature must be requested |
| 3843 | * via {@link #requestWindowFeature(int)}. |
| 3844 | * |
| 3845 | * @param visible Whether to show the progress bars in the title. |
| 3846 | */ |
| 3847 | public final void setProgressBarIndeterminateVisibility(boolean visible) { |
| 3848 | getWindow().setFeatureInt(Window.FEATURE_INDETERMINATE_PROGRESS, |
| 3849 | visible ? Window.PROGRESS_VISIBILITY_ON : Window.PROGRESS_VISIBILITY_OFF); |
| 3850 | } |
| 3851 | |
| 3852 | /** |
| 3853 | * Sets whether the horizontal progress bar in the title should be indeterminate (the circular |
| 3854 | * is always indeterminate). |
| 3855 | * <p> |
| 3856 | * In order for the progress bar to be shown, the feature must be requested |
| 3857 | * via {@link #requestWindowFeature(int)}. |
| 3858 | * |
| 3859 | * @param indeterminate Whether the horizontal progress bar should be indeterminate. |
| 3860 | */ |
| 3861 | public final void setProgressBarIndeterminate(boolean indeterminate) { |
| 3862 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, |
| 3863 | indeterminate ? Window.PROGRESS_INDETERMINATE_ON : Window.PROGRESS_INDETERMINATE_OFF); |
| 3864 | } |
| 3865 | |
| 3866 | /** |
| 3867 | * Sets the progress for the progress bars in the title. |
| 3868 | * <p> |
| 3869 | * In order for the progress bar to be shown, the feature must be requested |
| 3870 | * via {@link #requestWindowFeature(int)}. |
| 3871 | * |
| 3872 | * @param progress The progress for the progress bar. Valid ranges are from |
| 3873 | * 0 to 10000 (both inclusive). If 10000 is given, the progress |
| 3874 | * bar will be completely filled and will fade out. |
| 3875 | */ |
| 3876 | public final void setProgress(int progress) { |
| 3877 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress + Window.PROGRESS_START); |
| 3878 | } |
| 3879 | |
| 3880 | /** |
| 3881 | * Sets the secondary progress for the progress bar in the title. This |
| 3882 | * progress is drawn between the primary progress (set via |
| 3883 | * {@link #setProgress(int)} and the background. It can be ideal for media |
| 3884 | * scenarios such as showing the buffering progress while the default |
| 3885 | * progress shows the play progress. |
| 3886 | * <p> |
| 3887 | * In order for the progress bar to be shown, the feature must be requested |
| 3888 | * via {@link #requestWindowFeature(int)}. |
| 3889 | * |
| 3890 | * @param secondaryProgress The secondary progress for the progress bar. Valid ranges are from |
| 3891 | * 0 to 10000 (both inclusive). |
| 3892 | */ |
| 3893 | public final void setSecondaryProgress(int secondaryProgress) { |
| 3894 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, |
| 3895 | secondaryProgress + Window.PROGRESS_SECONDARY_START); |
| 3896 | } |
| 3897 | |
| 3898 | /** |
| 3899 | * Suggests an audio stream whose volume should be changed by the hardware |
| 3900 | * volume controls. |
| 3901 | * <p> |
| 3902 | * The suggested audio stream will be tied to the window of this Activity. |
| 3903 | * If the Activity is switched, the stream set here is no longer the |
| 3904 | * suggested stream. The client does not need to save and restore the old |
| 3905 | * suggested stream value in onPause and onResume. |
| 3906 | * |
| 3907 | * @param streamType The type of the audio stream whose volume should be |
| 3908 | * changed by the hardware volume controls. It is not guaranteed that |
| 3909 | * the hardware volume controls will always change this stream's |
| 3910 | * volume (for example, if a call is in progress, its stream's volume |
| 3911 | * may be changed instead). To reset back to the default, use |
| 3912 | * {@link AudioManager#USE_DEFAULT_STREAM_TYPE}. |
| 3913 | */ |
| 3914 | public final void setVolumeControlStream(int streamType) { |
| 3915 | getWindow().setVolumeControlStream(streamType); |
| 3916 | } |
| 3917 | |
| 3918 | /** |
| 3919 | * Gets the suggested audio stream whose volume should be changed by the |
| 3920 | * harwdare volume controls. |
| 3921 | * |
| 3922 | * @return The suggested audio stream type whose volume should be changed by |
| 3923 | * the hardware volume controls. |
| 3924 | * @see #setVolumeControlStream(int) |
| 3925 | */ |
| 3926 | public final int getVolumeControlStream() { |
| 3927 | return getWindow().getVolumeControlStream(); |
| 3928 | } |
| 3929 | |
| 3930 | /** |
| 3931 | * Runs the specified action on the UI thread. If the current thread is the UI |
| 3932 | * thread, then the action is executed immediately. If the current thread is |
| 3933 | * not the UI thread, the action is posted to the event queue of the UI thread. |
| 3934 | * |
| 3935 | * @param action the action to run on the UI thread |
| 3936 | */ |
| 3937 | public final void runOnUiThread(Runnable action) { |
| 3938 | if (Thread.currentThread() != mUiThread) { |
| 3939 | mHandler.post(action); |
| 3940 | } else { |
| 3941 | action.run(); |
| 3942 | } |
| 3943 | } |
| 3944 | |
| 3945 | /** |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 3946 | * Standard implementation of |
| 3947 | * {@link android.view.LayoutInflater.Factory#onCreateView} used when |
| 3948 | * inflating with the LayoutInflater returned by {@link #getSystemService}. |
| 3949 | * This implementation handles <fragment> tags to embed fragments inside |
| 3950 | * of the activity. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3951 | * |
| 3952 | * @see android.view.LayoutInflater#createView |
| 3953 | * @see android.view.Window#getLayoutInflater |
| 3954 | */ |
| 3955 | public View onCreateView(String name, Context context, AttributeSet attrs) { |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 3956 | if (!"fragment".equals(name)) { |
| 3957 | return null; |
| 3958 | } |
| 3959 | |
| 3960 | TypedArray a = |
| 3961 | context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Fragment); |
| 3962 | String fname = a.getString(com.android.internal.R.styleable.Fragment_name); |
| 3963 | int id = a.getInt(com.android.internal.R.styleable.Fragment_id, 0); |
| 3964 | String tag = a.getString(com.android.internal.R.styleable.Fragment_tag); |
| 3965 | a.recycle(); |
| 3966 | |
| 3967 | Constructor constructor = sConstructorMap.get(fname); |
| 3968 | Class clazz = null; |
| 3969 | |
| 3970 | try { |
| 3971 | if (constructor == null) { |
| 3972 | // Class not found in the cache, see if it's real, and try to add it |
| 3973 | clazz = getClassLoader().loadClass(fname); |
| 3974 | constructor = clazz.getConstructor(sConstructorSignature); |
| 3975 | sConstructorMap.put(fname, constructor); |
| 3976 | } |
| 3977 | Fragment fragment = (Fragment)constructor.newInstance(sConstructorArgs); |
| 3978 | fragment.onInflate(this, attrs); |
| 3979 | mFragments.addFragment(fragment, true); |
| 3980 | if (fragment.mView == null) { |
| 3981 | throw new IllegalStateException("Fragment " + fname |
| 3982 | + " did not create a view."); |
| 3983 | } |
| 3984 | return fragment.mView; |
| 3985 | |
| 3986 | } catch (NoSuchMethodException e) { |
| 3987 | InflateException ie = new InflateException(attrs.getPositionDescription() |
| 3988 | + ": Error inflating class " + fname); |
| 3989 | ie.initCause(e); |
| 3990 | throw ie; |
| 3991 | |
| 3992 | } catch (ClassNotFoundException e) { |
| 3993 | // If loadClass fails, we should propagate the exception. |
| 3994 | throw new RuntimeException(e); |
| 3995 | } catch (Exception e) { |
| 3996 | InflateException ie = new InflateException(attrs.getPositionDescription() |
| 3997 | + ": Error inflating class " |
| 3998 | + (clazz == null ? "<unknown>" : clazz.getName())); |
| 3999 | ie.initCause(e); |
| 4000 | throw new RuntimeException(ie); |
| 4001 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4002 | } |
| 4003 | |
| 4004 | // ------------------ Internal API ------------------ |
| 4005 | |
| 4006 | final void setParent(Activity parent) { |
| 4007 | mParent = parent; |
| 4008 | } |
| 4009 | |
| 4010 | final void attach(Context context, ActivityThread aThread, Instrumentation instr, IBinder token, |
| 4011 | Application application, Intent intent, ActivityInfo info, CharSequence title, |
| 4012 | Activity parent, String id, Object lastNonConfigurationInstance, |
| 4013 | Configuration config) { |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 4014 | attach(context, aThread, instr, token, 0, application, intent, info, title, parent, id, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4015 | lastNonConfigurationInstance, null, config); |
| 4016 | } |
| 4017 | |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 4018 | final void attach(Context context, ActivityThread aThread, |
| 4019 | Instrumentation instr, IBinder token, int ident, |
| 4020 | Application application, Intent intent, ActivityInfo info, |
| 4021 | CharSequence title, Activity parent, String id, |
| 4022 | Object lastNonConfigurationInstance, |
| 4023 | HashMap<String,Object> lastNonConfigurationChildInstances, |
| 4024 | Configuration config) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4025 | attachBaseContext(context); |
| 4026 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 4027 | mFragments.attachActivity(this); |
| 4028 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4029 | mWindow = PolicyManager.makeNewWindow(this); |
| 4030 | mWindow.setCallback(this); |
Dianne Hackborn | ba51c3d | 2010-05-05 18:49:48 -0700 | [diff] [blame] | 4031 | mWindow.getLayoutInflater().setFactory(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4032 | if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) { |
| 4033 | mWindow.setSoftInputMode(info.softInputMode); |
| 4034 | } |
| 4035 | mUiThread = Thread.currentThread(); |
| 4036 | |
| 4037 | mMainThread = aThread; |
| 4038 | mInstrumentation = instr; |
| 4039 | mToken = token; |
Dianne Hackborn | b06ea70 | 2009-07-13 13:07:51 -0700 | [diff] [blame] | 4040 | mIdent = ident; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4041 | mApplication = application; |
| 4042 | mIntent = intent; |
| 4043 | mComponent = intent.getComponent(); |
| 4044 | mActivityInfo = info; |
| 4045 | mTitle = title; |
| 4046 | mParent = parent; |
| 4047 | mEmbeddedID = id; |
| 4048 | mLastNonConfigurationInstance = lastNonConfigurationInstance; |
| 4049 | mLastNonConfigurationChildInstances = lastNonConfigurationChildInstances; |
| 4050 | |
| 4051 | mWindow.setWindowManager(null, mToken, mComponent.flattenToString()); |
| 4052 | if (mParent != null) { |
| 4053 | mWindow.setContainer(mParent.getWindow()); |
| 4054 | } |
| 4055 | mWindowManager = mWindow.getWindowManager(); |
| 4056 | mCurrentConfig = config; |
| 4057 | } |
| 4058 | |
| 4059 | final IBinder getActivityToken() { |
| 4060 | return mParent != null ? mParent.getActivityToken() : mToken; |
| 4061 | } |
| 4062 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 4063 | final void performCreate(Bundle icicle) { |
| 4064 | onCreate(icicle); |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 4065 | } |
| 4066 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4067 | final void performStart() { |
| 4068 | mCalled = false; |
| 4069 | mInstrumentation.callActivityOnStart(this); |
| 4070 | if (!mCalled) { |
| 4071 | throw new SuperNotCalledException( |
| 4072 | "Activity " + mComponent.toShortString() + |
| 4073 | " did not call through to super.onStart()"); |
| 4074 | } |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 4075 | mFragments.dispatchStart(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4076 | } |
| 4077 | |
| 4078 | final void performRestart() { |
Makoto Onuki | 2f6a018 | 2010-02-22 13:26:59 -0800 | [diff] [blame] | 4079 | synchronized (mManagedCursors) { |
| 4080 | final int N = mManagedCursors.size(); |
| 4081 | for (int i=0; i<N; i++) { |
| 4082 | ManagedCursor mc = mManagedCursors.get(i); |
| 4083 | if (mc.mReleased || mc.mUpdated) { |
| 4084 | mc.mCursor.requery(); |
| 4085 | mc.mReleased = false; |
| 4086 | mc.mUpdated = false; |
| 4087 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4088 | } |
| 4089 | } |
| 4090 | |
| 4091 | if (mStopped) { |
| 4092 | mStopped = false; |
| 4093 | mCalled = false; |
| 4094 | mInstrumentation.callActivityOnRestart(this); |
| 4095 | if (!mCalled) { |
| 4096 | throw new SuperNotCalledException( |
| 4097 | "Activity " + mComponent.toShortString() + |
| 4098 | " did not call through to super.onRestart()"); |
| 4099 | } |
| 4100 | performStart(); |
| 4101 | } |
| 4102 | } |
| 4103 | |
| 4104 | final void performResume() { |
| 4105 | performRestart(); |
| 4106 | |
| 4107 | mLastNonConfigurationInstance = null; |
| 4108 | |
| 4109 | // First call onResume() -before- setting mResumed, so we don't |
| 4110 | // send out any status bar / menu notifications the client makes. |
| 4111 | mCalled = false; |
| 4112 | mInstrumentation.callActivityOnResume(this); |
| 4113 | if (!mCalled) { |
| 4114 | throw new SuperNotCalledException( |
| 4115 | "Activity " + mComponent.toShortString() + |
| 4116 | " did not call through to super.onResume()"); |
| 4117 | } |
| 4118 | |
| 4119 | // Now really resume, and install the current status bar and menu. |
| 4120 | mResumed = true; |
| 4121 | mCalled = false; |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 4122 | |
| 4123 | mFragments.dispatchResume(); |
| 4124 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4125 | onPostResume(); |
| 4126 | if (!mCalled) { |
| 4127 | throw new SuperNotCalledException( |
| 4128 | "Activity " + mComponent.toShortString() + |
| 4129 | " did not call through to super.onPostResume()"); |
| 4130 | } |
| 4131 | } |
| 4132 | |
| 4133 | final void performPause() { |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 4134 | mFragments.dispatchPause(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4135 | onPause(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4136 | } |
| 4137 | |
| 4138 | final void performUserLeaving() { |
| 4139 | onUserInteraction(); |
| 4140 | onUserLeaveHint(); |
| 4141 | } |
| 4142 | |
| 4143 | final void performStop() { |
| 4144 | if (!mStopped) { |
| 4145 | if (mWindow != null) { |
| 4146 | mWindow.closeAllPanels(); |
| 4147 | } |
| 4148 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 4149 | mFragments.dispatchStop(); |
| 4150 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4151 | mCalled = false; |
| 4152 | mInstrumentation.callActivityOnStop(this); |
| 4153 | if (!mCalled) { |
| 4154 | throw new SuperNotCalledException( |
| 4155 | "Activity " + mComponent.toShortString() + |
| 4156 | " did not call through to super.onStop()"); |
| 4157 | } |
| 4158 | |
Makoto Onuki | 2f6a018 | 2010-02-22 13:26:59 -0800 | [diff] [blame] | 4159 | synchronized (mManagedCursors) { |
| 4160 | final int N = mManagedCursors.size(); |
| 4161 | for (int i=0; i<N; i++) { |
| 4162 | ManagedCursor mc = mManagedCursors.get(i); |
| 4163 | if (!mc.mReleased) { |
| 4164 | mc.mCursor.deactivate(); |
| 4165 | mc.mReleased = true; |
| 4166 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4167 | } |
| 4168 | } |
| 4169 | |
| 4170 | mStopped = true; |
| 4171 | } |
| 4172 | mResumed = false; |
| 4173 | } |
| 4174 | |
Dianne Hackborn | 2dedce6 | 2010-04-15 14:45:25 -0700 | [diff] [blame] | 4175 | final void performDestroy() { |
| 4176 | mFragments.dispatchDestroy(); |
| 4177 | onDestroy(); |
| 4178 | } |
| 4179 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4180 | final boolean isResumed() { |
| 4181 | return mResumed; |
| 4182 | } |
| 4183 | |
| 4184 | void dispatchActivityResult(String who, int requestCode, |
| 4185 | int resultCode, Intent data) { |
| 4186 | if (Config.LOGV) Log.v( |
| 4187 | TAG, "Dispatching result: who=" + who + ", reqCode=" + requestCode |
| 4188 | + ", resCode=" + resultCode + ", data=" + data); |
| 4189 | if (who == null) { |
| 4190 | onActivityResult(requestCode, resultCode, data); |
| 4191 | } |
| 4192 | } |
| 4193 | } |