Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2013 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.support.v7.app;
     18 
     19 import android.content.Context;
     20 import android.content.pm.ActivityInfo;
     21 import android.content.pm.PackageManager;
     22 import android.content.res.Configuration;
     23 import android.content.res.TypedArray;
     24 import android.os.Build;
     25 import android.os.Bundle;
     26 import android.support.v4.app.ActionBarDrawerToggle;
     27 import android.support.v7.appcompat.R;
     28 import android.support.v7.internal.view.SupportMenuInflater;
     29 import android.support.v7.view.ActionMode;
     30 import android.util.Log;
     31 import android.view.Menu;
     32 import android.view.MenuInflater;
     33 import android.view.MenuItem;
     34 import android.view.View;
     35 import android.view.ViewGroup;
     36 
     37 abstract class ActionBarActivityDelegate {
     38 
     39     static final String METADATA_UI_OPTIONS = "android.support.UI_OPTIONS";
     40     static final String UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW = "splitActionBarWhenNarrow";
     41 
     42     private static final String TAG = "ActionBarActivityDelegate";
     43 
     44     static ActionBarActivityDelegate createDelegate(ActionBarActivity activity) {
     45         final int version = Build.VERSION.SDK_INT;
     46         if (version >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
     47             return new ActionBarActivityDelegateICS(activity);
     48         } else if (version >= Build.VERSION_CODES.HONEYCOMB) {
     49             return new ActionBarActivityDelegateHC(activity);
     50         } else {
     51             return new ActionBarActivityDelegateBase(activity);
     52         }
     53     }
     54 
     55     final ActionBarActivity mActivity;
     56 
     57     private ActionBar mActionBar;
     58     private MenuInflater mMenuInflater;
     59 
     60     // true if this activity has an action bar.
     61     boolean mHasActionBar;
     62     // true if this activity's action bar overlays other activity content.
     63     boolean mOverlayActionBar;
     64 
     65     ActionBarActivityDelegate(ActionBarActivity activity) {
     66         mActivity = activity;
     67     }
     68 
     69     abstract ActionBar createSupportActionBar();
     70 
     71     final ActionBar getSupportActionBar() {
     72         // The Action Bar should be lazily created as mHasActionBar or mOverlayActionBar
     73         // could change after onCreate
     74         if (mHasActionBar || mOverlayActionBar) {
     75             if (mActionBar == null) {
     76                 mActionBar = createSupportActionBar();
     77             }
     78         } else {
     79             // If we're not set to have a Action Bar, null it just in case it's been set
     80             mActionBar = null;
     81         }
     82         return mActionBar;
     83     }
     84 
     85     MenuInflater getMenuInflater() {
     86         if (mMenuInflater == null) {
     87             ActionBar ab = getSupportActionBar();
     88             if (ab != null) {
     89                 mMenuInflater = new SupportMenuInflater(ab.getThemedContext());
     90             } else {
     91                 mMenuInflater = new SupportMenuInflater(mActivity);
     92             }
     93         }
     94         return mMenuInflater;
     95     }
     96 
     97     void onCreate(Bundle savedInstanceState) {
     98         TypedArray a = mActivity.obtainStyledAttributes(R.styleable.ActionBarWindow);
     99 
    100         if (!a.hasValue(R.styleable.ActionBarWindow_windowActionBar)) {
    101             a.recycle();
    102             throw new IllegalStateException(
    103                     "You need to use a Theme.AppCompat theme (or descendant) with this activity.");
    104         }
    105 
    106         mHasActionBar = a.getBoolean(R.styleable.ActionBarWindow_windowActionBar, false);
    107         mOverlayActionBar = a.getBoolean(R.styleable.ActionBarWindow_windowActionBarOverlay, false);
    108         a.recycle();
    109     }
    110 
    111     abstract void onConfigurationChanged(Configuration newConfig);
    112 
    113     abstract void onStop();
    114 
    115     abstract void onPostResume();
    116 
    117     abstract void setContentView(View v);
    118 
    119     abstract void setContentView(int resId);
    120 
    121     abstract void setContentView(View v, ViewGroup.LayoutParams lp);
    122 
    123     abstract void addContentView(View v, ViewGroup.LayoutParams lp);
    124 
    125     abstract void onTitleChanged(CharSequence title);
    126 
    127     abstract void supportInvalidateOptionsMenu();
    128 
    129     abstract boolean supportRequestWindowFeature(int featureId);
    130 
    131     // Methods used to create and respond to options menu
    132     abstract View onCreatePanelView(int featureId);
    133 
    134     abstract boolean onPreparePanel(int featureId, View view, Menu menu);
    135 
    136     abstract boolean onCreatePanelMenu(int featureId, Menu menu);
    137 
    138     abstract boolean onMenuItemSelected(int featureId, MenuItem item);
    139 
    140     abstract boolean onBackPressed();
    141 
    142     abstract ActionMode startSupportActionMode(ActionMode.Callback callback);
    143 
    144     abstract void setSupportProgressBarVisibility(boolean visible);
    145 
    146     abstract void setSupportProgressBarIndeterminateVisibility(boolean visible);
    147 
    148     abstract void setSupportProgressBarIndeterminate(boolean indeterminate);
    149 
    150     abstract void setSupportProgress(int progress);
    151 
    152     abstract ActionBarDrawerToggle.Delegate getDrawerToggleDelegate();
    153 
    154     protected final String getUiOptionsFromMetadata() {
    155         try {
    156             PackageManager pm = mActivity.getPackageManager();
    157             ActivityInfo info = pm.getActivityInfo(mActivity.getComponentName(),
    158                     PackageManager.GET_META_DATA);
    159 
    160             String uiOptions = null;
    161             if (info.metaData != null) {
    162                 uiOptions = info.metaData.getString(METADATA_UI_OPTIONS);
    163             }
    164             return uiOptions;
    165         } catch (PackageManager.NameNotFoundException e) {
    166             Log.e(TAG, "getUiOptionsFromMetadata: Activity '" + mActivity.getClass()
    167                     .getSimpleName() + "' not in manifest");
    168             return null;
    169         }
    170     }
    171 
    172     protected final Context getActionBarThemedContext() {
    173         Context context = mActivity;
    174 
    175         // If we have an action bar, initialize the menu with a context themed from it.
    176         ActionBar ab = getSupportActionBar();
    177         if (ab != null) {
    178             context = ab.getThemedContext();
    179         }
    180         return context;
    181     }
    182 }
    183