1/* 2 * Copyright (C) 2008 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 com.android.camera; 18 19import android.app.Activity; 20import android.app.AlertDialog; 21import android.content.ActivityNotFoundException; 22import android.content.Context; 23import android.content.DialogInterface; 24import android.content.DialogInterface.OnClickListener; 25import android.content.Intent; 26import android.net.Uri; 27import android.provider.MediaStore; 28import android.provider.MediaStore.Images; 29import android.util.Log; 30import android.view.Menu; 31import android.view.MenuItem; 32import android.view.MenuItem.OnMenuItemClickListener; 33 34 35/** 36 * A utility class to handle various kinds of menu operations. 37 */ 38public class MenuHelper { 39 private static final String TAG = "MenuHelper"; 40 41 // TODO: These should be public and added to frameworks. 42 private static final int INCLUDE_IMAGES = (1 << 0); 43 private static final int INCLUDE_VIDEOS = (1 << 2); 44 45 private static final int NO_ANIMATION = 0; 46 private static final String CAMERA_CLASS = "com.android.camera.Camera"; 47 private static final String PANORAMA_CLASS = "com.android.camera.panorama.PanoramaActivity"; 48 private static final String VIDEO_CAMERA_CLASS = "com.android.camera.VideoCamera"; 49 50 public static void addSwitchModeMenuItem(Menu menu, int mode, 51 final Runnable r) { 52 int labelId, iconId; 53 switch(mode) { 54 case ModePicker.MODE_VIDEO: 55 labelId = R.string.switch_to_video_label; 56 iconId = R.drawable.ic_menu_camera_video_view; 57 break; 58 case ModePicker.MODE_CAMERA: 59 labelId = R.string.switch_to_camera_label; 60 iconId = android.R.drawable.ic_menu_camera; 61 break; 62 case ModePicker.MODE_PANORAMA: 63 labelId = R.string.switch_to_panorama_label; 64 iconId = R.drawable.btn_ic_panorama; 65 break; 66 default: 67 // incorrect mode, do nothing. 68 return; 69 } 70 MenuItem item = menu.add(labelId).setOnMenuItemClickListener( 71 new OnMenuItemClickListener() { 72 public boolean onMenuItemClick(MenuItem item) { 73 r.run(); 74 return true; 75 } 76 }); 77 item.setIcon(iconId); 78 } 79 80 private static void startCameraActivity(Activity activity, Intent intent, 81 String className) { 82 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 83 intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); 84 intent.setClassName(activity.getPackageName(), className); 85 86 // Keep the camera instance for a while. 87 // This avoids re-opening the camera and saves time. 88 CameraHolder.instance().keep(); 89 90 try { 91 activity.startActivity(intent); 92 } catch (ActivityNotFoundException e) { 93 intent.setComponent(null); 94 activity.startActivity(intent); 95 } 96 activity.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); 97 } 98 99 public static void gotoMode(int mode, Activity activity) { 100 String action, className; 101 switch (mode) { 102 case ModePicker.MODE_PANORAMA: 103 action = PANORAMA_CLASS; 104 className = PANORAMA_CLASS; 105 break; 106 case ModePicker.MODE_VIDEO: 107 action = MediaStore.INTENT_ACTION_VIDEO_CAMERA; 108 className = VIDEO_CAMERA_CLASS; 109 break; 110 case ModePicker.MODE_CAMERA: 111 action = MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA; 112 className = CAMERA_CLASS; 113 break; 114 default: 115 Log.e(TAG, "unknown camera mode:" + mode); 116 return; 117 } 118 startCameraActivity(activity, new Intent(action), className); 119 } 120 121 public static void gotoVideoMode(Activity activity, boolean resetEffect) { 122 Intent intent = new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA); 123 intent.putExtra(VideoCamera.RESET_EFFECT_EXTRA, resetEffect); 124 startCameraActivity(activity, intent, VIDEO_CAMERA_CLASS); 125 } 126 127 public static void gotoCameraMode(Activity activity) { 128 Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); 129 startCameraActivity(activity, intent, CAMERA_CLASS); 130 } 131 132 public static void gotoVideoMode(Activity activity, Intent intent) { 133 startCameraActivity(activity, intent, VIDEO_CAMERA_CLASS); 134 } 135 136 public static void gotoCameraMode(Activity activity, Intent intent) { 137 startCameraActivity(activity, intent, CAMERA_CLASS); 138 } 139 140 public static void gotoCameraImageGallery(Activity activity) { 141 gotoGallery(activity, R.string.gallery_camera_bucket_name, INCLUDE_IMAGES); 142 } 143 144 public static void gotoCameraVideoGallery(Activity activity) { 145 gotoGallery(activity, R.string.gallery_camera_videos_bucket_name, INCLUDE_VIDEOS); 146 } 147 148 private static void gotoGallery(Activity activity, int windowTitleId, 149 int mediaTypes) { 150 Uri target = Images.Media.EXTERNAL_CONTENT_URI.buildUpon() 151 .appendQueryParameter("bucketId", Storage.BUCKET_ID).build(); 152 Intent intent = new Intent(Intent.ACTION_VIEW, target); 153 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 154 intent.putExtra("windowTitle", activity.getString(windowTitleId)); 155 intent.putExtra("mediaTypes", mediaTypes); 156 157 try { 158 activity.startActivity(intent); 159 } catch (ActivityNotFoundException e) { 160 Log.e(TAG, "Could not start gallery activity", e); 161 } 162 } 163} 164