Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2011 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 com.example.android.apis.app;
     18 
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.provider.Settings;
     23 import android.view.ActionProvider;
     24 import android.view.LayoutInflater;
     25 import android.view.Menu;
     26 import android.view.MenuItem;
     27 import android.view.View;
     28 import android.widget.ImageButton;
     29 import android.widget.Toast;
     30 
     31 import com.example.android.apis.R;
     32 
     33 /**
     34  * This activity demonstrates how to implement an {@link android.view.ActionProvider}
     35  * for adding functionality to the Action Bar. In particular this demo creates an
     36  * ActionProvider for launching the system settings and adds a menu item with that
     37  * provider.
     38  */
     39 public class ActionBarSettingsActionProviderActivity extends Activity {
     40 
     41     /**
     42      * {@inheritDoc}
     43      */
     44     @Override
     45     public boolean onCreateOptionsMenu(Menu menu) {
     46         super.onCreateOptionsMenu(menu);
     47         getMenuInflater().inflate(R.menu.action_bar_settings_action_provider, menu);
     48         return true;
     49     }
     50 
     51     /**
     52      * {@inheritDoc}
     53      */
     54     @Override
     55     public boolean onOptionsItemSelected(MenuItem item) {
     56         // If this callback does not handle the item click, onPerformDefaultAction
     57         // of the ActionProvider is invoked. Hence, the provider encapsulates the
     58         // complete functionality of the menu item.
     59         Toast.makeText(this, R.string.action_bar_settings_action_provider_no_handling,
     60                 Toast.LENGTH_SHORT).show();
     61         return false;
     62     }
     63 
     64     public static class SettingsActionProvider extends ActionProvider {
     65 
     66         /** An intent for launching the system settings. */
     67         private static final Intent sSettingsIntent = new Intent(Settings.ACTION_SETTINGS);
     68 
     69         /** Context for accessing resources. */
     70         private final Context mContext;
     71 
     72         /**
     73          * Creates a new instance.
     74          *
     75          * @param context Context for accessing resources.
     76          */
     77         public SettingsActionProvider(Context context) {
     78             super(context);
     79             mContext = context;
     80         }
     81 
     82         /**
     83          * {@inheritDoc}
     84          */
     85         @Override
     86         public View onCreateActionView() {
     87             // Inflate the action view to be shown on the action bar.
     88             LayoutInflater layoutInflater = LayoutInflater.from(mContext);
     89             View view = layoutInflater.inflate(R.layout.action_bar_settings_action_provider, null);
     90             ImageButton button = (ImageButton) view.findViewById(R.id.button);
     91             // Attach a click listener for launching the system settings.
     92             button.setOnClickListener(new View.OnClickListener() {
     93                 @Override
     94                 public void onClick(View v) {
     95                     mContext.startActivity(sSettingsIntent);
     96                 }
     97             });
     98             return view;
     99         }
    100 
    101         /**
    102          * {@inheritDoc}
    103          */
    104         @Override
    105         public boolean onPerformDefaultAction() {
    106             // This is called if the host menu item placed in the overflow menu of the
    107             // action bar is clicked and the host activity did not handle the click.
    108             mContext.startActivity(sSettingsIntent);
    109             return true;
    110         }
    111     }
    112 }
    113