• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.camera;
18 
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.content.ActivityNotFoundException;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.content.DialogInterface.OnClickListener;
25 import android.content.Intent;
26 import android.net.Uri;
27 import android.provider.MediaStore;
28 import android.provider.MediaStore.Images;
29 import android.util.Log;
30 import android.view.Menu;
31 import android.view.MenuItem;
32 import android.view.MenuItem.OnMenuItemClickListener;
33 
34 
35 /**
36  * A utility class to handle various kinds of menu operations.
37  */
38 public 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 
confirmAction(Context context, String title, String message, final Runnable action)50     public static void confirmAction(Context context, String title,
51             String message, final Runnable action) {
52         OnClickListener listener = new OnClickListener() {
53             public void onClick(DialogInterface dialog, int which) {
54                 switch (which) {
55                     case DialogInterface.BUTTON_POSITIVE:
56                         if (action != null) action.run();
57                 }
58             }
59         };
60         new AlertDialog.Builder(context)
61             .setIconAttribute(android.R.attr.alertDialogIcon)
62             .setTitle(title)
63             .setMessage(message)
64             .setPositiveButton(android.R.string.ok, listener)
65             .setNegativeButton(android.R.string.cancel, listener)
66             .create()
67             .show();
68     }
69 
addSwitchModeMenuItem(Menu menu, int mode, final Runnable r)70     public static void addSwitchModeMenuItem(Menu menu, int mode,
71             final Runnable r) {
72         int labelId, iconId;
73         switch(mode) {
74             case ModePicker.MODE_VIDEO:
75                 labelId = R.string.switch_to_video_label;
76                 iconId = R.drawable.ic_menu_camera_video_view;
77                 break;
78             case ModePicker.MODE_CAMERA:
79                 labelId = R.string.switch_to_camera_label;
80                 iconId = android.R.drawable.ic_menu_camera;
81                 break;
82             case ModePicker.MODE_PANORAMA:
83                 labelId = R.string.switch_to_panorama_label;
84                 iconId = R.drawable.btn_ic_panorama;
85                 break;
86             default:
87                   // incorrect mode, do nothing.
88                   return;
89         }
90         MenuItem item = menu.add(labelId).setOnMenuItemClickListener(
91                 new OnMenuItemClickListener() {
92             public boolean onMenuItemClick(MenuItem item) {
93                 r.run();
94                 return true;
95             }
96         });
97         item.setIcon(iconId);
98     }
99 
startCameraActivity(Activity activity, Intent intent, String className)100     private static void startCameraActivity(Activity activity, Intent intent,
101             String className) {
102         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
103         intent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
104         intent.setClassName(activity.getPackageName(), className);
105 
106         // Keep the camera instance for a while.
107         // This avoids re-opening the camera and saves time.
108         CameraHolder.instance().keep();
109 
110         try {
111             activity.startActivity(intent);
112         } catch (ActivityNotFoundException e) {
113             intent.setComponent(null);
114             activity.startActivity(intent);
115         }
116         activity.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
117     }
118 
gotoMode(int mode, Activity activity)119     public static void gotoMode(int mode, Activity activity) {
120         String action, className;
121         switch (mode) {
122             case ModePicker.MODE_PANORAMA:
123                 action = PANORAMA_CLASS;
124                 className = PANORAMA_CLASS;
125                 break;
126             case ModePicker.MODE_VIDEO:
127                 action = MediaStore.INTENT_ACTION_VIDEO_CAMERA;
128                 className = VIDEO_CAMERA_CLASS;
129                 break;
130             case ModePicker.MODE_CAMERA:
131                 action = MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA;
132                 className = CAMERA_CLASS;
133                 break;
134             default:
135                 Log.e(TAG, "unknown camera mode:" + mode);
136                 return;
137         }
138         startCameraActivity(activity, new Intent(action), className);
139     }
140 
gotoVideoMode(Activity activity, boolean resetEffect)141     public static void gotoVideoMode(Activity activity, boolean resetEffect) {
142         Intent intent = new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA);
143         intent.putExtra(VideoCamera.RESET_EFFECT_EXTRA, resetEffect);
144         startCameraActivity(activity, intent, VIDEO_CAMERA_CLASS);
145     }
146 
gotoCameraMode(Activity activity)147     public static void gotoCameraMode(Activity activity) {
148         Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
149         startCameraActivity(activity, intent, CAMERA_CLASS);
150     }
151 
gotoVideoMode(Activity activity, Intent intent)152     public static void gotoVideoMode(Activity activity, Intent intent) {
153         startCameraActivity(activity, intent, VIDEO_CAMERA_CLASS);
154      }
155 
gotoCameraMode(Activity activity, Intent intent)156     public static void gotoCameraMode(Activity activity, Intent intent) {
157         startCameraActivity(activity, intent, CAMERA_CLASS);
158     }
159 
gotoCameraImageGallery(Activity activity)160     public static void gotoCameraImageGallery(Activity activity) {
161         gotoGallery(activity, R.string.gallery_camera_bucket_name, INCLUDE_IMAGES);
162     }
163 
gotoCameraVideoGallery(Activity activity)164     public static void gotoCameraVideoGallery(Activity activity) {
165         gotoGallery(activity, R.string.gallery_camera_videos_bucket_name, INCLUDE_VIDEOS);
166     }
167 
gotoGallery(Activity activity, int windowTitleId, int mediaTypes)168     private static void gotoGallery(Activity activity, int windowTitleId,
169             int mediaTypes) {
170         Uri target = Images.Media.EXTERNAL_CONTENT_URI.buildUpon()
171                 .appendQueryParameter("bucketId", Storage.BUCKET_ID).build();
172         Intent intent = new Intent(Intent.ACTION_VIEW, target);
173         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
174         intent.putExtra("windowTitle", activity.getString(windowTitleId));
175         intent.putExtra("mediaTypes", mediaTypes);
176 
177         try {
178             activity.startActivity(intent);
179         } catch (ActivityNotFoundException e) {
180             Log.e(TAG, "Could not start gallery activity", e);
181         }
182     }
183 }
184