Home | History | Annotate | Download | only in quickaction
      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 
     17 package #package_name#;
     18 
     19 import android.app.Activity;
     20 import android.graphics.drawable.BitmapDrawable;
     21 import android.os.Bundle;
     22 import android.util.TypedValue;
     23 import android.view.MotionEvent;
     24 import android.view.View;
     25 import android.view.View.OnClickListener;
     26 import android.view.View.OnTouchListener;
     27 import android.view.ViewGroup;
     28 import android.view.ViewGroup.LayoutParams;
     29 import android.view.animation.Animation;
     30 import android.view.animation.AnimationUtils;
     31 import android.widget.ImageView;
     32 import android.widget.PopupWindow;
     33 import android.widget.TextView;
     34 import android.widget.Toast;
     35 
     36 import #ManifestPackageName#.R;
     37 
     38 public class #class_name# extends Activity {
     39 
     40     /*
     41      * Pixels added to QuickAction window growing window area to avoid fading edges
     42      */
     43     private static final int MARGIN_ADJUSTMENT = 8;
     44 
     45     /*
     46      * Pixels added to QuickAction window growing window area to avoid fading edges
     47      */
     48     private static final int ACTION_ITEM_SIZE = 64;
     49 
     50     /*
     51      * The current quick action window (to dismiss on application pause/rotation)
     52      */
     53     private PopupWindow currentQuickActionWindow = null;
     54 
     55 	@Override
     56 	public void onCreate(Bundle savedInstanceState) {
     57 		super.onCreate(savedInstanceState);
     58 		setContentView(R.layout.#layout_name#quickaction/quick_action_activity.xml#);
     59 	}
     60 
     61 	/**
     62 	 * Create an action item
     63 	 * @param name the action name
     64 	 * @param imageResId the image ID to be set in the action item
     65 	 * @return
     66 	 */
     67 	private View createAction (final String name, int imageResId) {
     68 		View action = getLayoutInflater().inflate(R.layout.#layout_name#quickaction/quick_action_item.xml#, null);
     69 		ImageView image = (ImageView) action.findViewById(R.id.action_image);
     70 		TextView text = (TextView) action.findViewById(R.id.action_name);
     71 		text.setText(name);
     72 
     73 		/*
     74 		 * Set image dimensions (Density Independent Pixels)
     75 		 */
     76 		action.setMinimumHeight((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, ACTION_ITEM_SIZE, getResources().getDisplayMetrics()));
     77 		action.setMinimumWidth((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, ACTION_ITEM_SIZE, getResources().getDisplayMetrics()));
     78 		image.setImageResource(imageResId);
     79 
     80 		/*
     81 		 *	Set action being performed when item action is clicked
     82 		 */
     83 		action.setOnClickListener(new OnClickListener() {
     84 			public void onClick(View v) {
     85 			    /*
     86 			     * TODO: Place your action code here
     87 			     */
     88 				Toast.makeText(#class_name#.this, "You have selected " + name , Toast.LENGTH_SHORT).show();
     89 			}
     90 		});
     91 		return action;
     92 	}
     93 
     94 	public void openQuickActionWindow(View target) {
     95 		/*
     96 		 * Get the layout of quick action window
     97 		 */
     98 		ViewGroup quickActionLayout = (ViewGroup) getLayoutInflater()
     99 				.inflate(R.layout.#layout_name#quickaction/quick_action_grid.xml#, null);
    100 
    101 		/*
    102 		 * Get the quick action bar
    103 		 */
    104 		ViewGroup quickActionBar = (ViewGroup)quickActionLayout.findViewById(R.id.quick_action_bar);
    105 
    106 		/*
    107 		 * Add the actions (change to your own needs)
    108 		 */
    109 		quickActionBar.addView(createAction("Action 1", R.drawable.#drawable_name#quickaction/icon.png#));
    110 		quickActionBar.addView(createAction("Action 2", R.drawable.#drawable_name#quickaction/icon.png#));
    111 		quickActionBar.addView(createAction("Action 3", R.drawable.#drawable_name#quickaction/icon.png#));
    112 		quickActionBar.addView(createAction("Action n", R.drawable.#drawable_name#quickaction/icon.png#));
    113 
    114 		/*
    115 		 * Create the window and set the content
    116 		 * It is important to set the right context due to touch events
    117 		 */
    118 		currentQuickActionWindow = new PopupWindow(getApplicationContext());
    119 		currentQuickActionWindow.setContentView(quickActionLayout);
    120 
    121 		/*
    122 		 * The popup must be touchable (to touch actions),
    123 		 * focusable (if you have more actions than space) and,
    124 		 * outside touchable (to be able to close the window)
    125 		 */
    126 		currentQuickActionWindow.setTouchable(true);
    127 		currentQuickActionWindow.setFocusable(true);
    128 		currentQuickActionWindow.setOutsideTouchable(true);
    129 
    130 		/*
    131 		 * Make background transparent
    132 		 */
    133 		currentQuickActionWindow.setBackgroundDrawable(new BitmapDrawable());
    134 
    135 		/*
    136 		 * Close Quick Action window when clicked outside
    137 		 */
    138 		currentQuickActionWindow.setTouchInterceptor(new OnTouchListener() {
    139 			public boolean onTouch(View v, MotionEvent event) {
    140 				if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
    141 					currentQuickActionWindow.dismiss();
    142 					return true;
    143 				}
    144 				return false;
    145 			}
    146 		});
    147 
    148 		/*
    149 		 * Measure the Quick Action window.
    150 		 * Add MARGIN_ADJUSTMENT density independent pixels to the size in order to not show scrolls and left some edge spaces
    151 		 * Check layout files to check
    152 		 */
    153 		quickActionLayout.measure(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    154 		currentQuickActionWindow.setWidth(quickActionLayout.getMeasuredWidth() + (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, MARGIN_ADJUSTMENT, getResources().getDisplayMetrics()));
    155 		currentQuickActionWindow.setHeight(quickActionLayout.getMeasuredHeight() + (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, MARGIN_ADJUSTMENT, getResources().getDisplayMetrics()));
    156 
    157 		/*
    158 		 * Open the Quick Action window
    159 		 */
    160 		currentQuickActionWindow.showAsDropDown(target);
    161 
    162 		/*
    163 		 * Animate action list
    164 		 * This must be manually called
    165 		 */
    166 		Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.#anim_name#quickaction/quickaction_list_anim.xml#);
    167 		quickActionBar.startAnimation(animation);
    168 	}
    169 
    170 	@Override
    171     protected void onPause() {
    172 	    /*
    173 	     * Dismiss quick action if still opened
    174 	     */
    175         if (currentQuickActionWindow != null) {
    176             currentQuickActionWindow.dismiss();
    177         }
    178         super.onPause();
    179     }
    180 }