-1

I am using Android 6.0 and open the camera intent by

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
     getApplication().startActivity(intent);
} catch (final ActivityNotFoundException e) {
     e.printStackTrace();
} 

It will open my camera intent. If I press the movie button, the movie GUI will be shown. I want to distinguish the camera and movie intent. How can I know it by programming because both of them have the name are camera. This is my code to get current process name

    String foregroundProcess = null;
    UsageStatsManager mUsageStatsManager = (UsageStatsManager) getApplicationContext().getSystemService(Service.USAGE_STATS_SERVICE);
    long time = System.currentTimeMillis();

    UsageEvents usageEvents = mUsageStatsManager.queryEvents(time - 1000 * 3600, time);
    UsageEvents.Event event = new UsageEvents.Event();
    while (usageEvents.hasNextEvent()) {
        usageEvents.getNextEvent(event);
        if(event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) {
            foregroundProcess = event.getPackageName();
        }
    }

    //Convert package name to application name
    final PackageManager pm = getApplicationContext().getPackageManager();
    ApplicationInfo ai;
    try {
        ai = pm.getApplicationInfo(foregroundProcess, 0);
    } catch (final PackageManager.NameNotFoundException e) {
        ai = null;
    }
    String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
    return applicationName;
John
  • 2,838
  • 7
  • 36
  • 65

2 Answers2

2

You can request that the camera take a picture via ACTION_IMAGE_CAPTURE, and you can request that the camera record a video via ACTION_VIDEO_CAPTURE.

If the camera app that you are testing allows the user to switch between those modes, when launched from these Intent actions, IMHO that is a bug in the camera app.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks. I update my question for more clear. Actually, I can switch two modes: camera and movie in rooted phone by pressing the record button. Using above function, I can get the current application name, and they are shown same result as camera in both modes – John Jan 18 '17 at 13:00
  • @user8264: The user is welcome to choose the same app for either `Intent`, and you have no means of distinguishing between whether that app is taking a picture, recording a video, on its settings screen, or doing anything else. If you need that degree of control, do not use these `Intent` actions and implement camera functionality directly in your app. Besides, `UsageStatsManager` is not real-time ("NOTE: The last few minutes of the event log will be truncated to prevent abuse by applications"), and so your existing code will not work reliably. – CommonsWare Jan 18 '17 at 13:02
  • So, we have no way to distinguish these modes. My goal is to detect the current process name such as camera capture and camera record. Is it possible? – John Jan 18 '17 at 13:06
  • @user8264: No, because usually it will be the same process name in either case, and you cannot get the current process name from `UsageStatsManager`, for privacy and security reasons. Moreover, if you were the one that launched these `Intents`, you know what you asked for. – CommonsWare Jan 18 '17 at 13:08
  • without security issue, how could I known it? Because I am working in rooted phone – John Jan 18 '17 at 13:13
  • @user8264: You still have no good way of knowing whether an arbitrary camera app is in still picture mode or in video-recording mode. If you are certain of the specific camera app (e.g., you control the device), you could reverse-engineer it, or examine its UI in `uiautomatorviewer`, and then try to find out what mode it is in by examining various widgets in its UI. I don't know all the details, as I do not work in rooted environments. – CommonsWare Jan 18 '17 at 13:48
1

The app that handles MediaStore.ACTION_IMAGE_CAPTURE should not allow you to switch to video mode because you clearly called it in intent to capturing the image (same for the ACTION_VIDEO_CAPTURE action, which should stick to video mode only). I'd report this issue to app vendor instead of wasting time trying to work around others' bugs.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • I can switch by press the record button in default camera app. It will go to record camera app. – John Jan 18 '17 at 12:57