ActionBarActivity.java revision ed34e2dffd0ce7da89063d06fd3b25687ca367a2
1/*
2 * Copyright (C) 2012 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
17package android.support.v7.app;
18
19import android.app.Activity;
20import android.content.res.Configuration;
21import android.os.Build;
22import android.os.Bundle;
23import android.support.v4.app.FragmentActivity;
24import android.support.v4.view.WindowCompat;
25import android.support.v7.view.ActionMode;
26import android.support.v7.view.MenuInflater;
27import android.support.v7.view.MenuItem;
28import android.view.View;
29import android.view.ViewGroup;
30import android.view.Window;
31
32/**
33 * Base class for activities that use the support library action bar features.
34 */
35public class ActionBarActivity extends FragmentActivity implements ActionBar.Callback {
36    ActionBarActivityDelegate mImpl;
37
38    /**
39     * Support library version of {@link Activity#getActionBar}.
40     *
41     * <p>Retrieve a reference to this activity's ActionBar.
42     *
43     * @return The Activity's ActionBar, or null if it does not have one.
44     */
45    public ActionBar getSupportActionBar() {
46        return mImpl.getSupportActionBar();
47    }
48
49    /**
50     * Support library version of {@link Activity#getMenuInflater}.
51     *
52     * <p>Returns a {@link MenuInflater} with this context.
53     *
54     * @return The Activity's menu inflater.
55     */
56    public MenuInflater getSupportMenuInflater() {
57        return mImpl.getSupportMenuInflater();
58    }
59
60    @Override
61    public void setContentView(int layoutResID) {
62        mImpl.setContentView(layoutResID);
63    }
64
65    @Override
66    public void setContentView(View view) {
67        mImpl.setContentView(view);
68    }
69
70    @Override
71    public void setContentView(View view, ViewGroup.LayoutParams params) {
72        mImpl.setContentView(view, params);
73    }
74
75    @Override
76    protected void onCreate(Bundle savedInstanceState) {
77        mImpl = ActionBarActivityDelegate.createDelegate(this);
78        super.onCreate(savedInstanceState);
79        mImpl.onCreate(savedInstanceState);
80    }
81
82    @Override
83    public void onConfigurationChanged(Configuration newConfig) {
84        super.onConfigurationChanged(newConfig);
85        mImpl.onConfigurationChanged(newConfig);
86    }
87
88    @Override
89    public boolean onCreatePanelMenu(int featureId, android.view.Menu frameworkMenu) {
90        return mImpl.onCreatePanelMenu(featureId, frameworkMenu);
91    }
92
93    @Override
94    public boolean onPreparePanel(int featureId, View view, android.view.Menu menu) {
95        return mImpl.onPreparePanel(featureId, view, menu);
96    }
97
98    @Override
99    public View onCreatePanelView(int featureId) {
100        if (featureId == Window.FEATURE_OPTIONS_PANEL)
101            return mImpl.onCreatePanelView(featureId);
102        else {
103            return super.onCreatePanelView(featureId);
104        }
105    }
106
107    @Override
108    public boolean onMenuItemSelected(int featureId, android.view.MenuItem item) {
109        if (mImpl.onMenuItemSelected(featureId, item)) {
110            return true;
111        }
112        return super.onMenuItemSelected(featureId, item);
113    }
114
115    @Override
116    protected void onTitleChanged(CharSequence title, int color) {
117        super.onTitleChanged(title, color);
118        mImpl.setTitle(title);
119    }
120
121    /**
122     * Enable extended support library window features.
123     * <p>
124     * This is a convenience for calling
125     * {@link android.view.Window#requestFeature getWindow().requestFeature()}.
126     * </p>
127     *
128     * @param featureId The desired feature as defined in
129     * {@link android.view.Window} or {@link WindowCompat}.
130     * @return Returns true if the requested feature is supported and now enabled.
131     *
132     * @see android.app.Activity#requestWindowFeature
133     * @see android.view.Window#requestFeature
134     */
135    public boolean supportRequestWindowFeature(int featureId) {
136        return mImpl.supportRequestWindowFeature(featureId);
137    }
138
139    @Override
140    public void supportInvalidateOptionsMenu() {
141        // Only call up to super on HC+
142        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
143            super.supportInvalidateOptionsMenu();
144        }
145        mImpl.supportInvalidateOptionsMenu();
146    }
147
148    @Override
149    public final void onActionModeFinished(android.view.ActionMode mode) {
150        mImpl.onActionModeFinished(mode);
151    }
152
153    @Override
154    public final void onActionModeStarted(android.view.ActionMode mode) {
155        mImpl.onActionModeStarted(mode);
156    }
157
158    /**
159     * Notifies the Activity that a support action mode has been started.
160     * Activity subclasses overriding this method should call the superclass implementation.
161     *
162     * @param mode The new action mode.
163     */
164    public void onSupportActionModeStarted(ActionMode mode) {
165    }
166
167    /**
168     * Notifies the activity that a support action mode has finished.
169     * Activity subclasses overriding this method should call the superclass implementation.
170     *
171     * @param mode The action mode that just finished.
172     */
173    public void onSupportActionModeFinished(ActionMode mode) {
174    }
175
176    /**
177     * Support library version of {@link Activity#onPrepareOptionsMenu}.
178     *
179     * <p>Prepare the Screen's standard options menu to be displayed.  This is
180     * called right before the menu is shown, every time it is shown.  You can
181     * use this method to efficiently enable/disable items or otherwise
182     * dynamically modify the contents.
183     *
184     * <p>The default implementation updates the system menu items based on the
185     * activity's state.  Deriving classes should always call through to the
186     * base class implementation.
187     *
188     * @param menu The options menu as last shown or first initialized by
189     *             onCreateSupportOptionsMenu().
190     *
191     * @return You must return true for the menu to be displayed;
192     *         if you return false it will not be shown.
193     *
194     * @see #onCreateSupportOptionsMenu
195     */
196    public boolean onPrepareSupportOptionsMenu(android.support.v7.view.Menu menu) {
197        return true;
198    }
199
200    /**
201     * Support library version of {@link Activity#onCreateOptionsMenu}.
202     *
203     * <p>Initialize the contents of the Activity's standard options menu.  You
204     * should place your menu items in to <var>menu</var>.
205     *
206     * <p>This is only called once, the first time the options menu is
207     * displayed.  To update the menu every time it is displayed, see
208     * {@link #onPrepareSupportOptionsMenu}.
209     *
210     * <p>The default implementation populates the menu with standard system
211     * menu items.  These are placed in the {@link android.support.v7.view.Menu#CATEGORY_SYSTEM}
212     * group so that they will be correctly ordered with application-defined menu items.
213     * Deriving classes should always call through to the base implementation.
214     *
215     * <p>You can safely hold on to <var>menu</var> (and any items created
216     * from it), making modifications to it as desired, until the next
217     * time onCreateSupportOptionsMenu() is called.
218     *
219     * <p>When you add items to the menu, you can implement the Activity's
220     * {@link #onSupportOptionsItemSelected} method to handle them there.
221     *
222     * @param menu The options menu in which you place your items.
223     *
224     * @return You must return true for the menu to be displayed;
225     *         if you return false it will not be shown.
226     *
227     * @see #onPrepareSupportOptionsMenu
228     * @see #onSupportOptionsItemSelected
229     */
230    public boolean onCreateSupportOptionsMenu(android.support.v7.view.Menu menu) {
231        return true;
232    }
233
234    /**
235     * Support library version of {@link Activity#onOptionsItemSelected}.
236     *
237     * <p>This hook is called whenever an item in your options menu is selected.
238     * The default implementation simply returns false to have the normal
239     * processing happen (calling the item's Runnable or sending a message to
240     * its Handler as appropriate).  You can use this method for any items
241     * for which you would like to do processing without those other
242     * facilities.
243     *
244     * <p>Derived classes should call through to the base class for it to
245     * perform the default menu handling.</p>
246     *
247     * @param item The menu item that was selected.
248     *
249     * @return boolean Return false to allow normal menu processing to
250     *         proceed, true to consume it here.
251     *
252     * @see #onPrepareSupportOptionsMenu
253     * @see #onCreateSupportOptionsMenu
254     */
255    public boolean onSupportOptionsItemSelected(android.support.v7.view.MenuItem item) {
256        return false;
257    }
258
259    /**
260     * Support library version of {@link Activity#onMenuItemSelected}.
261     *
262     * <p>Default implementation of {@link android.view.Window.Callback#onMenuItemSelected}
263     * for activities.  This calls through to the new {@link #onSupportOptionsItemSelected}
264     * method for the {@link android.view.Window#FEATURE_OPTIONS_PANEL}
265     * panel, so that subclasses of Activity don't need to deal with feature codes.
266     */
267    public boolean onSupportMenuItemSelected(int featureId, MenuItem item) {
268        return false;
269    }
270
271    public ActionMode startSupportActionMode(ActionMode.Callback callback) {
272        return mImpl.startSupportActionMode(callback);
273    }
274
275    void superSetContentView(int resId) {
276        super.setContentView(resId);
277    }
278
279    void superSetContentView(View v) {
280        super.setContentView(v);
281    }
282
283    void superSetContentView(View v, ViewGroup.LayoutParams lp) {
284        super.setContentView(v, lp);
285    }
286
287    void superAddContentView(View v, ViewGroup.LayoutParams lp) {
288        super.addContentView(v, lp);
289    }
290
291    boolean superOnCreatePanelMenu(int featureId, android.view.Menu frameworkMenu) {
292        return super.onCreatePanelMenu(featureId, frameworkMenu);
293    }
294
295    boolean superOnPreparePanelMenu(int featureId, View view, android.view.Menu menu) {
296        return super.onPreparePanel(featureId, view, menu);
297    }
298
299    @Override
300    public void onBackPressed() {
301        if (!mImpl.onBackPressed()) {
302            super.onBackPressed();
303        }
304    }
305
306    /**
307     * Support library version of {@link Activity#setProgressBarVisibility(boolean)}
308     * <p>
309     * Sets the visibility of the progress bar in the title.
310     * <p>
311     * In order for the progress bar to be shown, the feature must be requested
312     * via {@link #supportRequestWindowFeature(int)}.
313     *
314     * @param visible Whether to show the progress bars in the title.
315     */
316    public void setSupportProgressBarVisibility(boolean visible) {
317        mImpl.setSupportProgressBarVisibility(visible);
318    }
319
320    /**
321     * Support library version of {@link Activity#setProgressBarIndeterminateVisibility(boolean)}
322     * <p>
323     * Sets the visibility of the indeterminate progress bar in the title.
324     * <p>
325     * In order for the progress bar to be shown, the feature must be requested
326     * via {@link #supportRequestWindowFeature(int)}.
327     *
328     * @param visible Whether to show the progress bars in the title.
329     */
330    public void setSupportProgressBarIndeterminateVisibility(boolean visible) {
331        mImpl.setSupportProgressBarIndeterminateVisibility(visible);
332    }
333
334    /**
335     * Support library version of {@link Activity#setProgressBarIndeterminate(boolean)}
336     * <p>
337     * Sets whether the horizontal progress bar in the title should be indeterminate (the circular
338     * is always indeterminate).
339     * <p>
340     * In order for the progress bar to be shown, the feature must be requested
341     * via {@link #supportRequestWindowFeature(int)}.
342     *
343     * @param indeterminate Whether the horizontal progress bar should be indeterminate.
344     */
345    public void setSupportProgressBarIndeterminate(boolean indeterminate) {
346        mImpl.setSupportProgressBarIndeterminate(indeterminate);
347    }
348
349    /**
350     * Support library version of {@link Activity#setProgress(int)}.
351     * <p>
352     * Sets the progress for the progress bars in the title.
353     * <p>
354     * In order for the progress bar to be shown, the feature must be requested
355     * via {@link #supportRequestWindowFeature(int)}.
356     *
357     * @param progress The progress for the progress bar. Valid ranges are from
358     *            0 to 10000 (both inclusive). If 10000 is given, the progress
359     *            bar will be completely filled and will fade out.
360     */
361    public void setSupportProgress(int progress) {
362        mImpl.setSupportProgress(progress);
363    }
364
365}
366